Skip to content

Commit

Permalink
For Qt GUI, changed onFrameFinished callback to only update video buf…
Browse files Browse the repository at this point in the history
…fer. Don't do any input processing as this will mess up when running turbo mode. Added a draw timer to SDL video renderer to better align is scheduling with the next vsync.
  • Loading branch information
thor2016 committed Jan 31, 2024
1 parent d363d04 commit 0135840
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 18 deletions.
23 changes: 23 additions & 0 deletions src/drivers/Qt/ConsoleViewerSDL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ ConsoleViewSDL_t::ConsoleViewSDL_t(QWidget *parent)

vsyncEnabled = false;
mouseButtonMask = 0;
drawTimer = new QTimer(this);
drawTimer->setInterval(14);
drawTimer->setSingleShot(true);
drawTimer->setTimerType(Qt::PreciseTimer);
connect(drawTimer, &QTimer::timeout, this, &ConsoleViewSDL_t::onDrawSignal);

localBufSize = (4 * GL_NES_WIDTH) * (4 * GL_NES_HEIGHT) * sizeof(uint32_t);

Expand Down Expand Up @@ -123,6 +128,8 @@ ConsoleViewSDL_t::ConsoleViewSDL_t(QWidget *parent)
ConsoleViewSDL_t::~ConsoleViewSDL_t(void)
{
//printf("Destroying SDL Viewport\n");
drawTimer->stop();
delete drawTimer;

if ( localBuf )
{
Expand Down Expand Up @@ -585,6 +592,19 @@ void ConsoleViewSDL_t::getNormalizedCursorPos( double &x, double &y )
//printf("Normalized Cursor (%f,%f) \n", x, y );
}

void ConsoleViewSDL_t::queueRedraw(void)
{
if (!drawTimer->isActive())
{
render();
}
}

void ConsoleViewSDL_t::onDrawSignal(void)
{
render();
}

void ConsoleViewSDL_t::render(void)
{
int nesWidth = GL_NES_WIDTH;
Expand Down Expand Up @@ -707,5 +727,8 @@ void ConsoleViewSDL_t::render(void)

videoBufferSwapMark();

// Schedule draw timing inline with vsync
drawTimer->start();

nes_shm->render_count++;
}
4 changes: 3 additions & 1 deletion src/drivers/Qt/ConsoleViewerSDL.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class ConsoleViewSDL_t : public QWidget, public ConsoleViewerBase
void reset(void);
void cleanup(void);
void render(void);
void queueRedraw(void){ render(); };
void queueRedraw(void);
int driver(void){ return VIDEO_DRIVER_SDL; };

void transfer2LocalBuffer(void);
Expand Down Expand Up @@ -83,6 +83,7 @@ class ConsoleViewSDL_t : public QWidget, public ConsoleViewerBase
bool forceAspect;
bool autoScaleEna;
QColor *bgColor;
QTimer *drawTimer;

uint32_t *localBuf;
uint32_t localBufSize;
Expand All @@ -95,5 +96,6 @@ class ConsoleViewSDL_t : public QWidget, public ConsoleViewerBase
//SDL_Rect sdlViewport;

private slots:
void onDrawSignal();
};

33 changes: 17 additions & 16 deletions src/drivers/Qt/ConsoleWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ consoleWin_t::consoleWin_t(QWidget *parent)
mainMenuEmuWasPaused = false;
mainMenuPauseWhenActv = false;
autoHideMenuFullscreen = false;
redrawVideoRequest = false;

createMainMenu();

Expand Down Expand Up @@ -412,7 +413,7 @@ void consoleWin_t::winScreenChanged(QScreen *scr)
return;
}
refreshRate = scr->refreshRate();
//printf("Screen Refresh Rate: %f\n", scr->refreshRate() );
printf("Screen Refresh Rate: %f\n", scr->refreshRate() );

//printf("Screen Changed: %p\n", scr );
if ( viewport_GL != NULL )
Expand Down Expand Up @@ -4555,9 +4556,8 @@ int consoleWin_t::getPeriodicInterval(void)
return gameTimer->interval();
}

void consoleWin_t::transferVideoBuffer(void)
void consoleWin_t::transferVideoBuffer(bool allowRedraw)
{
bool redraw = false;
FCEU_PROFILE_FUNC(prof, "VideoXfer");

{
Expand All @@ -4569,21 +4569,22 @@ void consoleWin_t::transferVideoBuffer(void)
if (viewport_Interface != nullptr)
{
viewport_Interface->transfer2LocalBuffer();
redraw = true;
redrawVideoRequest = true;
}
}
}

// Don't queue redraw in mutex lock scope
if (redraw && (viewport_Interface != nullptr))
if (allowRedraw && redrawVideoRequest && (viewport_Interface != nullptr))
{
viewport_Interface->queueRedraw();
redrawVideoRequest = false;
}
}

void consoleWin_t::emuFrameFinish(void)
{
static bool eventProcessingInProg = false;
//static bool eventProcessingInProg = false;

guiSignalRecvMark();

Expand All @@ -4592,24 +4593,24 @@ void consoleWin_t::emuFrameFinish(void)
// return;
//}
// Prevent recursion as processEvents function can double back on us
if ( !eventProcessingInProg )
{
eventProcessingInProg = true;
// Process all events before attempting to render viewport
QCoreApplication::processEvents();
eventProcessingInProg = false;
}
//if ( !eventProcessingInProg )
//{
// eventProcessingInProg = true;
// // Process all events before attempting to render viewport
// QCoreApplication::processEvents();
// eventProcessingInProg = false;
//}

// Update Input Devices
FCEUD_UpdateInput();
//FCEUD_UpdateInput();

//printf("EMU Frame Finish\n");

//#ifdef __FCEU_QSCRIPT_ENABLE__
// QtScriptManager::getInstance()->frameFinishedUpdate();
//#endif

transferVideoBuffer();
transferVideoBuffer(false);
}

void consoleWin_t::updatePeriodic(void)
Expand All @@ -4634,7 +4635,7 @@ void consoleWin_t::updatePeriodic(void)
FCEUD_UpdateInput();

// RePaint Game Viewport
transferVideoBuffer();
transferVideoBuffer(true);

// Low Rate Updates
if ( (updateCounter % 30) == 0 )
Expand Down
3 changes: 2 additions & 1 deletion src/drivers/Qt/ConsoleWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ class consoleWin_t : public QMainWindow
bool contextMenuEnable;
bool soundUseGlobalFocus;
bool autoHideMenuFullscreen;
bool redrawVideoRequest;

std::list <std::string*> romList;
std::vector <autoFireMenuAction*> afActList;
Expand Down Expand Up @@ -313,7 +314,7 @@ class consoleWin_t : public QMainWindow
void changeState(int slot);
void saveState(int slot);
void loadState(int slot);
void transferVideoBuffer(void);
void transferVideoBuffer(bool allowRedraw);
void syncAutoFirePatternMenu(void);

std::string findHelpFile(void);
Expand Down

0 comments on commit 0135840

Please sign in to comment.