Skip to content

Commit ab76e2d

Browse files
committed
working on save and load
1 parent a25e6bf commit ab76e2d

7 files changed

+91
-53
lines changed

src/game.cpp

+17-7
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,11 @@ class Game *game;
233233
#include "game_load.hpp"
234234
#include "game_save.hpp"
235235

236-
void Config::fini(void) { TRACE_AND_INDENT(); }
236+
void Config::fini(void)
237+
{
238+
LOG("Game fini");
239+
TRACE_AND_INDENT();
240+
}
237241

238242
void Config::reset(void)
239243
{
@@ -509,7 +513,6 @@ void Game::state_change(uint8_t new_state, const std::string &why)
509513
wid_main_menu_destroy(g);
510514
wid_quit_destroy(g);
511515
wid_save_destroy(g);
512-
513516
wid_leftbar_fini(g);
514517
wid_rightbar_fini(g);
515518
wid_topcon_fini(g);
@@ -531,14 +534,21 @@ void Game::state_change(uint8_t new_state, const std::string &why)
531534
//
532535
switch (new_state) {
533536
case STATE_MAIN_MENU : wid_main_menu_select(g); break;
534-
535537
case STATE_QUITTING : break;
536538
case STATE_PLAYING :
537-
if (old_state == STATE_MAIN_MENU) {
538-
wid_leftbar_init(g);
539-
wid_rightbar_init(g);
539+
switch (old_state) {
540+
case STATE_QUITTING : /* from loading */
541+
case STATE_MAIN_MENU :
542+
LOG("Create left and right bars");
543+
wid_leftbar_init(g);
544+
wid_rightbar_init(g);
545+
break;
546+
case STATE_KEYBOARD_MENU :
547+
case STATE_PLAYING :
548+
case STATE_LOAD_MENU :
549+
case STATE_SAVE_MENU :
550+
case STATE_QUIT_MENU : break;
540551
}
541-
break;
542552
case STATE_KEYBOARD_MENU :
543553
case STATE_LOAD_MENU :
544554
case STATE_SAVE_MENU :

src/game_load.hpp

+34-17
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,8 @@ std::istream &operator>>(std::istream &in, Bits< Config & > my)
9999
LOG("Read config: config_pix_height = %d", my.t.config_pix_height);
100100
LOG("Read config: config_pix_width = %d", my.t.config_pix_width);
101101
LOG("Read config: aspect_ratio = %f", my.t.aspect_ratio);
102-
LOG("Read config: ascii_pix_height = %d", my.t.ascii_pix_height);
103-
LOG("Read config: ascii_pix_width = %d", my.t.ascii_pix_width);
102+
LOG("Read config: ascii_pix_height = %d", my.t.ascii_pix_height);
103+
LOG("Read config: ascii_pix_width = %d", my.t.ascii_pix_width);
104104
LOG("Read config: game_pix_height = %d", my.t.game_pix_height);
105105
LOG("Read config: game_pix_width = %d", my.t.game_pix_width);
106106
LOG("Read config: map_pix_height = %d", my.t.map_pix_height);
@@ -293,7 +293,9 @@ uint32_t csum(char *mem, uint32_t len)
293293

294294
bool Game::load(std::string file_to_load, class Game &target)
295295
{
296-
TRACE_NO_INDENT();
296+
LOG("Load: %s", file_to_load.c_str());
297+
TRACE_AND_INDENT();
298+
297299
game_load_error = "";
298300

299301
//
@@ -376,7 +378,9 @@ std::string Game::load_config(void)
376378

377379
void Game::load(void)
378380
{
379-
TRACE_NO_INDENT();
381+
LOG("Load");
382+
TRACE_AND_INDENT();
383+
380384
LOG("-");
381385
CON("INF: Loading %s", save_file.c_str());
382386
LOG("| | | | | | | | | | | | | | | | | | | | | | | | | | |");
@@ -387,16 +391,18 @@ void Game::load(void)
387391
g_loading = false;
388392

389393
sdl_config_update_all(game);
390-
391394
LOG("^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^");
392395
LOG("| | | | | | | | | | | | | | | | | | | | | | | | | | |");
393396
CON("INF: Loaded %s, seed %u", save_file.c_str(), seed);
394397
LOG("-");
398+
sdl_display_reset(game);
395399
}
396400

397401
void Game::load(int slot)
398402
{
399-
TRACE_NO_INDENT();
403+
LOG("Load slot: %d", slot);
404+
TRACE_AND_INDENT();
405+
400406
if (slot < 0) {
401407
return;
402408
}
@@ -410,6 +416,7 @@ void Game::load(int slot)
410416
return;
411417
}
412418

419+
LOG("Clean up current game");
413420
game->fini();
414421

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

430437
sdl_config_update_all(game);
431-
432438
LOG("^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^");
433439
LOG("| | | | | | | | | | | | | | | | | | | | | | | | | | |");
434440
CON("INF: Loaded %s, seed %d", this_save_file.c_str(), seed);
435441
LOG("-");
436442

437443
CON("Loaded the game from %s.", this_save_file.c_str());
438-
439444
sdl_display_reset(game);
440445
}
441446

442447
void Game::load_snapshot(void)
443448
{
444-
TRACE_NO_INDENT();
449+
LOG("Load snapshot");
450+
TRACE_AND_INDENT();
451+
445452
game->fini();
446453

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

468475
void wid_load_destroy(Gamep g)
469476
{
470-
TRACE_NO_INDENT();
471-
if (wid_load) {
472-
delete wid_load;
473-
wid_load = nullptr;
474-
game->state_reset("wid load destroy");
477+
if (! wid_load) {
478+
return;
475479
}
480+
481+
LOG("Wid load destroy");
482+
TRACE_AND_INDENT();
483+
484+
delete wid_load;
485+
wid_load = nullptr;
486+
game->state_reset("wid load destroy");
476487
}
477488

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

543554
static bool wid_load_mouse_up(Gamep g, Widp w, int x, int y, uint32_t button)
544555
{
545-
TRACE_NO_INDENT();
556+
CON("INF: Load selected slot");
557+
TRACE_AND_INDENT();
558+
546559
auto slot = wid_get_int_context(w);
547560
game->load(slot);
548561
wid_load_destroy(game);
@@ -551,15 +564,19 @@ static bool wid_load_mouse_up(Gamep g, Widp w, int x, int y, uint32_t button)
551564

552565
static bool wid_load_saved_snapshot(Gamep g, Widp w, int x, int y, uint32_t button)
553566
{
554-
TRACE_NO_INDENT();
567+
CON("INF: Load snapshot");
568+
TRACE_AND_INDENT();
569+
555570
game->load_snapshot();
556571
wid_load_destroy(game);
557572
return true;
558573
}
559574

560575
static bool wid_load_cancel(Gamep g, Widp w, int x, int y, uint32_t button)
561576
{
562-
TRACE_NO_INDENT();
577+
CON("INF: Load cancel");
578+
TRACE_AND_INDENT();
579+
563580
wid_load_destroy(game);
564581
return true;
565582
}

src/game_save.hpp

+9-5
Original file line numberDiff line numberDiff line change
@@ -293,12 +293,16 @@ void Game::save_config(void)
293293

294294
void wid_save_destroy(Gamep g)
295295
{
296-
TRACE_NO_INDENT();
297-
if (wid_save) {
298-
delete wid_save;
299-
wid_save = nullptr;
300-
g->state_reset("wid save destroy");
296+
if (! wid_save) {
297+
return;
301298
}
299+
300+
LOG("Wid save destroy");
301+
TRACE_AND_INDENT();
302+
303+
delete wid_save;
304+
wid_save = nullptr;
305+
g->state_reset("wid save destroy");
302306
}
303307

304308
static bool wid_save_key_up(Gamep g, Widp w, const struct SDL_Keysym *key)

src/gl.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ void gl_init_fbo(Gamep g)
421421
//
422422
if (g_fbo_size[ i ] == isize(tex_width, tex_height)) {
423423
LOG("No change in size for FBO %u, %ux%u", i, tex_width, tex_height);
424-
continue;
424+
// continue;
425425
}
426426
if (g_fbo_size[ i ].w) {
427427
LOG("Change in size for FBO %u, %ux%u -> %ux%u", i, g_fbo_size[ i ].w, g_fbo_size[ i ].h, tex_width,

src/main.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -680,6 +680,14 @@ int main(int argc, char *argv[])
680680
}
681681

682682
{
683+
TRACE_NO_INDENT();
684+
if (! sdl_init_display(g)) {
685+
ERR("SDL: Init");
686+
}
687+
}
688+
689+
{
690+
683691
TRACE_NO_INDENT();
684692
sdl_config_update_all(g);
685693
}

src/my_sdl_proto.hpp

+21-20
Original file line numberDiff line numberDiff line change
@@ -32,25 +32,26 @@ uint8_t sdl_init(void);
3232
class Tokens;
3333
uint8_t sdl_user_exit(Gamep, class Tokens *tokens, void *context);
3434

35-
void config_game_gfx_update(Gamep);
36-
void config_gfx_vsync_update(Gamep);
37-
void sdl_config_update_all(Gamep);
38-
void sdl_display(Gamep);
39-
void sdl_display_reset(Gamep);
40-
void sdl_event(Gamep, SDL_Event *event, bool &processed_mouse_motion_event);
41-
void sdl_prepare_to_exit(Gamep);
42-
void sdl_fbo_dump(Gamep, int fbo, const std::string &name);
43-
void sdl_fbo_load(Gamep, int fbo, const std::vector< uint8_t > &pixels);
44-
void sdl_fini(Gamep);
45-
void sdl_video_fini(Gamep);
46-
void sdl_flush_display(Gamep, bool force = false);
47-
void sdl_joy_rumble(float strength, uint32_t ms);
48-
void sdl_key_repeat_events(Gamep);
49-
void sdl_loop(Gamep);
50-
void sdl_mouse_center(Gamep);
51-
void sdl_mouse_warp(Gamep, int x, int y);
52-
void sdl_screenshot_do(Gamep);
53-
void sdl_screenshot(Gamep);
54-
void sdl_tick(Gamep);
35+
void config_game_gfx_update(Gamep);
36+
void config_gfx_vsync_update(Gamep);
37+
void sdl_config_update_all(Gamep);
38+
uint8_t sdl_init_display(Gamep);
39+
void sdl_display(Gamep);
40+
void sdl_display_reset(Gamep);
41+
void sdl_event(Gamep, SDL_Event *event, bool &processed_mouse_motion_event);
42+
void sdl_prepare_to_exit(Gamep);
43+
void sdl_fbo_dump(Gamep, int fbo, const std::string &name);
44+
void sdl_fbo_load(Gamep, int fbo, const std::vector< uint8_t > &pixels);
45+
void sdl_fini(Gamep);
46+
void sdl_video_fini(Gamep);
47+
void sdl_flush_display(Gamep, bool force = false);
48+
void sdl_joy_rumble(float strength, uint32_t ms);
49+
void sdl_key_repeat_events(Gamep);
50+
void sdl_loop(Gamep);
51+
void sdl_mouse_center(Gamep);
52+
void sdl_mouse_warp(Gamep, int x, int y);
53+
void sdl_screenshot_do(Gamep);
54+
void sdl_screenshot(Gamep);
55+
void sdl_tick(Gamep);
5556

5657
#endif

src/sdl.cpp

+1-3
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ uint8_t sdl_init(void)
192192
return true;
193193
}
194194

195-
static uint8_t sdl_init_display(Gamep g)
195+
uint8_t sdl_init_display(Gamep g)
196196
{
197197
int video_width;
198198
int video_height;
@@ -707,8 +707,6 @@ void config_game_gfx_update(Gamep g)
707707
LOG("SDL: Update");
708708
TRACE_AND_INDENT();
709709

710-
sdl_init_display(g);
711-
712710
//
713711
// Display ratio
714712
//

0 commit comments

Comments
 (0)