medfall

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

commit 0d9583a7aed34d8d1e55830a11ba636c80e831ea
parent 73c719c4e53842ed1dfc41bc68b54d83fcbd7f0a
Author: Michael Savage <mikejsavage@gmail.com>
Date:   Wed Sep  2 22:25:47 +0200

Use a memory arena for persistent memory

Diffstat:
Makefile | 4++--
bsp.cc | 6+-----
game.h | 20+++++---------------
hm.cc | 6++----
main.cc | 13++++++++-----
5 files changed, 18 insertions(+), 31 deletions(-)
diff --git a/Makefile b/Makefile @@ -1,8 +1,8 @@ all: medfall bsp.so hm.so pp -OBJS = main.o gl.o +OBJS = main.o gl.o memory_arena.o BSPOBJS = bsp.o bsp_renderer.o gl.o memory_arena.o immediate.o -HMOBJS = hm.o heightmap.o terrain_manager.o work_queue.o stb_image.o stb_perlin.o immediate.o stb_truetype.o +HMOBJS = hm.o heightmap.o terrain_manager.o memory_arena.o work_queue.o immediate.o stb_truetype.o stb_image.o stb_perlin.o PPOBJS = pp.o stb_image.o stb_image_write.o WARNINGS = -Wall -Wextra -Wno-unused-parameter -Wno-unused-function -Wno-write-strings -Wno-char-subscripts diff --git a/bsp.cc b/bsp.cc @@ -270,9 +270,7 @@ static const glm::mat4 P( glm::perspective( glm::radians( 120.0f ), 640.0f / 480 extern "C" GAME_INIT( game_init ) { state->bsp = BSP( "acidwdm2.bsp" ); - u8 * memory = reserve_persistent( mem, megabytes( 10 ) ); - MemoryArena arena; - memarena_init( &arena, memory, megabytes( 10 ) ); + MemoryArena arena = memarena_push_arena( &mem->persistent_arena, megabytes( 10 ) ); state->pos = glm::vec3( 0, -100, 450 ); state->angles = glm::radians( ( glm::vec3( -90, 135, 0 ) ) ); @@ -288,8 +286,6 @@ extern "C" GAME_INIT( game_init ) { } extern "C" GAME_FRAME( game_frame ) { - GameState * state = ( GameState * ) mem.persistent; - glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); const int fb = input->keys[ 'w' ] - input->keys[ 's' ]; diff --git a/game.h b/game.h @@ -10,6 +10,7 @@ #include "bsp_renderer.h" #include "immediate.h" #include "work_queue.h" +#include "memory_arena.h" #include "keys.h" #include "stb_truetype.h" @@ -41,29 +42,18 @@ struct GameState { }; struct GameMemory { - size_t persistent_size; - size_t persistent_used; - u8 * persistent; + MemoryArena persistent_arena; + GameState * state; }; -inline u8 * reserve_persistent( GameMemory & mem, const size_t size ) { - assert( mem.persistent_used + size <= mem.persistent_size ); - assert( mem.persistent_used + size > mem.persistent_used ); - - u8 * result = mem.persistent + mem.persistent_used; - mem.persistent_used += size; - - return result; -} - struct GameInput { bool keys[ KEY_COUNT ]; }; -#define GAME_INIT( name ) void name( GameState * state, GameMemory & mem ) +#define GAME_INIT( name ) void name( GameState * const state, GameMemory * const mem ) typedef GAME_INIT( GameInit ); -#define GAME_FRAME( name ) void name( GameMemory & mem, const GameInput * const input, const float dt ) +#define GAME_FRAME( name ) void name( GameState * const state, GameMemory * const mem, const GameInput * const input, const float dt ) typedef GAME_FRAME( GameFrame ); #endif // _GAME_H_ diff --git a/hm.cc b/hm.cc @@ -115,7 +115,7 @@ extern "C" GAME_INIT( game_init ) { // TODO: persistent memory should be an arena const size_t triangles = 65536; - ImmediateTriangle * immediate_memory = ( ImmediateTriangle * ) reserve_persistent( mem, sizeof( ImmediateTriangle ) * triangles ); + ImmediateTriangle * immediate_memory = memarena_push_many( &mem->persistent_arena, ImmediateTriangle, triangles ); immediate_init( &state->test_immediate, immediate_memory, triangles ); const float aspect = 640.0f / 480.0f; @@ -153,7 +153,7 @@ extern "C" GAME_INIT( game_init ) { // TODO u8 * const arial = file_get_contents( "Arial.ttf" ); - u8 * const font_memory = reserve_persistent( mem, 512 * 256 ); + u8 * const font_memory = memarena_push_size( &mem->persistent_arena, 512 * 256 ); const int offset = stbtt_GetFontOffsetForIndex( arial, 0 ); const int ok = stbtt_BakeFontBitmap( arial, offset, 48, font_memory, 512, 512, ' ', 127 - ' ', state->test_chars ); assert( ok > 0 ); @@ -219,8 +219,6 @@ static void draw_string( const GameState * state, } extern "C" GAME_FRAME( game_frame ) { - GameState * state = ( GameState * ) mem.persistent; - glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); const int fb = input->keys[ 'w' ] - input->keys[ 's' ]; diff --git a/main.cc b/main.cc @@ -74,16 +74,19 @@ int main( int argc, char ** argv ) { Game game = load_game( game_library_path ); GameMemory mem = { }; - mem.persistent_size = megabytes( 64 ); - mem.persistent = new u8[ mem.persistent_size ]; - GameState * state = ( GameState * ) reserve_persistent( mem, sizeof( GameState ) ); + const size_t persistent_size = megabytes( 64 ); + u8 * const persistent_memory = new u8[ persistent_size ]; + memarena_init( &mem.persistent_arena, persistent_memory, persistent_size ); + + GameState * state = memarena_push_type( &mem.persistent_arena, GameState ); + mem.state = state; GameInput input = { }; GLFWwindow * const window = GL::init(); - game.init( state, mem ); + game.init( state, &mem ); const float program_start_time = glfwGetTime(); float last_frame_time = program_start_time; @@ -119,7 +122,7 @@ int main( int argc, char ** argv ) { input.keys[ KEY_RIGHTARROW ] = glfwGetKey( window, GLFW_KEY_RIGHT ); if( game.frame ) { - game.frame( mem, &input, dt ); + game.frame( state, &mem, &input, dt ); } glfwSwapBuffers( window );