a-game

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

commit 25594fb69607757bfbc9e92f494ccb5400536e0a
parent 0115aa89327c3640fc6ead0ce20a1eb3c9ecdafe
Author: amin <dev@aminmesbah.com>
Date:   Sat, 13 Apr 2019 20:06:50 +0000

Clean up viewport position calculations

FossilOrigin-Name: 7a0051b54ec411b59f0bb8db570f9218399baef2ebeba189afce815531b77a74
Diffstat:
Msrc/game.c | 58++++++++++++++++++++++++++++++++++++++--------------------
Msrc/game.h | 1+
Msrc/glmth.c | 9+++++++++
Msrc/glmth.h | 2++
4 files changed, 50 insertions(+), 20 deletions(-)

diff --git a/src/game.c b/src/game.c @@ -95,23 +95,46 @@ void game_init(struct GameState *game_state, v2u framebuffer) void game_update_and_render(struct GameState *game_state, f32 dt, v2u framebuffer) { - v2u screen = (v2u) { - framebuffer.width, - framebuffer.height, - }; + rect viewport; f32 pixels_per_meter = 0.0f; - { - f32 screen_aspect_ratio = (f32)screen.width / (f32)screen.height; + f32 screen_aspect_ratio = (f32)framebuffer.width / (f32)framebuffer.height; if (screen_aspect_ratio < ROOM_ASPECT_RATIO) { - pixels_per_meter = (f32)screen.width / (f32)ROOM_TILE_DIM_X; + pixels_per_meter = (f32)framebuffer.width / (f32)ROOM_TILE_DIM_X; } else { - pixels_per_meter = (f32)screen.height / (f32)ROOM_TILE_DIM_Y; + pixels_per_meter = (f32)framebuffer.height / (f32)ROOM_TILE_DIM_Y; } + + v2 viewport_size = (v2) { + pixels_per_meter * (f32)ROOM_TILE_DIM_X, + pixels_per_meter * (f32)ROOM_TILE_DIM_Y, + }; + + f32 h_pad_size = (framebuffer.width - viewport_size.width) / 2.0f; + f32 v_pad_size = (framebuffer.height - viewport_size.height) / 2.0f; + printf("h pad: %f\n", h_pad_size); + printf("v pad: %f\n", v_pad_size); + + viewport.min = (v2) { + h_pad_size, + v_pad_size, + }; + + viewport.max = glmth_v2_a(viewport.min, viewport_size); + printf("---------------------------------\n"); + printf("Screen: "); + glmth_v2_print((v2) { (f32)framebuffer.x, (f32)framebuffer.y }); + printf("Size: "); + glmth_v2_print(viewport_size); + printf("Min: "); + glmth_v2_print(viewport.min); + printf("Max: "); + glmth_v2_print(viewport.max); + printf("---------------------------------\n"); } glClearColor(0.1f, 0.1f, 0.1f, 1.0f); @@ -144,22 +167,17 @@ void game_update_and_render(struct GameState *game_state, f32 dt, v2u framebuffe f32 tile_size = pixels_per_meter; - v2 tilemap = (v2) { - tile_size * (f32)ROOM_TILE_DIM_X, - tile_size * (f32)ROOM_TILE_DIM_Y, - }; - - v2 start = (v2) { - tile_size / 2.0f + ((screen.width - tilemap.width) / 2.0f), - tile_size / 2.0f + ((screen.height - tilemap.height) / 2.0f), - }; for (size_t y = 0; y < ROOM_TILE_DIM_Y; y++) { for (size_t x = 0; x < ROOM_TILE_DIM_X; x++) { u32 tile_id = tiles[y][x]; m4 model = glmth_m4_init_id(); - model = glmth_translate(model, (v3) {start.x + (tile_size * x), start.y + (tile_size * y), 0.0f}); + // our square verts are anchored around the center point of the + // square, so we want to offset by 0.5 to instead have our + // anchor in the min corner + model = glmth_translate(model, (v3) { tile_size / 2.0f, tile_size / 2.0f, 0.0f }); + model = glmth_translate(model, (v3) { viewport.min.x + (tile_size * x), viewport.min.y + (tile_size * y), 0.0f }); model = glmth_scale(model, (v3) {tile_size, tile_size, 1.0f}); shader_setm4(&game_state->tiles.shader, "model", &model); @@ -174,7 +192,7 @@ void game_update_and_render(struct GameState *game_state, f32 dt, v2u framebuffe } shader_setv3(&game_state->tiles.shader, "color", &color); - m4 projection = glmth_projection_ortho(0.0f, screen.width, screen.height, 0.0f, -1.0f, 0.0f); + m4 projection = glmth_projection_ortho(0.0f, framebuffer.width, framebuffer.height, 0.0f, -1.0f, 0.0f); shader_setm4(&game_state->tiles.shader, "projection", &projection); glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); @@ -238,7 +256,7 @@ void game_update_and_render(struct GameState *game_state, f32 dt, v2u framebuffe v3 color = (v3) { 1.0f, 0.0f, 1.0f }; shader_setv3(&game_state->tiles.shader, "color", &color); - m4 projection = glmth_projection_ortho(0.0f, screen.width, screen.height, 0.0f, -1.0f, 0.0f); + m4 projection = glmth_projection_ortho(0.0f, framebuffer.width, framebuffer.height, 0.0f, -1.0f, 0.0f); shader_setm4(&game_state->tiles.shader, "projection", &projection); glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); diff --git a/src/game.h b/src/game.h @@ -50,6 +50,7 @@ struct GameState struct Tiles tiles; struct GameInput input; struct Entity player; + rect viewport; }; #ifdef PLATFORM_HOTLOAD_GAME_CODE diff --git a/src/glmth.c b/src/glmth.c @@ -91,6 +91,15 @@ v2u glmth_v2u_init(u32 x, u32 y) return v; } +v2 glmth_v2_a(v2 vec1, v2 vec2) +{ + v2 r = { + .x = vec1.x + vec2.x, + .y = vec1.y + vec2.y, + }; + return r; +} + void glmth_v2_print(v2 v) { printf("( %f, %f )\n", v.x, v.y); diff --git a/src/glmth.h b/src/glmth.h @@ -105,6 +105,8 @@ bool glmth_m4m4_eq(m4 mat1, m4 mat2); m4 glmth_m4m4_m(m4 mat1, m4 mat2); v4 glmth_m4v4_m(m4 m, v4 v); v2u glmth_v2u_init(u32 x, u32 y); +v2 glmth_v2_a(v2 vec1, v2 vec2); +void glmth_v2_print(v2 v); v3 glmth_v3_cross(v3 vec1, v3 vec2); v3 glmth_v3_init(f32 x, f32 y, f32 z); v3 glmth_v3_init_f(f32 f);