make input coordinates respect the viewport
This commit is contained in:
parent
190eb1f107
commit
dc2535358e
@ -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(); {
|
||||
|
@ -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;
|
||||
|
@ -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: */
|
||||
/*
|
||||
|
@ -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();
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user