Skip to content

Commit cc234ae

Browse files
committed
Added on state loaded callback function to core so that driver code can be notified of a new state being loaded. In Qt driver, emit a signal on state loads that objects can connect to. For a resync of all netplay clients when server detects a new state load.
1 parent 2ff6084 commit cc234ae

File tree

6 files changed

+45
-3
lines changed

6 files changed

+45
-3
lines changed

src/drivers/Qt/ConsoleWindow.cpp

+11
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,17 @@ consoleWin_t::consoleWin_t(QWidget *parent)
274274
aviDiskThread = new AviRecordDiskThread_t(this);
275275

276276
scrHandlerConnected = false;
277+
278+
// Register State Load Callback with Emulation Core
279+
auto stateLoadCallback = []( bool loadSuccess )
280+
{
281+
//printf("State Loaded: %i \n", loadSuccess );
282+
if (loadSuccess && (consoleWindow != nullptr) )
283+
{
284+
emit consoleWindow->stateLoaded();
285+
}
286+
};
287+
FCEUSS_SetLoadCallback( stateLoadCallback );
277288
}
278289

279290
consoleWin_t::~consoleWin_t(void)

src/drivers/Qt/ConsoleWindow.h

+1
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,7 @@ class consoleWin_t : public QMainWindow
328328
public:
329329
signals:
330330
void romLoaded(void);
331+
void stateLoaded(void);
331332
void nesResetOccurred(void);
332333

333334
public slots:

src/drivers/Qt/NetPlay.cpp

+19-1
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ NetPlayServer::NetPlayServer(QObject *parent)
176176
connect(this, SIGNAL(newConnection(void)), this, SLOT(newConnectionRdy(void)));
177177

178178
connect(consoleWindow, SIGNAL(romLoaded(void)), this, SLOT(onRomLoad(void)));
179+
connect(consoleWindow, SIGNAL(stateLoaded(void)), this, SLOT(onStateLoad(void)));
179180
connect(consoleWindow, SIGNAL(nesResetOccurred(void)), this, SLOT(onNesReset(void)));
180181

181182
FCEU_WRAPPER_LOCK();
@@ -454,9 +455,26 @@ void NetPlayServer::onRomLoad()
454455
FCEU_WRAPPER_UNLOCK();
455456
}
456457
//-----------------------------------------------------------------------------
458+
void NetPlayServer::onStateLoad()
459+
{
460+
//printf("New State Loaded!\n");
461+
FCEU_WRAPPER_LOCK();
462+
463+
inputClear();
464+
inputFrameCount = static_cast<uint32_t>(currFrameCounter);
465+
466+
// New State has been loaded by server, signal clients to load and sync
467+
for (auto& client : clientList )
468+
{
469+
//sendRomLoadReq( client );
470+
sendStateSyncReq( client );
471+
}
472+
FCEU_WRAPPER_UNLOCK();
473+
}
474+
//-----------------------------------------------------------------------------
457475
void NetPlayServer::onNesReset()
458476
{
459-
//printf("New ROM Loaded!\n");
477+
//printf("NES Reset Event!\n");
460478
FCEU_WRAPPER_LOCK();
461479

462480
inputClear();

src/drivers/Qt/NetPlay.h

+1
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ class NetPlayServer : public QTcpServer
175175
public slots:
176176
void newConnectionRdy(void);
177177
void onRomLoad(void);
178+
void onStateLoad(void);
178179
void onNesReset(void);
179180
};
180181

src/state.cpp

+12-2
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ using namespace std;
6363

6464
static void (*SPreSave)(void) = NULL;
6565
static void (*SPostSave)(void) = NULL;
66+
static void (*SPostLoad)(bool) = NULL;
6667

6768
static int SaveStateStatus[10];
6869
static int StateShow;
@@ -640,8 +641,8 @@ int FCEUSS_LoadFP_old(EMUFILE* is, ENUM_SSLOADPARAMS params)
640641
}
641642

642643
#ifdef __QT_DRIVER__
643-
// Qt Driver NetPlay state load handler. This is to control state loading,
644-
// only hosts can load states and clients can request loads.
644+
// Qt Driver NetPlay state load handler. This is to control state loading
645+
// during netplay, only hosts can load states and clients can request loads.
645646
bool NetPlayStateLoadReq(EMUFILE* is);
646647
#endif
647648

@@ -730,9 +731,18 @@ bool FCEUSS_LoadFP(EMUFILE* is, ENUM_SSLOADPARAMS params)
730731
FCEUSS_LoadFP(&msBackupSavestate,SSLOADPARAM_NOBACKUP);
731732
}
732733

734+
// Post state load callback that is used to notify driver code that a new state load occurred.
735+
if (SPostLoad != NULL)
736+
{
737+
SPostLoad(x);
738+
}
733739
return x;
734740
}
735741

742+
void FCEUSS_SetLoadCallback( void (*cb)(bool) )
743+
{
744+
SPostLoad = cb;
745+
}
736746

737747
bool FCEUSS_Load(const char *fname, bool display_message)
738748
{

src/state.h

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ enum ENUM_SSLOADPARAMS
2828

2929
void FCEUSS_Save(const char *, bool display_message=true);
3030
bool FCEUSS_Load(const char *, bool display_message=true);
31+
void FCEUSS_SetLoadCallback( void (*cb)(bool) );
3132

3233
//zlib values: 0 (none) through 9 (max) or -1 (default)
3334
bool FCEUSS_SaveMS(EMUFILE* outstream, int compressionLevel);

0 commit comments

Comments
 (0)