Skip to content

Commit

Permalink
llllllllinnnnnnnnuuux
Browse files Browse the repository at this point in the history
  • Loading branch information
Cakez77 committed Oct 19, 2023
1 parent 9cea596 commit 1297ffb
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 98 deletions.
6 changes: 4 additions & 2 deletions .clangd
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ CompileFlags:
- "-I../third_party"
- "-I../third_party/Include"

- "-Wno-switch"
- "-Wno-writable-strings"
- "-Wno-sign-compare"
- "-Wno-deprecated-declarations"
- "-Wno-format-security"

---

If:
PathExclude: "src/game.cpp"
PathExclude: src/game\..*
CompileFlags:
Add: "-include main.cpp"
Add: "-include main.cpp"
18 changes: 4 additions & 14 deletions src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ constexpr float DEATH_ANIM_TIME = 0.25f;
// Game Globals
// #############################################################################
static BumpAllocator* transientStorage;
static GameState* gameState;

// #############################################################################
// Game Functions
Expand Down Expand Up @@ -50,16 +49,15 @@ void update();
// #############################################################################
// Update Game (Exported from DLL)
// #############################################################################
EXPORT_FN void update_game(GameState* gameStateIn, Input* inputIn,
RenderData* renderDataIn, SoundState* soundStateIn,
UIState* uiStateIn, BumpAllocator* transientStorageIn,
double frameTime)
EXPORT_FN void update_game(GameState* gameStateIn, Input* inputIn, RenderData* renderDataIn,
SoundState* soundStateIn, UIState* uiStateIn,
BumpAllocator* transientStorageIn, float frameTime)
{
if(gameState != gameStateIn)
{
gameState = gameStateIn;
input = inputIn;
renderData = renderDataIn;
gameState = gameStateIn;
soundState = soundStateIn;
uiState = uiStateIn;
transientStorage = transientStorageIn;
Expand Down Expand Up @@ -279,14 +277,6 @@ bool just_pressed(GameInputType type)
// #############################################################################
// Implementations Tiles
// #############################################################################
IVec2 get_world_pos(IVec2 mousePos)
{
Vec2 localPos = vec_2(mousePos) / worldScale;
localPos.x += -renderData->gameCamera.dimensions.x / 2.0f + renderData->gameCamera.position.x;
localPos.y = -(localPos.y - renderData->gameCamera.dimensions.y / 2.0f + renderData->gameCamera.position.y);
return ivec_2(localPos);
}

IVec2 get_player_coords()
{
// return {gameState->player.pos
Expand Down
10 changes: 8 additions & 2 deletions src/game.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,13 +147,19 @@ struct GameState
Sound deathSound;
};

static int worldScale = 5;
// #############################################################################
// Game Globals
// #############################################################################
static GameState* gameState;

// #############################################################################
// Game Functions (Exposed)
// #############################################################################
extern "C"
{
EXPORT_FN void update_game(GameState* gameStateIn, Input* inputIn, RenderData* renderDataIn,
SoundState* soundStateIn, UIState* uiStateIn,
BumpAllocator* transientStorageIn, double frameTime);
BumpAllocator* transientStorageIn, float frameTime);
}


Expand Down
158 changes: 87 additions & 71 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,38 +1,33 @@
#include "schnitzel_lib.h"

#include "input.h"
#include "ui.h"

#include "game.h"

#include "sound.h"
#include "render_interface.h"

// This is so glcorearb does not include windows.h on Windows
#include "ui.h"

#define APIENTRY
#define GL_GLEXT_PROTOTYPES // This is so we get the function declarations
#define GL_GLEXT_PROTOTYPES
#include "glcorearb.h"

// #############################################################################
// Game DLL Stuff(Hot Code Reloading)
// #############################################################################
// This is the function pointer to update_game in game.cpp
typedef decltype(update_game) update_game_type;
static update_game_type* update_game_ptr;

// #############################################################################
// Platform Includes
// #############################################################################
#include "platform.h"
#ifdef _WIN32
#include "win32_platform.cpp"
char* gameDLLName = "game.dll";
char* gameLoadDLLName = "game_load.dll";
const char* gameLibName = "game.dll";
const char* gameLoadLibName = "game_load.dll";
#elif defined(__APPLE__)
#include "mac_platform.cpp"
char* gameDLLName = "game.so"; // ?????
char* gameLoadDLLName = "game_load.so";
const char* gameLibName = "game.so"; // ?????
const char* gameLoadLibName = "game_load.so";
#else // Linux
#include "linux_platform.cpp"
char* gameDLLName = "game.so";
char* gameLoadDLLName = "game_load.so";
const char* gameLibName = "game.so";
const char* gameLoadLibName = "game_load.so";
#endif

// #############################################################################
Expand All @@ -41,96 +36,113 @@ char* gameLoadDLLName = "game_load.so";
#include "gl_renderer.cpp"

// #############################################################################
// Cross Platform
// Game DLL Stuff
// #############################################################################
// This is the function pointer to update_game in game.cpp
typedef decltype(update_game) update_game_type;
static update_game_type* update_game_ptr;

// #############################################################################
// Cross Platform functions
// #############################################################################
// Used to get Delta Time
#include <chrono>

double get_delta_time();
void reload_game_dll();
void reload_game_dll(BumpAllocator* transientStorage);


int main()
{
// Init lastTime
// Initialize timestamp
get_delta_time();

transientStorage = make_bump_allocator(TRANSIENT_STORAGE_SIZE);
persistentStorage = make_bump_allocator(PERSISTENT_STORAGE_SIZE);
BumpAllocator transientStorage = make_bump_allocator(MB(50));
BumpAllocator persistentStorage = make_bump_allocator(MB(256));

GameState * gameState = (GameState*)bump_alloc(&persistentStorage, sizeof(GameState));
input = (Input*)bump_alloc(&persistentStorage, sizeof(Input));
if(!input)
{
SM_ERROR("Failed to allocate Input");
return -1;
}

// Defined in the file render_interface.h
renderData = (RenderData*)bump_alloc(&persistentStorage, sizeof(RenderData));
if(!renderData)
{
SM_ERROR("Failed to allocate RenderData");
return -1;
}

// Defiend in "input.h"
input = (Input*)bump_alloc(&persistentStorage, sizeof(Input));
gameState = (GameState*)bump_alloc(&persistentStorage, sizeof(GameState));
if(!gameState)
{
SM_ERROR("Failed to allocate GameState");
return -1;
}

uiState = (UIState*)bump_alloc(&persistentStorage, sizeof(UIState));
if(!uiState)
{
SM_ERROR("Failed to allocate UIState")
return -1;
}

// Defines in "sound.h"
soundState = (SoundState*)bump_alloc(&persistentStorage, sizeof(SoundState));
soundState->transientStorage = &transientStorage;

// Allocating Data for Sounds
soundState->allocatedsoundsBuffer = bump_alloc(&persistentStorage, SOUNDS_BUFFER_SIZE);

if(!platform_create_window(ROOM_WIDTH * worldScale,
ROOM_HEIGHT * worldScale,
"Schnitzel Motor"))
if(!soundState)
{
SM_ERROR("Failed to create Windows Window");
SM_ERROR("Failed to allocate SoundState");
return -1;
}

platform_fill_keycode_lookup_table();

if(!gl_init(&transientStorage))
soundState->transientStorage = &transientStorage;
soundState->allocatedsoundsBuffer = bump_alloc(&persistentStorage, SOUNDS_BUFFER_SIZE);
if(!soundState->allocatedsoundsBuffer)
{
SM_ERROR("Failed to initialize OpenGL");
SM_ERROR("Failed to allocated Sounds Buffer");
return -1;
}

platform_create_window(1280, 720, "Schnitzel Motor");
platform_fill_keycode_lookup_table();
platform_set_vsync(true);
if(!platform_init_audio())
{
SM_ERROR("Failed to initialize Audio");
return -1;
}

platform_set_vsync(true);
gl_init(&transientStorage);

while(running)
{
// In seconds
double dt = get_delta_time();
float dt = get_delta_time();

// Resent transient Storage
transientStorage.used = 0;
reload_game_dll(&transientStorage);

// Load the update_game function pointer from the DLL
reload_game_dll();
// Update
platform_update_window();

update_game(gameState, input, renderData, soundState, uiState, &transientStorage, dt);
gl_render();

// This is platform specific!
platform_update_audio(dt);

platform_swap_buffers();

transientStorage.used = 0;
}

return 0;
}

void update_game(GameState* gameState, Input* inputIn,
RenderData* renderDataIn, SoundState* soundStateIn,
UIState* uiStateIn, BumpAllocator* transientStorageIn, double dt)
void update_game(GameState* gameStateIn,
Input* inputIn,
RenderData* renderDataIn,
SoundState* soundStateIn,
UIState* uiStateIn,
BumpAllocator* transientStorageIn,
float dt)
{
update_game_ptr(gameState, inputIn, renderDataIn, soundStateIn, uiStateIn, transientStorageIn, dt);
update_game_ptr(gameStateIn, inputIn, renderDataIn, soundStateIn, uiStateIn, transientStorageIn, dt);
}

// #############################################################################
// Cross Platform
// #############################################################################
double get_delta_time()
{
// Only executed once when entering the function (static)
Expand All @@ -144,33 +156,37 @@ double get_delta_time()
return delta;
}

void reload_game_dll()

void reload_game_dll(BumpAllocator* transientStorage)
{
static void* gameDLL;
static long long lastTimestampGameDLL;
static long long lastEditTimestampGameDLL;

long long currentTimestampGameDLL = get_timestamp(gameDLLName);
if(currentTimestampGameDLL > lastTimestampGameDLL)
long long currentTimestampGameDLL = get_timestamp(gameLibName);
if(currentTimestampGameDLL > lastEditTimestampGameDLL)
{
if(gameDLL)
{
bool freeResult = platform_free_dynamic_library(gameDLL);
SM_ASSERT(freeResult, "Failed to free game.dll");
SM_ASSERT(freeResult, "Failed to free %s", gameLibName);
gameDLL = nullptr;
SM_TRACE("Freed %s", gameDLLName);
SM_TRACE("Freed %s", gameLibName);
}

while(!copy_file(gameDLLName, gameLoadDLLName, &transientStorage))
while(!copy_file(gameLibName, gameLoadLibName, transientStorage))
{
platform_sleep(10);
}
SM_TRACE("Copied %s", gameDLLName);
SM_TRACE("Copied %s into %s", gameLibName, gameLoadLibName);

gameDLL = platform_load_dynamic_library(gameLoadDLLName);
SM_ASSERT(gameDLL, "Failed to load %s", gameDLLName);
gameDLL = platform_load_dynamic_library(gameLoadLibName);
SM_ASSERT(gameDLL, "Failed to load %s", gameLoadLibName);

update_game_ptr = (update_game_type*)platform_load_dynamic_function(gameDLL, "update_game");
SM_ASSERT(update_game_ptr, "Failed to load update_game function");
lastTimestampGameDLL = currentTimestampGameDLL;
lastEditTimestampGameDLL = currentTimestampGameDLL;
}
}
}



4 changes: 2 additions & 2 deletions src/platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ void platform_update_window();
void* platform_load_gl_func(char* funName);
void platform_swap_buffers();
void platform_set_vsync(bool vSync);
void* platform_load_dynamic_library(char* dll);
void* platform_load_dynamic_function(void* dll, char* funName);
void* platform_load_dynamic_library(const char* dll);
void* platform_load_dynamic_function(void* dll, const char* funName);
bool platform_free_dynamic_library(void* dll);
bool platform_init_audio();
void platform_update_audio(float dt);
Expand Down
10 changes: 5 additions & 5 deletions src/schnitzel_lib.h
Original file line number Diff line number Diff line change
Expand Up @@ -903,7 +903,7 @@ bool file_exists(char* filePath)
return true;
}

long get_file_size(char* filePath)
long get_file_size(const char* filePath)
{
SM_ASSERT(filePath, "No filePath supplied!");

Expand All @@ -928,7 +928,7 @@ long get_file_size(char* filePath)
* memory and therefore want more control over where it
* is allocated
*/
char* read_file(char* filePath, int* fileSize, char* buffer)
char* read_file(const char* filePath, int* fileSize, char* buffer)
{
SM_ASSERT(filePath, "No filePath supplied!");
SM_ASSERT(fileSize, "No fileSize supplied!");
Expand Down Expand Up @@ -968,7 +968,7 @@ void write_file(char* filePath, char* buffer, int size)
fclose(file);
}

char* read_file(char* filePath, int* fileSize, BumpAllocator* bumpAllocator)
char* read_file(const char* filePath, int* fileSize, BumpAllocator* bumpAllocator)
{
char* file = 0;
long fileSize2 = get_file_size(filePath);
Expand All @@ -983,7 +983,7 @@ char* read_file(char* filePath, int* fileSize, BumpAllocator* bumpAllocator)
return file;
}

bool copy_file(char* fileName, char* outputName, char* buffer)
bool copy_file(const char* fileName, const char* outputName, char* buffer)
{
int fileSize = 0;
char* data = read_file(fileName, &fileSize, buffer);
Expand All @@ -1007,7 +1007,7 @@ bool copy_file(char* fileName, char* outputName, char* buffer)
return true;
}

bool copy_file(char* fileName, char* outputName, BumpAllocator* bumpAllocator)
bool copy_file(const char* fileName, const char* outputName, BumpAllocator* bumpAllocator)
{
char* file = 0;
long fileSize2 = get_file_size(fileName);
Expand Down
Loading

0 comments on commit 1297ffb

Please sign in to comment.