Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add selectable savestates #550

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 44 additions & 2 deletions src/program/GameEvents.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
#include <future>
#include <stdint.h>

#define MAX_SELECTABLE_SAVESTATE 9

GameEvents::GameEvents(Context* c, MovieFile* m) : context(c), movie(m) {}

void GameEvents::init()
Expand Down Expand Up @@ -109,6 +111,7 @@ bool GameEvents::processEvent(GameEvents::EventType type, struct HotKey &hk)
case HOTKEY_SAVESTATE8:
case HOTKEY_SAVESTATE9:
case HOTKEY_SAVESTATE_BACKTRACK:
case HOTKEY_SAVESTATE_SELECTED:
{
/* Perform a savestate:
* - save the moviefile if we are recording
Expand All @@ -122,7 +125,13 @@ bool GameEvents::processEvent(GameEvents::EventType type, struct HotKey &hk)
}

/* Slot number */
int statei = hk.type - HOTKEY_SAVESTATE1 + 1;
int statei;
if (hk.type == HOTKEY_SAVESTATE_SELECTED) {
statei = selectedSavestateId;
}
else {
statei = hk.type - HOTKEY_SAVESTATE1 + 1;
}

/* Perform savestate */
int message = SaveStateList::save(statei, context, *movie);
Expand All @@ -146,6 +155,7 @@ bool GameEvents::processEvent(GameEvents::EventType type, struct HotKey &hk)
case HOTKEY_LOADSTATE8:
case HOTKEY_LOADSTATE9:
case HOTKEY_LOADSTATE_BACKTRACK:
case HOTKEY_LOADSTATE_SELECTED:
case HOTKEY_LOADBRANCH1:
case HOTKEY_LOADBRANCH2:
case HOTKEY_LOADBRANCH3:
Expand Down Expand Up @@ -180,7 +190,13 @@ bool GameEvents::processEvent(GameEvents::EventType type, struct HotKey &hk)
bool load_branch = (hk.type >= HOTKEY_LOADBRANCH1) && (hk.type <= HOTKEY_LOADBRANCH_BACKTRACK);

/* Slot number */
int statei = hk.type - (load_branch?HOTKEY_LOADBRANCH1:HOTKEY_LOADSTATE1) + 1;
int statei;
if (hk.type == HOTKEY_LOADSTATE_SELECTED) {
statei = selectedSavestateId;
}
else {
statei = hk.type - (load_branch?HOTKEY_LOADBRANCH1:HOTKEY_LOADSTATE1) + 1;
}

/* Perform state loading */
int error = SaveStateList::load(statei, context, *movie, load_branch);
Expand Down Expand Up @@ -268,6 +284,32 @@ bool GameEvents::processEvent(GameEvents::EventType type, struct HotKey &hk)
return false;
}

case HOTKEY_SELECTSTATE_NEXT:
if (selectedSavestateId >= MAX_SELECTABLE_SAVESTATE) {
selectedSavestateId = 1;
}
else {
selectedSavestateId++;
}

sendMessage(MSGN_OSD_MSG);
sendString("State " + std::to_string(selectedSavestateId) + " selected");

return false;

case HOTKEY_SELECTSTATE_PREVIOUS:
if (selectedSavestateId <= 1) {
selectedSavestateId = MAX_SELECTABLE_SAVESTATE;
}
else {
selectedSavestateId--;
}

sendMessage(MSGN_OSD_MSG);
sendString("State " + std::to_string(selectedSavestateId) + " selected");

return false;

case HOTKEY_READWRITE:
/* Switch between movie write and read-only */
switch (context->config.sc.recording) {
Expand Down
3 changes: 3 additions & 0 deletions src/program/GameEvents.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ class GameEvents : public QObject {
/* Indicate if at least one savestate was performed, for backtrack savestate */
bool didASavestate = false;

/* Track selected savestate id */
int selectedSavestateId = 1;

protected:
Context* context;
MovieFile* movie;
Expand Down
4 changes: 4 additions & 0 deletions src/program/KeyMapping.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ KeyMapping::KeyMapping(void* c)
hotkey_list.push_back({{SingleInput::IT_KEYBOARD, XK_F8 | XK_Shift_Flag}, HOTKEY_SAVESTATE8, "Save State 8"});
hotkey_list.push_back({{SingleInput::IT_KEYBOARD, XK_F9 | XK_Shift_Flag}, HOTKEY_SAVESTATE9, "Save State 9"});
hotkey_list.push_back({{SingleInput::IT_NONE, 0}, HOTKEY_SAVESTATE_BACKTRACK, "Save Backtrack State"});
hotkey_list.push_back({{SingleInput::IT_NONE, 0}, HOTKEY_SAVESTATE_SELECTED, "Save Selected State"});
hotkey_list.push_back({{SingleInput::IT_KEYBOARD, XK_F1}, HOTKEY_LOADSTATE1, "Load State 1"});
hotkey_list.push_back({{SingleInput::IT_KEYBOARD, XK_F2}, HOTKEY_LOADSTATE2, "Load State 2"});
hotkey_list.push_back({{SingleInput::IT_KEYBOARD, XK_F3}, HOTKEY_LOADSTATE3, "Load State 3"});
Expand All @@ -70,6 +71,7 @@ KeyMapping::KeyMapping(void* c)
hotkey_list.push_back({{SingleInput::IT_KEYBOARD, XK_F8}, HOTKEY_LOADSTATE8, "Load State 8"});
hotkey_list.push_back({{SingleInput::IT_KEYBOARD, XK_F9}, HOTKEY_LOADSTATE9, "Load State 9"});
hotkey_list.push_back({{SingleInput::IT_KEYBOARD, XK_F10}, HOTKEY_LOADSTATE_BACKTRACK, "Load Backtrack State"});
hotkey_list.push_back({{SingleInput::IT_NONE, 0}, HOTKEY_LOADSTATE_SELECTED, "Load Selected State"});
hotkey_list.push_back({{SingleInput::IT_KEYBOARD, XK_F1 | XK_Control_Flag}, HOTKEY_LOADBRANCH1, "Load Branch 1"});
hotkey_list.push_back({{SingleInput::IT_KEYBOARD, XK_F2 | XK_Control_Flag}, HOTKEY_LOADBRANCH2, "Load Branch 2"});
hotkey_list.push_back({{SingleInput::IT_KEYBOARD, XK_F3 | XK_Control_Flag}, HOTKEY_LOADBRANCH3, "Load Branch 3"});
Expand All @@ -80,6 +82,8 @@ KeyMapping::KeyMapping(void* c)
hotkey_list.push_back({{SingleInput::IT_KEYBOARD, XK_F8 | XK_Control_Flag}, HOTKEY_LOADBRANCH8, "Load Branch 8"});
hotkey_list.push_back({{SingleInput::IT_KEYBOARD, XK_F9 | XK_Control_Flag}, HOTKEY_LOADBRANCH9, "Load Branch 9"});
hotkey_list.push_back({{SingleInput::IT_KEYBOARD, XK_F10 | XK_Control_Flag}, HOTKEY_LOADBRANCH_BACKTRACK, "Load Backtrack Branch"});
hotkey_list.push_back({{SingleInput::IT_NONE, 0}, HOTKEY_SELECTSTATE_NEXT, "Select Next State"});
hotkey_list.push_back({{SingleInput::IT_NONE, 0}, HOTKEY_SELECTSTATE_PREVIOUS, "Select Previous State"});
hotkey_list.push_back({{SingleInput::IT_NONE, 0}, HOTKEY_TOGGLE_ENCODE, "Toggle encode"});

/* Add flags mapping */
Expand Down
4 changes: 4 additions & 0 deletions src/program/KeyMapping.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ typedef int HotKeyType; enum
HOTKEY_SAVESTATE8,
HOTKEY_SAVESTATE9,
HOTKEY_SAVESTATE_BACKTRACK,
HOTKEY_SAVESTATE_SELECTED,
HOTKEY_LOADSTATE1, // Load the entire state of the game
HOTKEY_LOADSTATE2,
HOTKEY_LOADSTATE3,
Expand All @@ -65,6 +66,7 @@ typedef int HotKeyType; enum
HOTKEY_LOADSTATE8,
HOTKEY_LOADSTATE9,
HOTKEY_LOADSTATE_BACKTRACK,
HOTKEY_LOADSTATE_SELECTED,
HOTKEY_TOGGLE_ENCODE, // Start/stop audio/video encoding
HOTKEY_CALIBRATE_MOUSE, // Calibrate mouse cursor position
HOTKEY_LOADBRANCH1, // Load state and movie
Expand All @@ -77,6 +79,8 @@ typedef int HotKeyType; enum
HOTKEY_LOADBRANCH8,
HOTKEY_LOADBRANCH9,
HOTKEY_LOADBRANCH_BACKTRACK,
HOTKEY_SELECTSTATE_NEXT,
HOTKEY_SELECTSTATE_PREVIOUS,
Copy link
Owner

@clementgallet clementgallet Sep 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We would want to append the extra hotkeys at the end, so that it does not shift existing hotkey mappings

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for noticing - I did not realize this. Maybe these enum values could be explicitly numbered to make that more apparant?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, just saw there is a comment about this at the top.

HOTKEY_TOGGLE_FASTFORWARD, // Toggle fastforward
HOTKEY_LEN
};
Expand Down