a-game

2D platformer written from scratch.
git clone git://git.amin.space/a-game.git
Log | Files | Refs | README | LICENSE

commit 9ff82f9e6c7700bc0f611e03b030c99b43d3a8d3
parent 425d408dbde1fde763da8862cc63b0b9db8b6479
Author: amin <dev@aminmesbah.com>
Date:   Wed, 26 Feb 2020 08:18:49 +0000

Move viewport logic into renderer

FossilOrigin-Name: 68401bcfab3aebccef4897bef3025b70e9464f204adf8261b2a486967213ac14
Diffstat:
Msrc/game.c | 39+--------------------------------------
Msrc/render.c | 40++++++++++++++++++++++++++++++++++++----
2 files changed, 37 insertions(+), 42 deletions(-)

diff --git a/src/game.c b/src/game.c @@ -183,43 +183,6 @@ void game_update_and_render(struct GameMemory *game_memory, struct GameInput *ga // TODO: pass total elapsed ms in as an i64. Use that for the timer. timer_update(&game_state->jump_allowance_timer, (i64)(dt * 1000.0f)); - rect viewport = {0}; - f32 ppm = 0.0f; - // game_update_viewport - { - f32 screen_aspect_ratio = (f32)framebuffer.width / (f32)framebuffer.height; - if (screen_aspect_ratio < ROOM_ASPECT_RATIO) - { - ppm = (f32)framebuffer.width / (f32)ROOM_DIM_X; - } - else - { - ppm = (f32)framebuffer.height / (f32)ROOM_DIM_Y; - } - - v2 viewport_size = { - ppm * (f32)ROOM_DIM_X, - ppm * (f32)ROOM_DIM_Y, - }; - - f32 h_pad_size = (framebuffer.width - viewport_size.width) / 2.0f; - f32 v_pad_size = (framebuffer.height - viewport_size.height) / 2.0f; - - viewport.min = (v2) { - h_pad_size, - v_pad_size, - }; - - viewport.max = math_v2_a(viewport.min, viewport_size); - - m4 view = math_m4_init_id(); - view = math_translate(view, (v3) {viewport.min.x, viewport.min.y, 0.0f}); - view = math_scale(view, (v3) {ppm, ppm, 1.0f}); - game_state->renderer.view = view; - - game_state->renderer.projection = math_projection_ortho(0.0f, framebuffer.width, 0.0f, framebuffer.height, -1.0f, 0.0f); - } - v2i current_room_i = game_state->player.pos.room; struct Room *current_room = world_room_get(game_state->world, current_room_i); @@ -694,7 +657,7 @@ void game_update_and_render(struct GameMemory *game_memory, struct GameInput *ga } renderer_jobs_sort(&game_state->renderer, &game_state->world_allocator); - renderer_jobs_draw(&game_state->renderer, framebuffer, viewport); + renderer_jobs_draw(&game_state->renderer, framebuffer); } internal void game_cleanup(struct GameMemory *game_memory) diff --git a/src/render.c b/src/render.c @@ -130,8 +130,41 @@ internal void renderer_init(struct RendererState *renderer, struct Image *images glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); } -internal void renderer_jobs_draw(struct RendererState *renderer, v2u framebuffer, rect viewport) +internal void renderer_jobs_draw(struct RendererState *renderer, v2u framebuffer) { + v2 viewport_min = {0}; + v2 viewport_dim = {0}; + // renderer_update_viewport + { + f32 ppm = 0.0f; + f32 screen_aspect_ratio = (f32)framebuffer.width / (f32)framebuffer.height; + if (screen_aspect_ratio < ROOM_ASPECT_RATIO) + { + ppm = (f32)framebuffer.width / (f32)ROOM_DIM_X; + } + else + { + ppm = (f32)framebuffer.height / (f32)ROOM_DIM_Y; + } + + viewport_dim = (v2) { + ppm * (f32)ROOM_DIM_X, + ppm * (f32)ROOM_DIM_Y, + }; + + f32 h_pad_size = (framebuffer.width - viewport_dim.width) / 2.0f; + f32 v_pad_size = (framebuffer.height - viewport_dim.height) / 2.0f; + + viewport_min = (v2) {h_pad_size, v_pad_size}; + + m4 view = math_m4_init_id(); + view = math_translate(view, (v3) {viewport_min.x, viewport_min.y, 0.0f}); + view = math_scale(view, (v3) {ppm, ppm, 1.0f}); + renderer->view = view; + + renderer->projection = math_projection_ortho(0.0f, framebuffer.width, 0.0f, framebuffer.height, -1.0f, 0.0f); + } + glViewport(0, 0, framebuffer.width, framebuffer.height); glClearColor(0.0f, 0.0f, 0.0f, 1.0f); @@ -139,10 +172,9 @@ internal void renderer_jobs_draw(struct RendererState *renderer, v2u framebuffer glClear(GL_COLOR_BUFFER_BIT); glEnable(GL_SCISSOR_TEST); - v2 viewport_dim = math_v2_s(viewport.max, viewport.min); glScissor( - viewport.min.x, - viewport.min.y, + viewport_min.x, + viewport_min.y, viewport_dim.x, viewport_dim.y);