a-game

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

commit 57e5470d7c9c0622306388f50da065d6ba573246
parent 8184b6442df94646fac162fab217abff111cf7b0
Author: amin <dev@aminmesbah.com>
Date:   Sat,  6 Jul 2019 01:25:40 +0000

Use a temp allocator for short lived memory

FossilOrigin-Name: 4555fedf2f4a142484fde0a3a2185baaed107eeb309af76bc7bd46d869ffca8b
Diffstat:
Msrc/game.c | 16+++++++++++-----
Msrc/game.h | 1+
Msrc/image.c | 2+-
Msrc/memory.c | 5++++-
Msrc/memory.h | 3+++
Msrc/platform.h | 3---
6 files changed, 20 insertions(+), 10 deletions(-)

diff --git a/src/game.c b/src/game.c @@ -90,10 +90,16 @@ internal void game_init(struct GameMemory *game_memory, v2u framebuffer) game_state->player.move_mode = MOVE_MODE_FALLING; game_state->player.facing = DIR_RIGHT; + size_t temp_memory_size = MEBIBYTES(1); + size_t world_memory_size = game_memory->buffer_size - (sizeof(struct GameState) + temp_memory_size); mem_st_init( - &game_state->world_allocator, + &game_state->temp_allocator, game_memory->buffer + sizeof(struct GameState), - game_memory->buffer_size - sizeof(struct GameState)); + temp_memory_size); + mem_st_init( + &game_state->world_allocator, + game_state->temp_allocator.base + temp_memory_size, + world_memory_size); game_state->world = mem_st_alloc_struct(&game_state->world_allocator, struct World); @@ -183,6 +189,7 @@ internal void game_init(struct GameMemory *game_memory, v2u framebuffer) game_state->renderer.shader = main_shader; } + size_t temp_free_marker = mem_st_get_marker(&game_state->temp_allocator); // game_texture_load { size_t file_len = 0; @@ -190,10 +197,9 @@ internal void game_init(struct GameMemory *game_memory, v2u framebuffer) u8 *t = platform.platform_read_entire_file("assets/test_sw_origin_rle.tga", &file_len); assert(t); - size_t image_load_free_marker = mem_st_get_marker(&game_state->world_allocator); i32 img_w = 0; i32 img_h = 0; - u8 *img = img_load_from_memory(t, file_len, &img_w, &img_h, &game_state->world_allocator); + u8 *img = img_load_from_memory(t, file_len, &img_w, &img_h, &game_state->temp_allocator); platform.platform_memory_free(t); u32 texture_id; @@ -209,10 +215,10 @@ internal void game_init(struct GameMemory *game_memory, v2u framebuffer) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glActiveTexture(GL_TEXTURE0); } - mem_st_free_to_marker(&game_state->world_allocator, image_load_free_marker); } renderer_init(&game_state->renderer, &game_state->world_allocator); + mem_st_free_to_marker(&game_state->temp_allocator, temp_free_marker); } // NOTE(amin): For now updating and rendering are interleaved. We simulate the diff --git a/src/game.h b/src/game.h @@ -129,6 +129,7 @@ struct GameState { struct RendererState renderer; struct Entity player; + struct StackAllocator temp_allocator; struct StackAllocator world_allocator; struct World *world; }; diff --git a/src/image.c b/src/image.c @@ -33,7 +33,7 @@ internal u8 *img_load_from_memory(u8 *buffer, size_t len, i32 *out_width, i32 *o assert(len >= sizeof(struct ImgTgaHeader)); struct ImgTgaHeader h = *(struct ImgTgaHeader *)buffer; buffer += sizeof(struct ImgTgaHeader); - img_tga_header_print(h); + //img_tga_header_print(h); // Image descriptor not currently accounted for assert(h.id_length == 0); diff --git a/src/memory.c b/src/memory.c @@ -38,7 +38,10 @@ internal size_t mem_st_get_marker(struct StackAllocator *allocator) internal void mem_st_free_to_marker(struct StackAllocator *allocator, size_t marker) { - allocator->used = marker; + if (allocator->used > marker) + { + allocator->used = marker; + } } internal void mem_st_free_all(struct StackAllocator *allocator) diff --git a/src/memory.h b/src/memory.h @@ -4,3 +4,6 @@ struct StackAllocator size_t size; size_t used; }; + +#define KIBIBYTES(n) (n) * 1024LL +#define MEBIBYTES(n) KIBIBYTES((n) * 1024LL) diff --git a/src/platform.h b/src/platform.h @@ -2,9 +2,6 @@ #include "types.h" -#define KIBIBYTES(n) (n) * 1024LL -#define MEBIBYTES(n) KIBIBYTES((n) * 1024LL) - #define PLATFORM_READ_ENTIRE_FILE(name) u8 *(name)(char *file_path, size_t *out_num_bytes) typedef PLATFORM_READ_ENTIRE_FILE(platform_read_entire_file_func);