diff --git a/src/window.cpp b/src/window.cpp index 769cb93..12326f5 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -11,7 +11,6 @@ #ifdef _WIN32 #include -#include #include #endif @@ -31,11 +30,8 @@ struct window_t { std::thread worker; std::atomic running{true}; std::atomic quit_requested{false}; - - std::mutex buffer_mutex; - uint32_t* shared_fb; - std::mutex input_mutex; + uint32_t* shared_fb; uint8_t kb_copy[SDL_NUM_SCANCODES]; #endif }; @@ -56,10 +52,7 @@ static void win32_internal_render(window_t* s) { void* pixels; int pitch; if (SDL_LockTexture(s->texture, nullptr, &pixels, &pitch) == 0) { - { - std::lock_guard lock(s->buffer_mutex); - std::memcpy(pixels, s->shared_fb, s->width * s->height * 4); - } + std::memcpy(pixels, s->shared_fb, s->width * s->height * 4); SDL_UnlockTexture(s->texture); } @@ -68,11 +61,11 @@ static void win32_internal_render(window_t* s) { SDL_RenderPresent(s->renderer); } -// THE FIX: This callback is triggered by SDL even when Windows is in a modal drag loop. +// This callback is triggered by SDL even when Windows is in a modal drag loop. static int SDLCALL win32_event_watch(void* userdata, SDL_Event* event) { window_t* s = (window_t*)userdata; if (event->type == SDL_WINDOWEVENT) { - if (event->window.event == SDL_WINDOWEVENT_MOVED || + if (event->window.event == SDL_WINDOWEVENT_MOVED || event->window.event == SDL_WINDOWEVENT_RESIZED || event->window.event == SDL_WINDOWEVENT_EXPOSED) { win32_internal_render(s); @@ -99,12 +92,9 @@ static void win32_window_worker(window_t* s, const char* title) { } // Sync input state for the main thread - { - std::lock_guard lock(s->input_mutex); - const uint8_t* state = SDL_GetKeyboardState(nullptr); - std::memcpy(s->kb_copy, state, SDL_NUM_SCANCODES); - s->mouse_buttons = SDL_GetMouseState(&s->mouse_x, &s->mouse_y); - } + const uint8_t* state = SDL_GetKeyboardState(nullptr); + std::memcpy(s->kb_copy, state, SDL_NUM_SCANCODES); + s->mouse_buttons = SDL_GetMouseState(&s->mouse_x, &s->mouse_y); // Normal frame render win32_internal_render(s); @@ -124,11 +114,11 @@ window_t* window_create(int width, int height, const char* title) { s->width = width; s->height = height; s->shared_fb = new uint32_t[width * height]; - s->keyboard_state = s->kb_copy; - + s->keyboard_state = s->kb_copy; + // Spawn the worker thread which owns the SDL context s->worker = std::thread(win32_window_worker, s, title); - + // Wait briefly for the worker to initialize window/renderer pointers while (s->running && !s->renderer) { std::this_thread::yield(); } return s; @@ -161,9 +151,7 @@ void window_destroy(window_t* s) { bool window_pollevent(window_t* s) { #ifdef _WIN32 - // Main thread just checks the status reported by the worker - std::lock_guard lock(s->input_mutex); - return s->quit_requested; + return s->quit_requested.load(); #else SDL_Event ev; bool quit = false; @@ -177,8 +165,6 @@ bool window_pollevent(window_t* s) { void window_blit(window_t* s, const uint32_t* fb) { #ifdef _WIN32 - // Just update the shared buffer; the worker thread will pick it up and present it - std::lock_guard lock(s->buffer_mutex); std::memcpy(s->shared_fb, fb, s->width * s->height * 4); #else // Original immediate blit logic