a-game

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

commit a472b7d982bb747c54b6177e47844476e0d32b2d
parent 0f0da7db2b8740a860888e4ed9d0ad9771b62f35
Author: amin <dev@aminmesbah.com>
Date:   Sun, 21 Apr 2019 22:30:47 +0000

Add a few small QOL improvements

C11 gives us _Generic and _Static_assert.

FossilOrigin-Name: 0f1c84a58743c4db91362be158295469124b586d96931abe77018354e8562335
Diffstat:
MMakefile | 2+-
Mbuild.bat | 2+-
Msrc/game.c | 54++++++++++++++++++++++++++++++++++++------------------
Msrc/glmth.h | 79++++++++++++++++++++++++++++++++++++++++++++++++++++++-------------------------
Msrc/platform_linux.c | 4++++
5 files changed, 96 insertions(+), 45 deletions(-)

diff --git a/Makefile b/Makefile @@ -1,5 +1,5 @@ CC = clang -CFLAGS = -std=c99 -Ilib -Wall -Wextra -Wshadow -Wswitch-enum -Wno-unused-parameter -Wno-unused-function -Wno-missing-braces +CFLAGS = -std=c11 -Ilib -Wall -Wextra -Wshadow -Wswitch-enum -Wno-unused-parameter -Wno-unused-function -Wno-missing-braces LDFLAGS = -ldl -lglfw -lGL -lm SRC_FILES = platform_linux.c diff --git a/build.bat b/build.bat @@ -1,6 +1,6 @@ @echo off -set CFLAGS=-std=c99 -nostdlib -D_CRT_SECURE_NO_WARNINGS -Ilib -I../include/glfw/include -Wall -Wextra -Wshadow -Wswitch-enum -Wno-unused-parameter -Wno-unused-function -Wno-missing-braces +set CFLAGS=-std=c11 -nostdlib -D_CRT_SECURE_NO_WARNINGS -Ilib -I../include/glfw/include -Wall -Wextra -Wshadow -Wswitch-enum -Wno-unused-parameter -Wno-unused-function -Wno-missing-braces set LDFLAGS=-luser32 -lgdi32 -lwinmm -lopengl32 -lshell32 set RELCFLAGS=-O2 -Os diff --git a/src/game.c b/src/game.c @@ -13,8 +13,16 @@ internal void game_init(struct GameMemory *game_memory, v2u framebuffer) { struct GameState *game_state = game_memory->game_state; + v2 a = {1.0f, 1.0f}; + v3 b = {1.0f, 1.0f, 1.0f}; + v4 c = {1.0f, 1.0f, 1.0f, 1.0f}; + glmth_print(a); + glmth_print(b); + glmth_print(c); + glmth_print(((v2) {1.0f, 1.0f})); + // init player - game_state->player.pos = (v2) {10.0f, 5.0f}; + game_state->player.pos = (v2) {12.5f, 5.0f}; // In Knytt, the player is 9 by 14 texels and a tile is 24 by 24 texels. // These dimensions are relative to a square 'meter', one tile @@ -59,7 +67,7 @@ internal void game_init(struct GameMemory *game_memory, v2u framebuffer) game_state->tiles.vao = vao; game_state->tiles.vbo = vbo; - game_state->tiles.ebo = vbo; + game_state->tiles.ebo = ebo; } { @@ -129,18 +137,33 @@ void game_update_and_render(struct GameMemory *game_memory, struct GameInput *ga m4 projection = glmth_projection_ortho(0.0f, framebuffer.width, framebuffer.height, 0.0f, -1.0f, 0.0f); // the little hanging fly village from knytt + //u32 tiles[ROOM_TILE_DIM_Y][ROOM_TILE_DIM_X] = { + // { 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1 }, + // { 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1 }, + // { 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1 }, + // { 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1 }, + // { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0 }, + + // { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0 }, + // { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,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 1, 1, 1 }, - { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, + { 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0 }, + { 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0 }, + { 0, 1, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 1, 0 }, + { 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0 }, + + { 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0 }, + { 0, 1, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 1, 0 }, + { 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0 }, + { 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0 }, + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, }; // One tile is one square meter @@ -228,12 +251,6 @@ void game_update_and_render(struct GameMemory *game_memory, struct GameInput *ga { player->pos = next_player_pos; } - else - { - printf("Invalid: "); - glmth_v2_print(next_player_pos); - player->velocity = (v2) {0.0f, 0.0f}; - } } glmth_clamp(&player->pos.x, 0.0f, ROOM_TILE_DIM_X); glmth_clamp(&player->pos.y, 0.0f, ROOM_TILE_DIM_Y); @@ -336,4 +353,5 @@ 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); } diff --git a/src/glmth.h b/src/glmth.h @@ -58,16 +58,6 @@ internal inline m4 glmth_m4_init_id() return m; } -internal inline void glmth_m4_print(m4 m) -{ - printf("[\n"); - printf(" %f, %f, %f, %f\n", m.E[0][0], m.E[0][1], m.E[0][2], m.E[0][3]); - printf(" %f, %f, %f, %f\n", m.E[1][0], m.E[1][1], m.E[1][2], m.E[1][3]); - printf(" %f, %f, %f, %f\n", m.E[2][0], m.E[2][1], m.E[2][2], m.E[2][3]); - printf(" %f, %f, %f, %f\n", m.E[3][0], m.E[3][1], m.E[3][2], m.E[3][3]); - printf("]\n"); -} - internal inline void glmth_m4_valueptr(m4 m, f32* out_valueptr) { for (u8 v = 0; v < 16; ++v) @@ -184,11 +174,6 @@ internal inline v2 glmth_v2_normalize(v2 v) return r; } -internal inline void glmth_v2_print(v2 v) -{ - printf("( %f, %f )\n", v.x, v.y); -} - internal inline v3 glmth_v3_cross(v3 vec1, v3 vec2) { v3 r = { @@ -224,11 +209,6 @@ internal inline v3 glmth_v3_normalize(v3 v) return r; } -internal inline void glmth_v3_print(v3 v) -{ - printf("( %f, %f, %f )\n", v.x, v.y, v.z); -} - internal inline v3 glmth_v3_a(v3 vec1, v3 vec2) { v3 r = { @@ -244,11 +224,6 @@ internal inline v3 glmth_v3_s(v3 vec1, v3 vec2) return glmth_v3_a(vec1, glmth_v3_negate(vec2)); } -internal inline void glmth_v4_print(v4 v) -{ - printf("( %f, %f, %f, %f )\n", v.x, v.y, v.z, v.w); -} - internal inline bool glmth_v4v4_eq(v4 vec1, v4 vec2) { for (u8 i = 0; i < 4; ++i) @@ -475,3 +450,57 @@ internal inline m4 glmth_camera_look_at(v3 camera_pos, v3 camera_target, v3 up) return glmth_m4m4_m(look, glmth_translate(glmth_m4_init_id(), glmth_v3_negate(camera_pos))); } + +internal inline void glmth_v2_print(v2 v) +{ + printf("( %f, %f )\n", v.x, v.y); +} + +internal inline void glmth_v2u_print(v2u v) +{ + printf("( %u, %u )\n", v.x, v.y); +} + +internal inline void glmth_v2i_print(v2i v) +{ + printf("( %i, %i )\n", v.x, v.y); +} + +internal inline void glmth_v3_print(v3 v) +{ + printf("( %f, %f, %f )\n", v.x, v.y, v.z); +} + +internal inline void glmth_v4_print(v4 v) +{ + printf("( %f, %f, %f, %f )\n", v.x, v.y, v.z, v.w); +} + +internal inline void glmth_rect_print(rect r) +{ + printf("rect:\n"); + printf(" min: "); + glmth_v2_print(r.min); + printf(" max: "); + glmth_v2_print(r.max); +} + +internal inline void glmth_m4_print(m4 m) +{ + printf("[\n"); + printf(" %f, %f, %f, %f\n", m.E[0][0], m.E[0][1], m.E[0][2], m.E[0][3]); + printf(" %f, %f, %f, %f\n", m.E[1][0], m.E[1][1], m.E[1][2], m.E[1][3]); + printf(" %f, %f, %f, %f\n", m.E[2][0], m.E[2][1], m.E[2][2], m.E[2][3]); + printf(" %f, %f, %f, %f\n", m.E[3][0], m.E[3][1], m.E[3][2], m.E[3][3]); + printf("]\n"); +} + +#define glmth_print(x) _Generic((x),\ + v2: glmth_v2_print, \ + v2u: glmth_v2u_print, \ + v2i: glmth_v2i_print, \ + v3: glmth_v3_print, \ + v4: glmth_v4_print, \ + rect: glmth_rect_print, \ + m4: glmth_m4_print \ + )(x) diff --git a/src/platform_linux.c b/src/platform_linux.c @@ -270,6 +270,10 @@ internal struct GameCode load_game_code(char *source_lib_path) // In this case, we are protected by POSIX, which specifies that // function and data pointers must be the same size. We will only ever // be using dlsym on POSIX-compliant platforms. + _Static_assert( + sizeof(void *) == sizeof(void(*)()), + "Object pointer must be the same size as function pointer" + ); game_code.game_load_opengl_symbols = (game_load_opengl_symbols_func *) dlsym(game_code.game_code_library, "game_load_opengl_symbols"); game_code.game_update_and_render = (game_update_and_render_func *) dlsym(game_code.game_code_library, "game_update_and_render"); game_code.is_valid = (game_code.game_load_opengl_symbols && game_code.game_update_and_render);