Game code is now loaded dynamically.
This commit is contained in:
+42
-6
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user