Reload game.so without dropping the old handle on failure, reset state via F2
This commit is contained in:
parent
ac4d458938
commit
65aafd0052
52
game.cpp
52
game.cpp
@ -31,6 +31,7 @@ GameState *InitState() {
|
|||||||
assets.textures[TEX_TARGET_RETICLE] = LoadTexture("./assets/UI/Pointers/02.png");
|
assets.textures[TEX_TARGET_RETICLE] = LoadTexture("./assets/UI/Pointers/02.png");
|
||||||
|
|
||||||
auto game = new GameState;
|
auto game = new GameState;
|
||||||
|
game->should_reset = false;
|
||||||
game->assets = assets;
|
game->assets = assets;
|
||||||
// TODO: Probably want to use idiomatic C++ here?
|
// TODO: Probably want to use idiomatic C++ here?
|
||||||
game->knights = (Knight*)calloc(MAX_KNIGHTS, sizeof(Knight));
|
game->knights = (Knight*)calloc(MAX_KNIGHTS, sizeof(Knight));
|
||||||
@ -114,8 +115,8 @@ void Input(GameState *state) {
|
|||||||
global_debug_mode = !global_debug_mode;
|
global_debug_mode = !global_debug_mode;
|
||||||
}
|
}
|
||||||
if (IsKeyPressed(KEY_F2)) {
|
if (IsKeyPressed(KEY_F2)) {
|
||||||
auto new_state = InitState();
|
state->should_reset = true;
|
||||||
*state = *new_state;
|
printf("Resetting GameState\n");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -290,26 +291,37 @@ extern "C" {
|
|||||||
GameState* game_init() {
|
GameState* game_init() {
|
||||||
return 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);
|
GameState* game_init_state() {
|
||||||
DrawFPS(SCREEN_WIDTH - 100, 10);
|
return InitState();
|
||||||
}
|
|
||||||
EndDrawing();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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;
|
delete state;
|
||||||
printf("Cleaning Up\n");
|
printf("Cleaning Up\n");
|
||||||
CloseWindow();
|
CloseWindow();
|
||||||
|
|||||||
3
game.h
3
game.h
@ -49,6 +49,7 @@ struct Assets {
|
|||||||
};
|
};
|
||||||
|
|
||||||
struct GameState {
|
struct GameState {
|
||||||
|
bool should_reset;
|
||||||
Camera2D camera;
|
Camera2D camera;
|
||||||
int frame_count;
|
int frame_count;
|
||||||
Point camera_position;
|
Point camera_position;
|
||||||
@ -73,6 +74,6 @@ struct GameState {
|
|||||||
UnloadTexture(assets.textures[i]);
|
UnloadTexture(assets.textures[i]);
|
||||||
}
|
}
|
||||||
free(assets.textures);
|
free(assets.textures);
|
||||||
printf("I freed the stuff?\n");
|
printf("Freeing GameState memory\n");
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
35
main.cpp
35
main.cpp
@ -3,14 +3,13 @@
|
|||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
|
|
||||||
struct GameState;
|
#include "game.h"
|
||||||
|
|
||||||
struct GameApi {
|
struct GameApi {
|
||||||
struct GameState *(*init)();
|
GameState *(*init)();
|
||||||
|
GameState *(*init_state)();
|
||||||
void (*step) (GameState *state);
|
void (*step) (GameState *state);
|
||||||
void (*finalize) (GameState *state);
|
void (*cleanup) (GameState *state);
|
||||||
void (*reload) (GameState *state);
|
|
||||||
void (*unload) (GameState *state);
|
|
||||||
bool (*window_should_close)();
|
bool (*window_should_close)();
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -38,20 +37,21 @@ bool load_game_api(Game* game) {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (game->gamelib_handle) {
|
void *gamelib_handle = dlopen(lib_path, RTLD_NOW);
|
||||||
dlclose(game->gamelib_handle);
|
if (!gamelib_handle) {
|
||||||
}
|
|
||||||
|
|
||||||
game->gamelib_handle = dlopen(lib_path, RTLD_NOW);
|
|
||||||
|
|
||||||
if (!game->gamelib_handle) {
|
|
||||||
printf("Failed to load game.so: %s\n", dlerror());
|
printf("Failed to load game.so: %s\n", dlerror());
|
||||||
return false;
|
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 = (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.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->api.window_should_close = (bool(*)())dlsym(game->gamelib_handle, "window_should_close");
|
||||||
|
|
||||||
game->last_write_time = write_time;
|
game->last_write_time = write_time;
|
||||||
@ -60,7 +60,7 @@ bool load_game_api(Game* game) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int main(void) {
|
int main(void) {
|
||||||
Game game;
|
Game game = {};
|
||||||
|
|
||||||
load_game_api(&game);
|
load_game_api(&game);
|
||||||
|
|
||||||
@ -70,9 +70,14 @@ int main(void) {
|
|||||||
// float dt = GetFrameTime();
|
// float dt = GetFrameTime();
|
||||||
load_game_api(&game);
|
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.step(game.state);
|
||||||
}
|
}
|
||||||
|
|
||||||
game.api.finalize(game.state);
|
game.api.cleanup(game.state);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user