Config: implemented toml reading

This commit is contained in:
Hyper 2024-10-17 21:20:35 +01:00
parent 554be01412
commit ee35458b5d
7 changed files with 121 additions and 21 deletions

View file

@ -7,6 +7,14 @@ add_compile_definitions(SDL_MAIN_HANDLED)
# Microsoft wtf?
add_compile_definitions(_DISABLE_CONSTEXPR_MUTEX_CONSTRUCTOR)
# Copy default config to build directory.
file(INSTALL
DESTINATION "${CMAKE_BINARY_DIR}/${PROJECT_NAME}"
TYPE FILE
FILES "res/config.toml"
RENAME "${TARGET_NAME}.toml"
)
BIN2H(SOURCE_FILE "res/icon.bmp" HEADER_FILE "res/icon.h" ARRAY_TYPE "unsigned char" VARIABLE_NAME "g_icon")
set(SWA_PRECOMPILED_HEADERS
@ -48,6 +56,7 @@ set(SWA_UI_CXX_SOURCES
)
set(SWA_CXX_SOURCES
"config.cpp"
"game.cpp"
"main.cpp"
"misc_impl.cpp"

View file

@ -0,0 +1,47 @@
void Config::Load()
{
auto toml = toml::parse_file(TOML_FILE);
TOML_BEGIN_SECTION("System")
{
TOML_READ_ENUM(ELanguage, Language);
TOML_READ_ENUM(EScoreBehaviour, ScoreBehaviour);
TOML_READ_BOOLEAN(Hints);
TOML_READ_BOOLEAN(WerehogHubTransformVideo);
}
TOML_END_SECTION();
TOML_BEGIN_SECTION("Controls")
{
TOML_READ_BOOLEAN(XButtonHoming);
TOML_READ_BOOLEAN(UnleashCancel);
}
TOML_END_SECTION();
TOML_BEGIN_SECTION("Audio")
{
TOML_READ_BOOLEAN(WerehogBattleMusic);
}
TOML_END_SECTION();
TOML_BEGIN_SECTION("Video")
{
TOML_READ_INTEGER(WindowWidth);
TOML_READ_INTEGER(WindowHeight);
TOML_READ_FLOAT(ResolutionScale);
TOML_READ_INTEGER(ShadowResolution);
TOML_READ_INTEGER(MSAA);
TOML_READ_ENUM(EMovieScaleMode, MovieScaleMode);
TOML_READ_ENUM(EUIScaleMode, UIScaleMode);
TOML_READ_BOOLEAN(AlphaToCoverage);
TOML_READ_BOOLEAN(Fullscreen);
TOML_READ_BOOLEAN(VSync);
TOML_READ_INTEGER(BufferCount);
}
TOML_END_SECTION();
}
void Config::Save()
{
// TODO
}

View file

@ -1,18 +1,17 @@
#pragma once
// TODO (Hyper): use toml.
// TODO: move this outside of the game directory?
#define TOML_FILE "SWA.toml"
#define INI_FILE "SWA.ini"
#define TOML_BEGIN_SECTION(name) if (auto pSection = toml[name].as_table()) { const auto& section = *pSection;
#define TOML_END_SECTION() }
#define INI_BEGIN_SECTION(section) { std::string CurrentSection = section;
#define INI_END_SECTION() }
#define INI_READ_STRING(var) var = BasicIni::Get(ini, CurrentSection, #var, var)
#define INI_READ_BOOLEAN(var) var = BasicIni::GetBoolean(ini, CurrentSection, #var, var)
#define INI_READ_FLOAT(var) var = BasicIni::GetFloat(ini, CurrentSection, #var, var)
#define INI_READ_INTEGER(var) var = BasicIni::GetInteger(ini, CurrentSection, #var, var)
#define INI_READ_DOUBLE(var) var = BasicIni::GetDouble(ini, CurrentSection, #var, var)
#define INI_READ_ENUM(type, var) var = (type)BasicIni::GetInteger(ini, CurrentSection, #var, var)
#define TOML_READ_STRING(var) var = section[#var].value_or<std::string>("");
#define TOML_READ_BOOLEAN(var) var = section[#var].value_or(false);
#define TOML_READ_FLOAT(var) var = section[#var].value_or(0.0f);
#define TOML_READ_INTEGER(var) var = section[#var].value_or(0);
#define TOML_READ_DOUBLE(var) var = section[#var].value_or(0.0);
#define TOML_READ_ENUM(type, var) var = (type)section[#var].value_or(0);
enum ELanguage : uint32_t
{
@ -52,7 +51,6 @@ public:
inline static EScoreBehaviour ScoreBehaviour = EScoreBehaviour_CheckpointReset;
inline static bool Hints = true;
inline static bool WerehogHubTransformVideo = true;
inline static bool BootToTitle = false;
// Controls
inline static bool XButtonHoming = true;
@ -62,8 +60,9 @@ public:
inline static bool WerehogBattleMusic = true;
// Video
inline static uint32_t Width = 1280;
inline static uint32_t Height = 720;
inline static uint32_t WindowWidth = 1280;
inline static uint32_t WindowHeight = 720;
inline static float ResolutionScale = 1.0f;
inline static int32_t ShadowResolution = 4096;
inline static size_t MSAA = 4;
inline static EMovieScaleMode MovieScaleMode = EMovieScaleMode_Fit;
@ -73,5 +72,6 @@ public:
inline static bool VSync = false;
inline static uint32_t BufferCount = 3;
static void Read();
static void Load();
static void Save();
};

View file

@ -124,6 +124,8 @@ uint32_t LdrLoadModule(const char* path)
int main()
{
Config::Load();
KiSystemStartup();
uint32_t entry = LdrLoadModule(FileSystem::TransformPath(GAME_XEX_PATH));

View file

@ -0,0 +1,37 @@
[System]
Language = 1 # The language displayed by the game.
# English = 1; Japanese = 2; German = 3; French = 4; Spanish = 5; Italian = 6.
ScoreBehaviour = 0 # Determines how the score behaves when restarting at a checkpoint.
# Reset to zero = 0; Reset to last checkpoint score = 1.
Hints = true # Determines whether to spawn hint rings and volumes.
WerehogHubTransformVideo = true # Determines whether to play the transition video for switching time of day in the hub areas.
# Setting this to false will instead play a generic transition without artificial loading times.
[Controls]
XButtonHoming = true # Determines whether to decouple the homing attack from the X button.
UnleashCancel = false # Determines whether Unleash can be cancelled by pressing the right shoulder button whilst activated.
[Audio]
WerehogBattleMusic = true # Determines whether to play the battle theme for enemy encounters as the Werehog.
[Video]
WindowWidth = 1280
WindowHeight = 720
ResolutionScale = 1.0
ShadowResolution = 4096 # The resolution of the shadow maps.
# Default = -1; otherwise, any power of two resolution (e.g. 512, 1024, 2048, 4096, 8192).
# Default will let the game determine the resolution.
MSAA = 4
MovieScaleMode = 1 # The strategy for scaling video playback (experimental).
# Stretch = 0; Fit = 1; Fill = 2.
# Fit is recommended for 21:9 or wider aspect ratios for the intro cinematic.
# Fill will scale the intro cinematic past the original aspect ratio to fit the window dimensions.
UIScaleMode = 2 # The strategy for scaling the UI (experimental).
# Stretch = 0; Edge = 1; Centre = 2.
# Corner is recommended for 21:9 or wider aspect ratios.
# Centre does not dynamically adjust to the window size until re-entering a stage.
AlphaToCoverage = true
Fullscreen = false
VSync = false
BufferCount = 3 # Double buffering = 2; Triple buffering = 3.
# Triple buffering can increase FPS and improve frame pacing at the expense of higher input latency.

View file

@ -87,7 +87,12 @@ void Window::Init()
SDL_EventState(SDL_SYSWMEVENT, SDL_ENABLE);
SDL_AddEventWatch(Window_OnSDLEvent, s_window);
s_window = SDL_CreateWindow(title, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, Window::s_width, Window::s_height, SDL_WINDOW_RESIZABLE);
s_window = SDL_CreateWindow(title,
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
Config::WindowWidth,
Config::WindowHeight,
SDL_WINDOW_RESIZABLE);
if (auto icon = GetIconSurface())
{

View file

@ -29,8 +29,8 @@ public:
static bool IsDisplayResolution(int w, int h, bool isExact = true)
{
auto width = w <= 0 ? Config::Width : w;
auto height = h <= 0 ? Config::Height : h;
auto width = w <= 0 ? Config::WindowWidth : w;
auto height = h <= 0 ? Config::WindowHeight : h;
SDL_Rect displayRect;
if (SDL_GetDisplayBounds(SDL_GetWindowDisplayIndex(s_window), &displayRect) == ERROR_SUCCESS)
@ -73,7 +73,7 @@ public:
if (SDL_GetDisplayBounds(SDL_GetWindowDisplayIndex(s_window), &displayRect) == ERROR_SUCCESS)
{
// Maximise window if the config resolution is greater than the display.
if (IsDisplayResolution(Config::Width, Config::Height, false))
if (IsDisplayResolution(s_width, s_height, false))
SDL_MaximizeWindow(s_window);
}
@ -83,8 +83,8 @@ public:
static void SetWindowDimensions(int w = -1, int h = -1)
{
auto width = w <= 0 ? Config::Width : w;
auto height = h <= 0 ? Config::Height : h;
auto width = w <= 0 ? Config::WindowWidth : w;
auto height = h <= 0 ? Config::WindowHeight : h;
auto isPendingMaximise = false;
if (IsDisplayResolution(width, height))