a-game

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

commit 45154d7306f1a3100061ddbdcf6e5823853d63e6
parent a472b7d982bb747c54b6177e47844476e0d32b2d
Author: amin <dev@aminmesbah.com>
Date:   Sun, 21 Apr 2019 23:13:18 +0000

Use simple function to render quads for debug viz

FossilOrigin-Name: a7f05133bf5dbd85b02ad5cb7ed39d7d8db92bc23e0f8852a3561b31c735d87e
Diffstat:
Msrc/game.c | 178++++++++++++++++++++++++++++++++++++++++++++-----------------------------------
Msrc/game.h | 1+
2 files changed, 100 insertions(+), 79 deletions(-)

diff --git a/src/game.c b/src/game.c @@ -13,14 +13,6 @@ internal void game_init(struct GameMemory *game_memory, v2u framebuffer) { struct GameState *game_state = game_memory->game_state; - v2 a = {1.0f, 1.0f}; - v3 b = {1.0f, 1.0f, 1.0f}; - v4 c = {1.0f, 1.0f, 1.0f, 1.0f}; - glmth_print(a); - glmth_print(b); - glmth_print(c); - glmth_print(((v2) {1.0f, 1.0f})); - // init player game_state->player.pos = (v2) {12.5f, 5.0f}; @@ -169,6 +161,75 @@ void game_update_and_render(struct GameMemory *game_memory, struct GameInput *ga // One tile is one square meter f32 tile_size = 1.0f; + // render tiles + { + glBindVertexArray(game_state->tiles.vao); + shader_use(&game_state->tiles.shader); + + for (size_t y = 0; y < ROOM_TILE_DIM_Y; y++) + { + for (size_t x = 0; x < ROOM_TILE_DIM_X; x++) + { + 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 * 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}); + + v3 color; + + if (tile_id > 0) + { + color = (v3) {0.4f, 0.4f, 0.4f}; + } + else + { + color = (v3) {0.3f, 0.3f, 0.3f}; + } + + { + struct Entity player = game_state->player; + f32 half_w = 0.5f * player.dimensions.width; + f32 half_h = 0.5f * player.dimensions.height; + rect player_aabb = { + .min = {player.pos.x - half_w, player.pos.y - half_h}, + .max = {player.pos.x + half_w, player.pos.y + half_h}, + }; + rect tile_aabb = { + .min = tile_pos, + .max = {tile_pos.x + tile_size, tile_pos.y + tile_size}, + }; + + bool player_is_in_tile = glmth_aabb_intersect(player_aabb, tile_aabb); + if (player_is_in_tile) + { + color = (v3) {0.4f, 0.8f, 0.4f}; + } + if (player_is_in_tile && tile_id > 0) + { + color = (v3) {0.8f, 0.4f, 0.4f}; + } + } + + 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); + + glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); + } + } + glBindVertexArray(0); + } + // update player { struct Entity *player = &game_state->player; @@ -217,13 +278,18 @@ void game_update_and_render(struct GameMemory *game_memory, struct GameInput *ga v2 next_player_pos = glmth_v2_a(player->pos, glmth_v2f_m(player->velocity, dt)); v2i player_tile = {(i32)next_player_pos.x, (i32)next_player_pos.y}; bool next_pos_is_valid = true; - for (i32 y = player_tile.y - 1; y <= player_tile.y + 1; y++) + rect search_range = { + .min = {player_tile.x - 1, player_tile.y - 1}, + .max = {player_tile.x + 2, player_tile.y + 2}, + }; + render_debug_quad(game_state, search_range, (v3) {0.8f, 0.8f, 0.8f}, &view, &projection); + for (i32 y = search_range.min.y; y <= search_range.max.y; y++) { if (y < 0 || y >= ROOM_TILE_DIM_Y) { continue; } - for (i32 x = player_tile.x - 1; x <= player_tile.x + 1; x++) + for (i32 x = search_range.min.x; x <= search_range.max.x + 1; x++) { if (x < 0 || x >= ROOM_TILE_DIM_X) { @@ -257,75 +323,6 @@ void game_update_and_render(struct GameMemory *game_memory, struct GameInput *ga } - // render tiles - { - glBindVertexArray(game_state->tiles.vao); - shader_use(&game_state->tiles.shader); - - for (size_t y = 0; y < ROOM_TILE_DIM_Y; y++) - { - for (size_t x = 0; x < ROOM_TILE_DIM_X; x++) - { - 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 * 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}); - - v3 color; - - if (tile_id > 0) - { - color = (v3) {0.4f, 0.4f, 0.4f}; - } - else - { - color = (v3) {0.3f, 0.3f, 0.3f}; - } - - { - struct Entity player = game_state->player; - f32 half_w = 0.5f * player.dimensions.width; - f32 half_h = 0.5f * player.dimensions.height; - rect player_aabb = { - .min = {player.pos.x - half_w, player.pos.y - half_h}, - .max = {player.pos.x + half_w, player.pos.y + half_h}, - }; - rect tile_aabb = { - .min = tile_pos, - .max = {tile_pos.x + tile_size, tile_pos.y + tile_size}, - }; - - bool player_is_in_tile = glmth_aabb_intersect(player_aabb, tile_aabb); - if (player_is_in_tile) - { - color = (v3) {0.4f, 0.8f, 0.4f}; - } - if (player_is_in_tile && tile_id > 0) - { - color = (v3) {0.8f, 0.4f, 0.4f}; - } - } - - 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); - - glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); - } - } - glBindVertexArray(0); - } - // render player { shader_use(&game_state->player.shader); @@ -355,3 +352,26 @@ internal void game_cleanup(struct GameMemory *game_memory) glDeleteBuffers(1, &game_state->tiles.vbo); glDeleteBuffers(1, &game_state->tiles.ebo); } + +internal void render_debug_quad(struct GameState *game_state, rect r, v3 color, m4 *view, m4 *projection) +{ + glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); + glBindVertexArray(game_state->tiles.vao); + shader_use(&game_state->tiles.shader); + + v2 dim = {r.max.x - r.min.x, r.max.y - r.min.y}; + + m4 model = glmth_m4_init_id(); + model = glmth_translate(model, (v3) {dim.x * 0.5f, dim.y * 0.5f, 0.0f}); + model = glmth_translate(model, (v3) {r.min.x, r.min.y, 0.0f}); + model = glmth_scale(model, (v3) {dim.width, dim.height, 1.0f}); + + 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); + + glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); + glBindVertexArray(0); + glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); +} diff --git a/src/game.h b/src/game.h @@ -41,3 +41,4 @@ struct GameState internal void game_init(struct GameMemory *game_memory, v2u framebuffer); internal void game_cleanup(struct GameMemory *game_memory); +internal void render_debug_quad(struct GameState *game_state, rect r, v3 color, m4 *view, m4 *projection);