From bb76573112df80626b3906204e30b396448e446f Mon Sep 17 00:00:00 2001 From: harry Date: Sat, 11 Mar 2023 20:45:13 -0500 Subject: [PATCH] State recorder config setup in work. --- src/drivers/Qt/StateRecorderConf.cpp | 32 ++++++++++++++ src/drivers/Qt/StateRecorderConf.h | 4 +- src/drivers/Qt/config.cpp | 5 +++ src/drivers/Qt/fceuWrapper.cpp | 7 ++++ src/state.cpp | 63 ++++++++++++++++++++++------ src/state.h | 17 ++++++++ 6 files changed, 114 insertions(+), 14 deletions(-) diff --git a/src/drivers/Qt/StateRecorderConf.cpp b/src/drivers/Qt/StateRecorderConf.cpp index 9c627957..a39de387 100644 --- a/src/drivers/Qt/StateRecorderConf.cpp +++ b/src/drivers/Qt/StateRecorderConf.cpp @@ -28,6 +28,7 @@ #include #include +#include "Qt/fceuWrapper.h" #include "Qt/StateRecorderConf.h" #include "../../fceu.h" @@ -54,6 +55,10 @@ StateRecorderDialog_t::StateRecorderDialog_t(QWidget *parent) snapSeconds = new QSpinBox(); historyDuration = new QSpinBox(); + recorderEnable->setChecked( FCEU_StateRecorderIsEnabled() ); + + connect( recorderEnable, SIGNAL(stateChanged(int)), this, SLOT(enableChanged(int)) ); + snapSeconds->setMinimum(0); snapSeconds->setMaximum(60); snapSeconds->setValue(3); @@ -64,6 +69,10 @@ StateRecorderDialog_t::StateRecorderDialog_t(QWidget *parent) historyDuration->setMaximum(180); historyDuration->setValue(15); + connect( snapSeconds, SIGNAL(valueChanged(int)), this, SLOT(spinBoxValueChanged(int)) ); + connect( snapMinutes, SIGNAL(valueChanged(int)), this, SLOT(spinBoxValueChanged(int)) ); + connect( historyDuration, SIGNAL(valueChanged(int)), this, SLOT(spinBoxValueChanged(int)) ); + frame = new QGroupBox(tr("Retain History For:")); hbox = new QHBoxLayout(); @@ -131,6 +140,9 @@ StateRecorderDialog_t::StateRecorderDialog_t(QWidget *parent) closeButton->setIcon( style()->standardIcon( QStyle::SP_DialogCloseButton ) ); applyButton->setIcon( style()->standardIcon( QStyle::SP_DialogApplyButton ) ); + connect(closeButton, SIGNAL(clicked(void)), this, SLOT(closeWindow(void))); + connect(applyButton, SIGNAL(clicked(void)), this, SLOT(applyChanges(void))); + hbox->addWidget(applyButton, 1); hbox->addStretch(8); hbox->addWidget(closeButton, 1); @@ -164,6 +176,26 @@ void StateRecorderDialog_t::closeWindow(void) deleteLater(); } //---------------------------------------------------------------------------- +void StateRecorderDialog_t::applyChanges(void) +{ + +} +//---------------------------------------------------------------------------- +void StateRecorderDialog_t::enableChanged(int val) +{ + bool ena = val ? true : false; + + FCEU_StateRecorderSetEnabled( ena ); + + g_config->setOption("SDL.StateRecorderEnable", ena); + g_config->save(); +} +//---------------------------------------------------------------------------- +void StateRecorderDialog_t::spinBoxValueChanged(int newValue) +{ + recalcMemoryUsage(); +} +//---------------------------------------------------------------------------- void StateRecorderDialog_t::recalcMemoryUsage(void) { char stmp[64]; diff --git a/src/drivers/Qt/StateRecorderConf.h b/src/drivers/Qt/StateRecorderConf.h index 75c28d08..aee06f79 100644 --- a/src/drivers/Qt/StateRecorderConf.h +++ b/src/drivers/Qt/StateRecorderConf.h @@ -43,5 +43,7 @@ protected: public slots: void closeWindow(void); private slots: - + void applyChanges(void); + void spinBoxValueChanged(int newValue); + void enableChanged(int); }; diff --git a/src/drivers/Qt/config.cpp b/src/drivers/Qt/config.cpp index 7d7c1d87..e2997b3a 100644 --- a/src/drivers/Qt/config.cpp +++ b/src/drivers/Qt/config.cpp @@ -754,6 +754,11 @@ InitConfig() config->addOption("loadstate", "SDL.AutoLoadState", INVALID_STATE); config->addOption("savestate", "SDL.AutoSaveState", INVALID_STATE); + config->addOption("SDL.StateRecorderEnable", false); + config->addOption("SDL.StateRecorderHistoryDurationMin", 15); + config->addOption("SDL.StateRecorderTimeBetweenSnapsMin", 0); + config->addOption("SDL.StateRecorderTimeBetweenSnapsSec", 3); + //TODO implement this config->addOption("periodicsaves", "SDL.PeriodicSaves", 0); diff --git a/src/drivers/Qt/fceuWrapper.cpp b/src/drivers/Qt/fceuWrapper.cpp index ef80bd2a..fe1cf6fd 100644 --- a/src/drivers/Qt/fceuWrapper.cpp +++ b/src/drivers/Qt/fceuWrapper.cpp @@ -52,6 +52,7 @@ #include "../../fceu.h" #include "../../cheat.h" #include "../../movie.h" +#include "../../state.h" #include "../../version.h" #ifdef _S9XLUA_H @@ -972,6 +973,12 @@ int fceuWrapperInit( int argc, char *argv[] ) // load the hotkeys from the config life setHotKeys(); + // Initialize the State Recorder + bool srEnable = false; + g_config->getOption("SDL.StateRecorderEnable", &srEnable); + + FCEU_StateRecorderSetEnabled( srEnable ); + if (romIndex >= 0) { QFileInfo fi( argv[romIndex] ); diff --git a/src/state.cpp b/src/state.cpp index fdfbec4c..16ee2289 100644 --- a/src/state.cpp +++ b/src/state.cpp @@ -1185,23 +1185,23 @@ void RedoLoadState() //----------- Save State History ---------------- //----------------------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------------------- +static StateRecorderConfigData stateRecorderConfig; + class StateRecorder { public: StateRecorder(void) { - size_t ringBufSize = 60; + loadConfig( stateRecorderConfig ); - for (size_t i=0; i config.historyDurationMinutes) + { + config.historyDurationMinutes = config.timeBetweenSnapsMinutes; + } + const double fhistMin = config.historyDurationMinutes; + const double fsnapMin = config.timeBetweenSnapsMinutes; + const double fnumSnaps = fhistMin / fsnapMin; + + ringBufSize = static_cast( fnumSnaps + 0.5f ); + + int32_t fps = FCEUI_GetDesiredFPS(); // Do >> 24 to get in Hz + + double hz = ( ((double)fps) / 16777216.0 ); + + double framesPerSnapf = hz * fsnapMin * 60.0; + + framesPerSnap = static_cast( framesPerSnapf + 0.50 ); + + printf("ringBufSize:%i framesPerSnap:%i\n", ringBufSize, framesPerSnap ); + + compressionLevel = stateRecorderConfig.compressionLevel; + } + void update(void) { bool isPaused = FCEUI_EmulationPaused() ? true : false; @@ -1224,8 +1253,6 @@ class StateRecorder if (!isPaused && loadIndexReset) { - int ringBufSize = static_cast( ringBuf.size() ); - ringHead = (lastState + 1) % ringBufSize; frameCounter = curFrame; @@ -1239,8 +1266,6 @@ class StateRecorder if ( (frameCounter % framesPerSnap) == 0 ) { - int ringBufSize = static_cast( ringBuf.size() ); - EMUFILE_MEMORY *em = ringBuf[ ringHead ]; em->set_len(0); @@ -1263,8 +1288,6 @@ class StateRecorder int loadStateRelativeToEnd( int numSnapsFromLatest ) { - int ringBufSize = static_cast( ringBuf.size() ); - if (numSnapsFromLatest < 0) { numSnapsFromLatest = 0; @@ -1280,8 +1303,6 @@ class StateRecorder int loadStateByIndex( int snapIdx ) { - int ringBufSize = static_cast( ringBuf.size() ); - if (snapIdx < 0) { snapIdx = snapIdx + ringBufSize; @@ -1339,6 +1360,7 @@ class StateRecorder int ringHead; int ringTail; int ringStart; + int ringBufSize; int compressionLevel; unsigned int frameCounter; unsigned int framesPerSnap; @@ -1383,6 +1405,11 @@ bool FCEU_StateRecorderIsEnabled(void) return StateRecorder::enabled; } +void FCEU_StateRecorderSetEnabled(bool enabled) +{ + StateRecorder::enabled = enabled; +} + bool FCEU_StateRecorderRunning(void) { return stateRecorder != nullptr; @@ -1401,3 +1428,13 @@ int FCEU_StateRecorderGetStateIndex(void) { return StateRecorder::lastState; } + +const StateRecorderConfigData& FCEU_StateRecorderGetConfigData(void) +{ + return stateRecorderConfig; +} +int FCEU_StateRecorderSetConfigData(const StateRecorderConfigData &newConfig) +{ + stateRecorderConfig = newConfig; + return 0; +} diff --git a/src/state.h b/src/state.h index 097f1977..3a58fad9 100644 --- a/src/state.h +++ b/src/state.h @@ -79,10 +79,27 @@ bool CheckBackupSaveStateExist(); //Checks if backupsavestate exists extern bool compressSavestates; //Whether or not to compress non-movie savestates (by default, yes) +struct StateRecorderConfigData +{ + float historyDurationMinutes; + float timeBetweenSnapsMinutes; + int compressionLevel; + + StateRecorderConfigData(void) + { + historyDurationMinutes = 15.0f; + timeBetweenSnapsMinutes = 3.0f / 60.0f; + compressionLevel = 0; + } +}; + int FCEU_StateRecorderStart(void); int FCEU_StateRecorderStop(void); int FCEU_StateRecorderUpdate(void); bool FCEU_StateRecorderRunning(void); bool FCEU_StateRecorderIsEnabled(void); +void FCEU_StateRecorderSetEnabled(bool enabled); int FCEU_StateRecorderGetStateIndex(void); int FCEU_StateRecorderLoadState(int snapIndex); +int FCEU_StateRecorderSetConfigData(const StateRecorderConfigData &newConfig); +const StateRecorderConfigData& FCEU_StateRecorderGetConfigData(void);