a-game

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

commit 02150a817068f774af47a986ae703d08baac3152
parent bb93277e46ee55dc501cb4c74eb2b4a654ae1147
Author: amin <dev@aminmesbah.com>
Date:   Wed, 20 Mar 2019 04:19:19 +0000

Clean things up ever so slightly

FossilOrigin-Name: 95ba01931d9f7e4ef170d0481eedcea65c0c094d8ce120e39947ccb836ce1349
Diffstat:
Msrc/game.c | 52+++++++++++++++++++++++++++++++---------------------
1 file changed, 31 insertions(+), 21 deletions(-)

diff --git a/src/game.c b/src/game.c @@ -41,8 +41,10 @@ f32 randf(f32 min, f32 max) void game_init(struct GameState *game_state, v2u framebuffer) { - u32 screen_width = framebuffer.width; - u32 screen_height = framebuffer.height; + v2u screen = (v2u) { + framebuffer.width, + framebuffer.height, + }; // set up and load star vertex data { @@ -56,8 +58,8 @@ void game_init(struct GameState *game_state, v2u framebuffer) 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); + game_state->stars.positions[i].x = (f32)(rand() % screen.width); + game_state->stars.positions[i].y = (f32)(rand() % screen.height); } GLuint star_vao_id; @@ -163,17 +165,19 @@ void game_init(struct GameState *game_state, v2u framebuffer) void game_update_and_render(struct GameState *game_state, f32 dt, v2u framebuffer) { - u32 screen_width = framebuffer.width; - u32 screen_height = framebuffer.height; + v2u screen = (v2u) { + framebuffer.width, + 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].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); + game_state->stars.positions[i].y = wrap(game_state->stars.positions[i].y, 0, screen.height); } glBindVertexArray(game_state->star_vao_id); @@ -205,25 +209,29 @@ void game_update_and_render(struct GameState *game_state, f32 dt, v2u framebuffe { 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, }, }; - f32 size = ((f32)screen_width) / 16.0f; - if (size * 9.0f > screen_height) + f32 tile_size = ((f32)screen.width) / 16.0f; + if (tile_size * 9.0f > screen.height) { - size = ((f32)screen_height) / 9.0f; + tile_size = ((f32)screen.height) / 9.0f; } - f32 tilemap_width = size * 16.0f; - f32 tilemap_height = size * 9.0f; + v2 tilemap = (v2) { + tile_size * 16.0f, + tile_size * 9.0f, + }; - f32 startx = size / 2.0f + ((screen_width - tilemap_width) / 2.0f); - f32 starty = size / 2.0f + ((screen_height - tilemap_height) / 2.0f); + v2 start = (v2) { + tile_size / 2.0f + ((screen.width - tilemap.width) / 2.0f), + tile_size / 2.0f + ((screen.height - tilemap.height) / 2.0f), + }; for (size_t y = 0; y < 9; y++) { for (size_t x = 0; x < 16; x++) { u32 tile_id = tile_map[y][x]; m4 model = glmth_m4_init_id(); - model = glmth_translate(model, (v3) {startx + (size * x), starty + (size * y), 0.0f}); - model = glmth_scale(model, (v3) {size, size, 1.0f}); + 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); v3 color; @@ -237,7 +245,7 @@ void game_update_and_render(struct GameState *game_state, f32 dt, v2u framebuffe } shader_setv3(&game_state->star_shader, "color", &color); - m4 projection = glmth_projection_ortho(0.0f, screen_width, screen_height, 0.0f, -1.0f, 0.0f); + 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); glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); @@ -246,6 +254,7 @@ void game_update_and_render(struct GameState *game_state, f32 dt, v2u framebuffe glBindVertexArray(0); } +#if 0 // render stars { m4 model = glmth_m4_init_id(); @@ -254,7 +263,7 @@ void game_update_and_render(struct GameState *game_state, f32 dt, v2u framebuffe 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); + 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); @@ -267,7 +276,7 @@ void game_update_and_render(struct GameState *game_state, f32 dt, v2u framebuffe // 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) {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}); @@ -278,13 +287,14 @@ void game_update_and_render(struct GameState *game_state, f32 dt, v2u framebuffe 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); + 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 }