Skip to content

Commit

Permalink
1545 savestates fixes (#1547)
Browse files Browse the repository at this point in the history
* bugfix: set last active config to the savestate name (#1545)

* Enhance filename determination in gui_update()

The code now includes additional conditions to determine the `filename` in the `gui_update()` function. Specifically, it checks if `whdload_prefs.whdload_filename` is not empty and uses it to set the `filename`. If that condition is not met, it then checks if `last_loaded_config` has a non-zero length and uses it to set the `filename`. The previous logic that set `filename` to either `last_loaded_config` or "default.uae" has been removed.

* Improve savestate handling and UI feedback in PanelSavestate

Only enable buttons is a savestate title is set.
Show the title when displaying a not found message.
If a savestate is not found, don't try to load the screenshot for it either.
Minor code optimization.
  • Loading branch information
midwan authored Dec 23, 2024
1 parent fcf14be commit 0d9fa4a
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 41 deletions.
2 changes: 2 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1076,6 +1076,7 @@ static void parse_cmdline (int argc, TCHAR **argv)
savestate_state = STATE_DORESTORE;
xfree(txt);
}
SetLastActiveConfig(txt);
#endif
}
loaded = true;
Expand Down Expand Up @@ -1175,6 +1176,7 @@ static void parse_cmdline (int argc, TCHAR **argv)
savestate_state = STATE_DORESTORE;
currprefs.start_gui = false;
}
SetLastActiveConfig(txt);
}
else if (_tcscmp(txt2.c_str(), ".cue") == 0
|| _tcscmp(txt2.c_str(), ".iso") == 0
Expand Down
2 changes: 0 additions & 2 deletions src/osdep/amiberry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4467,8 +4467,6 @@ int main(int argc, char* argv[])
abort();
}

snprintf(savestate_fname, sizeof savestate_fname, "%s/default.ads", fix_trailing(savestate_dir).c_str());

reginitializeinit(&inipath);
if (getregmode() == NULL)
{
Expand Down
8 changes: 5 additions & 3 deletions src/osdep/amiberry_gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -922,10 +922,12 @@ int gui_update()
filename = extract_filename(currprefs.floppyslots[0].df);
else if (currprefs.cdslots[0].inuse && strlen(currprefs.cdslots[0].name) > 0)
filename = extract_filename(currprefs.cdslots[0].name);
else if (!whdload_prefs.whdload_filename.empty())
filename = extract_filename(whdload_prefs.whdload_filename);
else if (strlen(last_loaded_config) > 0)
filename = extract_filename(std::string(last_loaded_config));
else
{
last_loaded_config[0] != '\0' ? filename = std::string(last_loaded_config) : filename = "default.uae";
}
return 0;

get_savestate_path(savestate_fname, MAX_DPATH - 1);
strncat(savestate_fname, filename.c_str(), MAX_DPATH - 1);
Expand Down
77 changes: 41 additions & 36 deletions src/osdep/gui/PanelSavestate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ static gcn::Image* imgSavestate = nullptr;
static gcn::Button* cmdLoadState;
static gcn::Button* cmdSaveState;

std::string get_file_timestamp(const std::string& filename)
static std::string get_file_timestamp(const std::string& filename)
{
struct stat st {};
tm tm{};
Expand All @@ -52,7 +52,6 @@ class SavestateActionListener : public gcn::ActionListener
{
current_state_num = std::distance(radioButtons.begin(), it);
}

else if (actionEvent.getSource() == cmdLoadState)
{
//------------------------------------------
Expand All @@ -70,12 +69,16 @@ class SavestateActionListener : public gcn::ActionListener
savestate_state = STATE_DORESTORE;
gui_running = false;
}
else
{
ShowMessage("Loading savestate", "Statefile doesn't exist.", "", "", "Ok", "");
}
}
if (savestate_state != STATE_DORESTORE)
ShowMessage("Loading savestate", "Statefile doesn't exist.", "", "", "Ok", "");
}
else
{
ShowMessage("Loading savestate", "Emulation hasn't started yet.", "", "", "Ok", "");
}

cmdLoadState->requestFocus();
}
Expand Down Expand Up @@ -107,7 +110,9 @@ class SavestateActionListener : public gcn::ActionListener
save_thumb(screenshot_filename);
}
else
{
ShowMessage("Saving state", "Emulation hasn't started yet.", "", "", "Ok", "");
}

cmdSaveState->requestFocus();
}
Expand Down Expand Up @@ -210,7 +215,7 @@ void RefreshPanelSavestate()
imgSavestate = nullptr;
}

if (current_state_num >= 0 && current_state_num < radioButtons.size()) {
if (current_state_num >= 0 && current_state_num < static_cast<int>(radioButtons.size())) {
radioButtons[current_state_num]->setSelected(true);
}

Expand All @@ -222,50 +227,50 @@ void RefreshPanelSavestate()
if (f) {
fclose(f);
lblTimestamp->setCaption(get_file_timestamp(savestate_fname));

if (!screenshot_filename.empty())
{
auto* const screenshot_file = fopen(screenshot_filename.c_str(), "rbe");
if (screenshot_file)
{
fclose(screenshot_file);
const auto rect = grpScreenshot->getChildrenArea();
auto* loaded_image = IMG_Load(screenshot_filename.c_str());
if (loaded_image != nullptr)
{
const SDL_Rect source = { 0, 0, loaded_image->w, loaded_image->h };
const SDL_Rect target = { 0, 0, rect.width, rect.height };
auto* scaled = SDL_CreateRGBSurface(0, rect.width, rect.height,
loaded_image->format->BitsPerPixel,
loaded_image->format->Rmask, loaded_image->format->Gmask,
loaded_image->format->Bmask, loaded_image->format->Amask);
SDL_SoftStretch(loaded_image, &source, scaled, &target);
SDL_FreeSurface(loaded_image);
imgSavestate = new gcn::SDLImage(scaled, true);
icoSavestate = new gcn::Icon(imgSavestate);
grpScreenshot->add(icoSavestate);
}
}
}
}
else
{
lblTimestamp->setCaption("No savestate found");
lblTimestamp->setCaption("No savestate found: " + extract_filename(std::string(savestate_fname)));
}
}

if (screenshot_filename.length() > 0)
else
{
auto* const f = fopen(screenshot_filename.c_str(), "rbe");
if (f)
{
fclose(f);
const auto rect = grpScreenshot->getChildrenArea();
auto* loadedImage = IMG_Load(screenshot_filename.c_str());
if (loadedImage != nullptr)
{
SDL_Rect source = {0, 0, 0, 0};
SDL_Rect target = {0, 0, 0, 0};
auto* scaled = SDL_CreateRGBSurface(0, rect.width, rect.height,
loadedImage->format->BitsPerPixel,
loadedImage->format->Rmask, loadedImage->format->Gmask,
loadedImage->format->Bmask, loadedImage->format->Amask);
source.w = loadedImage->w;
source.h = loadedImage->h;
target.w = rect.width;
target.h = rect.height;
SDL_SoftStretch(loadedImage, &source, scaled, &target);
SDL_FreeSurface(loadedImage);
loadedImage = nullptr;
imgSavestate = new gcn::SDLImage(scaled, true);
icoSavestate = new gcn::Icon(imgSavestate);
grpScreenshot->add(icoSavestate);
}
}
lblTimestamp->setCaption("No savestate loaded");
}
lblTimestamp->adjustSize();

for (const auto& radioButton : radioButtons) {
radioButton->setEnabled(true);
}

grpScreenshot->setVisible(true);
cmdLoadState->setEnabled(true);
cmdSaveState->setEnabled(true);
cmdLoadState->setEnabled(strlen(savestate_fname) > 0);
cmdSaveState->setEnabled(strlen(savestate_fname) > 0);
}

bool HelpPanelSavestate(std::vector<std::string>& helptext)
Expand Down

0 comments on commit 0d9fa4a

Please sign in to comment.