more cleanup

This commit is contained in:
Arisotura 2024-06-14 21:45:21 +02:00
parent 438c989716
commit 0cac63811a
2 changed files with 38 additions and 40 deletions

View File

@ -63,9 +63,8 @@ EmuThread::EmuThread(EmuInstance* inst, QObject* parent) : QThread(parent)
{ {
emuInstance = inst; emuInstance = inst;
EmuStatus = emuStatus_Exit; emuStatus = emuStatus_Paused;
EmuRunning = emuStatus_Paused; emuPauseStack = emuPauseStackRunning;
EmuPauseStack = EmuPauseStackRunning;
emuActive = false; emuActive = false;
} }
@ -152,7 +151,7 @@ void EmuThread::run()
char melontitle[100]; char melontitle[100];
while (EmuRunning != emuStatus_Exit) while (emuStatus != emuStatus_Exit)
{ {
emuInstance->inputProcess(); emuInstance->inputProcess();
@ -167,10 +166,9 @@ void EmuThread::run()
if (emuInstance->hotkeyPressed(HK_SwapScreens)) emit swapScreensToggle(); if (emuInstance->hotkeyPressed(HK_SwapScreens)) emit swapScreensToggle();
if (emuInstance->hotkeyPressed(HK_SwapScreenEmphasis)) emit screenEmphasisToggle(); if (emuInstance->hotkeyPressed(HK_SwapScreenEmphasis)) emit screenEmphasisToggle();
if (EmuRunning == emuStatus_Running || EmuRunning == emuStatus_FrameStep) if (emuStatus == emuStatus_Running || emuStatus == emuStatus_FrameStep)
{ {
EmuStatus = emuStatus_Running; if (emuStatus == emuStatus_FrameStep) emuStatus = emuStatus_Paused;
if (EmuRunning == emuStatus_FrameStep) EmuRunning = emuStatus_Paused;
if (emuInstance->hotkeyPressed(HK_SolarSensorDecrease)) if (emuInstance->hotkeyPressed(HK_SolarSensorDecrease))
{ {
@ -329,8 +327,6 @@ void EmuThread::run()
MelonCap::Update(); MelonCap::Update();
#endif // MELONCAP #endif // MELONCAP
if (EmuRunning == emuStatus_Exit) break;
winUpdateCount++; winUpdateCount++;
if (winUpdateCount >= winUpdateFreq && !useOpenGL) if (winUpdateCount >= winUpdateFreq && !useOpenGL)
{ {
@ -428,8 +424,6 @@ void EmuThread::run()
emit windowUpdate(); emit windowUpdate();
EmuStatus = EmuRunning;
int inst = emuInstance->instanceID; int inst = emuInstance->instanceID;
if (inst == 0) if (inst == 0)
sprintf(melontitle, "melonDS " MELONDS_VERSION); sprintf(melontitle, "melonDS " MELONDS_VERSION);
@ -457,8 +451,6 @@ void EmuThread::run()
Platform::CloseFile(file); Platform::CloseFile(file);
} }
EmuStatus = emuStatus_Exit;
NDS::Current = nullptr; NDS::Current = nullptr;
} }
@ -489,9 +481,16 @@ void EmuThread::handleMessages()
Message msg = msgQueue.dequeue(); Message msg = msgQueue.dequeue();
switch (msg.type) switch (msg.type)
{ {
case msg_Exit:
emuStatus = emuStatus_Exit;
emuPauseStack = emuPauseStackRunning;
emuInstance->audioDisable();
break;
case msg_EmuRun: case msg_EmuRun:
EmuRunning = emuStatus_Running; emuStatus = emuStatus_Running;
EmuPauseStack = EmuPauseStackRunning; emuPauseStack = emuPauseStackRunning;
emuActive = true; emuActive = true;
emuInstance->audioEnable(); emuInstance->audioEnable();
@ -499,13 +498,13 @@ void EmuThread::handleMessages()
break; break;
case msg_EmuPause: case msg_EmuPause:
EmuPauseStack++; emuPauseStack++;
if (EmuPauseStack > EmuPauseStackPauseThreshold) break; if (emuPauseStack > emuPauseStackPauseThreshold) break;
PrevEmuStatus = EmuRunning; prevEmuStatus = emuStatus;
EmuRunning = emuStatus_Paused; emuStatus = emuStatus_Paused;
if (PrevEmuStatus != emuStatus_Paused) if (prevEmuStatus != emuStatus_Paused)
{ {
emuInstance->audioDisable(); emuInstance->audioDisable();
emit windowEmuPause(true); emit windowEmuPause(true);
@ -514,14 +513,14 @@ void EmuThread::handleMessages()
break; break;
case msg_EmuUnpause: case msg_EmuUnpause:
if (EmuPauseStack < EmuPauseStackPauseThreshold) break; if (emuPauseStack < emuPauseStackPauseThreshold) break;
EmuPauseStack--; emuPauseStack--;
if (EmuPauseStack >= EmuPauseStackPauseThreshold) break; if (emuPauseStack >= emuPauseStackPauseThreshold) break;
EmuRunning = PrevEmuStatus; emuStatus = prevEmuStatus;
if (EmuRunning != emuStatus_Paused) if (emuStatus != emuStatus_Paused)
{ {
emuInstance->audioEnable(); emuInstance->audioEnable();
emit windowEmuPause(false); emit windowEmuPause(false);
@ -531,7 +530,7 @@ void EmuThread::handleMessages()
case msg_EmuStop: case msg_EmuStop:
if (msg.stopExternal) emuInstance->nds->Stop(); if (msg.stopExternal) emuInstance->nds->Stop();
EmuRunning = emuStatus_Paused; emuStatus = emuStatus_Paused;
emuActive = false; emuActive = false;
emuInstance->audioDisable(); emuInstance->audioDisable();
@ -591,7 +590,7 @@ void EmuThread::emuUnpause()
void EmuThread::emuTogglePause() void EmuThread::emuTogglePause()
{ {
if (EmuRunning == emuStatus_Paused) if (emuStatus == emuStatus_Paused)
emuUnpause(); emuUnpause();
else else
emuPause(); emuPause();
@ -605,21 +604,19 @@ void EmuThread::emuStop(bool external)
void EmuThread::emuExit() void EmuThread::emuExit()
{ {
EmuRunning = emuStatus_Exit; sendMessage(msg_Exit);
EmuPauseStack = EmuPauseStackRunning; waitAllMessages();
emuInstance->audioDisable();
} }
void EmuThread::emuFrameStep() void EmuThread::emuFrameStep()
{ {
//if (EmuPauseStack < EmuPauseStackPauseThreshold) emit windowEmuPause(); //if (emuPauseStack < emuPauseStackPauseThreshold) emit windowEmuPause();
EmuRunning = emuStatus_FrameStep; emuStatus = emuStatus_FrameStep;
} }
bool EmuThread::emuIsRunning() bool EmuThread::emuIsRunning()
{ {
return EmuRunning == emuStatus_Running; return emuStatus == emuStatus_Running;
} }
bool EmuThread::emuIsActive() bool EmuThread::emuIsActive()

View File

@ -57,6 +57,8 @@ public:
enum MessageType enum MessageType
{ {
msg_Exit,
msg_EmuRun, msg_EmuRun,
msg_EmuPause, msg_EmuPause,
msg_EmuUnpause, msg_EmuUnpause,
@ -139,15 +141,14 @@ private:
emuStatus_Paused, emuStatus_Paused,
emuStatus_FrameStep, emuStatus_FrameStep,
}; };
std::atomic<EmuStatusKind> EmuStatus;
EmuStatusKind PrevEmuStatus; EmuStatusKind prevEmuStatus;
EmuStatusKind EmuRunning; EmuStatusKind emuStatus;
bool emuActive; bool emuActive;
constexpr static int EmuPauseStackRunning = 0; constexpr static int emuPauseStackRunning = 0;
constexpr static int EmuPauseStackPauseThreshold = 1; constexpr static int emuPauseStackPauseThreshold = 1;
int EmuPauseStack; int emuPauseStack;
QMutex msgMutex; QMutex msgMutex;
QSemaphore msgSemaphore; QSemaphore msgSemaphore;