From 74a8599977385552dce16e3b648288f65d439e30 Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Tue, 13 May 2025 02:48:39 -0400 Subject: [PATCH] Fully converted to using the lel-guecs library externally now. --- Makefile | 2 +- backend.cpp | 67 ++++++++ backend.hpp | 19 +++ boss_fight_ui.hpp | 2 +- combat_ui.hpp | 2 +- components.hpp | 1 + config.cpp | 12 +- config.hpp | 5 + dbc.hpp | 4 +- debug_ui.hpp | 2 +- guecs.cpp | 354 ------------------------------------------- guecs.hpp | 340 ----------------------------------------- guecstra.hpp | 3 +- lel.cpp | 117 -------------- lel_parser.cpp | 261 ------------------------------- lel_parser.rl | 66 -------- main.cpp | 6 +- main_ui.hpp | 2 +- map_view.hpp | 2 +- meson.build | 7 +- mini_map.hpp | 2 +- overlay_ui.hpp | 4 +- ritual_ui.cpp | 3 +- ritual_ui.hpp | 2 +- status_ui.cpp | 2 +- status_ui.hpp | 2 +- tests/guecs.cpp | 32 ---- tilemap.cpp | 1 + worldbuilder.hpp | 2 +- wraps/lel-guecs.wrap | 9 ++ 30 files changed, 139 insertions(+), 1194 deletions(-) create mode 100644 backend.cpp create mode 100644 backend.hpp delete mode 100644 guecs.cpp delete mode 100644 guecs.hpp delete mode 100644 lel.cpp delete mode 100644 lel_parser.cpp delete mode 100644 lel_parser.rl delete mode 100644 tests/guecs.cpp create mode 100644 wraps/lel-guecs.wrap diff --git a/Makefile b/Makefile index 9e0fc75..6b74d7b 100644 --- a/Makefile +++ b/Makefile @@ -10,7 +10,7 @@ endif %.cpp : %.rl ragel -o $@ $< -build: lel_parser.cpp +build: meson compile -j 10 -C builddir release_build: diff --git a/backend.cpp b/backend.cpp new file mode 100644 index 0000000..aa4692a --- /dev/null +++ b/backend.cpp @@ -0,0 +1,67 @@ +#include "backend.hpp" +#include "shaders.hpp" +#include "sound.hpp" +#include "textures.hpp" +#include "config.hpp" + +namespace sfml { + guecs::SpriteTexture Backend::texture_get(const string& name) { + auto sp = textures::get(name); + return {sp.sprite, sp.texture}; + } + + Backend::Backend() { + sound::init(); + shaders::init(); + textures::init(); + } + + void Backend::sound_play(const string& name) { + sound::play(name); + } + + void Backend::sound_stop(const string& name) { + sound::stop(name); + } + + std::shared_ptr Backend::shader_get(const std::string& name) { + return shaders::get(name); + } + + bool Backend::shader_updated() { + if(shaders::updated($shaders_version)) { + $shaders_version = shaders::version(); + return true; + } else { + return false; + } + } + + guecs::Theme Backend::theme() { + guecs::Theme theme { + .BLACK={0, 0, 0}, + .DARK_DARK={10, 10, 10}, + .DARK_MID={30, 30, 30}, + .DARK_LIGHT={60, 60, 60}, + .MID={100, 100, 100}, + .LIGHT_DARK={150, 150, 150}, + .LIGHT_MID={200, 200, 200}, + .LIGHT_LIGHT={230, 230, 230}, + .WHITE={255, 255, 255}, + .TRANSPARENT = sf::Color::Transparent + }; + + theme.PADDING = 3; + theme.BORDER_PX = 1; + theme.TEXT_SIZE = 20; + theme.LABEL_SIZE = 20; + theme.FILL_COLOR = theme.DARK_DARK; + theme.TEXT_COLOR = theme.LIGHT_LIGHT; + theme.BG_COLOR = theme.MID; + theme.BORDER_COLOR = theme.LIGHT_DARK; + theme.BG_COLOR_DARK = theme.BLACK; + theme.FONT_FILE_NAME = Config::path_to("assets/text.otf").string(); + + return theme; + } +} diff --git a/backend.hpp b/backend.hpp new file mode 100644 index 0000000..fed18f8 --- /dev/null +++ b/backend.hpp @@ -0,0 +1,19 @@ +#include "guecs/ui.hpp" + +namespace sfml { + using std::string; + + class Backend : public guecs::Backend { + int $shaders_version = 0; + + public: + + Backend(); + guecs::SpriteTexture texture_get(const string& name); + void sound_play(const string& name); + void sound_stop(const string& name); + std::shared_ptr shader_get(const std::string& name); + bool shader_updated(); + guecs::Theme theme(); + }; +} diff --git a/boss_fight_ui.hpp b/boss_fight_ui.hpp index 3fbe208..447c719 100644 --- a/boss_fight_ui.hpp +++ b/boss_fight_ui.hpp @@ -1,7 +1,7 @@ #pragma once #include #include -#include "guecs.hpp" +#include #include "textures.hpp" #include "components.hpp" #include diff --git a/combat_ui.hpp b/combat_ui.hpp index be70f4f..96f2cb8 100644 --- a/combat_ui.hpp +++ b/combat_ui.hpp @@ -2,7 +2,7 @@ #include "levelmanager.hpp" #include #include -#include "guecs.hpp" +#include #include "events.hpp" namespace gui { diff --git a/components.hpp b/components.hpp index 90938cf..696d87e 100644 --- a/components.hpp +++ b/components.hpp @@ -13,6 +13,7 @@ #include "goap.hpp" namespace components { + using std::string; using namespace nlohmann; struct SpriteEffect { diff --git a/config.cpp b/config.cpp index ea3ef62..d6478d8 100644 --- a/config.cpp +++ b/config.cpp @@ -5,8 +5,10 @@ using nlohmann::json; using fmt::format; +std::filesystem::path Config::BASE_DIR{"."}; + Config::Config(const std::string src_path) : $src_path(src_path) { - std::ifstream infile($src_path); + std::ifstream infile(Config::path_to($src_path)); $config = json::parse(infile); } @@ -33,3 +35,11 @@ std::vector Config::keys() { return the_fucking_keys; } + +void Config::set_base_dir(const char *optarg) { + Config::BASE_DIR.assign(optarg); +} + +std::filesystem::path Config::path_to(const std::string& path) { + return Config::BASE_DIR / path; +} diff --git a/config.hpp b/config.hpp index 52bf664..2ca8d52 100644 --- a/config.hpp +++ b/config.hpp @@ -2,8 +2,10 @@ #include #include #include +#include struct Config { + static std::filesystem::path BASE_DIR; nlohmann::json $config; std::string $src_path; @@ -16,4 +18,7 @@ struct Config { nlohmann::json &json() { return $config; }; std::wstring wstring(const std::string main_key, const std::string sub_key); std::vector keys(); + + static void set_base_dir(const char *optarg); + static std::filesystem::path path_to(const std::string& path); }; diff --git a/dbc.hpp b/dbc.hpp index 87e4fb0..b85deda 100644 --- a/dbc.hpp +++ b/dbc.hpp @@ -5,9 +5,9 @@ #include #include -using std::string; - namespace dbc { + using std::string; + class Error { public: const string message; diff --git a/debug_ui.hpp b/debug_ui.hpp index 04adb26..3730024 100644 --- a/debug_ui.hpp +++ b/debug_ui.hpp @@ -2,7 +2,7 @@ #include "levelmanager.hpp" #include #include -#include "guecs.hpp" +#include #include "stats.hpp" namespace gui { diff --git a/guecs.cpp b/guecs.cpp deleted file mode 100644 index 22dba4b..0000000 --- a/guecs.cpp +++ /dev/null @@ -1,354 +0,0 @@ -#include "guecs.hpp" -#include "shaders.hpp" -#include "sound.hpp" -#include "textures.hpp" -#include - -namespace guecs { - using std::make_shared; - - void Textual::init(lel::Cell &cell, shared_ptr font_ptr) { - dbc::check(font_ptr != nullptr, "you failed to initialize this WideText"); - if(font == nullptr) font = font_ptr; - if(text == nullptr) text = make_shared(*font, content, size); - text->setFillColor(color); - - if(centered) { - auto bounds = text->getLocalBounds(); - auto text_cell = lel::center(bounds.size.x, bounds.size.y, cell); - // this stupid / 2 is because SFML renders from baseline rather than from the claimed bounding box - text->setPosition({float(text_cell.x), float(text_cell.y) - text_cell.h / 2}); - } else { - text->setPosition({float(cell.x + padding * 2), float(cell.y + padding * 2)}); - } - - text->setCharacterSize(size); - } - - void Textual::update(const wstring& new_content) { - content = new_content; - text->setString(content); - } - - void Sprite::update(const string& new_name) { - if(new_name != name) { - name = new_name; - auto sprite_texture = textures::get(name); - sprite->setTexture(*sprite_texture.texture); - sprite->setTextureRect(sprite_texture.sprite->getTextureRect()); - } - } - - void Sprite::init(lel::Cell &cell) { - auto sprite_texture = textures::get(name); - - sprite = make_shared( - *sprite_texture.texture, - sprite_texture.sprite->getTextureRect()); - - sprite->setPosition({ - float(cell.x + padding), - float(cell.y + padding)}); - - auto bounds = sprite->getLocalBounds(); - - sprite->setScale({ - float(cell.w - padding * 2) / bounds.size.x, - float(cell.h - padding * 2) / bounds.size.y}); - } - - void Rectangle::init(lel::Cell& cell) { - sf::Vector2f size{float(cell.w) - padding * 2, float(cell.h) - padding * 2}; - if(shape == nullptr) shape = make_shared(size); - shape->setPosition({float(cell.x + padding), float(cell.y + padding)}); - shape->setFillColor(color); - shape->setOutlineColor(border_color); - shape->setOutlineThickness(border_px); - } - - - void Meter::init(lel::Cell& cell) { - bar.init(cell); - } - - void Meter::render(lel::Cell& cell) { - float level = std::clamp(percent, 0.0f, 1.0f) * float(cell.w); - // ZED: this 6 is a border width, make it a thing - bar.shape->setSize({std::max(level, 0.0f), float(cell.h - 6)}); - } - - void Sound::play(bool hover) { - if(!hover) { - sound::play(on_click); - } - } - - void Sound::stop(bool hover) { - if(!hover) { - sound::stop(on_click); - } - } - - void Background::init() { - sf::Vector2f size{float(w), float(h)}; - if(shape == nullptr) shape = make_shared(size); - shape->setPosition({float(x), float(y)}); - shape->setFillColor(color); - } - - void Effect::init(lel::Cell &cell) { - $shader_version = shaders::version(); - $shader = shaders::get(name); - $shader->setUniform("u_resolution", sf::Vector2f({float(cell.w), float(cell.h)})); - $clock = std::make_shared(); - } - - void Effect::step() { - sf::Time cur_time = $clock->getElapsedTime(); - float u_time = cur_time.asSeconds(); - - if(u_time < $u_time_end) { - $shader->setUniform("u_duration", duration); - $shader->setUniform("u_time_end", $u_time_end); - $shader->setUniform("u_time", u_time); - } else { - $active = false; - } - } - - void Effect::run() { - $active = true; - sf::Time u_time = $clock->getElapsedTime(); - $u_time_end = u_time.asSeconds() + duration; - } - - shared_ptr Effect::checkout_ptr() { - if(shaders::updated($shader_version)) { - $shader = shaders::get(name); - $shader_version = shaders::version(); - } - - return $shader; - } - - void Effect::stop() { - $active = false; - } - - UI::UI() { - $font = make_shared(FONT_FILE_NAME); - } - - void UI::position(int x, int y, int width, int height) { - $parser.position(x, y, width, height); - } - - sf::Vector2f UI::get_position() { - return {float($parser.grid_x), float($parser.grid_y)}; - } - - sf::Vector2f UI::get_size() { - return {float($parser.grid_w), float($parser.grid_h)}; - } - - void UI::layout(const string& grid) { - $grid = grid; - bool good = $parser.parse($grid); - dbc::check(good, "LEL parsing failed."); - - for(auto& [name, cell] : $parser.cells) { - auto ent = init_entity(name); - set(ent, cell); - } - } - - Entity UI::init_entity(const string& name) { - auto ent = entity(); - // this lets you look up an entity by name - $name_ents.insert_or_assign(name, ent); - // this makes it easier to get the name during querying - set(ent, {name}); - return ent; - } - - Entity UI::entity(const string& name) { - dbc::check($name_ents.contains(name), - fmt::format("GUECS entity {} does not exist. Mispelled cell name?", name)); - return $name_ents.at(name); - } - - Entity UI::entity(const string& name, int id) { - return entity(fmt::format("{}{}", name, id)); - } - - void UI::init() { - query([](auto, auto& bg) { - bg.init(); - }); - - query([](auto, auto& cell, auto& rect) { - rect.init(cell); - }); - - query([](auto, auto& cell, auto& shader) { - shader.init(cell); - }); - - query([](auto, auto& bg, auto &meter) { - bg.shape->setFillColor(meter.color); - }); - - query([](auto, auto &cell, auto& meter) { - meter.init(cell); - }); - - query([this](auto, auto& cell, auto& text) { - text.init(cell, $font); - }); - - query([this](auto, auto& cell, auto& text) { - text.init(cell, $font); - }); - - query([&](auto, auto &cell, auto &sprite) { - sprite.init(cell); - }); - } - - void UI::debug_layout(sf::RenderWindow& window) { - query([&](const auto, auto &cell) { - sf::RectangleShape rect{{float(cell.w), float(cell.h)}}; - rect.setPosition({float(cell.x), float(cell.y)}); - rect.setFillColor(sf::Color::Transparent); - rect.setOutlineColor(sf::Color::Red); - rect.setOutlineThickness(2.0f); - window.draw(rect); - }); - } - - void UI::render(sf::RenderWindow& window) { - if(auto bg = get_if(MAIN)) { - window.draw(*(bg->shape)); - } - - query([&](auto, auto& shader) { - if(shader.$active) shader.step(); - }); - - query([&](auto ent, auto& rect) { - render_helper(window, ent, true, rect.shape); - }); - - query([&](auto ent, auto& cell, auto &meter) { - meter.render(cell); - render_helper(window, ent, true, meter.bar.shape); - }); - - query([&](auto ent, auto& sprite) { - render_helper(window, ent, false, sprite.sprite); - }); - - query