a-game

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

commit 4076e7945b53cf1f776a72dda0801475a0eeb445
parent 0cef6f18a8140f33375b8c6a1b1e248c552db435
Author: amin <dev@aminmesbah.com>
Date:   Thu, 25 Apr 2019 22:10:28 +0000

Simplify and clarify rendering data

FossilOrigin-Name: 97446262cc66e8dfb5a3472653c22451ad7e3ce04e7df0a4a87d88f2ec74177b
Diffstat:
Msrc/game.c | 67++++++++++++++++++++++++++++++-------------------------------------
Msrc/game.h | 9++++-----
2 files changed, 34 insertions(+), 42 deletions(-)

diff --git a/src/game.c b/src/game.c @@ -35,15 +35,17 @@ internal void game_init(struct GameMemory *game_memory, v2u framebuffer) GLuint square_elements[] = { 0, 1, 3, 1, 2, 3 }; GLuint vao; - GLuint vbo; - GLuint ebo; + GLuint quad_vbo; + GLuint quad_ebo; glGenVertexArrays(1, &vao); - glGenBuffers(1, &vbo); - glGenBuffers(1, &ebo); + glGenBuffers(1, &quad_vbo); + glGenBuffers(1, &quad_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, vbo); + glBindBuffer(GL_ARRAY_BUFFER, quad_vbo); glBufferData(GL_ARRAY_BUFFER, sizeof(square_vertices), square_vertices, GL_STATIC_DRAW); glVertexAttribPointer( 0, // Data location @@ -55,14 +57,12 @@ internal void game_init(struct GameMemory *game_memory, v2u framebuffer) ); glEnableVertexAttribArray(0); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, quad_ebo); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(square_elements), square_elements, GL_STATIC_DRAW); - glBindVertexArray(0); - - game_state->tiles.vao = vao; - game_state->tiles.vbo = vbo; - game_state->tiles.ebo = ebo; + game_state->renderer.vao = vao; + game_state->renderer.quad_vbo = quad_vbo; + game_state->renderer.quad_ebo = quad_ebo; } { @@ -72,8 +72,7 @@ internal void game_init(struct GameMemory *game_memory, v2u framebuffer) struct Shader main_shader = shader_compile(v_source, f_source); free(v_source); free(f_source); - game_state->tiles.shader = main_shader; - game_state->player.shader = main_shader; + game_state->renderer.shader = main_shader; } } @@ -317,8 +316,7 @@ 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); + shader_use(&game_state->renderer.shader); for (size_t y = 0; y < ROOM_TILE_DIM_Y; y++) { @@ -374,15 +372,14 @@ void game_update_and_render(struct GameMemory *game_memory, struct GameInput *ga } } - 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); + shader_setv3(&game_state->renderer.shader, "color", &color); + shader_setm4(&game_state->renderer.shader, "model", &model); + shader_setm4(&game_state->renderer.shader, "view", &view); + shader_setm4(&game_state->renderer.shader, "projection", &projection); glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); } } - glBindVertexArray(0); } // update player @@ -612,8 +609,7 @@ void game_update_and_render(struct GameMemory *game_memory, struct GameInput *ga // render player { - shader_use(&game_state->player.shader); - glBindVertexArray(game_state->tiles.vao); + shader_use(&game_state->renderer.shader); struct Entity player = game_state->player; m4 model = glmth_m4_init_id(); @@ -622,29 +618,27 @@ void game_update_and_render(struct GameMemory *game_memory, struct GameInput *ga v3 color = (v3) { 1.0f, 0.0f, 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); + shader_setv3(&game_state->renderer.shader, "color", &color); + shader_setm4(&game_state->renderer.shader, "model", &model); + shader_setm4(&game_state->renderer.shader, "view", &view); + shader_setm4(&game_state->renderer.shader, "projection", &projection); glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); - glBindVertexArray(0); } } internal void game_cleanup(struct GameMemory *game_memory) { struct GameState *game_state = game_memory->game_state; - glDeleteVertexArrays(1, &game_state->tiles.vao); - glDeleteBuffers(1, &game_state->tiles.vbo); - glDeleteBuffers(1, &game_state->tiles.ebo); + glDeleteVertexArrays(1, &game_state->renderer.vao); + glDeleteBuffers(1, &game_state->renderer.quad_vbo); + glDeleteBuffers(1, &game_state->renderer.quad_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); + shader_use(&game_state->renderer.shader); v2 dim = {r.max.x - r.min.x, r.max.y - r.min.y}; @@ -653,12 +647,11 @@ internal void render_debug_quad(struct GameState *game_state, rect r, v3 color, 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); + shader_setv3(&game_state->renderer.shader, "color", &color); + shader_setm4(&game_state->renderer.shader, "model", &model); + shader_setm4(&game_state->renderer.shader, "view", view); + shader_setm4(&game_state->renderer.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 @@ -16,11 +16,11 @@ #define ROOM_TILE_DIM_Y 10 #define ROOM_ASPECT_RATIO (f32)ROOM_TILE_DIM_X / (f32)ROOM_TILE_DIM_Y -struct Tiles +struct RendererState { GLuint vao; - GLuint vbo; - GLuint ebo; + GLuint quad_vbo; + GLuint quad_ebo; struct Shader shader; }; @@ -30,7 +30,6 @@ struct Entity v2 velocity; v2 pos; v2 dimensions; - struct Shader shader; }; struct Room @@ -41,7 +40,7 @@ struct Room struct GameState { - struct Tiles tiles; + struct RendererState renderer; struct Entity player; };