a-game

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

commit 0a2cf38e9bd76b4ce598ddf0c46cccc9bb1807aa
parent 965e65ab05cb7b71b65edbeefd3e96890e398d62
Author: amin <dev@aminmesbah.com>
Date:   Fri, 19 Apr 2019 23:17:57 +0000

Render everything with the same view matrix

Wow it is so satisfying to finally have a single clear transform between
world and screen coordinates!

FossilOrigin-Name: 7db581de69405ffbf8bcd3739477f8ca8e528ccccc4e182c63d81ee954a2bc15
Diffstat:
Msrc/game.c | 49++++++++++++++++---------------------------------
1 file changed, 16 insertions(+), 33 deletions(-)

diff --git a/src/game.c b/src/game.c @@ -109,9 +109,15 @@ void game_update_and_render(struct GameMemory *game_memory, struct GameInput *ga glClearColor(0.1f, 0.1f, 0.1f, 1.0f); glClear(GL_COLOR_BUFFER_BIT); - //glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); + m4 view = glmth_m4_init_id(); + // World origin is in the lower left + view = glmth_translate(view, (v3) {0.0f, framebuffer.height, 0.0f}); + view = glmth_scale(view, (v3) {1.0f, -1.0f, 1.0f}); + view = glmth_translate(view, (v3) {viewport.min.x, viewport.min.y, 0.0f}); + view = glmth_scale(view, (v3) {ppm, ppm, 1.0f}); + // Screen origin is in the upper left m4 projection = glmth_projection_ortho(0.0f, framebuffer.width, framebuffer.height, 0.0f, -1.0f, 0.0f); @@ -135,23 +141,26 @@ void game_update_and_render(struct GameMemory *game_memory, struct GameInput *ga { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, }; - f32 tile_size = ppm; - for (size_t y = 0; y < ROOM_TILE_DIM_Y; y++) { for (size_t x = 0; x < ROOM_TILE_DIM_X; x++) { + // One tile is one square meter + f32 tile_size = 1.0f; + v2 tile_pos = { + x, + ROOM_TILE_DIM_Y - 1.0f - y, + }; u32 tile_id = tiles[y][x]; + m4 model = glmth_m4_init_id(); // 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_translate(model, (v3) {tile_size * 0.5f, tile_size * 0.5f, 0.0f}); + model = glmth_translate(model, (v3) {tile_pos.x, tile_pos.y, 0.0f}); model = glmth_scale(model, (v3) {tile_size, tile_size, 1.0f}); - m4 view = glmth_m4_init_id(); - v3 color; if (tile_id > 0) { @@ -162,7 +171,6 @@ void game_update_and_render(struct GameMemory *game_memory, struct GameInput *ga color = (v3) {0.3f, 0.3f, 0.3f}; } shader_setv3(&game_state->tiles.shader, "color", &color); - shader_setm4(&game_state->tiles.shader, "model", &model); shader_setm4(&game_state->tiles.shader, "view", &view); shader_setm4(&game_state->tiles.shader, "projection", &projection); @@ -213,31 +221,6 @@ void game_update_and_render(struct GameMemory *game_memory, struct GameInput *ga model = glmth_translate(model, (v3) {player.pos.x, player.pos.y, 0.0f}); model = glmth_scale(model, (v3) {player_dim.x, player_dim.y, 1.0f}); - m4 view = glmth_m4_init_id(); - view = glmth_translate(view, (v3) {0.0f, framebuffer.height, 0.0f}); - view = glmth_scale(view, (v3) {1.0f, -1.0f, 1.0f}); - view = glmth_translate(view, (v3) {viewport.min.x, viewport.min.y, 0.0f}); - view = glmth_scale(view, (v3) {ppm, ppm, 1.0f}); - - { - printf("Framebuffer :"); - glmth_v2_print((v2) {framebuffer.width, framebuffer.height}); - printf("Viewport min:"); - glmth_v2_print(viewport.min); - printf("Viewport max:"); - glmth_v2_print(viewport.max); - printf("World :"); - glmth_v2_print(game_state->player.pos); - // TODO: Why does screen get larger than the framebuffer dimensions? - v4 screen = glmth_m4v4_m(model, (v4) {player.pos.x, player.pos.y, 0.0f, 1.0f}); - printf("Screen :"); - glmth_v2_print((v2) {screen.x, screen.y}); - v4 ndc = glmth_m4v4_m(projection, screen); - // TODO: Why isn't NDC between -1.0 an 1.0? - printf("NDC :"); - glmth_v2_print((v2) {ndc.x, ndc.y}); - } - v3 color = (v3) { 1.0f, 0.0f, 1.0f }; shader_setv3(&game_state->tiles.shader, "color", &color);