make input coordinates respect the viewport

This commit is contained in:
veclavtalica
2024-12-24 10:24:50 +03:00
parent 190eb1f107
commit dc2535358e
4 changed files with 34 additions and 21 deletions

View File

@ -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();