diff --git a/src/context.h b/src/context.h index b046c97..b80da22 100644 --- a/src/context.h +++ b/src/context.h @@ -62,6 +62,8 @@ typedef struct context { bool debug; bool is_running; bool resync_flag; + + bool window_size_has_changed; } t_ctx; extern t_ctx ctx; diff --git a/src/input.h b/src/input.h index f514342..0b7e501 100644 --- a/src/input.h +++ b/src/input.h @@ -54,7 +54,6 @@ struct action_hash_item { struct input_state { struct action_hash_item *action_hash; - SDL_Renderer *renderer; /* some input relates to the screen in some way */ const uint8_t *keyboard_state; /* array of booleans indexed by scancode */ uint32_t mouse_state; /* SDL mouse button bitmask */ t_vec2 mouse_window_position; diff --git a/src/main.c b/src/main.c index 910c040..3e83ffa 100644 --- a/src/main.c +++ b/src/main.c @@ -23,6 +23,8 @@ static void poll_events(void) { SDL_Event e; + ctx.window_size_has_changed = false; + while (SDL_PollEvent(&e)) { switch (e.type) { case SDL_QUIT: @@ -37,6 +39,7 @@ static void poll_events(void) { case SDL_WINDOWEVENT_RESIZED: ctx.window_w = e.window.data1; ctx.window_h = e.window.data2; + ctx.window_size_has_changed = true; break; } diff --git a/src/rendering.c b/src/rendering.c index a5d34cd..f9538bf 100644 --- a/src/rendering.c +++ b/src/rendering.c @@ -141,26 +141,27 @@ static void render_space(void) { void render(void) { textures_update_atlas(&ctx.texture_cache); - /* TODO: only do this when needed */ /* fit rendering context onto the resizable screen */ - if ((float)ctx.window_w / (float)ctx.window_h > RENDER_BASE_RATIO) { - float ratio = (float)ctx.window_h / (float)RENDER_BASE_HEIGHT; - int w = (int)((float)RENDER_BASE_WIDTH * ratio); - glViewport( - ctx.window_w / 2 - w / 2, - 0, - w, - ctx.window_h - ); - } else { - float ratio = (float)ctx.window_w / (float)RENDER_BASE_WIDTH; - int h = (int)((float)RENDER_BASE_HEIGHT * ratio); - glViewport( - 0, - ctx.window_h / 2 - h / 2, - ctx.window_w, - h - ); + if (ctx.window_size_has_changed) { + if ((float)ctx.window_w / (float)ctx.window_h > RENDER_BASE_RATIO) { + float ratio = (float)ctx.window_h / (float)RENDER_BASE_HEIGHT; + int w = (int)((float)RENDER_BASE_WIDTH * ratio); + glViewport( + ctx.window_w / 2 - w / 2, + 0, + w, + ctx.window_h + ); + } else { + float ratio = (float)ctx.window_w / (float)RENDER_BASE_WIDTH; + int h = (int)((float)RENDER_BASE_HEIGHT * ratio); + glViewport( + 0, + ctx.window_h / 2 - h / 2, + ctx.window_w, + h + ); + } } glClearColor((1.0f / 255) * 230,