diff --git a/Makefile b/Makefile index 08c53ac..23c0dc6 100644 --- a/Makefile +++ b/Makefile @@ -37,7 +37,7 @@ tracy_build: meson compile -j 10 -C builddir test: asset_build build - ./builddir/runtests "[map-sprite]" + ./builddir/runtests "[color-palette]" run: build test ifeq '$(OS)' 'Windows_NT' @@ -60,7 +60,7 @@ clean: meson compile --clean -C builddir debug_test: build - gdb --nx -x .gdbinit --ex run --args builddir/runtests -e "[map-sprite]" + gdb --nx -x .gdbinit --ex run --args builddir/runtests -e "[color-palette]" win_installer: powershell 'start "C:\Program Files (x86)\solicus\InstallForge\bin\ifbuilderenvx86.exe" scripts\win_installer.ifp' diff --git a/assets/palette.json b/assets/palette.json new file mode 100644 index 0000000..8ae68f2 --- /dev/null +++ b/assets/palette.json @@ -0,0 +1,19 @@ +{ + "gui/line": { + "light": [200,200,200], + "mid": [100,100,100], + "dark": [10,10,10] + }, + + "gui/text": { + "light": [200,200,200], + "mid": [100,100,100], + "dark": [10,10,10] + }, + + "gui/accent": { + "light": [200,200,200], + "mid": [100,100,100], + "dark": [10,10,10] + } +} diff --git a/meson.build b/meson.build index 40915a8..0a2221c 100644 --- a/meson.build +++ b/meson.build @@ -86,6 +86,7 @@ sources = [ 'autowalker.cpp', 'backend.cpp', 'battle.cpp', + 'palette.cpp', 'combat.cpp', 'components.cpp', 'config.cpp', @@ -131,6 +132,7 @@ executable('runtests', sources + [ 'tests/animation.cpp', 'tests/base.cpp', 'tests/battle.cpp', + 'tests/palette.cpp', 'tests/components.cpp', 'tests/config.cpp', 'tests/dbc.cpp', diff --git a/palette.cpp b/palette.cpp new file mode 100644 index 0000000..63c0c12 --- /dev/null +++ b/palette.cpp @@ -0,0 +1,40 @@ +#include +#include "palette.hpp" +#include "config.hpp" +#include "dbc.hpp" + +namespace palette { + using std::string; + using nlohmann::json; + + struct PaletteMgr { + std::unordered_map palettes; + }; + + static PaletteMgr COLOR; + + void init(const string &json_file) { + Config config(json_file); + json& colors = config.json(); + + for(auto [key, value_specs] : colors.items()) { + const string& base_key = key; + + for(auto [value, rgba] : value_specs.items()) { + auto color_path = base_key + ":" + value; + dbc::check(!COLOR.palettes.contains(color_path), + fmt::format("PALLETES already has a color path {}", color_path)); + + uint8_t alpha = rgba.size() == 3 ? 255 : (uint8_t)rgba[3]; + + sf::Color color{rgba[0], rgba[1], rgba[2], alpha}; + + COLOR.palettes.try_emplace(color_path, color); + } + } + } + + sf::Color get(const string& key) { + return COLOR.palettes.at(key); + } +} diff --git a/palette.hpp b/palette.hpp new file mode 100644 index 0000000..ad4a39a --- /dev/null +++ b/palette.hpp @@ -0,0 +1,10 @@ +#include +#include + +namespace palette { + using std::string; + + void init(const std::string &config="assets/palette.json"); + + sf::Color get(const string &key); +} diff --git a/tests/palette.cpp b/tests/palette.cpp new file mode 100644 index 0000000..c3d2bbd --- /dev/null +++ b/tests/palette.cpp @@ -0,0 +1,20 @@ +#include +#include +#include +#include "palette.hpp" + +using namespace fmt; + +TEST_CASE("color palette test", "[color-palette]") { + palette::init(); + sf::Color expect{10, 10, 10, 255}; + + auto gui_text = palette::get("gui/text:dark"); + REQUIRE(gui_text == expect); + + gui_text = palette::get("gui/text:mid"); + REQUIRE(gui_text != expect); + + expect = {100, 100, 100, 255}; + REQUIRE(gui_text == expect); +}