Skip to content

Commit

Permalink
Use a separate thread to update the display (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
drfiemost committed Nov 10, 2024
1 parent 0bd4819 commit 3d6be8e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
22 changes: 17 additions & 5 deletions src/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ using std::endl;
#include <sidplayfp/SidInfo.h>
#include <sidplayfp/SidTuneInfo.h>


#include <chrono>
#include <unordered_map>

using filter_map_t = std::unordered_map<std::string, double>;
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -884,7 +896,8 @@ bool ConsolePlayer::open (void)

// Update display
menu();
updateDisplay();
m_thread = new std::thread(&ConsolePlayer::displayThread, this);

return true;
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -1064,7 +1077,6 @@ uint_least32_t ConsolePlayer::getBufSize()
}


// External Timer Event
void ConsolePlayer::updateDisplay()
{
#ifdef FEAT_NEW_SONLEGTH_DB
Expand Down
5 changes: 5 additions & 0 deletions src/player.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@

#include <string>
#include <bitset>
#include <thread>

#ifdef HAVE_TSID
# if HAVE_TSID > 1
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 3d6be8e

Please sign in to comment.