a-game

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

commit 1f894f9b1b1cfe19b91b8293257db05d7ae02522
parent 66a432812e55acc8c8c941f18af82f6cafb0f853
Author: amin <dev@aminmesbah.com>
Date:   Tue,  5 Mar 2019 05:29:32 +0000

Use custom types everywhere in game layer

FossilOrigin-Name: 1797b8fe012137b6efda5ff5c93a930604301b9bb15dba9fee9cbd957c42da44
Diffstat:
Msrc/game.c | 130++++++++++++++++++++++++++++++++++++++++----------------------------------------
Msrc/game.h | 10++++++----
Msrc/glmth.c | 4++--
Msrc/glmth.h | 15++-------------
Msrc/shader.c | 4++--
Msrc/shader.h | 2+-
Asrc/types.h | 10++++++++++
7 files changed, 88 insertions(+), 87 deletions(-)

diff --git a/src/game.c b/src/game.c @@ -12,7 +12,7 @@ void game_load_opengl_symbols(void) #endif -float wrap(float n, float min, float max) +f32 wrap(f32 n, f32 min, f32 max) { if (n > max) { @@ -29,24 +29,24 @@ float wrap(float n, float min, float max) } -float randf(float min, float max) +f32 randf(f32 min, f32 max) { assert(min < max); - float random = ((float) rand()) / (float) RAND_MAX; - float diff = max - min; - float r = random * diff; + f32 random = ((f32) rand()) / (f32) RAND_MAX; + f32 diff = max - min; + f32 r = random * diff; return min + r; } void game_init(struct GameState *game_state, v2u framebuffer) { - uint32_t screen_width = framebuffer.width; - uint32_t screen_height = framebuffer.height; + u32 screen_width = framebuffer.width; + u32 screen_height = framebuffer.height; // set up and load star vertex data { - float max_speed = 0.3f; + f32 max_speed = 0.3f; game_state->stars.num_stars = NUM_STARS; for (size_t i = 0; i < game_state->stars.num_stars; ++i) { @@ -56,8 +56,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 = (float)(rand() % screen_width); - game_state->stars.positions[i].y = (float)(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; @@ -80,9 +80,9 @@ void game_init(struct GameState *game_state, v2u framebuffer) // load triangle vertex data { - float circumradius = 10.0f; - float inradius = circumradius * sinf(glmth_rad(30.0f)); - float half_side_length = circumradius * cosf(glmth_rad(30.0f)); + 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 @@ -161,10 +161,10 @@ void game_init(struct GameState *game_state, v2u framebuffer) } -void game_update_and_render(struct GameState *game_state, float dt, v2u framebuffer) +void game_update_and_render(struct GameState *game_state, f32 dt, v2u framebuffer) { - uint32_t screen_width = framebuffer.width; - uint32_t screen_height = framebuffer.height; + u32 screen_width = framebuffer.width; + u32 screen_height = framebuffer.height; // update stars { for (size_t i = 0; i < game_state->stars.num_stars; ++i) @@ -187,51 +187,11 @@ void game_update_and_render(struct GameState *game_state, float dt, v2u framebuf //glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); - // 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 = glmth_v3_init(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, glmth_v3_init(screen_width / 2.0f, screen_height / 2.0f, 0.0f)); - model = glmth_translate(model, glmth_v3_init(100.0f * cosf(dt), 100.0f * sinf(dt), 0.0f)); - model = glmth_rotate_z(model, -dt); - model = glmth_scale(model, glmth_v3_init(10.0f, 10.0f, 10.0f)); - float scale_factor = fabs(1.0f * sinf(dt)); - model = glmth_scale(model, glmth_v3_init(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); - } - // render tiles { glBindVertexArray(game_state->tiles.vao); shader_use(&game_state->star_shader); - uint32_t tile_map[9][16] = { + 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, }, { 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, }, @@ -245,35 +205,35 @@ void game_update_and_render(struct GameState *game_state, float dt, v2u framebuf { 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, }, }; - float size; + f32 size; if (screen_width < screen_height) { - size = ((float)screen_width) / 16.0f; + size = ((f32)screen_width) / 16.0f; } else { - size = ((float)screen_height) / 9.0f; + size = ((f32)screen_height) / 9.0f; } - float start = size / 2.0f; + f32 start = size / 2.0f; for (size_t y = 0; y < 9; y++) { for (size_t x = 0; x < 16; x++) { - uint32_t tile_id = tile_map[y][x]; + u32 tile_id = tile_map[y][x]; m4 model = glmth_m4_init_id(); - model = glmth_translate(model, glmth_v3_init(start + ((size + 1.0f) * x), start + ((size + 1.0f) * y), 0.0f)); + model = glmth_translate(model, glmth_v3_init(start + (size * x), start + (size * y), 0.0f)); model = glmth_scale(model, glmth_v3_init(size, size, 1.0f)); shader_setm4(&game_state->star_shader, "model", &model); v3 color; if (tile_id > 0) { - color = glmth_v3_init(0.0f, 1.0f, 0.0f); + color = (v3) {0.4f, 0.4f, 0.4f}; } else { - //color = glmth_v3_init(1.0f, 0.0f, 0.0f); + color = (v3) {0.3f, 0.3f, 0.3f}; } shader_setv3(&game_state->star_shader, "color", &color); @@ -285,6 +245,46 @@ void game_update_and_render(struct GameState *game_state, float dt, v2u framebuf } glBindVertexArray(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 = glmth_v3_init(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, glmth_v3_init(screen_width / 2.0f, screen_height / 2.0f, 0.0f)); + model = glmth_translate(model, glmth_v3_init(100.0f * cosf(dt), 100.0f * sinf(dt), 0.0f)); + model = glmth_rotate_z(model, -dt); + model = glmth_scale(model, glmth_v3_init(10.0f, 10.0f, 10.0f)); + f32 scale_factor = fabs(1.0f * sinf(dt)); + model = glmth_scale(model, glmth_v3_init(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); + } } diff --git a/src/game.h b/src/game.h @@ -1,11 +1,13 @@ #include <assert.h> #include <math.h> #include <stdbool.h> +#include <stdint.h> #include <stdio.h> #include <stdlib.h> #include <glad/glad.h> +#include "types.h" #include "glmth.h" #include "shader.h" @@ -13,7 +15,7 @@ struct Stars { - uint32_t num_stars; + u32 num_stars; v2 velocities[NUM_STARS]; v2 positions[NUM_STARS]; }; @@ -23,7 +25,7 @@ struct Tiles GLuint vao; GLuint vbo; GLuint ebo; - uint32_t count; + u32 count; }; struct GameState @@ -46,8 +48,8 @@ typedef void (game_load_opengl_symbols_func)(void); void game_load_opengl_symbols(void); #endif // PLATFORM_HOTLOAD_GAME_CODE -typedef void (game_update_and_render_func)(struct GameState *game_state, float dt, v2u framebuffer); -void game_update_and_render(struct GameState *game_state, float dt, v2u framebuffer); +typedef void (game_update_and_render_func)(struct GameState *game_state, f32 dt, v2u framebuffer); +void game_update_and_render(struct GameState *game_state, f32 dt, v2u framebuffer); void game_init(struct GameState *game_state, v2u framebuffer); void game_cleanup(struct GameState *game_state); diff --git a/src/glmth.c b/src/glmth.c @@ -183,7 +183,7 @@ bool glmth_v4v4_eq(v4 vec1, v4 vec2) } -void glmth_clampf(float *f, float min, float max) +void glmth_clampf(f32 *f, f32 min, f32 max) { if (*f < min) { @@ -202,7 +202,7 @@ f32 glmth_deg(f32 rad) } -float glmth_lerpf(float f, float min, float max) +f32 glmth_lerpf(f32 f, f32 min, f32 max) { assert(f >= 0.0f && f <= 1.0f); return (1.0f - f) * min + f * max; diff --git a/src/glmth.h b/src/glmth.h @@ -2,17 +2,6 @@ #define M_PI 3.14159265359f #endif -typedef uint8_t u8; -typedef uint16_t u16; -typedef uint32_t u32; -typedef uint64_t u64; -typedef int8_t s8; -typedef int16_t s16; -typedef int32_t s32; -typedef int64_t s64; -typedef float f32; -typedef double r64; - typedef union { struct @@ -125,9 +114,9 @@ v3 glmth_v3_a(v3 vec1, v3 vec2); v3 glmth_v3_s(v3 vec1, v3 vec2); void glmth_v4_print(v4 v); bool glmth_v4v4_eq(v4 vec1, v4 vec2); -void glmth_clampf(float *f, float min, float max); +void glmth_clampf(f32 *f, f32 min, f32 max); f32 glmth_deg(f32 rad); -float glmth_lerpf(float f, float min, float max); +f32 glmth_lerpf(f32 f, f32 min, f32 max); f32 glmth_rad(f32 deg); m4 glmth_rotate_x(m4 m, f32 rad); m4 glmth_rotate_y(m4 m, f32 rad); diff --git a/src/shader.c b/src/shader.c @@ -7,12 +7,12 @@ char *read_file(char *file_path) { // get file size fseek(handle, 0, SEEK_END); - uint32_t num_bytes_in_file = ftell(handle); + u32 num_bytes_in_file = ftell(handle); rewind(handle); buffer = (char*) malloc(sizeof(char) * (num_bytes_in_file + 1) ); - uint32_t bytes_read = fread(buffer, sizeof(char), num_bytes_in_file, handle); + u32 bytes_read = fread(buffer, sizeof(char), num_bytes_in_file, handle); // IMPORTANT! fread() doesn't add the '\0' buffer[num_bytes_in_file] = '\0'; diff --git a/src/shader.h b/src/shader.h @@ -1,6 +1,6 @@ struct Shader { - uint32_t program; + u32 program; }; char *read_file(char *file_path); diff --git a/src/types.h b/src/types.h @@ -0,0 +1,10 @@ +typedef uint8_t u8; +typedef uint16_t u16; +typedef uint32_t u32; +typedef uint64_t u64; +typedef int8_t i8; +typedef int16_t i16; +typedef int32_t i32; +typedef int64_t i64; +typedef float f32; +typedef double f64;