diff --git a/UnleashedRecomp/app.cpp b/UnleashedRecomp/app.cpp index 24640ab7..4b9de257 100644 --- a/UnleashedRecomp/app.cpp +++ b/UnleashedRecomp/app.cpp @@ -28,7 +28,7 @@ void App::Exit() std::_Exit(0); } -// SWA::CApplication +// SWA::CApplication::CApplication PPC_FUNC_IMPL(__imp__sub_824EB490); PPC_FUNC(sub_824EB490) { @@ -75,6 +75,10 @@ PPC_FUNC(sub_822C1130) AudioPatches::Update(App::s_deltaTime); InspirePatches::Update(); + // Apply subtitles option. + if (auto pApplicationDocument = SWA::CApplicationDocument::GetInstance()) + pApplicationDocument->m_InspireSubtitles = Config::Subtitles; + __imp__sub_822C1130(ctx, base); } diff --git a/UnleashedRecomp/exports.cpp b/UnleashedRecomp/exports.cpp index e5162436..8188ac27 100644 --- a/UnleashedRecomp/exports.cpp +++ b/UnleashedRecomp/exports.cpp @@ -31,13 +31,3 @@ void Game_PlaySound(const char* pName) g_userHeap.Free(strAllocation); } } - -void Window_SetDisplay(int displayIndex) -{ - GameWindow::SetDisplay(displayIndex); -} - -void Window_SetFullscreen(bool isEnabled) -{ - GameWindow::SetFullscreen(isEnabled); -} diff --git a/UnleashedRecomp/exports.h b/UnleashedRecomp/exports.h index 37a73b59..5d9eef7d 100644 --- a/UnleashedRecomp/exports.h +++ b/UnleashedRecomp/exports.h @@ -1,5 +1,3 @@ #pragma once void Game_PlaySound(const char* pName); -void Window_SetDisplay(int displayIndex); -void Window_SetFullscreen(bool isEnabled); diff --git a/UnleashedRecomp/patches/misc_patches.cpp b/UnleashedRecomp/patches/misc_patches.cpp index e56ec91b..f9365ec0 100644 --- a/UnleashedRecomp/patches/misc_patches.cpp +++ b/UnleashedRecomp/patches/misc_patches.cpp @@ -43,13 +43,6 @@ bool DisableDLCIconMidAsmHook() return Config::DisableDLCIcon; } -void ToggleSubtitlesMidAsmHook(PPCRegister& r27) -{ - auto pApplicationDocument = (SWA::CApplicationDocument*)g_memory.Translate(r27.u32); - - pApplicationDocument->m_InspireSubtitles = Config::Subtitles; -} - void WerehogBattleMusicMidAsmHook(PPCRegister& r11) { if (Config::BattleTheme) diff --git a/UnleashedRecomp/ui/game_window.cpp b/UnleashedRecomp/ui/game_window.cpp index a8983e34..3ed5fbb1 100644 --- a/UnleashedRecomp/ui/game_window.cpp +++ b/UnleashedRecomp/ui/game_window.cpp @@ -173,27 +173,6 @@ void GameWindow::Init(const char* sdlVideoDriver) SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE); #endif - Config::WindowSize.LockCallback = [](ConfigDef* def) - { - // Try matching the current window size with a known configuration. - if (def->Value < 0) - def->Value = FindNearestDisplayMode(); - }; - - Config::WindowSize.ApplyCallback = [](ConfigDef* def) - { - auto displayModes = GetDisplayModes(); - - // Use largest supported resolution if overflowed. - if (def->Value >= displayModes.size()) - def->Value = displayModes.size() - 1; - - auto& mode = displayModes[def->Value]; - auto centre = SDL_WINDOWPOS_CENTERED_DISPLAY(GetDisplay()); - - SetDimensions(mode.w, mode.h, centre, centre); - }; - s_x = Config::WindowX; s_y = Config::WindowY; s_width = Config::WindowWidth; diff --git a/UnleashedRecomp/user/config.cpp b/UnleashedRecomp/user/config.cpp index fe8699de..f9bbf157 100644 --- a/UnleashedRecomp/user/config.cpp +++ b/UnleashedRecomp/user/config.cpp @@ -1,7 +1,7 @@ #include "config.h" #include +#include #include -#include std::vector g_configDefinitions; @@ -407,11 +407,6 @@ CONFIG_DEFINE_ENUM_TEMPLATE(EUIAlignmentMode) extern CONFIG_ENUM_LOCALE(type) g_##type##_locale; \ ConfigDef Config::name{section, #name, &g_##name##_locale, defaultValue, &g_##type##_template, &g_##type##_locale}; -#undef CONFIG_DEFINE_CALLBACK -#define CONFIG_DEFINE_CALLBACK(section, type, name, defaultValue, readCallback) \ - extern CONFIG_LOCALE g_##name##_locale; \ - ConfigDef Config::name{section, #name, defaultValue, [](ConfigDef* def) readCallback}; - #include "config_def.h" // CONFIG_DEFINE @@ -448,13 +443,6 @@ ConfigDef::ConfigDef(std::string section, std::string name, CONFIG_ g_configDefinitions.emplace_back(this); } -// CONFIG_DEFINE_CALLBACK -template -ConfigDef::ConfigDef(std::string section, std::string name, T defaultValue, std::function*)> callback) : Section(section), Name(name), DefaultValue(defaultValue), Callback(callback) -{ - g_configDefinitions.emplace_back(this); -} - template ConfigDef::~ConfigDef() = default; @@ -723,8 +711,54 @@ std::filesystem::path Config::GetConfigPath() return GetUserPath() / "config.toml"; } +void Config::CreateCallbacks() +{ + Config::WindowSize.LockCallback = [](ConfigDef* def) + { + // Try matching the current window size with a known configuration. + if (def->Value < 0) + def->Value = GameWindow::FindNearestDisplayMode(); + }; + + Config::WindowSize.ApplyCallback = [](ConfigDef* def) + { + auto displayModes = GameWindow::GetDisplayModes(); + + // Use largest supported resolution if overflowed. + if (def->Value >= displayModes.size()) + def->Value = displayModes.size() - 1; + + auto& mode = displayModes[def->Value]; + auto centre = SDL_WINDOWPOS_CENTERED_DISPLAY(GameWindow::GetDisplay()); + + GameWindow::SetDimensions(mode.w, mode.h, centre, centre); + }; + + Config::Monitor.Callback = [](ConfigDef* def) + { + GameWindow::SetDisplay(def->Value); + }; + + Config::Fullscreen.Callback = [](ConfigDef* def) + { + GameWindow::SetFullscreen(def->Value); + GameWindow::SetDisplay(Config::Monitor); + }; + + Config::ResolutionScale.Callback = [](ConfigDef* def) + { + def->Value = std::clamp(def->Value, 0.25f, 2.0f); + }; +} + void Config::Load() { + if (!s_isCallbacksCreated) + { + CreateCallbacks(); + s_isCallbacksCreated = true; + } + auto configPath = GetConfigPath(); if (!std::filesystem::exists(configPath)) diff --git a/UnleashedRecomp/user/config.h b/UnleashedRecomp/user/config.h index 05fe7229..e038d023 100644 --- a/UnleashedRecomp/user/config.h +++ b/UnleashedRecomp/user/config.h @@ -25,6 +25,10 @@ public: #define CONFIG_LOCALE std::unordered_map> #define CONFIG_ENUM_LOCALE(type) std::unordered_map>> +#define CONFIG_CALLBACK(name) if (name.Callback) name.Callback(&name) +#define CONFIG_LOCK_CALLBACK(name) if (name.LockCallback) name.LockCallback(&name) +#define CONFIG_APPLY_CALLBACK(name) if (name.ApplyCallback) name.ApplyCallback(&name) + #define WINDOWPOS_CENTRED 0x2FFF0000 extern std::vector g_configDefinitions; @@ -174,9 +178,6 @@ public: // CONFIG_DEFINE_ENUM_LOCALISED ConfigDef(std::string section, std::string name, CONFIG_LOCALE* nameLocale, T defaultValue, std::unordered_map* enumTemplate, CONFIG_ENUM_LOCALE(T)* enumLocale); - // CONFIG_DEFINE_CALLBACK - ConfigDef(std::string section, std::string name, T defaultValue, std::function*)> callback); - ConfigDef(const ConfigDef&) = delete; ConfigDef(ConfigDef&&) = delete; ~ConfigDef(); @@ -221,15 +222,17 @@ public: #define CONFIG_DEFINE_LOCALISED(section, type, name, defaultValue) CONFIG_DECLARE(type, name) #define CONFIG_DEFINE_ENUM(section, type, name, defaultValue) CONFIG_DECLARE(type, name) #define CONFIG_DEFINE_ENUM_LOCALISED(section, type, name, defaultValue) CONFIG_DECLARE(type, name) -#define CONFIG_DEFINE_CALLBACK(section, type, name, defaultValue, readCallback) CONFIG_DECLARE(type, name) class Config { public: #include "config_def.h" + static inline bool s_isCallbacksCreated; + static std::filesystem::path GetConfigPath(); + static void CreateCallbacks(); static void Load(); static void Save(); }; diff --git a/UnleashedRecomp/user/config_def.h b/UnleashedRecomp/user/config_def.h index c97a8279..b42203d5 100644 --- a/UnleashedRecomp/user/config_def.h +++ b/UnleashedRecomp/user/config_def.h @@ -54,30 +54,10 @@ CONFIG_DEFINE_LOCALISED("Video", int32_t, WindowSize, -1); CONFIG_DEFINE("Video", int32_t, WindowWidth, 1280); CONFIG_DEFINE("Video", int32_t, WindowHeight, 720); CONFIG_DEFINE_ENUM("Video", EWindowState, WindowState, EWindowState::Normal); - -CONFIG_DEFINE_CALLBACK("Video", int32_t, Monitor, 0, -{ - def->Locale = &g_Monitor_locale; - - Window_SetDisplay(def->Value); -}); - +CONFIG_DEFINE_LOCALISED("Video", int32_t, Monitor, 0); CONFIG_DEFINE_ENUM_LOCALISED("Video", EAspectRatio, AspectRatio, EAspectRatio::Auto); - -CONFIG_DEFINE_CALLBACK("Video", float, ResolutionScale, 1.0f, -{ - def->Locale = &g_ResolutionScale_locale; - def->Value = std::clamp(def->Value, 0.25f, 2.0f); -}); - -CONFIG_DEFINE_CALLBACK("Video", bool, Fullscreen, true, -{ - def->Locale = &g_Fullscreen_locale; - - Window_SetFullscreen(def->Value); - Window_SetDisplay(Monitor); -}); - +CONFIG_DEFINE_LOCALISED("Video", float, ResolutionScale, 1.0f); +CONFIG_DEFINE_LOCALISED("Video", bool, Fullscreen, true); CONFIG_DEFINE_LOCALISED("Video", bool, VSync, true); CONFIG_DEFINE_ENUM("Video", ETripleBuffering, TripleBuffering, ETripleBuffering::Auto); CONFIG_DEFINE_LOCALISED("Video", int32_t, FPS, 60); diff --git a/UnleashedRecompLib/config/SWA.toml b/UnleashedRecompLib/config/SWA.toml index 509134d4..0d3e9bba 100644 --- a/UnleashedRecompLib/config/SWA.toml +++ b/UnleashedRecompLib/config/SWA.toml @@ -587,11 +587,6 @@ address = 0x824B08C0 registers = ["r3"] return_on_true = true -[[midasm_hook]] -name = "ToggleSubtitlesMidAsmHook" -address = 0x82B9BB74 -registers = ["r27"] - [[midasm_hook]] name = "AchievementManagerUnlockMidAsmHook" address = 0x82BCFF28