5#include "absl/status/status.h"
6#include "absl/strings/str_format.h"
12#include "imgui/backends/imgui_impl_sdl2.h"
13#include "imgui/backends/imgui_impl_sdlrenderer2.h"
14#include "imgui/imgui.h"
21 LOG_ERROR(
"ImGui",
"Assertion failed: %s\nFile: %s:%d\nMessage: %s",
22 expr, file, line, msg ? msg :
"");
25 static int error_count = 0;
28 if (error_count > 5) {
29 LOG_ERROR(
"ImGui",
"Too many assertions, resetting workspace settings...");
33 if (std::filesystem::exists(
"imgui.ini")) {
34 std::filesystem::copy(
"imgui.ini",
"imgui.ini.backup",
35 std::filesystem::copy_options::overwrite_existing);
36 std::filesystem::remove(
"imgui.ini");
37 LOG_INFO(
"ImGui",
"Workspace settings reset. Backup saved to imgui.ini.backup");
39 }
catch (
const std::exception& e) {
40 LOG_ERROR(
"ImGui",
"Failed to reset workspace: %s", e.what());
58 if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER) != 0) {
59 return absl::InternalError(
60 absl::StrFormat(
"SDL_Init: %s\n", SDL_GetError()));
63 SDL_DisplayMode display_mode;
64 SDL_GetCurrentDisplayMode(0, &display_mode);
65 int screen_width = display_mode.w * 0.8;
66 int screen_height = display_mode.h * 0.8;
68 window.
window_ = std::unique_ptr<SDL_Window, util::SDL_Deleter>(
69 SDL_CreateWindow(
"Yet Another Zelda3 Editor", SDL_WINDOWPOS_UNDEFINED,
70 SDL_WINDOWPOS_UNDEFINED, screen_width, screen_height,
73 if (window.
window_ ==
nullptr) {
74 return absl::InternalError(
75 absl::StrFormat(
"SDL_CreateWindow: %s\n", SDL_GetError()));
81 return absl::InternalError(
"Failed to initialize renderer");
86 ImGui::CreateContext();
87 ImGuiIO& io = ImGui::GetIO();
88 io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;
89 io.ConfigFlags |= ImGuiConfigFlags_DockingEnable;
92#ifdef IMGUI_DISABLE_DEFAULT_ASSERT_HANDLER
93 ImGui::SetAssertHandler(ImGuiAssertionHandler);
96 LOG_INFO(
"Window",
"ImGui assertions are disabled in this build");
100#if defined(YAZE_ENABLE_IMGUI_TEST_ENGINE) && YAZE_ENABLE_IMGUI_TEST_ENGINE
106 SDL_Renderer* sdl_renderer =
static_cast<SDL_Renderer*
>(renderer->
GetBackendRenderer());
107 ImGui_ImplSDL2_InitForSDLRenderer(window.
window_.get(), sdl_renderer);
108 ImGui_ImplSDLRenderer2_Init(sdl_renderer);
119 const int audio_frequency = 48000;
120 const size_t buffer_size = (audio_frequency / 50) * 2;
125 new int16_t[buffer_size],
126 std::default_delete<int16_t[]>());
130 LOG_INFO(
"Window",
"Audio buffer allocated: %zu int16_t samples (backend in Emulator)",
134 return absl::OkStatus();
142#ifdef YAZE_ENABLE_IMGUI_TEST_ENGINE
149 LOG_INFO(
"Window",
"Shutting down graphics arena...");
153 LOG_INFO(
"Window",
"Shutting down ImGui implementations...");
154 ImGui_ImplSDL2_Shutdown();
155 ImGui_ImplSDLRenderer2_Shutdown();
158 LOG_INFO(
"Window",
"Destroying ImGui context...");
159 ImGui::DestroyContext();
162#ifdef YAZE_ENABLE_IMGUI_TEST_ENGINE
167 LOG_INFO(
"Window",
"Destroying window...");
168 SDL_DestroyWindow(window.
window_.get());
170 LOG_INFO(
"Window",
"Shutting down SDL...");
173 LOG_INFO(
"Window",
"Shutdown complete");
174 return absl::OkStatus();
179 ImGuiIO& io = ImGui::GetIO();
183 while (SDL_PollEvent(&event)) {
184 ImGui_ImplSDL2_ProcessEvent(&event);
185 switch (event.type) {
188 io.KeyShift = ((SDL_GetModState() & KMOD_SHIFT) != 0);
189 io.KeyCtrl = ((SDL_GetModState() & KMOD_CTRL) != 0);
190 io.KeyAlt = ((SDL_GetModState() & KMOD_ALT) != 0);
191 io.KeySuper = ((SDL_GetModState() & KMOD_GUI) != 0);
194 case SDL_WINDOWEVENT:
195 switch (event.window.event) {
196 case SDL_WINDOWEVENT_CLOSE:
199 case SDL_WINDOWEVENT_SIZE_CHANGED:
200 case SDL_WINDOWEVENT_RESIZED:
202 io.DisplaySize.x =
static_cast<float>(
event.window.data1);
203 io.DisplaySize.y =
static_cast<float>(
event.window.data2);
206 case SDL_WINDOWEVENT_MINIMIZED:
207 case SDL_WINDOWEVENT_HIDDEN:
211 case SDL_WINDOWEVENT_RESTORED:
212 case SDL_WINDOWEVENT_SHOWN:
213 case SDL_WINDOWEVENT_EXPOSED:
223 const int buttons = SDL_GetMouseState(&mouseX, &mouseY);
225 io.DeltaTime = 1.0f / 60.0f;
226 io.MousePos = ImVec2(
static_cast<float>(mouseX),
static_cast<float>(mouseY));
227 io.MouseDown[0] = buttons & SDL_BUTTON(SDL_BUTTON_LEFT);
228 io.MouseDown[1] = buttons & SDL_BUTTON(SDL_BUTTON_RIGHT);
229 io.MouseDown[2] = buttons & SDL_BUTTON(SDL_BUTTON_MIDDLE);
232 io.MouseWheel =
static_cast<float>(wheel);
233 return absl::OkStatus();
Defines an abstract interface for all rendering operations.
virtual bool Initialize(SDL_Window *window)=0
Initializes the renderer with a given window.
virtual void * GetBackendRenderer()=0
Provides an escape hatch to get the underlying, concrete renderer object.
void DestroyUITestingContext()
static TestManager & Get()
void InitializeUITesting()
#define LOG_ERROR(category, format,...)
#define LOG_INFO(category, format,...)
#define RETURN_IF_ERROR(expression)
void ImGuiAssertionHandler(const char *expr, const char *file, int line, const char *msg)
absl::Status ShutdownWindow(Window &window)
bool g_window_is_resizing
absl::Status HandleEvents(Window &window)
absl::Status CreateWindow(Window &window, gfx::IRenderer *renderer, int flags)
Main namespace for the application.
absl::Status LoadPackageFonts()
std::shared_ptr< SDL_Window > window_
std::shared_ptr< int16_t > audio_buffer_
SDL_AudioDeviceID audio_device_
Deleter for SDL_Window and SDL_Renderer.