From 65aafd0052e4b92bb9d0286dbee16ac18085d613 Mon Sep 17 00:00:00 2001 From: Joseph Ferano Date: Sun, 19 Jul 2026 10:31:05 +0700 Subject: [PATCH] Reload game.so without dropping the old handle on failure, reset state via F2 --- game.cpp | 52 ++++++++++++++++++++++++++++++++-------------------- game.h | 3 ++- main.cpp | 35 ++++++++++++++++++++--------------- 3 files changed, 54 insertions(+), 36 deletions(-) diff --git a/game.cpp b/game.cpp index 82ca418..9de4cde 100644 --- a/game.cpp +++ b/game.cpp @@ -31,6 +31,7 @@ GameState *InitState() { assets.textures[TEX_TARGET_RETICLE] = LoadTexture("./assets/UI/Pointers/02.png"); auto game = new GameState; + game->should_reset = false; game->assets = assets; // TODO: Probably want to use idiomatic C++ here? game->knights = (Knight*)calloc(MAX_KNIGHTS, sizeof(Knight)); @@ -114,8 +115,8 @@ void Input(GameState *state) { global_debug_mode = !global_debug_mode; } if (IsKeyPressed(KEY_F2)) { - auto new_state = InitState(); - *state = *new_state; + state->should_reset = true; + printf("Resetting GameState\n"); } } @@ -290,26 +291,37 @@ extern "C" { GameState* game_init() { return Init(); } - - void game_step(GameState* state) { - Input(state); - Update(state); - BeginDrawing(); - { - ClearBackground({100, 149, 237, 255}); - BeginMode2D(state->camera); - { - Draw2D(state); - } - EndMode2D(); - DrawRectangle(SCREEN_WIDTH - 106, 4, 88, 30, WHITE); - DrawFPS(SCREEN_WIDTH - 100, 10); - } - EndDrawing(); + GameState* game_init_state() { + return InitState(); } - - void game_cleanup(GameState* state) { + + void game_step(GameState *state) { + try { + Input(state); + Update(state); + BeginDrawing(); + { + ClearBackground({100, 149, 237, 255}); + BeginMode2D(state->camera); + { + Draw2D(state); + } + EndMode2D(); + + DrawRectangle(SCREEN_WIDTH - 106, 4, 88, 30, WHITE); + DrawFPS(SCREEN_WIDTH - 100, 10); + } + EndDrawing(); + } + catch (const exception& e) { + printf("Exception: %s\n", e.what()); + TraceLog(LOG_ERROR, "Exception: %s", e.what()); + } + } + + void game_cleanup(GameState *state) { + // free(state->assets.textures); delete state; printf("Cleaning Up\n"); CloseWindow(); diff --git a/game.h b/game.h index 632f410..9aa8bce 100644 --- a/game.h +++ b/game.h @@ -49,6 +49,7 @@ struct Assets { }; struct GameState { + bool should_reset; Camera2D camera; int frame_count; Point camera_position; @@ -73,6 +74,6 @@ struct GameState { UnloadTexture(assets.textures[i]); } free(assets.textures); - printf("I freed the stuff?\n"); + printf("Freeing GameState memory\n"); } }; diff --git a/main.cpp b/main.cpp index c560583..359cf52 100644 --- a/main.cpp +++ b/main.cpp @@ -3,14 +3,13 @@ #include #include -struct GameState; +#include "game.h" struct GameApi { - struct GameState *(*init)(); + GameState *(*init)(); + GameState *(*init_state)(); void (*step) (GameState *state); - void (*finalize) (GameState *state); - void (*reload) (GameState *state); - void (*unload) (GameState *state); + void (*cleanup) (GameState *state); bool (*window_should_close)(); }; @@ -38,20 +37,21 @@ bool load_game_api(Game* game) { return false; } - if (game->gamelib_handle) { - dlclose(game->gamelib_handle); - } - - game->gamelib_handle = dlopen(lib_path, RTLD_NOW); - - if (!game->gamelib_handle) { + void *gamelib_handle = dlopen(lib_path, RTLD_NOW); + if (!gamelib_handle) { printf("Failed to load game.so: %s\n", dlerror()); return false; } + + if (game->gamelib_handle) { + dlclose(game->gamelib_handle); + } + game->gamelib_handle = gamelib_handle; game->api.init = (GameState*(*)())dlsym(game->gamelib_handle, "game_init"); + game->api.init_state = (GameState*(*)())dlsym(game->gamelib_handle, "game_init_state"); game->api.step = (void(*)(GameState*))dlsym(game->gamelib_handle, "game_step"); - game->api.finalize = (void(*)(GameState*))dlsym(game->gamelib_handle, "game_cleanup"); + game->api.cleanup = (void(*)(GameState*))dlsym(game->gamelib_handle, "game_cleanup"); game->api.window_should_close = (bool(*)())dlsym(game->gamelib_handle, "window_should_close"); game->last_write_time = write_time; @@ -60,7 +60,7 @@ bool load_game_api(Game* game) { } int main(void) { - Game game; + Game game = {}; load_game_api(&game); @@ -70,9 +70,14 @@ int main(void) { // float dt = GetFrameTime(); load_game_api(&game); + if (game.state->should_reset) { + delete game.state; + game.state = game.api.init_state(); + } + game.api.step(game.state); } - game.api.finalize(game.state); + game.api.cleanup(game.state); return 0; }