From 8af72b3576682e0f4a25d18f75594ef120c0da03 Mon Sep 17 00:00:00 2001 From: fleroviux Date: Thu, 4 Jan 2024 23:41:24 +0100 Subject: [PATCH] platform: sdl: handle emulator reset and direct boot via the message queue --- src/platform/sdl/src/application.cpp | 22 +++++----------------- src/platform/sdl/src/emulator_thread.cpp | 16 ++++++++++++++++ src/platform/sdl/src/emulator_thread.hpp | 4 ++++ 3 files changed, 25 insertions(+), 17 deletions(-) diff --git a/src/platform/sdl/src/application.cpp b/src/platform/sdl/src/application.cpp index 3b4d550..a5ee486 100644 --- a/src/platform/sdl/src/application.cpp +++ b/src/platform/sdl/src/application.cpp @@ -160,22 +160,6 @@ void Application::MainLoop() { UpdateFPS(); } - - // @todo: move this logic into HandleEvent() - - m_emu_thread.SetFastForward(SDL_GetKeyboardState(nullptr)[SDL_SCANCODE_SPACE]); - - if(SDL_GetKeyboardState(nullptr)[SDL_SCANCODE_F11]) { - m_nds = m_emu_thread.Stop(); - m_nds->Reset(); - m_emu_thread.Start(std::move(m_nds)); - } - - if(SDL_GetKeyboardState(nullptr)[SDL_SCANCODE_F12]) { - m_nds = m_emu_thread.Stop(); - m_nds->DirectBoot(); - m_emu_thread.Start(std::move(m_nds)); - } } } @@ -231,9 +215,10 @@ void Application::HandleEvent(const SDL_Event& event) { if(type == SDL_KEYUP || type == SDL_KEYDOWN) { const SDL_KeyboardEvent& keyboard_event = (const SDL_KeyboardEvent&)event; + const bool pressed = type == SDL_KEYDOWN; const auto update_key = [&](dual::nds::Key key) { - m_emu_thread.SetKeyState(key, type == SDL_KEYDOWN); + m_emu_thread.SetKeyState(key, pressed); }; switch(keyboard_event.keysym.sym) { @@ -249,6 +234,9 @@ void Application::HandleEvent(const SDL_Event& event) { case SDLK_DOWN: update_key(dual::nds::Key::Down); break; case SDLK_LEFT: update_key(dual::nds::Key::Left); break; case SDLK_RIGHT: update_key(dual::nds::Key::Right); break; + case SDLK_F11: if(!pressed) m_emu_thread.Reset(); break; + case SDLK_F12: if(!pressed) m_emu_thread.DirectBoot(); break; + case SDLK_SPACE: m_emu_thread.SetFastForward(pressed); break; } } } diff --git a/src/platform/sdl/src/emulator_thread.cpp b/src/platform/sdl/src/emulator_thread.cpp index 76786da..a729c8f 100644 --- a/src/platform/sdl/src/emulator_thread.cpp +++ b/src/platform/sdl/src/emulator_thread.cpp @@ -33,6 +33,14 @@ std::unique_ptr EmulatorThread::Stop() { return std::move(m_nds); } +void EmulatorThread::Reset() { + PushMessage({.type = MessageType::Reset}); +} + +void EmulatorThread::DirectBoot() { + PushMessage({.type = MessageType::DirectBoot}); +} + void EmulatorThread::SetKeyState(dual::nds::Key key, bool pressed) { PushMessage({ .type = MessageType::SetKeyState, @@ -73,6 +81,14 @@ void EmulatorThread::ProcessMessages() { const Message& message = m_msg_queue.front(); switch(message.type) { + case MessageType::Reset: { + m_nds->Reset(); + break; + } + case MessageType::DirectBoot: { + m_nds->DirectBoot(); + break; + } case MessageType::SetKeyState: { m_nds->SetKeyState(message.set_key_state.key, message.set_key_state.pressed); break; diff --git a/src/platform/sdl/src/emulator_thread.hpp b/src/platform/sdl/src/emulator_thread.hpp index 19859c3..d465a47 100644 --- a/src/platform/sdl/src/emulator_thread.hpp +++ b/src/platform/sdl/src/emulator_thread.hpp @@ -20,6 +20,8 @@ class EmulatorThread { void Start(std::unique_ptr nds); std::unique_ptr Stop(); + void Reset(); + void DirectBoot(); void SetKeyState(dual::nds::Key key, bool pressed); void SetTouchState(bool pen_down, u8 x, u8 y); @@ -31,6 +33,8 @@ class EmulatorThread { private: enum class MessageType : u8 { + Reset, + DirectBoot, SetKeyState, SetTouchState };