From fab958c963047cde7486b960dbd95728434e0ff6 Mon Sep 17 00:00:00 2001 From: veclavtalica Date: Mon, 7 Oct 2024 22:35:48 +0300 Subject: [PATCH] twn_loop.c: use event watcher for more immediate resize redraw --- src/twn_loop.c | 35 ++++++++++++++++++++++++++++------- 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/src/twn_loop.c b/src/twn_loop.c index 4380e5c..38f77fc 100644 --- a/src/twn_loop.c +++ b/src/twn_loop.c @@ -21,6 +21,31 @@ #include +static int event_callback(void *userdata, SDL_Event *event) { + switch (event->type) { + case SDL_WINDOWEVENT: + if (event->window.windowID != ctx.window_id) + break; + + switch (event->window.event) { + case SDL_WINDOWEVENT_SIZE_CHANGED: + ctx.game.window_w = event->window.data1; + ctx.game.window_h = event->window.data2; + ctx.resync_flag = true; + break; + + default: + break; + } + + break; + + default: + break; + } +} + + static void poll_events(void) { SDL_Event e; @@ -163,13 +188,6 @@ static void main_loop(void) { poll_events(); - if (ctx.game.window_size_has_changed) { - Vec2i size; - SDL_GetWindowSize(ctx.window, &size.x, &size.y); - ctx.game.window_w = size.x; - ctx.game.window_h = size.y; - } - input_state_update(&ctx.game.input); game_object_tick(); @@ -478,6 +496,9 @@ static bool initialize(void) { ctx.game.window_w = (int)ctx.base_render_width; ctx.game.window_h = (int)ctx.base_render_height; + /* add a watcher for immediate updates on window size */ + SDL_AddEventWatch(event_callback, NULL); + /* audio initialization */ {