changed QThread setup to be a separate object from the console window.

This commit is contained in:
Matthew Budd 2020-07-05 16:27:13 -04:00
parent acc02ee98e
commit f2e7b66c61
4 changed files with 40 additions and 46 deletions

View File

@ -9,7 +9,7 @@
#include "Qt/keyscan.h" #include "Qt/keyscan.h"
#include "Qt/nes_shm.h" #include "Qt/nes_shm.h"
gameWin_t::gameWin_t(QWidget *parent) consoleWin_t::consoleWin_t(QWidget *parent)
: QMainWindow( parent ) : QMainWindow( parent )
{ {
@ -21,27 +21,22 @@ gameWin_t::gameWin_t(QWidget *parent)
setCentralWidget(viewport); setCentralWidget(viewport);
gameTimer = new QTimer( this ); gameTimer = new QTimer( this );
gameThread = new QThread( this );
worker = new gameWorkerThread_t();
mutex = new QMutex( QMutex::NonRecursive ); mutex = new QMutex( QMutex::NonRecursive );
emulatorThread = new emulatorThread_t();
worker->moveToThread(gameThread); connect(emulatorThread, &QThread::finished, emulatorThread, &QObject::deleteLater);
connect(gameThread, &QThread::finished, worker, &QObject::deleteLater);
connect(gameThread, SIGNAL (started()), worker, SLOT( runEmulator() ));
connect(worker, SIGNAL (finished()), gameThread, SLOT (quit()));
connect(worker, SIGNAL (finished()), worker, SLOT (deleteLater()));
connect( gameTimer, &QTimer::timeout, this, &gameWin_t::runGameFrame ); connect( gameTimer, &QTimer::timeout, this, &consoleWin_t::updateDisplay );
gameTimer->setTimerType( Qt::PreciseTimer ); gameTimer->setTimerType( Qt::PreciseTimer );
gameTimer->start( 16 ); gameTimer->start( 16 ); // 60hz
gameThread->start(); emulatorThread->start();
gamePadConfWin = NULL; gamePadConfWin = NULL;
} }
gameWin_t::~gameWin_t(void) consoleWin_t::~consoleWin_t(void)
{ {
nes_shm->runEmulator = 0; nes_shm->runEmulator = 0;
@ -54,14 +49,14 @@ gameWin_t::~gameWin_t(void)
fceuWrapperUnLock(); fceuWrapperUnLock();
//printf("Thread Finished: %i \n", gameThread->isFinished() ); //printf("Thread Finished: %i \n", gameThread->isFinished() );
gameThread->exit(0); emulatorThread->quit();
gameThread->wait(); emulatorThread->wait();
delete viewport; delete viewport;
delete mutex; delete mutex;
} }
void gameWin_t::setCyclePeriodms( int ms ) void consoleWin_t::setCyclePeriodms( int ms )
{ {
// If timer is already running, it will be restarted. // If timer is already running, it will be restarted.
gameTimer->start( ms ); gameTimer->start( ms );
@ -69,7 +64,7 @@ void gameWin_t::setCyclePeriodms( int ms )
//printf("Period Set to: %i ms \n", ms ); //printf("Period Set to: %i ms \n", ms );
} }
void gameWin_t::closeEvent(QCloseEvent *event) void consoleWin_t::closeEvent(QCloseEvent *event)
{ {
//printf("Main Window Close Event\n"); //printf("Main Window Close Event\n");
if ( gamePadConfWin != NULL ) if ( gamePadConfWin != NULL )
@ -82,20 +77,20 @@ void gameWin_t::closeEvent(QCloseEvent *event)
closeApp(); closeApp();
} }
void gameWin_t::keyPressEvent(QKeyEvent *event) void consoleWin_t::keyPressEvent(QKeyEvent *event)
{ {
//printf("Key Press: 0x%x \n", event->key() ); //printf("Key Press: 0x%x \n", event->key() );
pushKeyEvent( event, 1 ); pushKeyEvent( event, 1 );
} }
void gameWin_t::keyReleaseEvent(QKeyEvent *event) void consoleWin_t::keyReleaseEvent(QKeyEvent *event)
{ {
//printf("Key Release: 0x%x \n", event->key() ); //printf("Key Release: 0x%x \n", event->key() );
pushKeyEvent( event, 0 ); pushKeyEvent( event, 0 );
} }
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
void gameWin_t::createMainMenu(void) void consoleWin_t::createMainMenu(void)
{ {
// This is needed for menu bar to show up on MacOS // This is needed for menu bar to show up on MacOS
menuBar()->setNativeMenuBar(false); menuBar()->setNativeMenuBar(false);
@ -161,7 +156,7 @@ void gameWin_t::createMainMenu(void)
helpMenu->addAction(aboutAct); helpMenu->addAction(aboutAct);
}; };
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
void gameWin_t::closeApp(void) void consoleWin_t::closeApp(void)
{ {
nes_shm->runEmulator = 0; nes_shm->runEmulator = 0;
@ -180,7 +175,7 @@ void gameWin_t::closeApp(void)
} }
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
void gameWin_t::openROMFile(void) void consoleWin_t::openROMFile(void)
{ {
int ret; int ret;
QString filename; QString filename;
@ -231,14 +226,14 @@ void gameWin_t::openROMFile(void)
return; return;
} }
void gameWin_t::closeROMCB(void) void consoleWin_t::closeROMCB(void)
{ {
fceuWrapperLock(); fceuWrapperLock();
CloseGame(); CloseGame();
fceuWrapperUnLock(); fceuWrapperUnLock();
} }
void gameWin_t::openGamePadConfWin(void) void consoleWin_t::openGamePadConfWin(void)
{ {
if ( gamePadConfWin != NULL ) if ( gamePadConfWin != NULL )
{ {
@ -256,7 +251,7 @@ void gameWin_t::openGamePadConfWin(void)
//printf("GamePad Config Window Destroyed\n"); //printf("GamePad Config Window Destroyed\n");
} }
void gameWin_t::openGameSndConfWin(void) void consoleWin_t::openGameSndConfWin(void)
{ {
GameSndConfDialog_t *sndConfWin; GameSndConfDialog_t *sndConfWin;
@ -272,13 +267,13 @@ void gameWin_t::openGameSndConfWin(void)
printf("Sound Config Window Destroyed\n"); printf("Sound Config Window Destroyed\n");
} }
void gameWin_t::aboutQPlot(void) void consoleWin_t::aboutQPlot(void)
{ {
printf("About QPlot\n"); printf("About QPlot\n");
return; return;
} }
void gameWin_t::runGameFrame(void) void consoleWin_t::updateDisplay(void)
{ {
//struct timespec ts; //struct timespec ts;
//double t; //double t;
@ -293,7 +288,7 @@ void gameWin_t::runGameFrame(void)
return; return;
} }
void gameWorkerThread_t::runEmulator(void) void emulatorThread_t::run(void)
{ {
printf("Emulator Start\n"); printf("Emulator Start\n");
nes_shm->runEmulator = 1; nes_shm->runEmulator = 1;

View File

@ -21,23 +21,23 @@
#include "Qt/GameViewerSDL.h" #include "Qt/GameViewerSDL.h"
#include "Qt/GamePadConf.h" #include "Qt/GamePadConf.h"
class gameWorkerThread_t : public QObject class emulatorThread_t : public QThread
{ {
Q_OBJECT Q_OBJECT
public slots: //public slots:
void runEmulator( void ); void run( void ) override;
signals: signals:
void finished(); void finished();
}; };
class gameWin_t : public QMainWindow class consoleWin_t : public QMainWindow
{ {
Q_OBJECT Q_OBJECT
public: public:
gameWin_t(QWidget *parent = 0); consoleWin_t(QWidget *parent = 0);
~gameWin_t(void); ~consoleWin_t(void);
gameViewGL_t *viewport; gameViewGL_t *viewport;
//gameViewSDL_t *viewport; //gameViewSDL_t *viewport;
@ -59,8 +59,7 @@ class gameWin_t : public QMainWindow
QAction *aboutAct; QAction *aboutAct;
QTimer *gameTimer; QTimer *gameTimer;
QThread *gameThread; emulatorThread_t *emulatorThread;
gameWorkerThread_t *worker;
GamePadConfDialog_t *gamePadConfWin; GamePadConfDialog_t *gamePadConfWin;
@ -79,10 +78,10 @@ class gameWin_t : public QMainWindow
void aboutQPlot(void); void aboutQPlot(void);
void openGamePadConfWin(void); void openGamePadConfWin(void);
void openGameSndConfWin(void); void openGameSndConfWin(void);
void runGameFrame(void); void updateDisplay(void);
}; };
extern gameWin_t *gameWindow; extern consoleWin_t *consoleWindow;
#endif #endif

View File

@ -776,7 +776,7 @@ static void DoFun(int frameskip, int periodic_saves)
void fceuWrapperLock(void) void fceuWrapperLock(void)
{ {
gameWindow->mutex->lock(); consoleWindow->mutex->lock();
mutexLocked = 1; mutexLocked = 1;
} }
@ -784,7 +784,7 @@ bool fceuWrapperTryLock(int timeout)
{ {
bool lockAcq; bool lockAcq;
lockAcq = gameWindow->mutex->tryLock( timeout ); lockAcq = consoleWindow->mutex->tryLock( timeout );
if ( lockAcq ) if ( lockAcq )
{ {
@ -797,7 +797,7 @@ void fceuWrapperUnLock(void)
{ {
if ( mutexLocked ) if ( mutexLocked )
{ {
gameWindow->mutex->unlock(); consoleWindow->mutex->unlock();
mutexLocked = 0; mutexLocked = 0;
} }
else else

View File

@ -3,7 +3,7 @@
#include "Qt/GameApp.h" #include "Qt/GameApp.h"
#include "Qt/fceuWrapper.h" #include "Qt/fceuWrapper.h"
gameWin_t *gameWindow = NULL; consoleWin_t *consoleWindow = NULL;
int main( int argc, char *argv[] ) int main( int argc, char *argv[] )
{ {
@ -12,18 +12,18 @@ int main( int argc, char *argv[] )
fceuWrapperInit( argc, argv ); fceuWrapperInit( argc, argv );
gameWindow = new gameWin_t(); consoleWindow = new consoleWin_t();
gameWindow->resize( 512, 512 ); consoleWindow->resize( 512, 512 );
gameWindow->show(); consoleWindow->show();
gameWindow->viewport->init(); consoleWindow->viewport->init();
retval = app.exec(); retval = app.exec();
//printf("App Return: %i \n", retval ); //printf("App Return: %i \n", retval );
delete gameWindow; delete consoleWindow;
return retval; return retval;
} }