a-game

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

commit dd1e59ec2efc9f5403dacbc0891276461618de6b
parent 1a32b2e02ebabb8a421fb514d87fb0180b15ed2d
Author: amin <dev@aminmesbah.com>
Date:   Tue, 30 Apr 2019 00:53:49 +0000

Allocate a big block of game memory

Note that this will break the Windows platform layer until I get around
to fixing it.

FossilOrigin-Name: 5ea913d2b41882f0c05977bf89d693692ba678ac8a73fafb612c7144c8353157
Diffstat:
Msrc/game.c | 20+++++++++++---------
Msrc/game.h | 1+
Msrc/platform.h | 6+++++-
Msrc/platform_linux.c | 14++++++++++++--
4 files changed, 29 insertions(+), 12 deletions(-)

diff --git a/src/game.c b/src/game.c @@ -14,8 +14,8 @@ void game_load_opengl_symbols(void) internal void game_init(struct GameMemory *game_memory, v2u framebuffer) { - struct GameState *game_state = game_memory->game_state; - + assert(sizeof(struct GameState) <= game_memory->buffer_size); + struct GameState *game_state = (struct GameState *)game_memory->buffer; // init player game_state->player.pos = (struct AbsolutePos) { @@ -295,7 +295,8 @@ internal bool wall_is_internal(u32 *tiles, segment wall) // to change this in the future. void game_update_and_render(struct GameMemory *game_memory, struct GameInput *game_input, v2u framebuffer) { - struct GameState *game_state = game_memory->game_state; + assert(sizeof(struct GameState) <= game_memory->buffer_size); + struct GameState *game_state = (struct GameState *)game_memory->buffer; rect viewport = {0}; f32 ppm = 0.0f; @@ -644,15 +645,15 @@ void game_update_and_render(struct GameMemory *game_memory, struct GameInput *ga v2 delta_to_nearest_collision = math_v2f_m(player_delta, smallest_distance_scale_factor); v2 nearest_collision_p = math_v2_a(player->pos.local, delta_to_nearest_collision); - f32 epsilon = 0.0001f; + f32 collision_epsilon = 0.0001f; rect local_inhabitable_region = { .min = { - math_min(player->pos.local.x, nearest_collision_p.x + epsilon), - math_min(player->pos.local.y, nearest_collision_p.y + epsilon), + math_min(player->pos.local.x, nearest_collision_p.x + collision_epsilon), + math_min(player->pos.local.y, nearest_collision_p.y + collision_epsilon), }, .max = { - math_max(player->pos.local.x, nearest_collision_p.x - epsilon), - math_max(player->pos.local.y, nearest_collision_p.y - epsilon), + math_max(player->pos.local.x, nearest_collision_p.x - collision_epsilon), + math_max(player->pos.local.y, nearest_collision_p.y - collision_epsilon), }, }; @@ -703,7 +704,8 @@ void game_update_and_render(struct GameMemory *game_memory, struct GameInput *ga internal void game_cleanup(struct GameMemory *game_memory) { - struct GameState *game_state = game_memory->game_state; + assert(sizeof(struct GameState) <= game_memory->buffer_size); + struct GameState *game_state = (struct GameState *)game_memory->buffer; glDeleteVertexArrays(1, &game_state->renderer.vao); glDeleteBuffers(1, &game_state->renderer.quad_vbo); glDeleteBuffers(1, &game_state->renderer.quad_ebo); diff --git a/src/game.h b/src/game.h @@ -52,6 +52,7 @@ struct GameState { struct RendererState renderer; struct Entity player; + struct Room *rooms; }; internal void game_init(struct GameMemory *game_memory, v2u framebuffer); diff --git a/src/platform.h b/src/platform.h @@ -4,6 +4,9 @@ #include "types.h" +#define KIBIBYTES(n) (n) * 1024LL +#define MEBIBYTES(n) KIBIBYTES((n) * 1024LL) + enum InputKeyAction { KEY_RELEASE, @@ -30,7 +33,8 @@ struct PlatformApi struct GameMemory { - struct GameState *game_state; + u64 buffer_size; + void *buffer; struct PlatformApi platform; }; diff --git a/src/platform_linux.c b/src/platform_linux.c @@ -50,13 +50,20 @@ int main(void) srand((uint32_t)time(NULL)); #endif - struct GameState game_state = {0}; struct GameMemory game_memory = { - .game_state = &game_state, + .buffer_size = MEBIBYTES(64), .platform = { &linux_read_entire_file, }, }; + // TODO: replace with mmap + game_memory.buffer = malloc(game_memory.buffer_size); + if (!game_memory.buffer) + { + fprintf(stderr, "Game memory allocation failed\n"); + glfwTerminate(); + return -1; + } struct GameInput game_input = {0}; glfwSetWindowUserPointer(window, &game_input); @@ -117,6 +124,9 @@ int main(void) game_cleanup(&game_memory); + // TODO: replace with munmap + free(game_memory.buffer); + glfwDestroyWindow(window); glfwTerminate(); return 0;