a-game

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

commit 3418f76c8a1741b85df476212d4375f860c9c9bc
parent 4076e7945b53cf1f776a72dda0801475a0eeb445
Author: amin <dev@aminmesbah.com>
Date:   Thu, 25 Apr 2019 22:52:59 +0000

Make the debug quads look nicer

FossilOrigin-Name: f9765db60ad8d5f8cbd6fce6f333841879f846f498958324db5b338c75096c75
Diffstat:
Msrc/game.c | 33++++++++++++++++++++++++++-------
Msrc/game.h | 1+
2 files changed, 27 insertions(+), 7 deletions(-)

diff --git a/src/game.c b/src/game.c @@ -25,28 +25,31 @@ internal void game_init(struct GameMemory *game_memory, v2u framebuffer) // set up and load tiles { - GLfloat square_vertices[] = { + GLfloat quad_vertices[] = { 0.5f, 0.5f, 0.5f, -0.5f, -0.5f, -0.5f, -0.5f, 0.5f, }; - GLuint square_elements[] = { 0, 1, 3, 1, 2, 3 }; + GLuint quad_elements[] = { 0, 1, 3, 1, 2, 3 }; + GLuint rect_elements[] = { 0, 1, 1, 2, 2, 3, 3, 0 }; GLuint vao; GLuint quad_vbo; GLuint quad_ebo; + GLuint rect_ebo; glGenVertexArrays(1, &vao); glGenBuffers(1, &quad_vbo); glGenBuffers(1, &quad_ebo); + glGenBuffers(1, &rect_ebo); // NOTE(amin): We will leave this bound for the duration of the game. // There should not be any calls to glBindVertexArray(0). glBindVertexArray(vao); glBindBuffer(GL_ARRAY_BUFFER, quad_vbo); - glBufferData(GL_ARRAY_BUFFER, sizeof(square_vertices), square_vertices, GL_STATIC_DRAW); + glBufferData(GL_ARRAY_BUFFER, sizeof(quad_vertices), quad_vertices, GL_STATIC_DRAW); glVertexAttribPointer( 0, // Data location 2, // Number of values @@ -58,11 +61,17 @@ internal void game_init(struct GameMemory *game_memory, v2u framebuffer) glEnableVertexAttribArray(0); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, quad_ebo); - glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(square_elements), square_elements, GL_STATIC_DRAW); + glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(quad_elements), quad_elements, GL_STATIC_DRAW); + + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, rect_ebo); + glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(rect_elements), rect_elements, GL_STATIC_DRAW); + + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, quad_ebo); game_state->renderer.vao = vao; game_state->renderer.quad_vbo = quad_vbo; game_state->renderer.quad_ebo = quad_ebo; + game_state->renderer.rect_ebo = rect_ebo; } { @@ -441,10 +450,8 @@ void game_update_and_render(struct GameMemory *game_memory, struct GameInput *ga .min = {glmth_min(player->pos.x, new_p.x), glmth_min(player->pos.y, new_p.y)}, .max = {glmth_max(player->pos.x, new_p.x), glmth_max(player->pos.y, new_p.y)}, }; - //RENDER_COLLISION_DEBUG_QUAD(player_traversal_bb, ((v3) {0.4f, 0.4f, 0.4f})); rect player_traversal_occupancy_bb = glmth_minkowski_sum_rect_rect(player_traversal_bb, player->dimensions); - //RENDER_COLLISION_DEBUG_QUAD(player_traversal_occupancy_bb, ((v3) {0.8f, 0.4f, 0.8f})); rect tile_search_range = { .min = { @@ -633,10 +640,20 @@ internal void game_cleanup(struct GameMemory *game_memory) glDeleteVertexArrays(1, &game_state->renderer.vao); glDeleteBuffers(1, &game_state->renderer.quad_vbo); glDeleteBuffers(1, &game_state->renderer.quad_ebo); + glDeleteBuffers(1, &game_state->renderer.rect_ebo); } internal void render_debug_quad(struct GameState *game_state, rect r, v3 color, m4 *view, m4 *projection) { + GLint initial_ebo = 0; + // TODO: Remove this potentially expensive call and just track the current + // EBO manually. + glGetIntegerv(GL_ELEMENT_ARRAY_BUFFER_BINDING, &initial_ebo); + // NOTE(amin): If the GLint we get is less than zero, it will underflow + // when it gets converted to a GLuint as an argument to glBindBuffer. + assert(initial_ebo >= 0); + + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, game_state->renderer.rect_ebo); glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); shader_use(&game_state->renderer.shader); @@ -652,6 +669,8 @@ internal void render_debug_quad(struct GameState *game_state, rect r, v3 color, shader_setm4(&game_state->renderer.shader, "view", view); shader_setm4(&game_state->renderer.shader, "projection", projection); - glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); + glDrawElements(GL_LINES, 8, GL_UNSIGNED_INT, 0); glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); + + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, initial_ebo); } diff --git a/src/game.h b/src/game.h @@ -21,6 +21,7 @@ struct RendererState GLuint vao; GLuint quad_vbo; GLuint quad_ebo; + GLuint rect_ebo; struct Shader shader; };