Skip to content

Commit

Permalink
platform: sdl: handle emulator reset and direct boot via the message …
Browse files Browse the repository at this point in the history
…queue
  • Loading branch information
fleroviux committed Jan 4, 2024
1 parent a5d8cb2 commit 8af72b3
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 17 deletions.
22 changes: 5 additions & 17 deletions src/platform/sdl/src/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
}

Expand Down Expand Up @@ -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) {
Expand All @@ -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;
}
}
}
Expand Down
16 changes: 16 additions & 0 deletions src/platform/sdl/src/emulator_thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ std::unique_ptr<dual::nds::NDS> 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,
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 4 additions & 0 deletions src/platform/sdl/src/emulator_thread.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ class EmulatorThread {
void Start(std::unique_ptr<dual::nds::NDS> nds);
std::unique_ptr<dual::nds::NDS> Stop();

void Reset();
void DirectBoot();
void SetKeyState(dual::nds::Key key, bool pressed);
void SetTouchState(bool pen_down, u8 x, u8 y);

Expand All @@ -31,6 +33,8 @@ class EmulatorThread {

private:
enum class MessageType : u8 {
Reset,
DirectBoot,
SetKeyState,
SetTouchState
};
Expand Down

0 comments on commit 8af72b3

Please sign in to comment.