Skip to content

Commit 45b9ea7

Browse files
committed
Added savestate autoload/autosave feature
1 parent 05cc148 commit 45b9ea7

File tree

6 files changed

+231
-61
lines changed

6 files changed

+231
-61
lines changed

dist/manual.txt

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,11 @@ saving states.
3939
The default slot is always Slot 0.
4040

4141
The default slot will always be selected when
42-
starting the emulator. Selected savestate slot
43-
will not be saved on emulator exit.
42+
starting the emulator. The slot selection will
43+
not be saved on emulator exit.
44+
45+
The default slot will be used by the savestate
46+
autosave/autoload feature when enabled.
4447

4548

4649
Emulator Options
@@ -74,9 +77,15 @@ Here is a list of the available config options:
7477
with the same name as the ROM file, and it
7578
will load the default border if it fails.
7679

77-
- System:
78-
Allows to select the system priority when
80+
- System Priority:
81+
Allows to select the system to use when
7982
a ROM supports both DMG and GBC modes.
83+
Default is "GBC".
84+
85+
- Savestates:
86+
Allows to set savestate related options,
87+
like autoload on game boot and autosave
88+
on game exit.
8089

8190
- Boot Logos:
8291
Allows to use GB/GBC BIOS files to display
@@ -88,7 +97,8 @@ Here is a list of the available config options:
8897
Default is "DMG only".
8998

9099
- Controls:
91-
Allows the user to tune the controls.
100+
Allows the user to tune the control related
101+
settings.
92102

93103
- Sound:
94104
Allows the user to select between Mono and

gambatte_sdl/builddate.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
#define BUILDDATE "20210421-010751"
1+
#define BUILDDATE "20210731-123313"

gambatte_sdl/libmenu.cpp

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ Mix_Chunk *menusound_move = NULL;
5656
Mix_Chunk *menusound_ok = NULL;
5757

5858
// Default config values
59-
int showfps = 0, ghosting = 1, biosenabled = 0, colorfilter = 0, gameiscgb = 0, buttonlayout = 0, stereosound = 1, prefercgb = 0, ffwhotkey = 1;
59+
int showfps = 0, ghosting = 1, biosenabled = 0, colorfilter = 0, gameiscgb = 0, buttonlayout = 0, stereosound = 1, prefercgb = 1, ffwhotkey = 1, stateautoload = 0, stateautosave = 0;
6060
uint32_t menupalblack = 0x000000, menupaldark = 0x505450, menupallight = 0xA8A8A8, menupalwhite = 0xF8FCF8;
6161
int filtervalue[12] = {135, 20, 0, 25, 0, 125, 20, 25, 0, 20, 105, 30};
6262
std::string selectedscaler= "No Scaling", dmgbordername = "DEFAULT", gbcbordername = "DEFAULT", palname = "DEFAULT", filtername = "NONE", currgamename = "default";
@@ -441,6 +441,60 @@ int textanim_reset(){
441441
return 0;
442442
}
443443

444+
void stateload_dms(int saveslot) {
445+
gambatte_p->selectState_NoOsd(saveslot);
446+
char overlaytext[20];
447+
if(gambatte_p->loadState_NoOsd()){
448+
can_reset = 1;//allow user to reset or save state once a savestate is loaded
449+
sprintf(overlaytext, "State %d loaded", gambatte_p->currentState());
450+
printOverlay(overlaytext);//print overlay text
451+
} else {
452+
sprintf(overlaytext, "State %d empty", gambatte_p->currentState());
453+
printOverlay(overlaytext);//print overlay text
454+
}
455+
}
456+
457+
void statesave_dms(int saveslot) {
458+
gambatte_p->selectState_NoOsd(saveslot);
459+
if(can_reset == 1){//boot logo already ended, can save state safely
460+
if(gameiscgb == 0){ //set palette to greyscale
461+
Uint32 value;
462+
for (int i = 0; i < 3; ++i) {
463+
for (int k = 0; k < 4; ++k) {
464+
if(k == 0)
465+
value = 0xF8FCF8;
466+
if(k == 1)
467+
value = 0xA8A8A8;
468+
if(k == 2)
469+
value = 0x505450;
470+
if(k == 3)
471+
value = 0x000000;
472+
gambatte_p->setDmgPaletteColor(i, k, value);
473+
}
474+
}
475+
} else { // disable color filter
476+
gambatte_p->setColorFilter(0, filtervalue);
477+
}
478+
//run the emulator for 1 frame, so the screen buffer is updated without color palettes
479+
std::size_t fakesamples = 35112;
480+
Array<Uint32> const fakeBuf(35112 + 2064);
481+
gambatte_p->runFor(blitter_p->inBuf().pixels, blitter_p->inBuf().pitch, fakeBuf, fakesamples);
482+
//save state. the snapshot will now be in greyscale
483+
gambatte_p->saveState_NoOsd(blitter_p->inBuf().pixels, blitter_p->inBuf().pitch);
484+
if(gameiscgb == 0){
485+
loadPalette(palname); //restore palette
486+
} else {
487+
loadFilter(filtername); //restore color filter
488+
}
489+
490+
char overlaytext[14];
491+
sprintf(overlaytext, "State %d saved", gambatte_p->currentState());
492+
printOverlay(overlaytext);//print overlay text
493+
} else if (can_reset == 0){//boot logo is still running, can't save state
494+
printOverlay("Unable to save");//print overlay text
495+
}
496+
}
497+
444498
int menu_main(menu_t *menu) {
445499
SDL_Event event;
446500
int dirty, loop, i;
@@ -2208,6 +2262,8 @@ void saveConfig(int pergame){
22082262
"DMGBORDERNAME %s\n"
22092263
"GBCBORDERNAME %s\n"
22102264
"PREFERCGB %d\n"
2265+
"STATEAUTOLOAD %d\n"
2266+
"STATEAUTOSAVE %d\n"
22112267
"BIOSENABLED %d\n"
22122268
"GHOSTING %d\n"
22132269
"BUTTONLAYOUT %d\n"
@@ -2220,6 +2276,8 @@ void saveConfig(int pergame){
22202276
dmgbordername.c_str(),
22212277
gbcbordername.c_str(),
22222278
prefercgb,
2279+
stateautoload,
2280+
stateautosave,
22232281
biosenabled,
22242282
ghosting,
22252283
buttonlayout,
@@ -2324,6 +2382,12 @@ void loadConfig(){
23242382
} else if (!strcmp(line, "PREFERCGB")) {
23252383
sscanf(arg, "%d", &value);
23262384
prefercgb = value;
2385+
} else if (!strcmp(line, "STATEAUTOLOAD")) {
2386+
sscanf(arg, "%d", &value);
2387+
stateautoload = value;
2388+
} else if (!strcmp(line, "STATEAUTOSAVE")) {
2389+
sscanf(arg, "%d", &value);
2390+
stateautosave = value;
23272391
} else if (!strcmp(line, "BIOSENABLED")) {
23282392
sscanf(arg, "%d", &value);
23292393
biosenabled = value;

gambatte_sdl/libmenu.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ extern SDL_Surface *menuscreen;
109109
extern SDL_Surface *surface_menuinout;
110110
extern SDL_Surface *textoverlay;
111111
extern SDL_Surface *textoverlaycolored;
112-
extern int showfps, ghosting, biosenabled, colorfilter, gameiscgb, buttonlayout, stereosound, prefercgb, ffwhotkey;
112+
extern int showfps, ghosting, biosenabled, colorfilter, gameiscgb, buttonlayout, stereosound, prefercgb, ffwhotkey, stateautoload, stateautosave;
113113
extern uint32_t menupalblack, menupaldark, menupallight, menupalwhite;
114114
extern int filtervalue[12];
115115
extern std::string selectedscaler, dmgbordername, gbcbordername, palname, filtername, currgamename, homedir, ipuscaling;
@@ -185,6 +185,9 @@ void apply_cfilter(SDL_Surface *surface);
185185
void printOverlay(const char *text);
186186
void clearAllCheats();
187187

188+
void stateload_dms(int saveslot);
189+
void statesave_dms(int saveslot);
190+
188191
#ifdef MIYOO_BATTERY_WARNING
189192
void checkBatt();
190193
#endif

0 commit comments

Comments
 (0)