From 4fa0d0651a22fa943455420dd69923b0d1897f6f Mon Sep 17 00:00:00 2001 From: harry Date: Wed, 31 Jan 2024 07:08:08 -0500 Subject: [PATCH] Added vsync timer logic to Qt OpenGL video driver. --- src/drivers/Qt/ConsoleViewerGL.cpp | 24 ++++++++++++++++++++++++ src/drivers/Qt/ConsoleViewerGL.h | 4 +++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/drivers/Qt/ConsoleViewerGL.cpp b/src/drivers/Qt/ConsoleViewerGL.cpp index b6210272..3f203519 100644 --- a/src/drivers/Qt/ConsoleViewerGL.cpp +++ b/src/drivers/Qt/ConsoleViewerGL.cpp @@ -86,6 +86,12 @@ ConsoleViewGL_t::ConsoleViewGL_t(QWidget *parent) setFocusPolicy(Qt::StrongFocus); //setAttribute(Qt::WA_OpaquePaintEvent); + drawTimer = new QTimer(this); + drawTimer->setInterval(14); + drawTimer->setSingleShot(true); + drawTimer->setTimerType(Qt::PreciseTimer); + connect(drawTimer, &QTimer::timeout, this, &ConsoleViewGL_t::onDrawSignal); + localBufSize = (4 * GL_NES_WIDTH) * (4 * GL_NES_HEIGHT) * sizeof(uint32_t); localBuf = (uint32_t*)malloc( localBufSize ); @@ -137,6 +143,8 @@ ConsoleViewGL_t::ConsoleViewGL_t(QWidget *parent) ConsoleViewGL_t::~ConsoleViewGL_t(void) { //printf("Destroying GL Viewport\n"); + drawTimer->stop(); + delete drawTimer; if ( localBuf ) { @@ -566,6 +574,22 @@ void ConsoleViewGL_t::getNormalizedCursorPos( double &x, double &y ) void ConsoleViewGL_t::renderFinished(void) { videoBufferSwapMark(); + + // Schedule draw timing inline with vsync + drawTimer->start(); +} + +void ConsoleViewGL_t::queueRedraw(void) +{ + if (!drawTimer->isActive()) + { + update(); + } +} + +void ConsoleViewGL_t::onDrawSignal(void) +{ + update(); } void ConsoleViewGL_t::paintGL(void) diff --git a/src/drivers/Qt/ConsoleViewerGL.h b/src/drivers/Qt/ConsoleViewerGL.h index 12768ff7..22d92ee9 100644 --- a/src/drivers/Qt/ConsoleViewerGL.h +++ b/src/drivers/Qt/ConsoleViewerGL.h @@ -22,7 +22,7 @@ class ConsoleViewGL_t : public QOpenGLWidget, protected QOpenGLFunctions, public int init(void); void reset(void); - void queueRedraw(void){ update(); }; + void queueRedraw(void); int driver(void){ return VIDEO_DRIVER_OPENGL; }; void transfer2LocalBuffer(void); @@ -90,6 +90,7 @@ class ConsoleViewGL_t : public QOpenGLWidget, protected QOpenGLFunctions, public unsigned int textureType; unsigned int mouseButtonMask; QColor *bgColor; + QTimer *drawTimer; uint32_t *localBuf; uint32_t localBufSize; @@ -97,5 +98,6 @@ class ConsoleViewGL_t : public QOpenGLWidget, protected QOpenGLFunctions, public private slots: void cleanupGL(void); void renderFinished(void); + void onDrawSignal(); };