first work on remappable inputs

This commit is contained in:
2026-05-08 14:27:37 +02:00
parent 609fa2fb08
commit ba39535acf
26 changed files with 1224 additions and 1038 deletions
+3 -2
View File
@@ -171,11 +171,12 @@ add_executable(kaizen
src/frontend/Settings/CPUSettings.cpp
src/frontend/Settings/AudioSettings.hpp
src/frontend/Settings/AudioSettings.cpp
src/frontend/NativeWindow.hpp
src/frontend/Window.hpp
src/utils/Options.cpp
src/utils/File.cpp
src/frontend/Debugger.hpp
src/frontend/Debugger.cpp)
src/frontend/Debugger.cpp
src/frontend/Gamepad.hpp)
if (WIN32)
+1
View File
@@ -13,6 +13,7 @@
//-----------------------------------------------------------------------------
#pragma once
#define IMGUI_IMPL_VULKAN_USE_VOLK
//---- Define assertion handler. Defaults to calling assert().
// - If your macro uses multiple statements, make sure is enclosed in a 'do { .. } while (0)' block so it can be used as a single statement.
+46 -1
View File
@@ -129,6 +129,50 @@ struct Controller {
return *this;
}
static const char *as_c_str(Axis a) {
switch (a) {
case X:
return "X-Axis";
case Y:
return "Y-Axis";
break;
}
}
static const char *as_c_str(Key k) {
switch (k) {
case A:
return "A";
case B:
return "B";
case Z:
return "Z";
case Start:
return "Start";
case DUp:
return "Up";
case DDown:
return "Down";
case DLeft:
return "Left";
case DRight:
return "Right";
case CUp:
return "C-Up";
case CDown:
return "C-Down";
case CLeft:
return "C-Left";
case CRight:
return "C-Right";
case LT:
return "Left Trigger";
case RT:
return "Right Trigger";
break;
}
}
};
static_assert(sizeof(Controller) == 4);
@@ -227,7 +271,8 @@ struct PIF {
return joybusDevices[channel].accessoryType;
}
private:
private:
void ConfigureJoyBusFrame();
};
} // namespace n64
+6 -9
View File
@@ -5,8 +5,9 @@
EmuThread::EmuThread(double &fps, SettingsWindow &settings) noexcept : settings(settings), fps(fps) {}
void EmuThread::run() const noexcept {
n64::Core& core = n64::Core::GetInstance();
if(!core.romLoaded) return;
n64::Core &core = n64::Core::GetInstance();
if (!core.romLoaded)
return;
auto lastSample = std::chrono::high_resolution_clock::now();
auto avgFps = 16.667;
@@ -38,16 +39,12 @@ void EmuThread::run() const noexcept {
}
}
void EmuThread::TogglePause() const noexcept {
n64::Core::GetInstance().TogglePause();
}
void EmuThread::TogglePause() const noexcept { n64::Core::GetInstance().TogglePause(); }
void EmuThread::Reset() const noexcept {
n64::Core::GetInstance().Reset();
}
void EmuThread::Reset() const noexcept { n64::Core::GetInstance().Reset(); }
void EmuThread::Stop() const noexcept {
n64::Core& core = n64::Core::GetInstance();
n64::Core &core = n64::Core::GetInstance();
core.Stop();
core.rom = {};
}
+3 -3
View File
@@ -1,7 +1,6 @@
#pragma once
#include <RenderWidget.hpp>
#include <SettingsWindow.hpp>
#include <memory>
namespace n64 {
struct Core;
@@ -9,7 +8,8 @@ struct Core;
class EmuThread final {
bool started = false;
public:
public:
explicit EmuThread(double &, SettingsWindow &) noexcept;
~EmuThread() = default;
void run() const noexcept;
@@ -19,5 +19,5 @@ public:
bool interruptionRequested = false, parallelRDPInitialized = false;
SettingsWindow &settings;
double& fps;
double &fps;
};
+80
View File
@@ -0,0 +1,80 @@
#pragma once
#include <variant>
#include <PIF.hpp>
#include <Options.hpp>
#include <SDL3/SDL_gamepad.h>
struct Gamepad {
private:
struct MapEntry {
bool isAxis;
int sdlVal;
std::variant<n64::Controller::Key, n64::Controller::Axis> emuVal;
const char *as_c_str() {
if (std::holds_alternative<n64::Controller::Key>(emuVal)) {
switch (std::get<n64::Controller::Key>(emuVal)) {
case n64::Controller::A:
return "A";
case n64::Controller::B:
return "B";
case n64::Controller::Z:
return "Z";
case n64::Controller::Start:
return "Start";
case n64::Controller::DUp:
return "DUp";
case n64::Controller::DDown:
return "DDown";
case n64::Controller::DLeft:
return "DLeft";
case n64::Controller::DRight:
return "DRight";
case n64::Controller::CUp:
return "CUp";
case n64::Controller::CDown:
return "CDown";
case n64::Controller::CLeft:
return "CLeft";
case n64::Controller::CRight:
return "CRight";
case n64::Controller::LT:
return "LT";
case n64::Controller::RT:
return "RT";
}
}
switch (std::get<n64::Controller::Axis>(emuVal)) {
case n64::Controller::X:
return "X";
case n64::Controller::Y:
return "Y";
}
}
} entries[14] = {
{false, SDL_GAMEPAD_BUTTON_SOUTH, n64::Controller::A},
{false, SDL_GAMEPAD_BUTTON_WEST, n64::Controller::B},
{false, SDL_GAMEPAD_BUTTON_START, n64::Controller::Start},
{false, SDL_GAMEPAD_BUTTON_DPAD_UP, n64::Controller::DUp},
{false, SDL_GAMEPAD_BUTTON_DPAD_DOWN, n64::Controller::DDown},
{false, SDL_GAMEPAD_BUTTON_DPAD_LEFT, n64::Controller::DLeft},
{false, SDL_GAMEPAD_BUTTON_DPAD_RIGHT, n64::Controller::DRight},
{false, SDL_GAMEPAD_BUTTON_LEFT_SHOULDER, n64::Controller::LT},
{false, SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER, n64::Controller::RT},
{false, SDL_GAMEPAD_AXIS_LEFT_TRIGGER, n64::Controller::Z},
{true, SDL_GAMEPAD_AXIS_RIGHTX, n64::Controller::X},
{true, SDL_GAMEPAD_AXIS_RIGHTY, n64::Controller::Y},
{true, SDL_GAMEPAD_AXIS_LEFTX, n64::Controller::X},
{true, SDL_GAMEPAD_AXIS_LEFTY, n64::Controller::Y},
};
public:
void SetValue(const MapEntry &);
void Serialize() {
auto &options = Options::GetInstance();
for(const auto& entry : entries) {
options.SetValue<int>(, const std::string &field, const T &value)
}
}
};
-2
View File
@@ -1,10 +1,8 @@
#pragma once
#define IMGUI_IMPL_VULKAN_NO_PROTOTYPES
#include <imgui.h>
#include <imgui_impl_sdl3.h>
#include <imgui_impl_vulkan.h>
#include <utils/log.hpp>
#include <memory>
namespace gui {
static VkAllocationCallbacks *g_Allocator = NULL;
+12 -11
View File
@@ -1,19 +1,20 @@
#pragma once
#include <imgui.h>
#include <imgui_internal.h>
#include <cstdlib>
namespace ImGui {
inline bool Spinner(const char* label, const float radius, const int thickness, const ImU32& color) {
ImGuiWindow* window = GetCurrentWindow();
inline bool Spinner(const char *label, const float radius, const int thickness, const ImU32 &color) {
ImGuiWindow *window = GetCurrentWindow();
if (window->SkipItems)
return false;
const ImGuiContext & g = *GImGui;
const ImGuiStyle& style = g.Style;
const ImGuiContext &g = *GImGui;
const ImGuiStyle &style = g.Style;
const ImGuiID id = window->GetID(label);
const ImVec2 pos = window->DC.CursorPos;
const ImVec2 size(radius*2, (radius + style.FramePadding.y)*2);
const ImVec2 size(radius * 2, (radius + style.FramePadding.y) * 2);
const ImRect bb(pos, ImVec2(pos.x + size.x, pos.y + size.y));
ItemSize(bb, style.FramePadding.y);
@@ -24,20 +25,20 @@ inline bool Spinner(const char* label, const float radius, const int thickness,
window->DrawList->PathClear();
constexpr int num_segments = 30;
const int start = abs(ImSin(g.Time*1.8f)*(num_segments-5));
const int start = std::abs(ImSin(g.Time * 1.8f) * (num_segments - 5));
const float a_min = IM_PI * 2.0f * static_cast<float>(start) / static_cast<float>(num_segments);
constexpr float a_max = IM_PI*2.0f * (static_cast<float>(num_segments) -3) / static_cast<float>(num_segments);
constexpr float a_max = IM_PI * 2.0f * (static_cast<float>(num_segments) - 3) / static_cast<float>(num_segments);
const auto centre = ImVec2(pos.x+radius, pos.y+radius+style.FramePadding.y);
const auto centre = ImVec2(pos.x + radius, pos.y + radius + style.FramePadding.y);
for (int i = 0; i < num_segments; i++) {
const float a = a_min + static_cast<float>(i) / static_cast<float>(num_segments) * (a_max - a_min);
window->DrawList->PathLineTo(ImVec2(centre.x + ImCos(a+g.Time*8) * radius,
centre.y + ImSin(a+g.Time*8) * radius));
window->DrawList->PathLineTo(
ImVec2(centre.x + ImCos(a + g.Time * 8) * radius, centre.y + ImSin(a + g.Time * 8) * radius));
}
window->DrawList->PathStroke(color, false, thickness);
return true;
}
}
} // namespace ImGui
+19 -14
View File
@@ -3,19 +3,22 @@
#include <imgui_internal.h>
namespace ImGui {
inline bool BeginMainStatusBar()
{
ImGuiContext& g = *GetCurrentContext();
ImGuiViewportP* viewport = (ImGuiViewportP*)(void*)GetMainViewport();
inline bool BeginMainStatusBar() {
ImGuiContext &g = *GetCurrentContext();
ImGuiViewportP *viewport = (ImGuiViewportP *)(void *)GetMainViewport();
// Notify of viewport change so GetFrameHeight() can be accurate in case of DPI change
SetCurrentViewport(NULL, viewport);
// For the main menu bar, which cannot be moved, we honor g.Style.DisplaySafeAreaPadding to ensure text can be visible on a TV set.
// For the main menu bar, which cannot be moved, we honor g.Style.DisplaySafeAreaPadding to ensure text can be
// visible on a TV set.
// FIXME: This could be generalized as an opt-in way to clamp window->DC.CursorStartPos to avoid SafeArea?
// FIXME: Consider removing support for safe area down the line... it's messy. Nowadays consoles have support for TV calibration in OS settings.
g.NextWindowData.MenuBarOffsetMinVal = ImVec2(g.Style.DisplaySafeAreaPadding.x, ImMax(g.Style.DisplaySafeAreaPadding.y - g.Style.FramePadding.y, 0.0f));
ImGuiWindowFlags window_flags = ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_MenuBar;
// FIXME: Consider removing support for safe area down the line... it's messy. Nowadays consoles have support for TV
// calibration in OS settings.
g.NextWindowData.MenuBarOffsetMinVal = ImVec2(
g.Style.DisplaySafeAreaPadding.x, ImMax(g.Style.DisplaySafeAreaPadding.y - g.Style.FramePadding.y, 0.0f));
ImGuiWindowFlags window_flags =
ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_MenuBar;
float height = GetFrameHeight();
bool is_open = BeginViewportSideBar("##MainStatusBar", viewport, ImGuiDir_Down, height, window_flags);
g.NextWindowData.MenuBarOffsetMinVal = ImVec2(0.0f, 0.0f);
@@ -27,16 +30,18 @@ inline bool BeginMainStatusBar()
return is_open;
}
inline void EndMainStatusBar()
{
inline void EndMainStatusBar() {
EndMenuBar();
// When the user has left the menu layer (typically: closed menus through activation of an item), we restore focus to the previous window
// When the user has left the menu layer (typically: closed menus through activation of an item), we restore focus
// to the previous window
// FIXME: With this strategy we won't be able to restore a NULL focus.
ImGuiContext& g = *GImGui;
ImGuiContext &g = *GImGui;
if (g.CurrentWindow == g.NavWindow && g.NavLayer == ImGuiNavLayer_Main && !g.NavAnyRequest)
FocusTopMostWindowUnderOne(g.NavWindow, NULL, NULL, ImGuiFocusRequestFlags_UnlessBelowModal | ImGuiFocusRequestFlags_RestoreFocusedChild);
FocusTopMostWindowUnderOne(g.NavWindow, NULL, NULL,
ImGuiFocusRequestFlags_UnlessBelowModal |
ImGuiFocusRequestFlags_RestoreFocusedChild);
End();
}
}
} // namespace ImGui
+117 -88
View File
@@ -5,7 +5,9 @@
#include <ImGuiImpl/StatusBar.hpp>
#include <resources/gamecontrollerdb.h>
KaizenGui::KaizenGui() noexcept : window("Kaizen " KAIZEN_VERSION_STR, 1280, 720), settingsWindow(window), vulkanWidget(window.getHandle()), emuThread(fpsCounter, settingsWindow) {
KaizenGui::KaizenGui() noexcept :
window("Kaizen " KAIZEN_VERSION_STR, 1280, 720), settingsWindow(window), vulkanWidget(window.getHandle()),
emuThread(fpsCounter, settingsWindow) {
gui::Initialize(n64::Core::GetInstance().parallel.wsi, window.getHandle());
SDL_InitSubSystem(SDL_INIT_GAMEPAD);
@@ -33,23 +35,30 @@ void KaizenGui::QueryDevices(const SDL_Event &event) {
if (gamepad)
SDL_CloseGamepad(gamepad);
break;
default: break;
default:
break;
}
}
void KaizenGui::HandleInput(const SDL_Event &event) {
const n64::Core& core = n64::Core::GetInstance();
const n64::Core &core = n64::Core::GetInstance();
n64::PIF &pif = n64::Core::GetMem().mmio.si.pif;
switch(event.type) {
switch (event.type) {
case SDL_EVENT_GAMEPAD_AXIS_MOTION:
if(!gamepad)
if (!gamepad)
break;
{
pif.UpdateButton(0, n64::Controller::Key::Z, SDL_GetGamepadAxis(gamepad, SDL_GAMEPAD_AXIS_LEFT_TRIGGER) == SDL_JOYSTICK_AXIS_MAX);
pif.UpdateButton(0, n64::Controller::Key::CUp, SDL_GetGamepadAxis(gamepad, SDL_GAMEPAD_AXIS_RIGHTY) <= -127);
pif.UpdateButton(0, n64::Controller::Key::CDown, SDL_GetGamepadAxis(gamepad, SDL_GAMEPAD_AXIS_RIGHTY) >= 127);
pif.UpdateButton(0, n64::Controller::Key::CLeft, SDL_GetGamepadAxis(gamepad, SDL_GAMEPAD_AXIS_RIGHTX) <= -127);
pif.UpdateButton(0, n64::Controller::Key::CRight, SDL_GetGamepadAxis(gamepad, SDL_GAMEPAD_AXIS_RIGHTX) >= 127);
pif.UpdateButton(0, n64::Controller::Key::Z,
SDL_GetGamepadAxis(gamepad, SDL_GAMEPAD_AXIS_LEFT_TRIGGER) == SDL_JOYSTICK_AXIS_MAX ||
SDL_GetGamepadAxis(gamepad, SDL_GAMEPAD_AXIS_RIGHT_TRIGGER) == SDL_JOYSTICK_AXIS_MAX);
pif.UpdateButton(0, n64::Controller::Key::CUp,
SDL_GetGamepadAxis(gamepad, SDL_GAMEPAD_AXIS_RIGHTY) <= -127);
pif.UpdateButton(0, n64::Controller::Key::CDown,
SDL_GetGamepadAxis(gamepad, SDL_GAMEPAD_AXIS_RIGHTY) >= 127);
pif.UpdateButton(0, n64::Controller::Key::CLeft,
SDL_GetGamepadAxis(gamepad, SDL_GAMEPAD_AXIS_RIGHTX) <= -127);
pif.UpdateButton(0, n64::Controller::Key::CRight,
SDL_GetGamepadAxis(gamepad, SDL_GAMEPAD_AXIS_RIGHTX) >= 127);
float xclamped = SDL_GetGamepadAxis(gamepad, SDL_GAMEPAD_AXIS_LEFTX);
if (xclamped < 0) {
@@ -70,12 +79,12 @@ void KaizenGui::HandleInput(const SDL_Event &event) {
yclamped *= 86;
pif.UpdateAxis(0, n64::Controller::Axis::Y, static_cast<s8>(-yclamped));
pif.UpdateAxis(0, n64::Controller::Axis::X, static_cast<s8>( xclamped));
pif.UpdateAxis(0, n64::Controller::Axis::X, static_cast<s8>(xclamped));
}
break;
case SDL_EVENT_GAMEPAD_BUTTON_DOWN:
case SDL_EVENT_GAMEPAD_BUTTON_UP:
if(!gamepad)
if (!gamepad)
break;
pif.UpdateButton(0, n64::Controller::Key::A, SDL_GetGamepadButton(gamepad, SDL_GAMEPAD_BUTTON_SOUTH));
@@ -92,29 +101,29 @@ void KaizenGui::HandleInput(const SDL_Event &event) {
case SDL_EVENT_KEY_UP:
{
const auto keys = SDL_GetKeyboardState(nullptr);
if((keys[SDL_SCANCODE_LCTRL] || keys[SDL_SCANCODE_RCTRL]) && keys[SDL_SCANCODE_O]) {
if ((keys[SDL_SCANCODE_LCTRL] || keys[SDL_SCANCODE_RCTRL]) && keys[SDL_SCANCODE_O]) {
fileDialogOpen = true;
}
fastForward = keys[SDL_SCANCODE_SPACE];
if(!unlockFramerate)
if (!unlockFramerate)
core.parallel.SetFramerateUnlocked(fastForward);
if(core.romLoaded) {
if(keys[SDL_SCANCODE_P]) {
if (core.romLoaded) {
if (keys[SDL_SCANCODE_P]) {
emuThread.TogglePause();
}
if(keys[SDL_SCANCODE_R]) {
if (keys[SDL_SCANCODE_R]) {
emuThread.Reset();
}
if(keys[SDL_SCANCODE_Q]) {
if (keys[SDL_SCANCODE_Q]) {
emuThread.Stop();
}
}
if(gamepad)
if (gamepad)
break;
pif.UpdateButton(0, n64::Controller::Key::Z, keys[SDL_SCANCODE_Z]);
@@ -134,31 +143,36 @@ void KaizenGui::HandleInput(const SDL_Event &event) {
float x = 0, y = 0;
if (keys[SDL_SCANCODE_UP]) y = 86;
if (keys[SDL_SCANCODE_DOWN]) y = -86;
if (keys[SDL_SCANCODE_LEFT]) x = -86;
if (keys[SDL_SCANCODE_RIGHT]) x = 86;
if (keys[SDL_SCANCODE_UP])
y = 86;
if (keys[SDL_SCANCODE_DOWN])
y = -86;
if (keys[SDL_SCANCODE_LEFT])
x = -86;
if (keys[SDL_SCANCODE_RIGHT])
x = 86;
pif.UpdateAxis(0, n64::Controller::Axis::X, x);
pif.UpdateAxis(0, n64::Controller::Axis::Y, y);
}
break;
default: break;
default:
break;
}
}
std::pair<std::optional<s64>, std::optional<Util::Error::MemoryAccess>> RenderErrorMessageDetails() {
auto lastPC = Util::Error::GetLastPC();
if(lastPC.has_value()) {
if (lastPC.has_value()) {
ImGui::Text("%s", std::format("Occurred @ PC = {:016X}", Util::Error::GetLastPC().value()).c_str());
}
auto memoryAccess = Util::Error::GetMemoryAccess();
if(memoryAccess.has_value()) {
if (memoryAccess.has_value()) {
const auto [is_write, size, address, written_val] = memoryAccess.value();
ImGui::Text("%s", std::format("{} {}-bit value @ {:08X}{}", is_write ? "Writing" : "Reading",
static_cast<u8>(size), address,
is_write ? std::format(" (value = 0x{:X})", written_val) : "")
ImGui::Text("%s",
std::format("{} {}-bit value @ {:08X}{}", is_write ? "Writing" : "Reading", static_cast<u8>(size),
address, is_write ? std::format(" (value = 0x{:X})", written_val) : "")
.c_str());
}
@@ -166,53 +180,53 @@ std::pair<std::optional<s64>, std::optional<Util::Error::MemoryAccess>> RenderEr
}
void KaizenGui::RenderUI() {
n64::Core& core = n64::Core::GetInstance();
n64::Core &core = n64::Core::GetInstance();
gui::StartFrame();
if(ImGui::BeginMainMenuBar()) {
if(ImGui::BeginMenu("File")) {
if(ImGui::MenuItem("Open", "Ctrl-O")) {
if (ImGui::BeginMainMenuBar()) {
if (ImGui::BeginMenu("File")) {
if (ImGui::MenuItem("Open", "Ctrl-O")) {
fileDialogOpen = true;
}
if(ImGui::MenuItem("Exit")) {
if (ImGui::MenuItem("Exit")) {
quit = true;
emuThread.Stop();
}
ImGui::EndMenu();
}
if(ImGui::BeginMenu("Emulation")) {
if (ImGui::BeginMenu("Emulation")) {
ImGui::BeginDisabled(!core.romLoaded);
if(ImGui::MenuItem(core.pause ? "Resume" : "Pause", "P")) {
if (ImGui::MenuItem(core.pause ? "Resume" : "Pause", "P")) {
emuThread.TogglePause();
}
if(ImGui::MenuItem("Reset", "R")) {
if (ImGui::MenuItem("Reset", "R")) {
emuThread.Reset();
}
if(ImGui::MenuItem("Stop", "Q")) {
if (ImGui::MenuItem("Stop", "Q")) {
emuThread.Stop();
core.romLoaded = false;
}
if(ImGui::Checkbox("Unlock framerate", &unlockFramerate)) {
if (ImGui::Checkbox("Unlock framerate", &unlockFramerate)) {
core.parallel.SetFramerateUnlocked(unlockFramerate);
}
if(ImGui::MenuItem("Open Debugger")) {
if (ImGui::MenuItem("Open Debugger")) {
debugger.Open();
}
ImGui::EndDisabled();
if(ImGui::MenuItem("Options")) {
if (ImGui::MenuItem("Options")) {
settingsWindow.isOpen = true;
}
ImGui::EndMenu();
}
if(ImGui::BeginMenu("Help")) {
if(ImGui::MenuItem("About")) {
if (ImGui::BeginMenu("Help")) {
if (ImGui::MenuItem("About")) {
aboutOpen = true;
}
@@ -221,15 +235,15 @@ void KaizenGui::RenderUI() {
ImGui::EndMainMenuBar();
}
if(!Util::Error::IsHandled()) {
if (!Util::Error::IsHandled()) {
ImGui::OpenPopup(Util::Error::GetSeverity().as_c_str());
}
if(settingsWindow.isOpen) {
if (settingsWindow.isOpen) {
ImGui::OpenPopup("Settings", ImGuiPopupFlags_None);
}
if(aboutOpen) {
if (aboutOpen) {
ImGui::OpenPopup("About Kaizen");
}
@@ -247,7 +261,7 @@ void KaizenGui::RenderUI() {
ImGui::Separator();
ImGui::Text("Kaizen %s%s", KAIZEN_USE_HASH ? "dev build " : "", KAIZEN_VERSION_STR);
ImGui::Separator();
if(ImGui::Button("OK")) {
if (ImGui::Button("OK")) {
aboutOpen = false;
ImGui::CloseCurrentPopup();
}
@@ -259,8 +273,9 @@ void KaizenGui::RenderUI() {
if (ImGui::BeginPopupModal(Util::Error::GetSeverity().as_c_str(), nullptr, ImGuiWindowFlags_AlwaysAutoResize)) {
emuThread.TogglePause();
switch(Util::Error::GetSeverity().as_enum) {
case Util::Error::Severity::WARN: {
switch (Util::Error::GetSeverity().as_enum) {
case Util::Error::Severity::WARN:
{
ImGui::PushStyleColor(ImGuiCol_TitleBg, 0x8054eae5);
ImGui::PushStyleColor(ImGuiCol_Text, 0xff7be4e1);
ImGui::Text("Warning of type: %s", Util::Error::GetType().as_c_str());
@@ -269,33 +284,37 @@ void KaizenGui::RenderUI() {
ImGui::Text(R"(Warning message: "%s")", Util::Error::GetError().c_str());
RenderErrorMessageDetails();
if(n64::Core::GetInstance().romLoaded && !n64::Core::GetInstance().pause) {
const bool ignore = ImGui::Button("Try continuing"); ImGui::SameLine();
const bool stop = ImGui::Button("Stop emulation"); ImGui::SameLine();
if (n64::Core::GetInstance().romLoaded && !n64::Core::GetInstance().pause) {
const bool ignore = ImGui::Button("Try continuing");
ImGui::SameLine();
const bool stop = ImGui::Button("Stop emulation");
ImGui::SameLine();
const bool chooseAnother = ImGui::Button("Choose another ROM");
if(ignore || stop || chooseAnother) {
if (ignore || stop || chooseAnother) {
Util::Error::SetHandled();
ImGui::CloseCurrentPopup();
}
if(ignore) {
if (ignore) {
emuThread.TogglePause();
}
if(stop || chooseAnother) {
if (stop || chooseAnother) {
emuThread.Stop();
}
if(chooseAnother) {
if (chooseAnother) {
fileDialogOpen = true;
}
break;
}
if(ImGui::Button("OK"))
if (ImGui::Button("OK"))
ImGui::CloseCurrentPopup();
} break;
case Util::Error::Severity::UNRECOVERABLE: {
}
break;
case Util::Error::Severity::UNRECOVERABLE:
{
emuThread.Stop();
ImGui::PushStyleColor(ImGuiCol_TitleBg, 0x800000ff);
ImGui::PushStyleColor(ImGuiCol_Text, 0xff3b3bbf);
@@ -305,10 +324,12 @@ void KaizenGui::RenderUI() {
ImGui::PopStyleColor();
ImGui::Text(R"(Error message: "%s")", Util::Error::GetError().c_str());
RenderErrorMessageDetails();
if(ImGui::Button("OK"))
if (ImGui::Button("OK"))
ImGui::CloseCurrentPopup();
} break;
case Util::Error::Severity::NON_FATAL: {
}
break;
case Util::Error::Severity::NON_FATAL:
{
ImGui::PushStyleColor(ImGuiCol_TitleBg, 0x800000ff);
ImGui::PushStyleColor(ImGuiCol_Text, 0xff3b3bbf);
ImGui::Text("An error has occurred!");
@@ -318,48 +339,54 @@ void KaizenGui::RenderUI() {
ImGui::Text(R"(Error message: "%s")", Util::Error::GetError().c_str());
auto [lastPC, memoryAccess] = RenderErrorMessageDetails();
const bool ignore = ImGui::Button("Try continuing"); ImGui::SameLine();
const bool stop = ImGui::Button("Stop emulation"); ImGui::SameLine();
const bool ignore = ImGui::Button("Try continuing");
ImGui::SameLine();
const bool stop = ImGui::Button("Stop emulation");
ImGui::SameLine();
const bool chooseAnother = ImGui::Button("Choose another ROM");
const bool openInDebugger = lastPC.has_value() ? ImGui::Button("Add breakpoint at this PC and open the debugger") : false;
if(ignore || stop || chooseAnother || openInDebugger) {
const bool openInDebugger =
lastPC.has_value() ? ImGui::Button("Add breakpoint at this PC and open the debugger") : false;
if (ignore || stop || chooseAnother || openInDebugger) {
Util::Error::SetHandled();
ImGui::CloseCurrentPopup();
}
if(ignore) {
if (ignore) {
emuThread.TogglePause();
}
if(stop || chooseAnother) {
if (stop || chooseAnother) {
emuThread.Stop();
}
if(chooseAnother) {
if (chooseAnother) {
fileDialogOpen = true;
}
if(openInDebugger) {
if(!n64::Core::GetInstance().breakpoints.contains(lastPC.value()))
if (openInDebugger) {
if (!n64::Core::GetInstance().breakpoints.contains(lastPC.value()))
n64::Core::GetInstance().ToggleBreakpoint(lastPC.value());
debugger.Open();
emuThread.Reset();
}
} break;
default: break;
}
break;
default:
break;
}
ImGui::EndPopup();
}
if(ImGui::BeginMainStatusBar()) {
if (ImGui::BeginMainStatusBar()) {
ImGui::Text("FPS: %.2f", ImGui::GetIO().Framerate);
ImGui::EndMainStatusBar();
}
if (shouldDisplaySpinner) {
ImGui::SetNextWindowPos({static_cast<float>(width) * 0.5f, static_cast<float>(height) * 0.5f}, 0, ImVec2(0.5f, 0.5f));
ImGui::SetNextWindowPos({static_cast<float>(width) * 0.5f, static_cast<float>(height) * 0.5f}, 0,
ImVec2(0.5f, 0.5f));
ImGui::PushStyleColor(ImGuiCol_WindowBg, IM_COL32_BLACK_TRANS);
ImGui::PushStyleVar(ImGuiStyleVar_WindowBorderSize, 0.0f);
@@ -384,11 +411,14 @@ void KaizenGui::RenderUI() {
ImGui::RenderPlatformWindowsDefault();
}
if(fileDialogOpen) {
if (fileDialogOpen) {
fileDialogOpen = false;
constexpr SDL_DialogFileFilter filters[] = {{"All files", "*"}, {"Nintendo 64 executable", "n64;z64;v64"}, {"Nintendo 64 executable archive", "rar;tar;zip;7z"}};
SDL_ShowOpenFileDialog([](void *userdata, const char * const *filelist, int) {
auto kaizen = static_cast<KaizenGui*>(userdata);
constexpr SDL_DialogFileFilter filters[] = {{"All files", "*"},
{"Nintendo 64 executable", "n64;z64;v64"},
{"Nintendo 64 executable archive", "rar;tar;zip;7z"}};
SDL_ShowOpenFileDialog(
[](void *userdata, const char *const *filelist, int) {
auto kaizen = static_cast<KaizenGui *>(userdata);
if (!filelist) {
panic("An error occured: {}", SDL_GetError());
@@ -405,13 +435,14 @@ void KaizenGui::RenderUI() {
std::thread fileWorker(&KaizenGui::FileWorker, kaizen);
fileWorker.detach();
}, this, window.getHandle(), filters, 3, nullptr, false);
},
this, window.getHandle(), filters, 3, nullptr, false);
}
if(minimized)
if (minimized)
return;
if(core.romLoaded) {
if (core.romLoaded) {
core.parallel.UpdateScreen<true>();
return;
}
@@ -420,18 +451,18 @@ void KaizenGui::RenderUI() {
}
void KaizenGui::LoadROM(const std::string &path) noexcept {
n64::Core& core = n64::Core::GetInstance();
n64::Core &core = n64::Core::GetInstance();
core.LoadROM(path);
const auto gameNameDB = n64::Core::GetMem().rom.gameNameDB;
SDL_SetWindowTitle(window.getHandle(), ("Kaizen " KAIZEN_VERSION_STR " - " + gameNameDB).c_str());
}
void KaizenGui::run() {
while(!quit) {
while (!quit) {
SDL_Event e;
while (SDL_PollEvent(&e)) {
ImGui_ImplSDL3_ProcessEvent(&e);
switch(e.type) {
switch (e.type) {
case SDL_EVENT_QUIT:
quit = true;
emuThread.Stop();
@@ -455,6 +486,4 @@ void KaizenGui::run() {
}
}
void KaizenGui::LoadTAS(const std::string &path) noexcept {
n64::Core::GetInstance().LoadTAS(fs::path(path));
}
void KaizenGui::LoadTAS(const std::string &path) noexcept { n64::Core::GetInstance().LoadTAS(fs::path(path)); }
+8 -5
View File
@@ -1,13 +1,15 @@
#pragma once
#include <RenderWidget.hpp>
#include <NativeWindow.hpp>
#include <Window.hpp>
#include <Debugger.hpp>
#include <EmuThread.hpp>
#include <SDL3/SDL_gamepad.h>
#include <Gamepad.hpp>
class KaizenGui final {
gui::NativeWindow window;
public:
gui::Window window;
public:
explicit KaizenGui() noexcept;
~KaizenGui();
@@ -21,12 +23,13 @@ public:
EmuThread emuThread;
Debugger debugger;
SDL_Gamepad* gamepad = nullptr;
SDL_Gamepad *gamepad = nullptr;
void run();
static void LoadTAS(const std::string &path) noexcept;
void LoadROM(const std::string &path) noexcept;
private:
private:
int width{}, height{};
bool aboutOpen = false;
bool fileDialogOpen = false;
-28
View File
@@ -1,28 +0,0 @@
#pragma once
#include <SDL3/SDL.h>
#include <string>
#include <memory>
#include <volk.h>
#include <utils/log.hpp>
namespace gui {
struct NativeWindow {
NativeWindow(const std::string& title, int w, int h, int posX = SDL_WINDOWPOS_CENTERED, int posY = SDL_WINDOWPOS_CENTERED) {
SDL_Init(SDL_INIT_VIDEO);
float scale = SDL_GetDisplayContentScale(SDL_GetPrimaryDisplay());
window = SDL_CreateWindow(title.c_str(), w * scale, h * scale, SDL_WINDOW_VULKAN | SDL_WINDOW_RESIZABLE | SDL_WINDOW_HIGH_PIXEL_DENSITY);
if(volkInitialize() != VK_SUCCESS) {
panic("Failed to initialize Volk!");
}
}
~NativeWindow() {
SDL_DestroyWindow(window);
}
SDL_Window* getHandle() { return window; }
private:
SDL_Window* window;
};
}
+2 -2
View File
@@ -4,9 +4,9 @@
#include <SDL3/SDL.h>
#include <imgui_impl_sdl3.h>
RenderWidget::RenderWidget(SDL_Window* window) {
RenderWidget::RenderWidget(SDL_Window *window) {
wsiPlatform = std::make_shared<SDLWSIPlatform>(window);
windowInfo = std::make_shared<SDLParallelRdpWindowInfo>(window);
n64::Core& core = n64::Core::GetInstance();
n64::Core &core = n64::Core::GetInstance();
core.parallel.Init(wsiPlatform, windowInfo, core.GetMem().GetRDRAMPtr());
}
+12 -11
View File
@@ -10,21 +10,21 @@ struct Core;
}
class SDLParallelRdpWindowInfo final : public ParallelRDP::WindowInfo {
public:
explicit SDLParallelRdpWindowInfo(SDL_Window* window) : window(window) {}
public:
explicit SDLParallelRdpWindowInfo(SDL_Window *window) : window(window) {}
CoordinatePair get_window_size() override {
int w,h;
int w, h;
SDL_GetWindowSizeInPixels(window, &w, &h);
return CoordinatePair{static_cast<float>(w), static_cast<float>(h)};
}
private:
SDL_Window* window{};
private:
SDL_Window *window{};
};
class SDLWSIPlatform final : public Vulkan::WSIPlatform {
public:
explicit SDLWSIPlatform(SDL_Window* window) : window(window) {}
public:
explicit SDLWSIPlatform(SDL_Window *window) : window(window) {}
~SDLWSIPlatform() = default;
std::vector<const char *> get_instance_extensions() override {
@@ -64,15 +64,16 @@ public:
VkApplicationInfo appInfo{.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO, .apiVersion = VK_API_VERSION_1_3};
SDL_Window* window{};
SDL_Window *window{};
VkSurfaceKHR surface;
private:
private:
bool gamepadConnected = false;
};
class RenderWidget final {
public:
explicit RenderWidget(SDL_Window*);
public:
explicit RenderWidget(SDL_Window *);
std::shared_ptr<ParallelRDP::WindowInfo> windowInfo;
std::shared_ptr<SDLWSIPlatform> wsiPlatform;
+4 -4
View File
@@ -9,9 +9,9 @@ AudioSettings::AudioSettings() {
}
void AudioSettings::render() {
if(ImGui::Checkbox("Lock channels:", &lockChannels)) {
if (ImGui::Checkbox("Lock channels:", &lockChannels)) {
Options::GetInstance().SetValue("audio", "lock", lockChannels);
if(lockChannels) {
if (lockChannels) {
volumeR = volumeL;
Options::GetInstance().SetValue("audio", "volumeR", volumeR / 100.f);
}
@@ -19,7 +19,7 @@ void AudioSettings::render() {
modified = true;
}
if(ImGui::SliderFloat("Volume L", &volumeL, 0.f, 100.f, "%.2f")) {
if (ImGui::SliderFloat("Volume L", &volumeL, 0.f, 100.f, "%.2f")) {
Options::GetInstance().SetValue("audio", "volumeL", volumeL / 100.f);
if (lockChannels) {
volumeR = volumeL;
@@ -30,7 +30,7 @@ void AudioSettings::render() {
}
ImGui::BeginDisabled(lockChannels);
if(ImGui::SliderFloat("Volume R", &volumeR, 0.f, 100.f, "%.2f")) {
if (ImGui::SliderFloat("Volume R", &volumeR, 0.f, 100.f, "%.2f")) {
Options::GetInstance().SetValue("audio", "volumeR", volumeR / 100.f);
modified = true;
}
+8 -6
View File
@@ -3,14 +3,15 @@
#include <imgui.h>
#include <log.hpp>
GeneralSettings::GeneralSettings(gui::NativeWindow& window) : window(window) {
GeneralSettings::GeneralSettings(gui::Window &window) : window(window) {
savesPath = Options::GetInstance().GetValue<std::string>("general", "savePath");
}
void GeneralSettings::render() {
if(ImGui::Button("Pick...")) {
SDL_ShowOpenFolderDialog([](void *userdata, const char * const *filelist, int _) {
auto* general = static_cast<GeneralSettings*>(userdata);
if (ImGui::Button("Pick...")) {
SDL_ShowOpenFolderDialog(
[](void *userdata, const char *const *filelist, int _) {
auto *general = static_cast<GeneralSettings *>(userdata);
if (!filelist) {
panic("An error occurred: {}", SDL_GetError());
@@ -26,10 +27,11 @@ void GeneralSettings::render() {
general->savesPath = fs::absolute(*filelist).string();
Options::GetInstance().SetValue<std::string>("general", "savePath", general->savesPath);
general->modified = true;
}, this, window.getHandle(), nullptr, false);
},
this, window.getHandle(), nullptr, false);
}
ImGui::SameLine();
ImGui::BeginDisabled();
ImGui::InputText("Save Path", const_cast<char*>(savesPath.c_str()), savesPath.length());
ImGui::InputText("Save Path", const_cast<char *>(savesPath.c_str()), savesPath.length());
ImGui::EndDisabled();
}
+5 -4
View File
@@ -1,11 +1,12 @@
#pragma once
#include <SettingsTab.hpp>
#include <NativeWindow.hpp>
#include <Window.hpp>
struct GeneralSettings final : SettingsTab {
void render() override;
explicit GeneralSettings(gui::NativeWindow&);
private:
gui::NativeWindow& window;
explicit GeneralSettings(gui::Window &);
private:
gui::Window &window;
std::string savesPath;
};
+5 -5
View File
@@ -7,13 +7,13 @@ bool SettingsWindow::render() {
const ImVec2 center = ImGui::GetMainViewport()->GetCenter();
ImGui::SetNextWindowPos(center, ImGuiCond_Appearing, ImVec2(0.5f, 0.5f));
if(!ImGui::BeginPopupModal("Settings", &isOpen, ImGuiWindowFlags_AlwaysAutoResize))
if (!ImGui::BeginPopupModal("Settings", &isOpen, ImGuiWindowFlags_AlwaysAutoResize))
return false;
if(!ImGui::BeginTabBar("SettingsTabBar"))
if (!ImGui::BeginTabBar("SettingsTabBar"))
return false;
for (auto& [name, tab] : tabs) {
for (auto &[name, tab] : tabs) {
if (ImGui::BeginTabItem(name.c_str())) {
tab->render();
if (tab->modified && !applyEnabled)
@@ -26,7 +26,7 @@ bool SettingsWindow::render() {
ImGui::EndTabBar();
ImGui::BeginDisabled(!applyEnabled);
if(ImGui::Button("Apply")) {
if (ImGui::Button("Apply")) {
applyEnabled = false;
Options::GetInstance().Apply();
@@ -38,7 +38,7 @@ bool SettingsWindow::render() {
ImGui::SameLine();
if(ImGui::Button("Cancel")) {
if (ImGui::Button("Cancel")) {
isOpen = false;
ImGui::CloseCurrentPopup();
}
+8 -8
View File
@@ -2,25 +2,25 @@
#include <AudioSettings.hpp>
#include <CPUSettings.hpp>
#include <GeneralSettings.hpp>
#include <NativeWindow.hpp>
#include <Window.hpp>
#include <vector>
class SettingsWindow final {
gui::NativeWindow& window;
GeneralSettings generalSettings;
CPUSettings cpuSettings;
AudioSettings audioSettings;
bool applyEnabled = false;
std::vector<std::pair<std::string, SettingsTab*>> tabs = {
{ "General", &generalSettings },
{ "CPU", &cpuSettings },
{ "Audio", &audioSettings },
std::vector<std::pair<std::string, SettingsTab *>> tabs = {
{"General", &generalSettings},
{"CPU", &cpuSettings},
{"Audio", &audioSettings},
};
public:
public:
bool isOpen = false;
bool render();
explicit SettingsWindow(gui::NativeWindow& window) : window(window), generalSettings(window) {}
explicit SettingsWindow(gui::Window &window) : generalSettings(window) {}
[[nodiscard]] float getVolumeL() const { return audioSettings.volumeL / 100.f; }
[[nodiscard]] float getVolumeR() const { return audioSettings.volumeR / 100.f; }
};
+23
View File
@@ -0,0 +1,23 @@
#pragma once
#include <SDL3/SDL.h>
#include <string>
#include <utils/log.hpp>
namespace gui {
struct Window {
Window(const std::string &title, int w, int h, int posX = SDL_WINDOWPOS_CENTERED,
int posY = SDL_WINDOWPOS_CENTERED) {
SDL_Init(SDL_INIT_VIDEO);
float scale = SDL_GetDisplayContentScale(SDL_GetPrimaryDisplay());
window = SDL_CreateWindow(title.c_str(), w * scale, h * scale,
SDL_WINDOW_VULKAN | SDL_WINDOW_RESIZABLE | SDL_WINDOW_HIGH_PIXEL_DENSITY);
}
~Window() { SDL_DestroyWindow(window); }
SDL_Window *getHandle() { return window; }
private:
SDL_Window *window;
};
} // namespace gui
+5 -3
View File
@@ -4,10 +4,12 @@
int main(const int argc, char **argv) {
KaizenGui kaizenGui;
cflags::cflags flags;
flags.add_string_callback('\0', "rom", [&kaizenGui](const std::string& v) { kaizenGui.LoadROM(v); }, "Rom to launch from command-line");
flags.add_string_callback('\0', "movie", [](const std::string& v) { KaizenGui::LoadTAS(v); }, "Mupen Movie to replay");
flags.add_string_callback(
'\0', "rom", [&kaizenGui](const std::string &v) { kaizenGui.LoadROM(v); }, "Rom to launch from command-line");
flags.add_string_callback(
'\0', "movie", [](const std::string &v) { KaizenGui::LoadTAS(v); }, "Mupen Movie to replay");
if(!flags.parse(argc, argv)) {
if (!flags.parse(argc, argv)) {
return -1;
}
+13 -3
View File
@@ -10,22 +10,32 @@ void Options::SetValue<float>(const std::string &key, const std::string &field,
structure[key][field] = std::format("{:.2f}", value);
}
template <>
void Options::SetValue<int>(const std::string &key, const std::string &field, const int &value) {
structure[key][field] = std::format("{}", value);
}
template <>
void Options::SetValue<bool>(const std::string &key, const std::string &field, const bool &value) {
structure[key][field] = value ? "true" : "false";
}
template<>
template <>
std::string Options::GetValue<std::string>(const std::string &key, const std::string &field) {
return structure[key][field];
}
template<>
template <>
float Options::GetValue<float>(const std::string &key, const std::string &field) {
return std::stof(structure[key][field]);
}
template<>
template <>
int Options::GetValue<int>(const std::string &key, const std::string &field) {
return std::stoi(structure[key][field]);
}
template <>
bool Options::GetValue<bool>(const std::string &key, const std::string &field) {
return structure[key][field] == "true" ? true : false;
}
+22 -7
View File
@@ -10,19 +10,33 @@ namespace fs = std::filesystem;
struct Options {
Options() : file{"resources/options.ini"} {
auto fileExists = fs::exists("resources/options.ini");
if(fileExists) {
if (fileExists) {
file.read(structure);
return;
}
structure["general"]["savePath"] = "saves";
if (!fs::exists("saves"))
fs::create_directory("saves");
structure["cpu"]["type"] = "interpreter";
structure["audio"]["lock"] = "true";
structure["audio"]["volumeL"] = "0.5";
structure["audio"]["volumeR"] = "0.5";
structure["audio"]["lock"] = "true";
structure["cpu"]["type"] = "interpreter";
structure["general"]["savePath"] = "saves";
structure["gamepad"]["A"] = "SDL_GAMEPAD_BUTTON_SOUTH";
structure["gamepad"]["B"] = "SDL_GAMEPAD_BUTTON_WEST";
structure["gamepad"]["Start"] = "SDL_GAMEPAD_BUTTON_START";
structure["gamepad"]["DUp"] = "SDL_GAMEPAD_BUTTON_DPAD_UP";
structure["gamepad"]["DDown"] = "SDL_GAMEPAD_BUTTON_DPAD_DOWN";
structure["gamepad"]["DLeft"] = "SDL_GAMEPAD_BUTTON_DPAD_LEFT";
structure["gamepad"]["DRight"] = "SDL_GAMEPAD_BUTTON_DPAD_RIGHT";
structure["gamepad"]["LT"] = "SDL_GAMEPAD_BUTTON_LEFT_SHOULDER";
structure["gamepad"]["RT"] = "SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER";
structure["gamepad"]["Z"] = "SDL_GAMEPAD_AXIS_LEFT_TRIGGER";
structure["gamepad"]["X"] = "SDL_GAMEPAD_AXIS_RIGHTX";
structure["gamepad"]["Y"] = "SDL_GAMEPAD_AXIS_RIGHTY";
if(!file.generate(structure))
if (!file.generate(structure))
panic("Couldn't generate settings' INI!");
}
@@ -37,10 +51,11 @@ struct Options {
T GetValue(const std::string &key, const std::string &field);
void Apply() {
if(!file.write(structure))
if (!file.write(structure))
panic("Could not modify options on disk!");
}
private:
private:
mINI::INIFile file;
mINI::INIStructure structure;
};