From aef23bd6cdd4fcc5dda3768391c91e17d203a691 Mon Sep 17 00:00:00 2001 From: igor Date: Wed, 18 Mar 2026 00:02:50 -0700 Subject: [PATCH] Game code is now loaded dynamically. --- Makefile | 11 ++++++++--- handmade.cpp | 11 ++++++----- handmade.h | 15 +++++++++++---- sdl_handmade.cpp | 48 ++++++++++++++++++++++++++++++++++++++++++------ sdl_handmade.h | 1 + 5 files changed, 68 insertions(+), 18 deletions(-) diff --git a/Makefile b/Makefile index c9e4986..4004d6f 100644 --- a/Makefile +++ b/Makefile @@ -39,11 +39,16 @@ endif EXE=handmadehero -$(EXE): sdl_handmade.cpp - $(CC) $(FLAGS) $(UFLAGS) $(INCLUDE) $^ $(LIBS) $(FRAMEWORKS) -o $@ +default: libhandmade $(EXE) + +libhandmade: handmade.cpp + $(CC) $(FLAGS) $(UFLAGS) -fPIC -shared $(INCLUDE) $^ $(LIBS) $(FRAMEWORKS) -o libhandmade.so + +$(EXE): sdl_handmade.cpp libhandmade + $(CC) $(FLAGS) $(UFLAGS) $(INCLUDE) sdl_handmade.cpp $(LIBS) $(FRAMEWORKS) -o $@ tags: $(OBJS) ctags -R *.h *.c clean: - rm -rf $(EXE) $(EXE).dSYM + rm -rf $(EXE) $(EXE).dSYM libhandmade.so diff --git a/handmade.cpp b/handmade.cpp index eef79fd..61480bc 100644 --- a/handmade.cpp +++ b/handmade.cpp @@ -1,4 +1,5 @@ #include "handmade.h" +#include "sdl_handmade.h" #include @@ -37,7 +38,7 @@ static void RenderWeirdGradient(game_offscreen_buffer *Buffer, S32 XOffset, S32 } } -static void GameUpdateAndRender(game_memory *Memory, game_input *Input, game_offscreen_buffer *Buffer, game_sound_output_buffer *SoundBuffer) +void UpdateAndRender(game_memory *Memory, game_input *Input, game_offscreen_buffer *Buffer, game_sound_output_buffer *SoundBuffer) { Assert((&Input->Controllers[0].Terminator - &Input->Controllers[0].Buttons[0]) == ARRAY_SIZE(Input->Controllers[0].Buttons)); Assert(sizeof(game_state) <= Memory->PermanentStorageSize); @@ -45,10 +46,10 @@ static void GameUpdateAndRender(game_memory *Memory, game_input *Input, game_off game_state *GameState = (game_state *)Memory->PermanentStorage; if(!Memory->IsInitialized) { char FileName[] = "/Users/igor/Developer/handmade_hero_course/handmade/code/sdl_handmade.cpp"; - debug_read_file_result BitmapMemory = DEBUGPlatformReadEntireFile(FileName); + debug_read_file_result BitmapMemory = Memory->DEBUGPlatformReadEntireFile(FileName); if(BitmapMemory.Contents) { - DEBUGPlatformWriteEntireFile("test.out", BitmapMemory.ContentsSize, BitmapMemory.Contents); - DEBUGPlatformFreeFileMemory(BitmapMemory.Contents); + Memory->DEBUGPlatformWriteEntireFile("test.out", BitmapMemory.ContentsSize, BitmapMemory.Contents); + Memory->DEBUGPlatformFreeFileMemory(BitmapMemory.Contents); } GameState->ToneHz = 256; @@ -71,7 +72,7 @@ static void GameUpdateAndRender(game_memory *Memory, game_input *Input, game_off GameState->GreenOffset -= 1; } if(Controller->MoveDown.EndedDown) { - GameState->GreenOffset += 1; + GameState->GreenOffset += 2; } if(Controller->MoveLeft.EndedDown) { GameState->BlueOffset -= 1; diff --git a/handmade.h b/handmade.h index aa65503..3fba5a1 100644 --- a/handmade.h +++ b/handmade.h @@ -1,6 +1,9 @@ #ifndef HANDMADE_H #define HANDMADE_H +#include "sdl_handmade.h" +#include "core.h" + /////////////////////////////////////////////////////////////////////////////// // NOTE: Servcies that the platform layer provides to the game. // /////////////////////////////////////////////////////////////////////////////// @@ -13,9 +16,9 @@ typedef struct debug_read_file_result { void *Contents; } debug_read_file_result; -static debug_read_file_result DEBUGPlatformReadEntireFile(const char *Filename); -static B32 DEBUGPlatformWriteEntireFile(const char *Filename, U32 MemorySize, void *Memory); -static void DEBUGPlatformFreeFileMemory(void *Memory); +debug_read_file_result DEBUGPlatformReadEntireFile(const char *Filename); +B32 DEBUGPlatformWriteEntireFile(const char *Filename, U32 MemorySize, void *Memory); +void DEBUGPlatformFreeFileMemory(void *Memory); #endif /////////////////////////////////////////////////////////////////////////////// @@ -94,6 +97,10 @@ typedef struct game_memory { U64 TransientStorageSize; void *TransientStorage; // WARNING: This memory is REQUIRED to be zero initialized!!! (Done by the OS layer.) + + debug_read_file_result (*DEBUGPlatformReadEntireFile)(const char *); + B32 (*DEBUGPlatformWriteEntireFile)(const char *, U32, void *); + void (*DEBUGPlatformFreeFileMemory)(void *); } game_memory; // typedef struct game_clocks { @@ -101,7 +108,7 @@ typedef struct game_memory { // F32 SecondsElapsed; // } game_clocks; -static void GameUpdateAndRender(game_memory *Memory, game_input *Input, game_offscreen_buffer *Buffer, game_sound_output_buffer *SoundBuffer); +extern "C" void UpdateAndRender(game_memory *Memory, game_input *Input, game_offscreen_buffer *Buffer, game_sound_output_buffer *SoundBuffer); // // diff --git a/sdl_handmade.cpp b/sdl_handmade.cpp index d5db700..b34186d 100644 --- a/sdl_handmade.cpp +++ b/sdl_handmade.cpp @@ -1,6 +1,7 @@ #include "core.h" + +#include "handmade.h" #include "sdl_handmade.h" -#include "handmade.cpp" #include #include // mmap @@ -8,10 +9,10 @@ #include // memset #include -#include #include // fstat #include // open #include // close, read +#include // dlopen // NOTE: This is so it compiles on ARM. #if __x86_64__ || __i386__ @@ -35,7 +36,7 @@ static U32 SafeTruncateUInt64(U64 Value) return Result; } -static debug_read_file_result DEBUGPlatformReadEntireFile(const char *Filename) +debug_read_file_result DEBUGPlatformReadEntireFile(const char *Filename) { debug_read_file_result Result = {0}; S32 FileHandle = open(Filename, O_RDONLY); @@ -77,7 +78,7 @@ static debug_read_file_result DEBUGPlatformReadEntireFile(const char *Filename) return Result; } -static B32 DEBUGPlatformWriteEntireFile(const char *Filename, U32 MemorySize, void *Memory) +B32 DEBUGPlatformWriteEntireFile(const char *Filename, U32 MemorySize, void *Memory) { S32 FileHandle = open(Filename, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); if(FileHandle == -1) @@ -100,7 +101,7 @@ static B32 DEBUGPlatformWriteEntireFile(const char *Filename, U32 MemorySize, vo return true; } -static void DEBUGPlatformFreeFileMemory(void *Memory) +void DEBUGPlatformFreeFileMemory(void *Memory) { if(Memory) free(Memory); @@ -279,6 +280,8 @@ static void SDLProcessMessages(game_controller_input *KeyboardController) bool AltKeyWasDown = (Event.key.keysym.mod & KMOD_ALT); if(KeyCode == SDLK_F4 && AltKeyWasDown) GlobalRunning = false; + if(KeyCode == SDLK_ESCAPE) + GlobalRunning = false; } break; default: { @@ -321,8 +324,36 @@ static F32 SDLGetSecondsElapsed(U64 Start, U64 End) return (F32)(End - Start) / (F32)GlobalPerfCountFrequency; } +typedef struct sdl_game_code { + void *GameCode; + void (*UpdateAndRender)(game_memory *, game_input *, game_offscreen_buffer *, game_sound_output_buffer *); + + B32 IsValid; +} sdl_game_code; + +static sdl_game_code SDLLoadGameCode() +{ + sdl_game_code Result = {0}; + + Result.GameCode = dlopen("./libhandmade.so", RTLD_NOW); + if(Result.GameCode) { + Result.UpdateAndRender = (void(*)(game_memory *, game_input *, game_offscreen_buffer *, game_sound_output_buffer *))dlsym(Result.GameCode, "UpdateAndRender"); + if(!Result.UpdateAndRender) { + // TODO: Diagnostic. + fprintf(stderr, "%s\n", dlerror()); + } + } else { + // TODO: Diagnostic. + fprintf(stderr, "%s\n", dlerror()); + } + + return Result; +} + S32 main(S32 argc, char *argv[]) { + sdl_game_code Game = SDLLoadGameCode(); + if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_GAMECONTROLLER | SDL_INIT_AUDIO)) { /* TODO: This didn't work . . . */ } @@ -373,6 +404,10 @@ S32 main(S32 argc, char *argv[]) GameMemory.PermanentStorage = mmap(BaseAddress, TotalStorageSize, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0); GameMemory.TransientStorage = (U8 *)(GameMemory.PermanentStorage) + GameMemory.PermanentStorageSize; + GameMemory.DEBUGPlatformReadEntireFile = DEBUGPlatformReadEntireFile; + GameMemory.DEBUGPlatformWriteEntireFile = DEBUGPlatformWriteEntireFile; + GameMemory.DEBUGPlatformFreeFileMemory = DEBUGPlatformFreeFileMemory; + if(SoundOutput.Samples && GameMemory.PermanentStorage && GameMemory.TransientStorage) { game_input Input[2]; game_input *NewInput = &Input[0]; @@ -480,7 +515,8 @@ S32 main(S32 argc, char *argv[]) Buffer.Width = GlobalBackbuffer.Width; Buffer.Height = GlobalBackbuffer.Height; Buffer.Pitch = GlobalBackbuffer.Pitch; - GameUpdateAndRender(&GameMemory, NewInput, &Buffer, &SoundBuffer); + if(Game.UpdateAndRender) + Game.UpdateAndRender(&GameMemory, NewInput, &Buffer, &SoundBuffer); SDLFillSoundBuffer(&SoundOutput, BytesToWrite); U64 WorkCounter = SDLGetWallClock(); diff --git a/sdl_handmade.h b/sdl_handmade.h index df1915f..12963b0 100644 --- a/sdl_handmade.h +++ b/sdl_handmade.h @@ -1,6 +1,7 @@ #ifndef SDL_HANDMADE_H #define SDL_HANDMADE_H +#include "core.h" #include #if !defined(BUILD_DEBUG)