Skip to content

Commit

Permalink
working on save and load
Browse files Browse the repository at this point in the history
  • Loading branch information
goblinhack committed Feb 10, 2025
1 parent a25e6bf commit ab76e2d
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 53 deletions.
24 changes: 17 additions & 7 deletions src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,11 @@ class Game *game;
#include "game_load.hpp"
#include "game_save.hpp"

void Config::fini(void) { TRACE_AND_INDENT(); }
void Config::fini(void)
{
LOG("Game fini");
TRACE_AND_INDENT();
}

void Config::reset(void)
{
Expand Down Expand Up @@ -509,7 +513,6 @@ void Game::state_change(uint8_t new_state, const std::string &why)
wid_main_menu_destroy(g);
wid_quit_destroy(g);
wid_save_destroy(g);

wid_leftbar_fini(g);
wid_rightbar_fini(g);
wid_topcon_fini(g);
Expand All @@ -531,14 +534,21 @@ void Game::state_change(uint8_t new_state, const std::string &why)
//
switch (new_state) {
case STATE_MAIN_MENU : wid_main_menu_select(g); break;

case STATE_QUITTING : break;
case STATE_PLAYING :
if (old_state == STATE_MAIN_MENU) {
wid_leftbar_init(g);
wid_rightbar_init(g);
switch (old_state) {
case STATE_QUITTING : /* from loading */
case STATE_MAIN_MENU :
LOG("Create left and right bars");
wid_leftbar_init(g);
wid_rightbar_init(g);
break;
case STATE_KEYBOARD_MENU :
case STATE_PLAYING :
case STATE_LOAD_MENU :
case STATE_SAVE_MENU :
case STATE_QUIT_MENU : break;
}
break;
case STATE_KEYBOARD_MENU :
case STATE_LOAD_MENU :
case STATE_SAVE_MENU :
Expand Down
51 changes: 34 additions & 17 deletions src/game_load.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ std::istream &operator>>(std::istream &in, Bits< Config & > my)
LOG("Read config: config_pix_height = %d", my.t.config_pix_height);
LOG("Read config: config_pix_width = %d", my.t.config_pix_width);
LOG("Read config: aspect_ratio = %f", my.t.aspect_ratio);
LOG("Read config: ascii_pix_height = %d", my.t.ascii_pix_height);
LOG("Read config: ascii_pix_width = %d", my.t.ascii_pix_width);
LOG("Read config: ascii_pix_height = %d", my.t.ascii_pix_height);
LOG("Read config: ascii_pix_width = %d", my.t.ascii_pix_width);
LOG("Read config: game_pix_height = %d", my.t.game_pix_height);
LOG("Read config: game_pix_width = %d", my.t.game_pix_width);
LOG("Read config: map_pix_height = %d", my.t.map_pix_height);
Expand Down Expand Up @@ -293,7 +293,9 @@ uint32_t csum(char *mem, uint32_t len)

bool Game::load(std::string file_to_load, class Game &target)
{
TRACE_NO_INDENT();
LOG("Load: %s", file_to_load.c_str());
TRACE_AND_INDENT();

game_load_error = "";

//
Expand Down Expand Up @@ -376,7 +378,9 @@ std::string Game::load_config(void)

void Game::load(void)
{
TRACE_NO_INDENT();
LOG("Load");
TRACE_AND_INDENT();

LOG("-");
CON("INF: Loading %s", save_file.c_str());
LOG("| | | | | | | | | | | | | | | | | | | | | | | | | | |");
Expand All @@ -387,16 +391,18 @@ void Game::load(void)
g_loading = false;

sdl_config_update_all(game);

LOG("^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^");
LOG("| | | | | | | | | | | | | | | | | | | | | | | | | | |");
CON("INF: Loaded %s, seed %u", save_file.c_str(), seed);
LOG("-");
sdl_display_reset(game);
}

void Game::load(int slot)
{
TRACE_NO_INDENT();
LOG("Load slot: %d", slot);
TRACE_AND_INDENT();

if (slot < 0) {
return;
}
Expand All @@ -410,6 +416,7 @@ void Game::load(int slot)
return;
}

LOG("Clean up current game");
game->fini();

auto this_save_file = saved_dir + "saved-slot-" + std::to_string(slot);
Expand All @@ -428,20 +435,20 @@ void Game::load(int slot)
g_loading = false;

sdl_config_update_all(game);

LOG("^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^");
LOG("| | | | | | | | | | | | | | | | | | | | | | | | | | |");
CON("INF: Loaded %s, seed %d", this_save_file.c_str(), seed);
LOG("-");

CON("Loaded the game from %s.", this_save_file.c_str());

sdl_display_reset(game);
}

void Game::load_snapshot(void)
{
TRACE_NO_INDENT();
LOG("Load snapshot");
TRACE_AND_INDENT();

game->fini();

auto this_save_file = saved_dir + "saved-snapshot";
Expand All @@ -467,12 +474,16 @@ void Game::load_snapshot(void)

void wid_load_destroy(Gamep g)
{
TRACE_NO_INDENT();
if (wid_load) {
delete wid_load;
wid_load = nullptr;
game->state_reset("wid load destroy");
if (! wid_load) {
return;
}

LOG("Wid load destroy");
TRACE_AND_INDENT();

delete wid_load;
wid_load = nullptr;
game->state_reset("wid load destroy");
}

static bool wid_load_key_up(Gamep g, Widp w, const struct SDL_Keysym *key)
Expand Down Expand Up @@ -542,7 +553,9 @@ static bool wid_load_key_down(Gamep g, Widp w, const struct SDL_Keysym *key)

static bool wid_load_mouse_up(Gamep g, Widp w, int x, int y, uint32_t button)
{
TRACE_NO_INDENT();
CON("INF: Load selected slot");
TRACE_AND_INDENT();

auto slot = wid_get_int_context(w);
game->load(slot);
wid_load_destroy(game);
Expand All @@ -551,15 +564,19 @@ static bool wid_load_mouse_up(Gamep g, Widp w, int x, int y, uint32_t button)

static bool wid_load_saved_snapshot(Gamep g, Widp w, int x, int y, uint32_t button)
{
TRACE_NO_INDENT();
CON("INF: Load snapshot");
TRACE_AND_INDENT();

game->load_snapshot();
wid_load_destroy(game);
return true;
}

static bool wid_load_cancel(Gamep g, Widp w, int x, int y, uint32_t button)
{
TRACE_NO_INDENT();
CON("INF: Load cancel");
TRACE_AND_INDENT();

wid_load_destroy(game);
return true;
}
Expand Down
14 changes: 9 additions & 5 deletions src/game_save.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -293,12 +293,16 @@ void Game::save_config(void)

void wid_save_destroy(Gamep g)
{
TRACE_NO_INDENT();
if (wid_save) {
delete wid_save;
wid_save = nullptr;
g->state_reset("wid save destroy");
if (! wid_save) {
return;
}

LOG("Wid save destroy");
TRACE_AND_INDENT();

delete wid_save;
wid_save = nullptr;
g->state_reset("wid save destroy");
}

static bool wid_save_key_up(Gamep g, Widp w, const struct SDL_Keysym *key)
Expand Down
2 changes: 1 addition & 1 deletion src/gl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ void gl_init_fbo(Gamep g)
//
if (g_fbo_size[ i ] == isize(tex_width, tex_height)) {
LOG("No change in size for FBO %u, %ux%u", i, tex_width, tex_height);
continue;
// continue;
}
if (g_fbo_size[ i ].w) {
LOG("Change in size for FBO %u, %ux%u -> %ux%u", i, g_fbo_size[ i ].w, g_fbo_size[ i ].h, tex_width,
Expand Down
8 changes: 8 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,14 @@ int main(int argc, char *argv[])
}

{
TRACE_NO_INDENT();
if (! sdl_init_display(g)) {
ERR("SDL: Init");
}
}

{

TRACE_NO_INDENT();
sdl_config_update_all(g);
}
Expand Down
41 changes: 21 additions & 20 deletions src/my_sdl_proto.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,25 +32,26 @@ uint8_t sdl_init(void);
class Tokens;
uint8_t sdl_user_exit(Gamep, class Tokens *tokens, void *context);

void config_game_gfx_update(Gamep);
void config_gfx_vsync_update(Gamep);
void sdl_config_update_all(Gamep);
void sdl_display(Gamep);
void sdl_display_reset(Gamep);
void sdl_event(Gamep, SDL_Event *event, bool &processed_mouse_motion_event);
void sdl_prepare_to_exit(Gamep);
void sdl_fbo_dump(Gamep, int fbo, const std::string &name);
void sdl_fbo_load(Gamep, int fbo, const std::vector< uint8_t > &pixels);
void sdl_fini(Gamep);
void sdl_video_fini(Gamep);
void sdl_flush_display(Gamep, bool force = false);
void sdl_joy_rumble(float strength, uint32_t ms);
void sdl_key_repeat_events(Gamep);
void sdl_loop(Gamep);
void sdl_mouse_center(Gamep);
void sdl_mouse_warp(Gamep, int x, int y);
void sdl_screenshot_do(Gamep);
void sdl_screenshot(Gamep);
void sdl_tick(Gamep);
void config_game_gfx_update(Gamep);
void config_gfx_vsync_update(Gamep);
void sdl_config_update_all(Gamep);
uint8_t sdl_init_display(Gamep);
void sdl_display(Gamep);
void sdl_display_reset(Gamep);
void sdl_event(Gamep, SDL_Event *event, bool &processed_mouse_motion_event);
void sdl_prepare_to_exit(Gamep);
void sdl_fbo_dump(Gamep, int fbo, const std::string &name);
void sdl_fbo_load(Gamep, int fbo, const std::vector< uint8_t > &pixels);
void sdl_fini(Gamep);
void sdl_video_fini(Gamep);
void sdl_flush_display(Gamep, bool force = false);
void sdl_joy_rumble(float strength, uint32_t ms);
void sdl_key_repeat_events(Gamep);
void sdl_loop(Gamep);
void sdl_mouse_center(Gamep);
void sdl_mouse_warp(Gamep, int x, int y);
void sdl_screenshot_do(Gamep);
void sdl_screenshot(Gamep);
void sdl_tick(Gamep);

#endif
4 changes: 1 addition & 3 deletions src/sdl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ uint8_t sdl_init(void)
return true;
}

static uint8_t sdl_init_display(Gamep g)
uint8_t sdl_init_display(Gamep g)
{
int video_width;
int video_height;
Expand Down Expand Up @@ -707,8 +707,6 @@ void config_game_gfx_update(Gamep g)
LOG("SDL: Update");
TRACE_AND_INDENT();

sdl_init_display(g);

//
// Display ratio
//
Expand Down

0 comments on commit ab76e2d

Please sign in to comment.