a-game

2D platformer written from scratch.
Log | Files | Refs | README | LICENSE

commit 1b00e7bbe60ad8a9de551b79d4b66577f453633c
parent 49185a3b4db43c10f06ba87e9bb5acaf5fdebb81
Author: Amin Mesbah <dev@aminmesbah.com>
Date:   Tue, 16 Jul 2019 19:14:15 -0700

Use stack allocator for platform file loading

Diffstat:
Msrc/game.c | 17+++++++++--------
Msrc/platform.h | 3++-
Msrc/platform_linux.c | 6+++---
Msrc/platform_windows.c | 6+++---
4 files changed, 17 insertions(+), 15 deletions(-)

diff --git a/src/game.c b/src/game.c @@ -177,42 +177,43 @@ internal void game_init(struct GameMemory *game_memory, v2u framebuffer) } } + size_t asset_loading_free_marker = mem_st_get_marker(&game_state->temp_allocator); struct PlatformApi platform = game_memory->platform; // game_shader_load { - char *v_source = (char *)platform.platform_read_entire_file("shader/main_v.glsl", NULL); - char *f_source = (char *)platform.platform_read_entire_file("shader/main_f.glsl", NULL); + size_t temp_free_marker = mem_st_get_marker(&game_state->temp_allocator); + char *v_source = (char *)platform.platform_read_entire_file("shader/main_v.glsl", NULL, &game_state->temp_allocator); + char *f_source = (char *)platform.platform_read_entire_file("shader/main_f.glsl", NULL, &game_state->temp_allocator); struct Shader main_shader = {0}; if (!shader_compile(v_source, f_source, &main_shader)) { exit(1); // TODO: handle error } - platform.platform_memory_free(v_source); - platform.platform_memory_free(f_source); game_state->renderer.shader = main_shader; + mem_st_free_to_marker(&game_state->temp_allocator, temp_free_marker); } - size_t temp_free_marker = mem_st_get_marker(&game_state->temp_allocator); struct Image img = {0}; // game_load_images { + size_t temp_free_marker = mem_st_get_marker(&game_state->temp_allocator); size_t file_len = 0; - u8 *t = platform.platform_read_entire_file("assets/tileset0.tga", &file_len); + u8 *t = platform.platform_read_entire_file("assets/tileset0.tga", &file_len, &game_state->temp_allocator); assert(t); i32 img_w = 0; i32 img_h = 0; u8 *data = img_load_from_memory(t, file_len, &img_w, &img_h, &game_state->temp_allocator); + mem_st_free_to_marker(&game_state->temp_allocator, temp_free_marker); assert(data); - platform.platform_memory_free(t); img.data = data; img.dim = (v2u) {img_w, img_h}; } renderer_init(&game_state->renderer, &img, 1, &game_state->world_allocator); - mem_st_free_to_marker(&game_state->temp_allocator, temp_free_marker); + mem_st_free_to_marker(&game_state->temp_allocator, asset_loading_free_marker); } // NOTE(amin): For now updating and rendering are interleaved. We simulate the diff --git a/src/platform.h b/src/platform.h @@ -2,9 +2,10 @@ #include "types.h" -#define PLATFORM_READ_ENTIRE_FILE(name) u8 *(name)(char *file_path, size_t *out_num_bytes) +#define PLATFORM_READ_ENTIRE_FILE(name) u8 *(name)(char *file_path, size_t *out_num_bytes, struct StackAllocator *a) typedef PLATFORM_READ_ENTIRE_FILE(platform_read_entire_file_func); +// TODO: Remove this from the platform api #define PLATFORM_MEMORY_FREE(name) void (name)(void *ptr) typedef PLATFORM_MEMORY_FREE(platform_memory_free_func); diff --git a/src/platform_linux.c b/src/platform_linux.c @@ -210,6 +210,7 @@ PLATFORM_MEMORY_FREE(linux_memory_free) internal PLATFORM_READ_ENTIRE_FILE(linux_read_entire_file) { + assert(a); FILE *handle = fopen(file_path, "r"); u8 *buffer = NULL; @@ -225,16 +226,15 @@ internal PLATFORM_READ_ENTIRE_FILE(linux_read_entire_file) *out_num_bytes = num_bytes_in_file; } - // TODO: replace malloc with own allocator so I stop having nightmares - buffer = malloc(sizeof(u8) * (num_bytes_in_file + 1) ); + buffer = mem_st_alloc_buffer(a, u8, num_bytes_in_file + 1); u32 bytes_read = fread(buffer, sizeof(u8), num_bytes_in_file, handle); // IMPORTANT! fread() doesn't add the '\0' buffer[num_bytes_in_file] = '\0'; + // TODO: handle this case more loudly if (num_bytes_in_file != bytes_read) { - free(buffer); buffer = NULL; } diff --git a/src/platform_windows.c b/src/platform_windows.c @@ -174,6 +174,7 @@ PLATFORM_MEMORY_FREE(windows_memory_free) internal PLATFORM_READ_ENTIRE_FILE(windows_read_entire_file) { + assert(a); FILE *handle = fopen(file_path, "rb"); u8 *buffer = NULL; @@ -189,16 +190,15 @@ internal PLATFORM_READ_ENTIRE_FILE(windows_read_entire_file) *out_num_bytes = num_bytes_in_file; } - // TODO: replace malloc with own allocator so I stop having nightmares - buffer = malloc(sizeof(u8) * (num_bytes_in_file + 1)); + buffer = mem_st_alloc_buffer(a, u8, num_bytes_in_file + 1); u32 bytes_read = fread(buffer, sizeof(u8), num_bytes_in_file, handle); // IMPORTANT! fread() doesn't add the '\0' buffer[num_bytes_in_file] = '\0'; + // TODO: handle this case more loudly if (num_bytes_in_file != bytes_read) { - free(buffer); buffer = NULL; }