Game code is now loaded dynamically.

This commit is contained in:
2026-03-18 00:02:50 -07:00
parent 5b4f3e2218
commit aef23bd6cd
5 changed files with 68 additions and 18 deletions
+8 -3
View File
@@ -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
+6 -5
View File
@@ -1,4 +1,5 @@
#include "handmade.h"
#include "sdl_handmade.h"
#include <math.h>
@@ -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;
+11 -4
View File
@@ -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);
//
//
+42 -6
View File
@@ -1,6 +1,7 @@
#include "core.h"
#include "handmade.h"
#include "sdl_handmade.h"
#include "handmade.cpp"
#include <SDL.h>
#include <sys/mman.h> // mmap
@@ -8,10 +9,10 @@
#include <string.h> // memset
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h> // fstat
#include <fcntl.h> // open
#include <unistd.h> // close, read
#include <dlfcn.h> // 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();
+1
View File
@@ -1,6 +1,7 @@
#ifndef SDL_HANDMADE_H
#define SDL_HANDMADE_H
#include "core.h"
#include <SDL.h>
#if !defined(BUILD_DEBUG)