From 3d6be8e271fd44647efae7200e1d23ed89c09b3f Mon Sep 17 00:00:00 2001 From: Leandro Nini Date: Wed, 16 Oct 2024 20:32:21 +0200 Subject: [PATCH] Use a separate thread to update the display (#57) --- src/player.cpp | 22 +++++++++++++++++----- src/player.h | 5 +++++ 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/src/player.cpp b/src/player.cpp index 2963ba0..7cff6a3 100644 --- a/src/player.cpp +++ b/src/player.cpp @@ -52,7 +52,7 @@ using std::endl; #include #include - +#include #include using filter_map_t = std::unordered_map; @@ -774,6 +774,18 @@ bool ConsolePlayer::createSidEmu (SIDEMUS emu, const SidTuneInfo *tuneInfo) return false; } +void ConsolePlayer::displayThread() +{ + using namespace std::chrono_literals; + + while (m_state == playerRunning) + { + updateDisplay(); + // TODO 16ms for NTSC? + std::this_thread::sleep_for(20ms); + } +} + bool ConsolePlayer::open (void) { @@ -884,7 +896,8 @@ bool ConsolePlayer::open (void) // Update display menu(); - updateDisplay(); + m_thread = new std::thread(&ConsolePlayer::displayThread, this); + return true; } @@ -917,6 +930,8 @@ void ConsolePlayer::close () cerr << endl; #endif } + m_thread->join(); + delete m_thread; } // Flush any hardware sid fifos so all music is played @@ -946,8 +961,6 @@ bool ConsolePlayer::play() uint_least32_t frames = 0; if (m_state == playerRunning) { - updateDisplay(); - // Fill buffer short *buffer = m_driver.selected->buffer(); // getBufSize returns the number of frames @@ -1064,7 +1077,6 @@ uint_least32_t ConsolePlayer::getBufSize() } -// External Timer Event void ConsolePlayer::updateDisplay() { #ifdef FEAT_NEW_SONLEGTH_DB diff --git a/src/player.h b/src/player.h index 399d06f..17563ab 100644 --- a/src/player.h +++ b/src/player.h @@ -41,6 +41,7 @@ #include #include +#include #ifdef HAVE_TSID # if HAVE_TSID > 1 @@ -184,6 +185,8 @@ class ConsolePlayer int m_precision; int m_buffer_size; + std::thread *m_thread; + struct m_filter_t { // Filter parameter for reSID @@ -261,6 +264,8 @@ class ConsolePlayer inline bool tryOpenTune(const char *hvscBase); inline bool tryOpenDatabase(const char *hvscBase, const char *suffix); + void displayThread(); + public: ConsolePlayer (const char * const name); virtual ~ConsolePlayer() = default;