Refactor so parallel-rdp is in charge of polling inputs and not the EmuThread
This commit is contained in:
@@ -1,102 +1,53 @@
|
||||
#include <Core.hpp>
|
||||
#include <EmuThread.hpp>
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
EmuThread::EmuThread(RenderWidget &renderWidget, SettingsWindow &settings) noexcept :
|
||||
renderWidget(renderWidget), core(parallel), settings(settings) {}
|
||||
EmuThread::EmuThread(const std::shared_ptr<n64::Core> &core, RenderWidget &renderWidget,
|
||||
SettingsWindow &settings) noexcept : renderWidget(renderWidget), core(core), settings(settings) {}
|
||||
|
||||
[[noreturn]] void EmuThread::run() noexcept {
|
||||
parallel.Init(renderWidget.qtVkInstanceFactory, renderWidget.wsiPlatform, renderWidget.windowInfo,
|
||||
core.cpu->GetMem().GetRDRAMPtr());
|
||||
core->parallel.Init(renderWidget.qtVkInstanceFactory, renderWidget.wsiPlatform, renderWidget.windowInfo,
|
||||
core->cpu->GetMem().GetRDRAMPtr());
|
||||
|
||||
SDL_InitSubSystem(SDL_INIT_GAMEPAD);
|
||||
bool controllerConnected = false;
|
||||
|
||||
if (SDL_AddGamepadMappingsFromFile("resources/gamecontrollerdb.txt") < 0) {
|
||||
Util::warn("[SDL] Could not load game controller DB");
|
||||
}
|
||||
|
||||
auto pollEvents = [&] {
|
||||
SDL_Event e;
|
||||
while (SDL_PollEvent(&e)) {
|
||||
switch (e.type) {
|
||||
case SDL_EVENT_GAMEPAD_ADDED:
|
||||
{
|
||||
const int index = e.gdevice.which;
|
||||
controller = SDL_OpenGamepad(index);
|
||||
Util::info("Found controller!");
|
||||
auto serial = SDL_GetGamepadSerial(controller);
|
||||
auto name = SDL_GetGamepadName(controller);
|
||||
auto path = SDL_GetGamepadPath(controller);
|
||||
Util::info("\tName: {}", name ? name : "Not available");
|
||||
Util::info("\tSerial: {}", serial ? serial : "Not available");
|
||||
Util::info("\tPath: {}", path ? path : "Not available");
|
||||
controllerConnected = true;
|
||||
}
|
||||
break;
|
||||
case SDL_EVENT_GAMEPAD_REMOVED:
|
||||
{
|
||||
controllerConnected = false;
|
||||
SDL_CloseGamepad(controller);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
while (!isInterruptionRequested()) {
|
||||
if (!core.pause) {
|
||||
core.Run(settings.getVolumeL(), settings.getVolumeR());
|
||||
if (!core->pause) {
|
||||
core->Run(settings.getVolumeL(), settings.getVolumeR());
|
||||
}
|
||||
|
||||
if (core.render) {
|
||||
parallel.UpdateScreen(core.cpu->GetMem().mmio.vi);
|
||||
}
|
||||
|
||||
pollEvents();
|
||||
|
||||
if (controllerConnected) {
|
||||
n64::PIF &pif = core.cpu->GetMem().mmio.si.pif;
|
||||
pif.UpdateButton(0, n64::Controller::Key::A, SDL_GetGamepadButton(controller, SDL_GAMEPAD_BUTTON_SOUTH));
|
||||
pif.UpdateButton(0, n64::Controller::Key::B, SDL_GetGamepadButton(controller, SDL_GAMEPAD_BUTTON_WEST));
|
||||
pif.UpdateButton(0, n64::Controller::Key::Z,
|
||||
SDL_GetGamepadAxis(controller, SDL_GAMEPAD_AXIS_LEFT_TRIGGER) == SDL_JOYSTICK_AXIS_MAX);
|
||||
pif.UpdateButton(0, n64::Controller::Key::Start, SDL_GetGamepadButton(controller, SDL_GAMEPAD_BUTTON_START));
|
||||
pif.UpdateButton(0, n64::Controller::Key::DUp, SDL_GetGamepadButton(controller, SDL_GAMEPAD_BUTTON_DPAD_UP));
|
||||
pif.UpdateButton(0, n64::Controller::Key::DDown, SDL_GetGamepadButton(controller, SDL_GAMEPAD_BUTTON_DPAD_DOWN));
|
||||
pif.UpdateButton(0, n64::Controller::Key::DLeft, SDL_GetGamepadButton(controller, SDL_GAMEPAD_BUTTON_DPAD_LEFT));
|
||||
pif.UpdateButton(0, n64::Controller::Key::DRight,
|
||||
SDL_GetGamepadButton(controller, SDL_GAMEPAD_BUTTON_DPAD_RIGHT));
|
||||
pif.UpdateButton(0, n64::Controller::Key::LT, SDL_GetGamepadButton(controller, SDL_GAMEPAD_BUTTON_LEFT_SHOULDER));
|
||||
pif.UpdateButton(0, n64::Controller::Key::RT,
|
||||
SDL_GetGamepadButton(controller, SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER));
|
||||
pif.UpdateButton(0, n64::Controller::Key::CUp, SDL_GetGamepadAxis(controller, SDL_GAMEPAD_AXIS_RIGHTY) <= -127);
|
||||
pif.UpdateButton(0, n64::Controller::Key::CDown, SDL_GetGamepadAxis(controller, SDL_GAMEPAD_AXIS_RIGHTY) >= 127);
|
||||
pif.UpdateButton(0, n64::Controller::Key::CLeft, SDL_GetGamepadAxis(controller, SDL_GAMEPAD_AXIS_RIGHTX) <= -127);
|
||||
pif.UpdateButton(0, n64::Controller::Key::CRight, SDL_GetGamepadAxis(controller, SDL_GAMEPAD_AXIS_RIGHTX) >= 127);
|
||||
|
||||
float xclamped = SDL_GetGamepadAxis(controller, SDL_GAMEPAD_AXIS_LEFTX);
|
||||
if (xclamped < 0) {
|
||||
xclamped /= float(std::abs(SDL_JOYSTICK_AXIS_MAX));
|
||||
} else {
|
||||
xclamped /= SDL_JOYSTICK_AXIS_MAX;
|
||||
}
|
||||
|
||||
xclamped *= 86;
|
||||
|
||||
float yclamped = SDL_GetGamepadAxis(controller, SDL_GAMEPAD_AXIS_LEFTY);
|
||||
if (yclamped < 0) {
|
||||
yclamped /= float(std::abs(SDL_JOYSTICK_AXIS_MIN));
|
||||
} else {
|
||||
yclamped /= SDL_JOYSTICK_AXIS_MAX;
|
||||
}
|
||||
|
||||
yclamped *= 86;
|
||||
|
||||
pif.UpdateAxis(0, n64::Controller::Axis::Y, -yclamped);
|
||||
pif.UpdateAxis(0, n64::Controller::Axis::X, xclamped);
|
||||
if (core->render) {
|
||||
core->parallel.UpdateScreen(core->cpu->GetMem().mmio.vi);
|
||||
}
|
||||
}
|
||||
|
||||
SetRender(false);
|
||||
Stop();
|
||||
}
|
||||
|
||||
void EmuThread::TogglePause() const noexcept {
|
||||
core->TogglePause();
|
||||
Util::RPC::GetInstance().Update(core->pause ? Util::RPC::Paused : Util::RPC::GetInstance().GetState(),
|
||||
core->cpu->GetMem().rom.gameNameDB,
|
||||
core->cpu->GetMem().mmio.si.pif.movie.GetFilename());
|
||||
}
|
||||
|
||||
void EmuThread::SetRender(bool v) const noexcept { core->render = v; }
|
||||
|
||||
void EmuThread::Reset() const noexcept {
|
||||
core->pause = true;
|
||||
core->Stop();
|
||||
core->LoadROM(core->rom);
|
||||
core->pause = false;
|
||||
}
|
||||
|
||||
void EmuThread::Stop() const noexcept {
|
||||
Util::RPC::GetInstance().Update(Util::RPC::Idling);
|
||||
core->rom = {};
|
||||
core->pause = true;
|
||||
core->Stop();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user