diff --git a/UnleashedRecomp/config.cpp b/UnleashedRecomp/config.cpp index 56b7878..d36243d 100644 --- a/UnleashedRecomp/config.cpp +++ b/UnleashedRecomp/config.cpp @@ -11,8 +11,6 @@ void Config::Load() { printf("Failed to parse configuration: %s\n", err.what()); } - - ResolutionScale = std::clamp(ResolutionScale.Value, 0.25f, 2.0f); } void Config::Save() diff --git a/UnleashedRecomp/config.h b/UnleashedRecomp/config.h index d59c051..12099d2 100644 --- a/UnleashedRecomp/config.h +++ b/UnleashedRecomp/config.h @@ -28,7 +28,12 @@ public: CONFIG_DEFINE("Video", EGraphicsAPI, GraphicsAPI, EGraphicsAPI_D3D12); CONFIG_DEFINE("Video", size_t, WindowWidth, 1280); CONFIG_DEFINE("Video", size_t, WindowHeight, 720); - CONFIG_DEFINE("Video", float, ResolutionScale, 1.0f); + + CONFIG_DEFINE_CALLBACK("Video", float, ResolutionScale, 1.0f, + { + def->Value = std::clamp(def->Value, 0.25f, 2.0f); + }); + CONFIG_DEFINE("Video", bool, Fullscreen, false); CONFIG_DEFINE("Video", bool, VSync, true); CONFIG_DEFINE("Video", size_t, BufferCount, 3); diff --git a/UnleashedRecomp/config_detail.h b/UnleashedRecomp/config_detail.h index 4702709..6759e26 100644 --- a/UnleashedRecomp/config_detail.h +++ b/UnleashedRecomp/config_detail.h @@ -4,8 +4,11 @@ #define TOML_FILE "config.toml" -#define CONFIG_DEFINE(section, type, name, defaultValue) inline static ConfigDef name{section, #name, defaultValue}; -#define CONFIG_VALUE(name) Config::name.Value +#define CONFIG_DEFINE(section, type, name, defaultValue) \ + inline static ConfigDef name{section, #name, defaultValue}; + +#define CONFIG_DEFINE_CALLBACK(section, type, name, defaultValue, readCallback) \ + inline static ConfigDef name{section, #name, defaultValue, [](ConfigDef* def) readCallback}; #define CONFIG_GET_DEFAULT(name) Config::name.DefaultValue #define CONFIG_SET_DEFAULT(name) Config::name.MakeDefault(); @@ -26,12 +29,20 @@ template class ConfigDef : public ConfigDefBase { public: - std::string Section; - std::string Name; + std::string Section{}; + std::string Name{}; T DefaultValue{}; T Value{ DefaultValue }; + std::function*)> ReadCallback; - ConfigDef(std::string section, std::string name, T defaultValue) : Section(section), Name(name), DefaultValue(defaultValue) + ConfigDef(std::string section, std::string name, T defaultValue) + : Section(section), Name(name), DefaultValue(defaultValue) + { + Config::Definitions.emplace_back(this); + } + + ConfigDef(std::string section, std::string name, T defaultValue, std::function*)> readCallback) + : Section(section), Name(name), DefaultValue(defaultValue), ReadCallback(readCallback) { Config::Definitions.emplace_back(this); } @@ -51,6 +62,9 @@ public: Value = section[Name].value_or(DefaultValue); } } + + if (ReadCallback) + ReadCallback(this); } void MakeDefault() override