Skip to content

Commit

Permalink
Added on state loaded callback function to core so that driver code c…
Browse files Browse the repository at this point in the history
…an 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.
  • Loading branch information
thor2016 committed Mar 23, 2024
1 parent 2ff6084 commit cc234ae
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 3 deletions.
11 changes: 11 additions & 0 deletions src/drivers/Qt/ConsoleWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,17 @@ consoleWin_t::consoleWin_t(QWidget *parent)
aviDiskThread = new AviRecordDiskThread_t(this);

scrHandlerConnected = false;

// Register State Load Callback with Emulation Core
auto stateLoadCallback = []( bool loadSuccess )
{
//printf("State Loaded: %i \n", loadSuccess );
if (loadSuccess && (consoleWindow != nullptr) )
{
emit consoleWindow->stateLoaded();
}
};
FCEUSS_SetLoadCallback( stateLoadCallback );
}

consoleWin_t::~consoleWin_t(void)
Expand Down
1 change: 1 addition & 0 deletions src/drivers/Qt/ConsoleWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,7 @@ class consoleWin_t : public QMainWindow
public:
signals:
void romLoaded(void);
void stateLoaded(void);
void nesResetOccurred(void);

public slots:
Expand Down
20 changes: 19 additions & 1 deletion src/drivers/Qt/NetPlay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ NetPlayServer::NetPlayServer(QObject *parent)
connect(this, SIGNAL(newConnection(void)), this, SLOT(newConnectionRdy(void)));

connect(consoleWindow, SIGNAL(romLoaded(void)), this, SLOT(onRomLoad(void)));
connect(consoleWindow, SIGNAL(stateLoaded(void)), this, SLOT(onStateLoad(void)));
connect(consoleWindow, SIGNAL(nesResetOccurred(void)), this, SLOT(onNesReset(void)));

FCEU_WRAPPER_LOCK();
Expand Down Expand Up @@ -454,9 +455,26 @@ void NetPlayServer::onRomLoad()
FCEU_WRAPPER_UNLOCK();
}
//-----------------------------------------------------------------------------
void NetPlayServer::onStateLoad()
{
//printf("New State Loaded!\n");
FCEU_WRAPPER_LOCK();

inputClear();
inputFrameCount = static_cast<uint32_t>(currFrameCounter);

// New State has been loaded by server, signal clients to load and sync
for (auto& client : clientList )
{
//sendRomLoadReq( client );
sendStateSyncReq( client );
}
FCEU_WRAPPER_UNLOCK();
}
//-----------------------------------------------------------------------------
void NetPlayServer::onNesReset()
{
//printf("New ROM Loaded!\n");
//printf("NES Reset Event!\n");
FCEU_WRAPPER_LOCK();

inputClear();
Expand Down
1 change: 1 addition & 0 deletions src/drivers/Qt/NetPlay.h
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ class NetPlayServer : public QTcpServer
public slots:
void newConnectionRdy(void);
void onRomLoad(void);
void onStateLoad(void);
void onNesReset(void);
};

Expand Down
14 changes: 12 additions & 2 deletions src/state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ using namespace std;

static void (*SPreSave)(void) = NULL;
static void (*SPostSave)(void) = NULL;
static void (*SPostLoad)(bool) = NULL;

static int SaveStateStatus[10];
static int StateShow;
Expand Down Expand Up @@ -640,8 +641,8 @@ int FCEUSS_LoadFP_old(EMUFILE* is, ENUM_SSLOADPARAMS params)
}

#ifdef __QT_DRIVER__
// Qt Driver NetPlay state load handler. This is to control state loading,
// only hosts can load states and clients can request loads.
// Qt Driver NetPlay state load handler. This is to control state loading
// during netplay, only hosts can load states and clients can request loads.
bool NetPlayStateLoadReq(EMUFILE* is);
#endif

Expand Down Expand Up @@ -730,9 +731,18 @@ bool FCEUSS_LoadFP(EMUFILE* is, ENUM_SSLOADPARAMS params)
FCEUSS_LoadFP(&msBackupSavestate,SSLOADPARAM_NOBACKUP);
}

// Post state load callback that is used to notify driver code that a new state load occurred.
if (SPostLoad != NULL)
{
SPostLoad(x);
}
return x;
}

void FCEUSS_SetLoadCallback( void (*cb)(bool) )
{
SPostLoad = cb;
}

bool FCEUSS_Load(const char *fname, bool display_message)
{
Expand Down
1 change: 1 addition & 0 deletions src/state.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ enum ENUM_SSLOADPARAMS

void FCEUSS_Save(const char *, bool display_message=true);
bool FCEUSS_Load(const char *, bool display_message=true);
void FCEUSS_SetLoadCallback( void (*cb)(bool) );

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

0 comments on commit cc234ae

Please sign in to comment.