a-game

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

commit 17b68bf6d0c99abb98db0264c28d50b9bfbdb40c
parent 661fdbaec8299826e20d386da3476cebd9820239
Author: amin <dev@aminmesbah.com>
Date:   Mon,  1 Apr 2019 01:13:34 +0000

Remove unused code

FossilOrigin-Name: 188da5ab1c52e2872414aa0f59cdfacf0c6f498a91ec0f280b86cce1100dc611
Diffstat:
Rshader/star_f.glsl -> shader/main_f.glsl | 0
Rshader/star_v.glsl -> shader/main_v.glsl | 0
Dshader/triangle_f.glsl | 10----------
Dshader/triangle_v.glsl | 15---------------
Msrc/game.c | 154+++++++------------------------------------------------------------------------
Msrc/game.h | 18++----------------
6 files changed, 15 insertions(+), 182 deletions(-)

diff --git a/shader/star_f.glsl b/shader/main_f.glsl diff --git a/shader/star_v.glsl b/shader/main_v.glsl diff --git a/shader/triangle_f.glsl b/shader/triangle_f.glsl @@ -1,10 +0,0 @@ -#version 330 core - -in vec4 vertex_color; - -out vec4 frag_color; - -void main() -{ - frag_color = vertex_color; -} diff --git a/shader/triangle_v.glsl b/shader/triangle_v.glsl @@ -1,15 +0,0 @@ -#version 330 core - -layout (location = 0) in vec2 position; -layout (location = 1) in vec3 color; - -out vec4 vertex_color; - -uniform mat4 model; -uniform mat4 projection; - -void main() -{ - gl_Position = projection * model * vec4(position, 0.0f, 1.0f); - vertex_color = vec4(color, 1.0f); -} diff --git a/src/game.c b/src/game.c @@ -49,75 +49,6 @@ void game_init(struct GameState *game_state, v2u framebuffer) // init player game_state->player.pos = (v2) { screen.width / 2.0f, screen.height / 2.0f }; - // set up and load star vertex data - { - f32 max_speed = 0.3f; - game_state->stars.num_stars = NUM_STARS; - for (size_t i = 0; i < game_state->stars.num_stars; ++i) - { - game_state->stars.velocities[i].x = randf(-max_speed, max_speed); - game_state->stars.velocities[i].y = randf(-max_speed, max_speed); - } - - for (size_t i = 0; i < game_state->stars.num_stars; ++i) - { - game_state->stars.positions[i].x = (f32)(rand() % screen.width); - game_state->stars.positions[i].y = (f32)(rand() % screen.height); - } - - GLuint star_vao_id; - GLuint star_vbo_id; - glGenVertexArrays(1, &star_vao_id); - glGenBuffers(1, &star_vbo_id); - - glBindVertexArray(star_vao_id); - glBindBuffer(GL_ARRAY_BUFFER, star_vbo_id); - glBufferData(GL_ARRAY_BUFFER, sizeof(game_state->stars.positions), game_state->stars.positions, GL_DYNAMIC_DRAW); - - // positions - glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, 0); - glEnableVertexAttribArray(0); - glBindVertexArray(0); - - game_state->star_vao_id = star_vao_id; - game_state->star_vbo_id = star_vbo_id; - } - - // load triangle vertex data - { - f32 circumradius = 10.0f; - f32 inradius = circumradius * sinf(glmth_rad(30.0f)); - f32 half_side_length = circumradius * cosf(glmth_rad(30.0f)); - - GLfloat triangle_vertices[] = { - // positions // colors - -half_side_length, -inradius, 1.0f, 0.0f, 0.0f, - half_side_length, -inradius, 0.0f, 1.0f, 0.0f, - 0.0f, circumradius, 0.0f, 0.0f, 1.0f, - }; - - GLuint triangle_vao_id; - GLuint triangle_vbo_id; - glGenVertexArrays(1, &triangle_vao_id); - glGenBuffers(1, &triangle_vbo_id); - - glBindVertexArray(triangle_vao_id); - glBindBuffer(GL_ARRAY_BUFFER, triangle_vbo_id); - glBufferData(GL_ARRAY_BUFFER, sizeof(triangle_vertices), triangle_vertices, GL_STATIC_DRAW); - - // positions - glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(*triangle_vertices), (GLvoid*)0); - glEnableVertexAttribArray(0); - - // colors - glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(*triangle_vertices), (GLvoid*)(2 * sizeof(*triangle_vertices))); - glEnableVertexAttribArray(1); - glBindVertexArray(0); - - game_state->triangle_vao_id = triangle_vao_id; - game_state->triangle_vbo_id = triangle_vbo_id; - } - // set up and load tiles { GLfloat square_vertices[] = { @@ -161,8 +92,9 @@ void game_init(struct GameState *game_state, v2u framebuffer) game_state->tiles.count = 1; } - game_state->star_shader = shader_compile("shader/star_v.glsl", "shader/star_f.glsl"); - game_state->triangle_shader = shader_compile("shader/triangle_v.glsl", "shader/triangle_f.glsl"); + struct Shader main_shader = shader_compile("shader/main_v.glsl", "shader/main_f.glsl"); + game_state->tiles.shader = main_shader; + game_state->player.shader = main_shader; } @@ -173,23 +105,6 @@ void game_update_and_render(struct GameState *game_state, f32 dt, v2u framebuffe framebuffer.height, }; - // update stars - { - for (size_t i = 0; i < game_state->stars.num_stars; ++i) - { - game_state->stars.positions[i].x += game_state->stars.velocities[i].x; - game_state->stars.positions[i].x = wrap(game_state->stars.positions[i].x, 0, screen.width); - - game_state->stars.positions[i].y += game_state->stars.velocities[i].y; - game_state->stars.positions[i].y = wrap(game_state->stars.positions[i].y, 0, screen.height); - } - - glBindVertexArray(game_state->star_vao_id); - glBindBuffer(GL_ARRAY_BUFFER, game_state->star_vbo_id); - glBufferData(GL_ARRAY_BUFFER, sizeof(game_state->stars.positions), game_state->stars.positions, GL_DYNAMIC_DRAW); - glBindVertexArray(0); - } - glClearColor(0.1f, 0.1f, 0.1f, 1.0f); glClear(GL_COLOR_BUFFER_BIT); @@ -198,7 +113,7 @@ void game_update_and_render(struct GameState *game_state, f32 dt, v2u framebuffe // render tiles { glBindVertexArray(game_state->tiles.vao); - shader_use(&game_state->star_shader); + shader_use(&game_state->tiles.shader); u32 tile_map[9][16] = { { 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, }, { 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, }, @@ -237,7 +152,7 @@ void game_update_and_render(struct GameState *game_state, f32 dt, v2u framebuffe model = glmth_translate(model, (v3) {start.x + (tile_size * x), start.y + (tile_size * y), 0.0f}); model = glmth_scale(model, (v3) {tile_size, tile_size, 1.0f}); - shader_setm4(&game_state->star_shader, "model", &model); + shader_setm4(&game_state->tiles.shader, "model", &model); v3 color; if (tile_id > 0) { @@ -247,10 +162,10 @@ void game_update_and_render(struct GameState *game_state, f32 dt, v2u framebuffe { color = (v3) {0.3f, 0.3f, 0.3f}; } - shader_setv3(&game_state->star_shader, "color", &color); + 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); - shader_setm4(&game_state->star_shader, "projection", &projection); + shader_setm4(&game_state->tiles.shader, "projection", &projection); glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); } @@ -284,70 +199,27 @@ void game_update_and_render(struct GameState *game_state, f32 dt, v2u framebuffe // render player { + shader_use(&game_state->player.shader); struct Entity player = game_state->player; glBindVertexArray(game_state->tiles.vao); m4 model = glmth_m4_init_id(); model = glmth_translate(model, (v3) { player.pos.x, player.pos.y, 0.0f }); model = glmth_scale(model, (v3) {15.0f, 30.0f, 1.0f}); - shader_setm4(&game_state->star_shader, "model", &model); + shader_setm4(&game_state->tiles.shader, "model", &model); v3 color = (v3) { 1.0f, 0.0f, 1.0f }; - shader_setv3(&game_state->star_shader, "color", &color); + 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); - shader_setm4(&game_state->star_shader, "projection", &projection); + shader_setm4(&game_state->tiles.shader, "projection", &projection); glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); glBindVertexArray(0); } - -#if 0 - // render stars - { - m4 model = glmth_m4_init_id(); - - shader_use(&game_state->star_shader); - shader_setm4(&game_state->star_shader, "model", &model); - - m4 projection = glmth_m4_init_id(); - projection = glmth_projection_ortho(0.0f, screen.width, screen.height, 0.0f, -1.0f, 1.0f); - shader_setm4(&game_state->star_shader, "projection", &projection); - v3 color = (v3) {1.0f, 1.0f, 1.0f}; - shader_setv3(&game_state->star_shader, "color", &color); - - glBindVertexArray(game_state->star_vao_id); - glDrawArrays(GL_POINTS, 0, game_state->stars.num_stars - 1); - glBindVertexArray(0); - } - - // render triangle - { - m4 model = glmth_m4_init_id(); - model = glmth_translate(model, (v3) {screen.width / 2.0f, screen.height / 2.0f, 0.0f}); - model = glmth_translate(model, (v3) {100.0f * cosf(dt), 100.0f * sinf(dt), 0.0f}); - model = glmth_rotate_z(model, -dt); - model = glmth_scale(model, (v3) {10.0f, 10.0f, 10.0f}); - f32 scale_factor = fabs(1.0f * sinf(dt)); - model = glmth_scale(model, (v3) {scale_factor, scale_factor, scale_factor}); - - shader_use(&game_state->triangle_shader); - shader_setm4(&game_state->triangle_shader, "model", &model); - - m4 projection = glmth_m4_init_id(); - projection = glmth_projection_ortho(0.0f, screen.width, screen.height, 0.0f, -1.0f, 1.0f); - shader_setm4(&game_state->triangle_shader, "projection", &projection); - - glBindVertexArray(game_state->triangle_vao_id); - glDrawArrays(GL_TRIANGLES, 0, 3); - glBindVertexArray(0); - } -#endif } void game_cleanup(struct GameState *game_state) { - glDeleteVertexArrays(1, &game_state->star_vao_id); - glDeleteVertexArrays(1, &game_state->triangle_vao_id); - glDeleteBuffers(1, &game_state->star_vbo_id); - glDeleteBuffers(1, &game_state->triangle_vbo_id); + glDeleteVertexArrays(1, &game_state->tiles.vao); + glDeleteBuffers(1, &game_state->tiles.vbo); } diff --git a/src/game.h b/src/game.h @@ -11,21 +11,13 @@ #include "glmth.h" #include "shader.h" -#define NUM_STARS 10000 - -struct Stars -{ - u32 num_stars; - v2 velocities[NUM_STARS]; - v2 positions[NUM_STARS]; -}; - struct Tiles { GLuint vao; GLuint vbo; GLuint ebo; u32 count; + struct Shader shader; }; enum InputKeyAction @@ -46,18 +38,12 @@ struct GameInput struct Entity { v2 pos; + struct Shader shader; }; struct GameState { struct Tiles tiles; - GLuint star_vao_id; - GLuint star_vbo_id; - GLuint triangle_vao_id; - GLuint triangle_vbo_id; - struct Shader star_shader; - struct Shader triangle_shader; - struct Stars stars; struct GameInput input; struct Entity player; };