From 00636d65a9983934f3aaf027deec25662fc1cff8 Mon Sep 17 00:00:00 2001 From: veclavtalica Date: Tue, 4 Feb 2025 00:24:31 +0300 Subject: [PATCH] fix resizing events, clean up the code --- src/twn_loop.c | 60 +++++++++++++------------------------------------- 1 file changed, 15 insertions(+), 45 deletions(-) diff --git a/src/twn_loop.c b/src/twn_loop.c index 86e5395..f753408 100644 --- a/src/twn_loop.c +++ b/src/twn_loop.c @@ -24,46 +24,7 @@ static SDL_Thread *opengl_load_thread; -static int event_callback(void *userdata, SDL_Event *event) { - (void)userdata; - - switch (event->type) { - case SDL_WINDOWEVENT: - if (event->window.windowID != ctx.window_id) - break; - - switch (event->window.event) { - case SDL_WINDOWEVENT_SIZE_CHANGED: - ctx.window_dims.x = (float)event->window.data1; - ctx.window_dims.y = (float)event->window.data2; - ctx.resync_flag = true; - break; - - case SDL_WINDOWEVENT_FOCUS_LOST: { - ctx.window_mouse_resident = false; - break; - } - - case SDL_WINDOWEVENT_FOCUS_GAINED: { - ctx.window_mouse_resident = true; - break; - } - - default: - break; - } - - break; - - default: - break; - } - - /* ignored */ - return 0; -} - - +/* note: it drives most of IO implicitly, such as audio callbacks */ static void poll_events(void) { SDL_Event e; @@ -81,9 +42,22 @@ static void poll_events(void) { switch (e.window.event) { case SDL_WINDOWEVENT_SIZE_CHANGED: + ctx.window_dims.x = (float)e.window.data1; + ctx.window_dims.y = (float)e.window.data2; + ctx.resync_flag = true; ctx.window_size_has_changed = true; break; + case SDL_WINDOWEVENT_FOCUS_LOST: { + ctx.window_mouse_resident = false; + break; + } + + case SDL_WINDOWEVENT_FOCUS_GAINED: { + ctx.window_mouse_resident = true; + break; + } + default: break; } @@ -547,9 +521,6 @@ static bool initialize(void) { ctx.game.resolution.x = (float)ctx.base_render_width; ctx.game.resolution.y = (float)ctx.base_render_height; - /* add a watcher for immediate updates on window size */ - SDL_AddEventWatch(event_callback, NULL); - /* random seeding */ /* SDL_GetPerformanceCounter returns some platform-dependent number. */ /* it should vary between game instances. i checked! random enough for me. */ @@ -584,8 +555,7 @@ static bool initialize(void) { ctx.time_averager[i] = ctx.desired_frametime; } - /* rendering */ - /* configuration */ + /* engine configuration */ { toml_datum_t datum_texture_atlas_size = toml_int_in(engine, "texture_atlas_size"); if (!datum_texture_atlas_size.ok) {