From dc2535358edb53957640843ee2ac2c8f12d8539a Mon Sep 17 00:00:00 2001 From: veclavtalica Date: Tue, 24 Dec 2024 10:24:50 +0300 Subject: [PATCH] make input coordinates respect the viewport --- src/rendering/twn_draw.c | 20 +------------------- src/twn_engine_context_c.h | 2 ++ src/twn_input.c | 4 ++-- src/twn_loop.c | 29 +++++++++++++++++++++++++++++ 4 files changed, 34 insertions(+), 21 deletions(-) diff --git a/src/rendering/twn_draw.c b/src/rendering/twn_draw.c index 939ae06..9403189 100644 --- a/src/rendering/twn_draw.c +++ b/src/rendering/twn_draw.c @@ -354,25 +354,7 @@ void render(void) { /* fit rendering context onto the resizable screen */ if (ctx.window_size_has_changed) { - if ((float)ctx.window_dims.x / (float)ctx.window_dims.y > (float)ctx.base_render_width / (float)ctx.base_render_height) { - float ratio = (float)ctx.window_dims.y / (float)ctx.base_render_height; - int w = (int)((float)ctx.base_render_width * ratio); - setup_viewport( - (int)ctx.window_dims.x / 2 - w / 2, - 0, - w, - (int)ctx.window_dims.y - ); - } else { - float ratio = (float)ctx.window_dims.x / (float)ctx.base_render_width; - int h = (int)((float)ctx.base_render_height * ratio); - setup_viewport( - 0, - (int)ctx.window_dims.y / 2 - h / 2, - (int)ctx.window_dims.x, - h - ); - } + setup_viewport((int)ctx.viewport_rect.x, (int)ctx.viewport_rect.y, (int)ctx.viewport_rect.w, (int)ctx.viewport_rect.h); } start_render_frame(); { diff --git a/src/twn_engine_context_c.h b/src/twn_engine_context_c.h index ad80e4f..014adb8 100644 --- a/src/twn_engine_context_c.h +++ b/src/twn_engine_context_c.h @@ -30,6 +30,8 @@ typedef struct EngineContext { char *base_dir; Vec2 window_dims; + Rect viewport_rect; + float viewport_scale; /* configuration */ toml_table_t *config_table; diff --git a/src/twn_input.c b/src/twn_input.c index f30c32d..550f762 100644 --- a/src/twn_input.c +++ b/src/twn_input.c @@ -45,8 +45,8 @@ static void update_action_pressed_state(InputState *input, Action *action) { else { action->just_changed = !action->is_pressed; action->is_pressed = true; - action->position.x = (float)input->mouse_window_position.x; - action->position.y = (float)input->mouse_window_position.y; + action->position.x = ((float)input->mouse_window_position.x - ctx.viewport_rect.x) / ctx.viewport_scale; + action->position.y = ((float)input->mouse_window_position.y - ctx.viewport_rect.y) / ctx.viewport_scale; /* TODO: */ /* diff --git a/src/twn_loop.c b/src/twn_loop.c index 7d07ce0..5c12be2 100644 --- a/src/twn_loop.c +++ b/src/twn_loop.c @@ -117,6 +117,31 @@ static void preserve_persistent_ctx_fields(void) { } +static void update_viewport(void) { + Rect new_viewport; + float new_scale; + if ((float)ctx.window_dims.x / (float)ctx.window_dims.y > (float)ctx.base_render_width / (float)ctx.base_render_height) { + float ratio = (float)ctx.window_dims.y / (float)ctx.base_render_height; + float w = ((float)ctx.base_render_width * ratio); + new_viewport.x = ctx.window_dims.x / 2 - w / 2; + new_viewport.y = 0; + new_viewport.w = w; + new_viewport.h = ctx.window_dims.y; + new_scale = ratio; + } else { + float ratio = (float)ctx.window_dims.x / (float)ctx.base_render_width; + float h = ((float)ctx.base_render_height * ratio); + new_viewport.x = 0; + new_viewport.y = ctx.window_dims.y / 2 - h / 2; + new_viewport.w = ctx.window_dims.x; + new_viewport.h = h; + new_scale = ratio; + } + ctx.viewport_rect = new_viewport; + ctx.viewport_scale = new_scale; +} + + static void main_loop(void) { /* if (!ctx.is_running) { @@ -204,6 +229,8 @@ static void main_loop(void) { /* TODO: disable rendering pushes on not-last ? */ render_queue_clear(); poll_events(); + if (ctx.window_size_has_changed) + update_viewport(); game_object_tick(); input_state_update(&ctx.input); preserve_persistent_ctx_fields(); @@ -773,6 +800,8 @@ int enter_loop(int argc, char **argv) { ctx.game.debug = false; } + update_viewport(); + /* now we can actually start doing stuff */ game_object_load();