From 1297ffb3fd2b7bf50f1b076ba19ce4d0aa38b14d Mon Sep 17 00:00:00 2001 From: Cakez77 Date: Thu, 19 Oct 2023 14:26:57 +0200 Subject: [PATCH] llllllllinnnnnnnnuuux --- .clangd | 6 +- src/game.cpp | 18 ++--- src/game.h | 10 ++- src/main.cpp | 158 +++++++++++++++++++++++------------------ src/platform.h | 4 +- src/schnitzel_lib.h | 10 +-- src/win32_platform.cpp | 4 +- 7 files changed, 112 insertions(+), 98 deletions(-) diff --git a/.clangd b/.clangd index 593da94..366a8e0 100644 --- a/.clangd +++ b/.clangd @@ -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" \ No newline at end of file diff --git a/src/game.cpp b/src/game.cpp index 33d1e4c..a3e9df9 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -12,7 +12,6 @@ constexpr float DEATH_ANIM_TIME = 0.25f; // Game Globals // ############################################################################# static BumpAllocator* transientStorage; -static GameState* gameState; // ############################################################################# // Game Functions @@ -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; @@ -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 diff --git a/src/game.h b/src/game.h index 303f8f2..092fba8 100644 --- a/src/game.h +++ b/src/game.h @@ -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); } diff --git a/src/main.cpp b/src/main.cpp index 76ebe42..9b8d32c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -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 // ############################################################################# @@ -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 - 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) @@ -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; } -} \ No newline at end of file +} + + + diff --git a/src/platform.h b/src/platform.h index 18eef03..24a3e94 100644 --- a/src/platform.h +++ b/src/platform.h @@ -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); diff --git a/src/schnitzel_lib.h b/src/schnitzel_lib.h index 6255165..c04f982 100644 --- a/src/schnitzel_lib.h +++ b/src/schnitzel_lib.h @@ -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!"); @@ -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!"); @@ -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); @@ -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); @@ -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); diff --git a/src/win32_platform.cpp b/src/win32_platform.cpp index c98b20c..79792ef 100644 --- a/src/win32_platform.cpp +++ b/src/win32_platform.cpp @@ -494,7 +494,7 @@ void platform_set_vsync(bool vSync) wglSwapIntervalEXT_ptr(vSync); } -void* platform_load_dynamic_library(char* dll) +void* platform_load_dynamic_library(const char* dll) { HMODULE result = LoadLibraryA(dll); SM_ASSERT(result, "Failed to load dll: %s", dll); @@ -502,7 +502,7 @@ void* platform_load_dynamic_library(char* dll) return result; } -void* platform_load_dynamic_function(void* dll, char* funName) +void* platform_load_dynamic_function(void* dll, const char* funName) { FARPROC proc = GetProcAddress((HMODULE)dll, funName); SM_ASSERT(proc, "Failed to load function: %s from DLL", funName);