From 55bcb3d41a8afd8fd80cafc7ebb741694361d5e0 Mon Sep 17 00:00:00 2001 From: harry Date: Sat, 1 Apr 2023 21:17:34 -0400 Subject: [PATCH] Added a load prev and next state functions for state recorder. --- src/drivers/Qt/ConsoleWindow.cpp | 4 +- src/state.cpp | 63 +++++++++++++++++++++++++++++++- src/state.h | 2 + 3 files changed, 65 insertions(+), 4 deletions(-) diff --git a/src/drivers/Qt/ConsoleWindow.cpp b/src/drivers/Qt/ConsoleWindow.cpp index 3b94cfb9..7e2e3da4 100644 --- a/src/drivers/Qt/ConsoleWindow.cpp +++ b/src/drivers/Qt/ConsoleWindow.cpp @@ -2758,14 +2758,14 @@ void consoleWin_t::loadState9(void){ loadState(9); } void consoleWin_t::loadPrevState(void) { FCEU_WRAPPER_LOCK(); - FCEU_StateRecorderLoadState( FCEU_StateRecorderGetStateIndex()-1 ); + FCEU_StateRecorderLoadPrevState(); FCEU_WRAPPER_UNLOCK(); } void consoleWin_t::loadNextState(void) { FCEU_WRAPPER_LOCK(); - FCEU_StateRecorderLoadState( FCEU_StateRecorderGetStateIndex()-1 ); + FCEU_StateRecorderLoadNextState(); FCEU_WRAPPER_UNLOCK(); } diff --git a/src/state.cpp b/src/state.cpp index d2bd658d..db77ad77 100644 --- a/src/state.cpp +++ b/src/state.cpp @@ -1338,6 +1338,41 @@ class StateRecorder return 0; } + int loadPrevState(void) + { + int snapIdx = lastState; + + if ( lastState == ringHead ) + { // No States to Load + return -1; + } + if ( lastState != ringStart ) + { + if ( (lastLoadFrame+30) > frameCounter) + { + snapIdx--; + + if (snapIdx < 0) + { + snapIdx += ringBufSize; + } + } + } + return loadStateByIndex( snapIdx ); + } + + int loadNextState(void) + { + int snapIdx = lastState; + int nextIdx = (lastState + 1) % ringBufSize; + + if ( nextIdx != ringHead ) + { + snapIdx = nextIdx; + } + return loadStateByIndex( snapIdx ); + } + int getHeadIndex(void) { return ringHead; @@ -1436,11 +1471,13 @@ bool FCEU_StateRecorderRunning(void) int FCEU_StateRecorderLoadState(int snapIndex) { + int ret = -1; + if (stateRecorder != nullptr) { - stateRecorder->loadStateByIndex(snapIndex); + ret = stateRecorder->loadStateByIndex(snapIndex); } - return 0; + return ret; } int FCEU_StateRecorderGetStateIndex(void) @@ -1448,6 +1485,28 @@ int FCEU_StateRecorderGetStateIndex(void) return StateRecorder::lastState; } +int FCEU_StateRecorderLoadPrevState(void) +{ + int ret = -1; + + if (stateRecorder != nullptr) + { + ret = stateRecorder->loadPrevState(); + } + return ret; +} + +int FCEU_StateRecorderLoadNextState(void) +{ + int ret = -1; + + if (stateRecorder != nullptr) + { + ret = stateRecorder->loadNextState(); + } + return ret; +} + const StateRecorderConfigData& FCEU_StateRecorderGetConfigData(void) { return stateRecorderConfig; diff --git a/src/state.h b/src/state.h index 133f206a..1c1155a8 100644 --- a/src/state.h +++ b/src/state.h @@ -111,5 +111,7 @@ bool FCEU_StateRecorderIsEnabled(void); void FCEU_StateRecorderSetEnabled(bool enabled); int FCEU_StateRecorderGetStateIndex(void); int FCEU_StateRecorderLoadState(int snapIndex); +int FCEU_StateRecorderLoadPrevState(void); +int FCEU_StateRecorderLoadNextState(void); int FCEU_StateRecorderSetConfigData(const StateRecorderConfigData &newConfig); const StateRecorderConfigData& FCEU_StateRecorderGetConfigData(void);