Skip to content

Commit 7a41159

Browse files
committed
better reliability and working save/load game in box-emu menu
1 parent 5b9ae33 commit 7a41159

File tree

12 files changed

+175
-45
lines changed

12 files changed

+175
-45
lines changed

components/doom/include/doom.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ void init_doom(const std::string& rom_filename, uint8_t *romdata, size_t rom_dat
1010
void doom_init_shared_memory();
1111
void pause_doom_tasks();
1212
void resume_doom_tasks();
13-
void load_doom(int save_slot);
14-
void save_doom(int save_slot);
13+
void load_doom(std::string_view save_path, int save_slot);
14+
void save_doom(std::string_view save_path, int save_slot);
1515
void run_doom_rom();
1616
void deinit_doom();
1717
std::span<uint8_t> get_doom_video_buffer();

components/doom/prboom/doomstat.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ boolean modifiedgame;
4949
// CPhipps - compatibility vars
5050
complevel_t compatibility_level, default_compatibility_level;
5151

52-
int comp[COMP_TOTAL], default_comp[COMP_TOTAL]; // killough 10/98
52+
int comp[COMP_TOTAL];
53+
const int default_comp[COMP_TOTAL]; // killough 10/98
5354

5455
// v1.1-like pitched sounds
5556
int pitched_sounds; // killough

components/doom/prboom/doomstat.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,8 @@ enum {
114114
COMP_TOTAL=32 // Some extra room for additional variables
115115
};
116116

117-
extern int comp[COMP_TOTAL], default_comp[COMP_TOTAL];
117+
extern int comp[COMP_TOTAL];
118+
extern const int default_comp[COMP_TOTAL];
118119

119120
// -------------------------------------------
120121
// Selected skill type, map etc.

components/doom/prboom/g_game.c

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1500,6 +1500,10 @@ static const struct {
15001500

15011501
static const size_t num_version_headers = sizeof(version_headers) / sizeof(version_headers[0]);
15021502

1503+
// used for the cart to be able to explicitly set its own save/load game paths
1504+
char *save_game_name = NULL;
1505+
int save_game_name_len = 0;
1506+
15031507
void G_DoLoadGame(void)
15041508
{
15051509
lprintf(LO_INFO, "G_DoLoadGame... \n");
@@ -1508,7 +1512,15 @@ void G_DoLoadGame(void)
15081512
char name[PATH_MAX+1]; // killough 3/22/98
15091513
int savegame_compatibility = -1;
15101514

1511-
G_SaveGameName(name,sizeof(name),savegameslot, demoplayback);
1515+
if (save_game_name_len) {
1516+
strncpy(name, save_game_name, save_game_name_len);
1517+
name[save_game_name_len] = '\0'; // ensure null termination
1518+
// now unset the name so that we don't use it again
1519+
save_game_name_len = 0;
1520+
} else {
1521+
G_SaveGameName(name,sizeof(name),savegameslot);
1522+
}
1523+
15121524
gameaction = ga_nothing;
15131525

15141526
length = M_ReadFile(name, &savebuffer);
@@ -1682,7 +1694,14 @@ void (CheckSaveGame)(size_t size, const char* file, int line)
16821694
* cph - Avoid possible buffer overflow problems by passing
16831695
* size to this function and using snprintf */
16841696

1685-
void G_SaveGameName(char *name, size_t size, int slot, boolean demoplayback)
1697+
void G_SetSaveGameFileName(char *name, size_t size)
1698+
{
1699+
strncpy(save_game_name, name, size);
1700+
save_game_name[size] = '\0'; // ensure null termination
1701+
save_game_name_len = strlen(save_game_name);
1702+
}
1703+
1704+
void G_SaveGameName(char *name, size_t size, int slot)
16861705
{
16871706
snprintf(name, size, "%s/sav%d-%d.dsg", basesavegame, gamemission, slot);
16881707
}
@@ -1699,7 +1718,16 @@ static void G_DoSaveGame (boolean menu)
16991718
gameaction = ga_nothing; // cph - cancel savegame at top of this function,
17001719
// in case later problems cause a premature exit
17011720

1702-
G_SaveGameName(name,sizeof(name),savegameslot, demoplayback && !menu);
1721+
// if the cart has set the name, use it instead
1722+
if (save_game_name_len) {
1723+
strncpy(name, save_game_name, save_game_name_len);
1724+
name[save_game_name_len] = '\0'; // ensure null termination
1725+
// now unset the name so that we don't use it again
1726+
save_game_name_len = 0;
1727+
} else {
1728+
// fall back on original name / use (used by game's menu)
1729+
G_SaveGameName(name,sizeof(name),savegameslot);
1730+
}
17031731

17041732
description = savedescription;
17051733

components/doom/prboom/g_game.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ void G_WorldDone(void);
6666
void G_EndGame(void); /* cph - make m_menu.c call a G_* function for this */
6767
void G_Ticker(void);
6868
void G_ReloadDefaults(void); // killough 3/1/98: loads game defaults
69-
void G_SaveGameName(char *, size_t, int, boolean); /* killough 3/22/98: sets savegame filename */
69+
void G_SetSaveGameFileName(char *name, size_t size);
70+
void G_SaveGameName(char *, size_t, int slot); /* killough 3/22/98: sets savegame filename */
7071
void G_SetFastParms(int); // killough 4/10/98: sets -fast parameters
7172
void G_DoNewGame(void);
7273
void G_DoReborn(int playernum);

components/doom/prboom/m_menu.c

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,8 @@ static const menu_t SetupDef =
462462
0
463463
};
464464

465-
static const setup_menu_t gen_settings3[] = // General Settings
465+
setup_menu_t *gen_settings3 = NULL;
466+
const setup_menu_t gen_settings3_init[] = // General Settings
466467
{
467468
{"GENERAL",S_SKIP|S_TITLE,m_null,200,G_Y},
468469
{"Enable Translucency", S_YESNO, m_null, G_X, G_Y + 1*G_H, {"translucency"}, M_Trans},
@@ -484,8 +485,10 @@ static const setup_menu_t gen_settings3[] = // General Settings
484485

485486
{0,S_SKIP|S_END,m_null}
486487
};
488+
const int num_gen_settings3 = sizeof(gen_settings3_init) / sizeof(gen_settings3_init[0]);
487489

488-
static const setup_menu_t weap_settings1[] = // Weapons Settings screen
490+
setup_menu_t *weap_settings1 = NULL;
491+
const setup_menu_t weap_settings1_init[] = // Weapons Settings screen
489492
{
490493
{"WEAPONS",S_SKIP|S_TITLE,m_null,200,G_Y},
491494
{"ENABLE RECOIL", S_YESNO,m_null,G_X, G_Y+1*G_H, {"weapon_recoil"}},
@@ -505,8 +508,10 @@ static const setup_menu_t weap_settings1[] = // Weapons Settings screen
505508

506509
{0,S_SKIP|S_END,m_null}
507510
};
511+
const int num_weap_settings1 = sizeof(weap_settings1_init) / sizeof(weap_settings1_init[0]);
508512

509-
static const setup_menu_t stat_settings1[] = // Status Bar and HUD Settings screen
513+
setup_menu_t *stat_settings1 = NULL;
514+
const setup_menu_t stat_settings1_init[] = // Status Bar and HUD Settings screen
510515
{
511516
{"STATUS BAR" ,S_SKIP|S_TITLE,m_null,200,G_Y},
512517
{"Use Red Numbers" ,S_YESNO, m_null,G_X,G_Y+ 1*G_H, {"sts_always_red"}},
@@ -523,8 +528,10 @@ static const setup_menu_t stat_settings1[] = // Status Bar and HUD Settings scr
523528

524529
{0,S_SKIP|S_END,m_null}
525530
};
531+
const int num_stat_settings1 = sizeof(stat_settings1_init) / sizeof(stat_settings1_init[0]);
526532

527-
static const setup_menu_t enem_settings1[] = // Enemy Settings screen
533+
setup_menu_t *enem_settings1 = NULL;
534+
const setup_menu_t enem_settings1_init[] = // Enemy Settings screen
528535
{
529536
{"ENEMIES" ,S_SKIP|S_TITLE,m_null,200,G_Y},
530537
{"Monster Infighting When Provoked",S_YESNO,m_null,G_X,G_Y+ 1*G_H, {"monster_infighting"}},
@@ -542,8 +549,10 @@ static const setup_menu_t enem_settings1[] = // Enemy Settings screen
542549

543550
{0,S_SKIP|S_END,m_null}
544551
};
552+
const int num_enem_settings1 = sizeof(enem_settings1_init) / sizeof(enem_settings1_init[0]);
545553

546-
static const setup_menu_t comp_settings1[] = // Compatibility Settings screen #1
554+
setup_menu_t *comp_settings1 = NULL;
555+
const setup_menu_t comp_settings1_init[] = // Compatibility Settings screen #1
547556
{
548557
{"COMPAT 1/1",S_SKIP|S_TITLE,m_null,200,G_Y},
549558
{"Any monster can telefrag on MAP30", S_YESNO, m_null, G_X, G_Y + 1*G_H, {"comp_telefrag"}},
@@ -560,8 +569,10 @@ static const setup_menu_t comp_settings1[] = // Compatibility Settings screen #
560569

561570
{0,S_SKIP|S_END,m_null}
562571
};
572+
const int num_comp_settings1 = sizeof(comp_settings1_init) / sizeof(comp_settings1_init[0]);
563573

564-
static const setup_menu_t comp_settings2[] = // Compatibility Settings screen #2
574+
setup_menu_t *comp_settings2 = NULL;
575+
const setup_menu_t comp_settings2_init[] = // Compatibility Settings screen #2
565576
{
566577
{"COMPAT 2/2",S_SKIP|S_TITLE,m_null,200,G_Y},
567578
{"Lost souls don't bounce off flat surfaces", S_YESNO, m_null, G_X, G_Y + 1*G_H, {"comp_soul"}},
@@ -578,8 +589,10 @@ static const setup_menu_t comp_settings2[] = // Compatibility Settings screen #
578589

579590
{0,S_SKIP|S_END,m_null}
580591
};
592+
const int num_comp_settings2 = sizeof(comp_settings2_init) / sizeof(comp_settings2_init[0]);
581593

582-
static const setup_menu_t keys_settings1[] = // Key Binding screen strings
594+
setup_menu_t *keys_settings1 = NULL;
595+
const setup_menu_t keys_settings1_init[] = // Key Binding screen strings
583596
{
584597
{"KEYBOARD 1/2",S_SKIP|S_TITLE,m_null,200,G_Y},
585598
{"MOVEMENT" ,S_SKIP|S_TITLE,m_null,G_X,G_Y+1*G_H},
@@ -601,8 +614,10 @@ static const setup_menu_t keys_settings1[] = // Key Binding screen strings
601614

602615
{0,S_SKIP|S_END,m_null}
603616
};
617+
const int num_keys_settings1 = sizeof(keys_settings1_init) / sizeof(keys_settings1_init[0]);
604618

605-
static const setup_menu_t keys_settings2[] = // Key Binding screen strings
619+
setup_menu_t *keys_settings2 = NULL;
620+
const setup_menu_t keys_settings2_init[] = // Key Binding screen strings
606621
{
607622
{"KEYBOARD 2/2",S_SKIP|S_TITLE,m_null,200,G_Y},
608623
{"SCREEN" ,S_SKIP|S_TITLE,m_null,G_X,G_Y+1*G_H},
@@ -625,6 +640,7 @@ static const setup_menu_t keys_settings2[] = // Key Binding screen strings
625640

626641
{0,S_SKIP|S_END,m_null}
627642
};
643+
const int num_keys_settings2 = sizeof(keys_settings2_init) / sizeof(keys_settings2_init[0]);
628644

629645
static const setup_menu_t helpstrings[] = // HELP screen strings
630646
{
@@ -713,19 +729,20 @@ static const setup_menu_t cred_settings[] =
713729
{0,S_SKIP|S_END,m_null},
714730
};
715731

716-
static setup_menu_t *setup_screens[] =
717-
{
718-
gen_settings3,
719-
weap_settings1,
720-
stat_settings1,
721-
enem_settings1,
722-
comp_settings1,
723-
comp_settings2,
724-
keys_settings1,
725-
keys_settings2,
732+
setup_menu_t **setup_screens = NULL;
733+
const setup_menu_t *setup_screens_init[] =
734+
{
735+
NULL, // gen_settings3,
736+
NULL, // weap_settings1,
737+
NULL, // stat_settings1,
738+
NULL, // enem_settings1,
739+
NULL, // comp_settings1,
740+
NULL, // comp_settings2,
741+
NULL, // keys_settings1,
742+
NULL, // keys_settings2,
726743
NULL
727744
};
728-
745+
const int num_setup_screens = sizeof(setup_screens_init) / sizeof(setup_menu_t*);
729746

730747
static void M_SetupNextMenu(menu_t *menudef)
731748
{
@@ -1244,6 +1261,7 @@ static void M_InitDefaults(void)
12441261
if (!(dp = M_LookupDefault(t->var.name))) {
12451262
I_Error("M_InitDefaults: Couldn't find config variable '%s'\n", t->var.name);
12461263
} else {
1264+
// I_Error("M_InitDefaults: found config variable '%s'\n", t->var.name);
12471265
t->var.def = dp;
12481266
}
12491267
}
@@ -1379,7 +1397,7 @@ static void M_ReadSaveStrings(void)
13791397
char name[PATH_MAX+1];
13801398
FILE *fp;
13811399

1382-
G_SaveGameName(name,sizeof(name),i,false);
1400+
G_SaveGameName(name,sizeof(name),i);
13831401

13841402
if ((fp = fopen(name, "rb"))) {
13851403
fread(&savegamestrings[i], SAVESTRINGSIZE, 1, fp);

components/doom/prboom/m_misc.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,9 @@ extern const char* chat_macros[];
155155

156156
extern int map_point_coordinates;
157157

158-
static const default_t defaults[] =
158+
default_t *defaults = NULL;
159+
160+
const default_t defaults_init[] =
159161
{
160162
{"Misc settings",{NULL},{0},UL,UL,def_none,ss_none},
161163
{"default_compatibility_level",{(int*)&default_compatibility_level},
@@ -631,7 +633,8 @@ static const default_t defaults[] =
631633

632634
};
633635

634-
static const int numdefaults = sizeof(defaults)/sizeof(defaults[0]);
636+
const int numdefaults_bytes = sizeof(defaults_init);
637+
const int numdefaults = sizeof(defaults_init)/sizeof(defaults_init[0]);
635638
static const char* configfile;
636639

637640
//

components/doom/src/doom.cpp

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ static bool unlock = false;
1515

1616
static uint16_t doom_palette[256];
1717

18+
static constexpr int DEFAULT_AUDIO_VOLUME = 15;
1819
static std::unique_ptr<espp::Task> audio_task;
1920

2021
static const char *doom_argv[10];
@@ -405,8 +406,8 @@ extern "C" {
405406
void I_Init(void) {
406407
snd_channels = NUM_MIX_CHANNELS;
407408
snd_samplerate = AUDIO_SAMPLE_RATE;
408-
snd_MusicVolume = 50;
409-
snd_SfxVolume = 50;
409+
snd_MusicVolume = DEFAULT_AUDIO_VOLUME;
410+
snd_SfxVolume = DEFAULT_AUDIO_VOLUME;
410411
usegamma = 0;
411412
}
412413
} // extern "C"
@@ -439,7 +440,7 @@ void init_doom(const std::string& wad_filename, uint8_t *wad_data, size_t wad_da
439440
myargc = 5;
440441
doom_argv[0] = "doom";
441442
doom_argv[1] = "-save";
442-
doom_argv[2] = "/sdcard/doom";
443+
doom_argv[2] = "/sdcard/saves";
443444
doom_argv[3] = "-iwad";
444445
doom_argv[4] = wad_filename.c_str();
445446
// doom_argv[5] = "-file";
@@ -510,19 +511,25 @@ void pause_doom_tasks() {
510511
}
511512

512513
void resume_doom_tasks() {
513-
snd_MusicVolume = 50;
514-
snd_SfxVolume = 50;
514+
snd_MusicVolume = DEFAULT_AUDIO_VOLUME;
515+
snd_SfxVolume = DEFAULT_AUDIO_VOLUME;
515516
}
516517

517-
void load_doom(int save_slot) {
518+
void load_doom(std::string_view save_path, int save_slot) {
518519
if (save_slot >= 0) {
519-
bool command = false; // TODO: what does this do?
520+
// set the save game file name
521+
G_SetSaveGameFileName(const_cast<char*>(save_path.data()), save_path.size());
522+
// load the game
523+
bool command = false;
520524
G_LoadGame(save_slot, command);
521525
}
522526
}
523527

524-
void save_doom(int save_slot) {
528+
void save_doom(std::string_view save_path, int save_slot) {
525529
if (save_slot >= 0) {
530+
// set the save game file name
531+
G_SetSaveGameFileName(const_cast<char*>(save_path.data()), save_path.size());
532+
// save the game
526533
auto description = fmt::format("Save Slot {}", save_slot);
527534
G_SaveGame(save_slot, const_cast<char*>(description.c_str()));
528535
}

0 commit comments

Comments
 (0)