Preliminary controller support
This commit is contained in:
@@ -4,10 +4,39 @@
|
||||
#include "Audio.hpp"
|
||||
|
||||
EmuThread::EmuThread(std::unique_ptr<QtInstanceFactory>&& instance_, std::unique_ptr<Vulkan::WSIPlatform>&& wsiPlatform_, std::unique_ptr<ParallelRDP::WindowInfo>&& windowInfo_, SettingsWindow& settings) noexcept
|
||||
: instance(std::move(instance_)), wsiPlatform(std::move(wsiPlatform_)), windowInfo(std::move(windowInfo_)), core(parallel), settings(settings) {}
|
||||
: instance(std::move(instance_)), wsiPlatform(std::move(wsiPlatform_)),
|
||||
windowInfo(std::move(windowInfo_)),
|
||||
controller(SDL_GameControllerClose),
|
||||
core(parallel), settings(settings) {}
|
||||
|
||||
[[noreturn]] void EmuThread::run() noexcept {
|
||||
parallel.Init(instance.get(), std::move(wsiPlatform), std::move(windowInfo), core.cpu->GetMem().GetRDRAMPtr());
|
||||
SDL_InitSubSystem(SDL_INIT_GAMECONTROLLER);
|
||||
bool controllerConnected = false;
|
||||
|
||||
auto pollEvents = [&]() {
|
||||
SDL_Event e;
|
||||
while(SDL_PollEvent(&e)) {
|
||||
switch(e.type) {
|
||||
case SDL_CONTROLLERDEVICEADDED: {
|
||||
const int index = e.cdevice.which;
|
||||
controller.Construct(SDL_GameControllerOpen, index);
|
||||
Util::info("Found controller!");
|
||||
auto serial = SDL_GameControllerGetSerial(controller.get());
|
||||
auto name = SDL_GameControllerName(controller.get());
|
||||
auto path = SDL_GameControllerPath(controller.get());
|
||||
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_CONTROLLERDEVICEREMOVED: {
|
||||
controllerConnected = false;
|
||||
controller.Destroy();
|
||||
} break;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
while (true) {
|
||||
if (!core.pause) {
|
||||
@@ -20,5 +49,46 @@ EmuThread::EmuThread(std::unique_ptr<QtInstanceFactory>&& instance_, std::unique
|
||||
parallel.UpdateScreen(core.cpu->GetMem().mmio.vi, true);
|
||||
}
|
||||
}
|
||||
|
||||
pollEvents();
|
||||
|
||||
if(controllerConnected) {
|
||||
n64::PIF& pif = core.cpu->GetMem().mmio.si.pif;
|
||||
pif.UpdateButton(0, n64::Controller::Key::A, SDL_GameControllerGetButton(controller.get(), SDL_CONTROLLER_BUTTON_A));
|
||||
pif.UpdateButton(0, n64::Controller::Key::B, SDL_GameControllerGetButton(controller.get(), SDL_CONTROLLER_BUTTON_X));
|
||||
pif.UpdateButton(0, n64::Controller::Key::Z, SDL_GameControllerGetAxis(controller.get(), SDL_CONTROLLER_AXIS_TRIGGERLEFT) == SDL_JOYSTICK_AXIS_MAX);
|
||||
pif.UpdateButton(0, n64::Controller::Key::Start, SDL_GameControllerGetButton(controller.get(), SDL_CONTROLLER_BUTTON_START));
|
||||
pif.UpdateButton(0, n64::Controller::Key::DUp, SDL_GameControllerGetButton(controller.get(), SDL_CONTROLLER_BUTTON_DPAD_UP));
|
||||
pif.UpdateButton(0, n64::Controller::Key::DDown, SDL_GameControllerGetButton(controller.get(), SDL_CONTROLLER_BUTTON_DPAD_DOWN));
|
||||
pif.UpdateButton(0, n64::Controller::Key::DLeft, SDL_GameControllerGetButton(controller.get(), SDL_CONTROLLER_BUTTON_DPAD_LEFT));
|
||||
pif.UpdateButton(0, n64::Controller::Key::DRight, SDL_GameControllerGetButton(controller.get(), SDL_CONTROLLER_BUTTON_DPAD_RIGHT));
|
||||
pif.UpdateButton(0, n64::Controller::Key::LT, SDL_GameControllerGetButton(controller.get(), SDL_CONTROLLER_BUTTON_LEFTSHOULDER));
|
||||
pif.UpdateButton(0, n64::Controller::Key::RT, SDL_GameControllerGetButton(controller.get(), SDL_CONTROLLER_BUTTON_RIGHTSHOULDER));
|
||||
pif.UpdateButton(0, n64::Controller::Key::CUp, SDL_GameControllerGetAxis(controller.get(), SDL_CONTROLLER_AXIS_RIGHTY) <= -127);
|
||||
pif.UpdateButton(0, n64::Controller::Key::CDown, SDL_GameControllerGetAxis(controller.get(), SDL_CONTROLLER_AXIS_RIGHTY) >= 127);
|
||||
pif.UpdateButton(0, n64::Controller::Key::CLeft, SDL_GameControllerGetAxis(controller.get(), SDL_CONTROLLER_AXIS_RIGHTX) <= -127);
|
||||
pif.UpdateButton(0, n64::Controller::Key::CRight, SDL_GameControllerGetAxis(controller.get(), SDL_CONTROLLER_AXIS_RIGHTX) >= 127);
|
||||
|
||||
float xclamped = SDL_GameControllerGetAxis(controller.get(), SDL_CONTROLLER_AXIS_LEFTX);
|
||||
if(xclamped < 0) {
|
||||
xclamped /= float(std::abs(SDL_JOYSTICK_AXIS_MIN));
|
||||
} else {
|
||||
xclamped /= SDL_JOYSTICK_AXIS_MAX;
|
||||
}
|
||||
|
||||
xclamped *= 86;
|
||||
|
||||
float yclamped = SDL_GameControllerGetAxis(controller.get(), SDL_CONTROLLER_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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@
|
||||
#include <Core.hpp>
|
||||
#include <SettingsWindow.hpp>
|
||||
#include <memory>
|
||||
#include <SDL2/SDL.h>
|
||||
|
||||
class EmuThread : public QThread
|
||||
{
|
||||
@@ -16,6 +17,7 @@ public:
|
||||
|
||||
[[noreturn]] void run() noexcept override;
|
||||
|
||||
Util::AutoRelease<SDL_GameController, int> controller;
|
||||
ParallelRDP parallel;
|
||||
n64::Core core;
|
||||
SettingsWindow& settings;
|
||||
|
||||
@@ -62,8 +62,8 @@ InputSettings::InputSettings(nlohmann::json& settings) : settings(settings), QWi
|
||||
|
||||
for (int i = 0; i < 18; i++) {
|
||||
connect(kb_buttons[i], &QPushButton::pressed, this, [&, i]() {
|
||||
for (int i = 0; i < 18; i++) {
|
||||
kb_buttons[i]->setEnabled(false);
|
||||
for (auto kb_button : kb_buttons) {
|
||||
kb_button->setEnabled(false);
|
||||
}
|
||||
grabKeyboard();
|
||||
grabbing = true;
|
||||
@@ -71,16 +71,16 @@ InputSettings::InputSettings(nlohmann::json& settings) : settings(settings), QWi
|
||||
});
|
||||
}
|
||||
|
||||
QHBoxLayout* AB = new QHBoxLayout;
|
||||
QHBoxLayout* ZStart = new QHBoxLayout;
|
||||
QHBoxLayout* LR = new QHBoxLayout;
|
||||
QHBoxLayout* DupDdown = new QHBoxLayout;
|
||||
QHBoxLayout* DleftDright = new QHBoxLayout;
|
||||
QHBoxLayout* CupCdown = new QHBoxLayout;
|
||||
QHBoxLayout* CleftCright = new QHBoxLayout;
|
||||
QHBoxLayout* AupAdown = new QHBoxLayout;
|
||||
QHBoxLayout* AleftAright = new QHBoxLayout;
|
||||
QVBoxLayout* mainLayout = new QVBoxLayout;
|
||||
auto AB = new QHBoxLayout;
|
||||
auto ZStart = new QHBoxLayout;
|
||||
auto LR = new QHBoxLayout;
|
||||
auto DupDdown = new QHBoxLayout;
|
||||
auto DleftDright = new QHBoxLayout;
|
||||
auto CupCdown = new QHBoxLayout;
|
||||
auto CleftCright = new QHBoxLayout;
|
||||
auto AupAdown = new QHBoxLayout;
|
||||
auto AleftAright = new QHBoxLayout;
|
||||
auto mainLayout = new QVBoxLayout;
|
||||
|
||||
AB->addWidget(n64_button_labels[0]);
|
||||
AB->addWidget(kb_buttons[0]);
|
||||
@@ -139,8 +139,8 @@ void InputSettings::keyPressEvent(QKeyEvent* e) {
|
||||
kb_buttons[which_grabbing]->setText(k);
|
||||
grabbing = false;
|
||||
which_grabbing = -1;
|
||||
for (int i = 0; i < 18; i++) {
|
||||
kb_buttons[i]->setEnabled(true);
|
||||
for (auto kb_button : kb_buttons) {
|
||||
kb_button->setEnabled(true);
|
||||
}
|
||||
releaseKeyboard();
|
||||
emit modified();
|
||||
|
||||
Reference in New Issue
Block a user