Now have a simple color palette system.
This commit is contained in:
parent
0272ba8540
commit
48a7f72411
6 changed files with 93 additions and 2 deletions
4
Makefile
4
Makefile
|
@ -37,7 +37,7 @@ tracy_build:
|
||||||
meson compile -j 10 -C builddir
|
meson compile -j 10 -C builddir
|
||||||
|
|
||||||
test: asset_build build
|
test: asset_build build
|
||||||
./builddir/runtests "[map-sprite]"
|
./builddir/runtests "[color-palette]"
|
||||||
|
|
||||||
run: build test
|
run: build test
|
||||||
ifeq '$(OS)' 'Windows_NT'
|
ifeq '$(OS)' 'Windows_NT'
|
||||||
|
@ -60,7 +60,7 @@ clean:
|
||||||
meson compile --clean -C builddir
|
meson compile --clean -C builddir
|
||||||
|
|
||||||
debug_test: build
|
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:
|
win_installer:
|
||||||
powershell 'start "C:\Program Files (x86)\solicus\InstallForge\bin\ifbuilderenvx86.exe" scripts\win_installer.ifp'
|
powershell 'start "C:\Program Files (x86)\solicus\InstallForge\bin\ifbuilderenvx86.exe" scripts\win_installer.ifp'
|
||||||
|
|
19
assets/palette.json
Normal file
19
assets/palette.json
Normal file
|
@ -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]
|
||||||
|
}
|
||||||
|
}
|
|
@ -86,6 +86,7 @@ sources = [
|
||||||
'autowalker.cpp',
|
'autowalker.cpp',
|
||||||
'backend.cpp',
|
'backend.cpp',
|
||||||
'battle.cpp',
|
'battle.cpp',
|
||||||
|
'palette.cpp',
|
||||||
'combat.cpp',
|
'combat.cpp',
|
||||||
'components.cpp',
|
'components.cpp',
|
||||||
'config.cpp',
|
'config.cpp',
|
||||||
|
@ -131,6 +132,7 @@ executable('runtests', sources + [
|
||||||
'tests/animation.cpp',
|
'tests/animation.cpp',
|
||||||
'tests/base.cpp',
|
'tests/base.cpp',
|
||||||
'tests/battle.cpp',
|
'tests/battle.cpp',
|
||||||
|
'tests/palette.cpp',
|
||||||
'tests/components.cpp',
|
'tests/components.cpp',
|
||||||
'tests/config.cpp',
|
'tests/config.cpp',
|
||||||
'tests/dbc.cpp',
|
'tests/dbc.cpp',
|
||||||
|
|
40
palette.cpp
Normal file
40
palette.cpp
Normal file
|
@ -0,0 +1,40 @@
|
||||||
|
#include <fmt/core.h>
|
||||||
|
#include "palette.hpp"
|
||||||
|
#include "config.hpp"
|
||||||
|
#include "dbc.hpp"
|
||||||
|
|
||||||
|
namespace palette {
|
||||||
|
using std::string;
|
||||||
|
using nlohmann::json;
|
||||||
|
|
||||||
|
struct PaletteMgr {
|
||||||
|
std::unordered_map<string, sf::Color> 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);
|
||||||
|
}
|
||||||
|
}
|
10
palette.hpp
Normal file
10
palette.hpp
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
#include <string>
|
||||||
|
#include <SFML/Graphics/Color.hpp>
|
||||||
|
|
||||||
|
namespace palette {
|
||||||
|
using std::string;
|
||||||
|
|
||||||
|
void init(const std::string &config="assets/palette.json");
|
||||||
|
|
||||||
|
sf::Color get(const string &key);
|
||||||
|
}
|
20
tests/palette.cpp
Normal file
20
tests/palette.cpp
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
#include <catch2/catch_test_macros.hpp>
|
||||||
|
#include <fmt/core.h>
|
||||||
|
#include <string>
|
||||||
|
#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);
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue