config: implement read callbacks

This commit is contained in:
Hyper 2024-10-21 18:20:45 +01:00
parent 438a315e5b
commit dd59190347
3 changed files with 25 additions and 8 deletions

View file

@ -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()

View file

@ -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);

View file

@ -4,8 +4,11 @@
#define TOML_FILE "config.toml"
#define CONFIG_DEFINE(section, type, name, defaultValue) inline static ConfigDef<type> name{section, #name, defaultValue};
#define CONFIG_VALUE(name) Config::name.Value
#define CONFIG_DEFINE(section, type, name, defaultValue) \
inline static ConfigDef<type> name{section, #name, defaultValue};
#define CONFIG_DEFINE_CALLBACK(section, type, name, defaultValue, readCallback) \
inline static ConfigDef<type> name{section, #name, defaultValue, [](ConfigDef<type>* def) readCallback};
#define CONFIG_GET_DEFAULT(name) Config::name.DefaultValue
#define CONFIG_SET_DEFAULT(name) Config::name.MakeDefault();
@ -26,12 +29,20 @@ template<typename T>
class ConfigDef : public ConfigDefBase
{
public:
std::string Section;
std::string Name;
std::string Section{};
std::string Name{};
T DefaultValue{};
T Value{ DefaultValue };
std::function<void(ConfigDef<T>*)> 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<void(ConfigDef<T>*)> 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