diff options
author | Loek Le Blansch <loek@pipeframe.xyz> | 2024-12-09 11:22:35 +0100 |
---|---|---|
committer | Loek Le Blansch <loek@pipeframe.xyz> | 2024-12-09 11:22:35 +0100 |
commit | b845376e270c060730d4f8b9b0946a63908871da (patch) | |
tree | da4f0f33b90a4f338cd0f1c1eeaf8fd5dc8c3970 | |
parent | 3822e47f205f0fd949c64b2f4ab94aa50b1131fb (diff) |
feature_config
-rw-r--r-- | src/crepe/api/Config.h | 50 | ||||
-rw-r--r-- | src/crepe/facade/SDLContext.cpp | 6 | ||||
-rw-r--r-- | src/doc/feature/config.dox | 61 | ||||
-rw-r--r-- | src/doc/features.dox | 2 | ||||
-rw-r--r-- | src/test/main.cpp | 11 |
5 files changed, 87 insertions, 43 deletions
diff --git a/src/crepe/api/Config.h b/src/crepe/api/Config.h index a9745c3..e762d89 100644 --- a/src/crepe/api/Config.h +++ b/src/crepe/api/Config.h @@ -3,34 +3,21 @@ #include <string> #include "../util/Log.h" - -#include "types.h" +#include "../types.h" namespace crepe { /** * \brief Global configuration interface * - * This class stores engine default settings. Properties on this class are only supposed to be - * modified *before* execution is handed over from the game programmer to the engine (i.e. the - * main loop is started). + * This struct stores both engine default settings and global configuration parameters. */ -class Config final { -public: +struct Config { //! Retrieve handle to global Config instance static Config & get_instance(); -private: - Config() = default; - ~Config() = default; - Config(const Config &) = default; - Config(Config &&) = default; - Config & operator=(const Config &) = default; - Config & operator=(Config &&) = default; - -public: //! Logging-related settings - struct { + struct log { // NOLINT /** * \brief Log level * @@ -38,7 +25,7 @@ public: */ Log::Level level = Log::Level::INFO; /** - * \brief Colored log output + * \brief Enable colored log output * * Enables log coloring using ANSI escape codes. */ @@ -46,7 +33,7 @@ public: } log; //! Save manager - struct { + struct savemgr { // NOLINT /** * \brief Save file location * @@ -56,25 +43,22 @@ public: std::string location = "save.crepe.db"; } savemgr; - //! physics-related settings - struct { - /** - * \brief gravity value of physics system - * - * Gravity value of game. - */ + //! Physics-related settings + struct physics { // NOLINT + //! Gravity value of physics system double gravity = 1; } physics; - //! default window settings - struct { - //! default screen size in pixels - ivec2 default_size = {1280, 720}; - std::string window_title = "Jetpack joyride clone"; - } window_settings; + //! Default window settings + struct window { // NOLINT + //! Default window size (in pixels) + ivec2 size = {1280, 720}; + //! Default window title + std::string title = "Jetpack joyride clone"; + } window; //! Asset loading options - struct { + struct asset { // NOLINT /** * \brief Pattern to match for Asset base directory * diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index 4cc2206..223dd11 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -39,10 +39,10 @@ SDLContext::SDLContext() { throw runtime_error(format("SDLContext: SDL_Init error: {}", SDL_GetError())); } - auto & cfg = Config::get_instance().window_settings; + auto & cfg = Config::get_instance().window; SDL_Window * tmp_window - = SDL_CreateWindow(cfg.window_title.c_str(), SDL_WINDOWPOS_CENTERED, - SDL_WINDOWPOS_CENTERED, cfg.default_size.x, cfg.default_size.y, 0); + = SDL_CreateWindow(cfg.title.c_str(), SDL_WINDOWPOS_CENTERED, + SDL_WINDOWPOS_CENTERED, cfg.size.x, cfg.size.y, 0); if (!tmp_window) { throw runtime_error(format("SDLContext: SDL_Window error: {}", SDL_GetError())); } diff --git a/src/doc/feature/config.dox b/src/doc/feature/config.dox new file mode 100644 index 0000000..85d6803 --- /dev/null +++ b/src/doc/feature/config.dox @@ -0,0 +1,61 @@ +// vim:ft=doxygen +namespace crepe { +/** + +\defgroup feature_config Engine configuration +\ingroup feature +\brief Configure default values and global options + +Default values and options that apply to the engine globally are read from a +singleton struct named Config. + +\see Config + +\par Example + +Configuration options may be set individually or by assigning a [designated +initializer list][desginit]. All of Config's members have default values and can +safely be omitted from initializer lists. + +[desginit]: https://en.cppreference.com/w/cpp/language/aggregate_initialization#Designated_initializers + +```cpp +#include <crepe/api/Config.h> + +int main() { + auto & config = crepe::Config::get_instance(); + + // Designated initializer list + config = { + // specify options here + }; + + // Reset default options + config = {}; + + // Set specific option + config.log.color = false; +} +``` + +\par Options + +\noop Display config properties in monospace font +\htmlonly +<style> +tr td:first-child { font-family: monospace; } +</style> +\endhtmlonly + +|Option|Description| +|-|-| +|\ref Config::asset::root_pattern ".asset.root_pattern"|\copybrief Config::asset::root_pattern| +|\ref Config::log::color ".log.color"|\copybrief Config::log::color| +|\ref Config::log::level ".log.level"|\copybrief Config::log::level| +|\ref Config::physics::gravity ".physics.gravity"|\copybrief Config::physics::gravity| +|\ref Config::savemgr::location ".savemgr.location"|\copybrief Config::savemgr::location| +|\ref Config::window::size ".window.size"|\copybrief Config::window::size| +|\ref Config::window::title ".window.title"|\copybrief Config::window::title| + +*/ +} diff --git a/src/doc/features.dox b/src/doc/features.dox index 7d20ccb..3bcbffe 100644 --- a/src/doc/features.dox +++ b/src/doc/features.dox @@ -21,7 +21,7 @@ feature. - Basics - \todo Hello world / engine initialization - - \todo Engine configuration options + - \ref feature_config \n\copybrief feature_config - Scenes - \ref feature_scene \n\copybrief feature_scene diff --git a/src/test/main.cpp b/src/test/main.cpp index aece72d..0e1bc75 100644 --- a/src/test/main.cpp +++ b/src/test/main.cpp @@ -1,8 +1,5 @@ #include <gtest/gtest.h> -#define protected public -#define private public - #include <crepe/api/Config.h> using namespace crepe; @@ -11,12 +8,14 @@ using namespace testing; class GlobalConfigReset : public EmptyTestEventListener { public: Config & cfg = Config::get_instance(); - Config cfg_default = Config(); // This function is called before each test void OnTestStart(const TestInfo &) override { - cfg = cfg_default; - cfg.log.level = Log::Level::WARNING; + cfg = { + .log = { + .level = Log::Level::WARNING, + }, + }; } }; |