From ac22a11c9f3c9c0ed10e0f31cf61edd5d4ffb3b9 Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Sat, 10 May 2025 11:06:38 -0400 Subject: [PATCH] Now mostly working with GUECS but shaders are still busted. Have to find out why they stopped working. --- Makefile | 5 +- backend.cpp | 66 +++++++++ backend.hpp | 19 +++ color.hpp | 15 -- constants.hpp | 10 -- dbc.hpp | 3 +- game_engine.cpp | 6 +- game_engine.hpp | 4 +- guecs.cpp | 316 ------------------------------------------ guecs.hpp | 233 ------------------------------- gui.cpp | 11 +- gui.hpp | 6 +- lel.cpp | 117 ---------------- lel.hpp | 57 -------- lel_parser.cpp | 263 ----------------------------------- lel_parser.rl | 68 --------- main.cpp | 29 ++-- meson.build | 11 +- shaders.cpp | 10 +- shaders.hpp | 1 - sound.cpp | 11 +- tests/dbc.cpp | 39 ------ tests/game_engine.cpp | 4 +- textures.cpp | 43 +----- textures.hpp | 10 +- wraps/lel-guecs.wrap | 9 ++ wraps/sfml.wrap | 4 +- 27 files changed, 161 insertions(+), 1209 deletions(-) create mode 100644 backend.cpp create mode 100644 backend.hpp delete mode 100644 color.hpp delete mode 100644 guecs.cpp delete mode 100644 guecs.hpp delete mode 100644 lel.cpp delete mode 100644 lel.hpp delete mode 100644 lel_parser.cpp delete mode 100644 lel_parser.rl delete mode 100644 tests/dbc.cpp create mode 100644 wraps/lel-guecs.wrap diff --git a/Makefile b/Makefile index 2c3678b..728e302 100644 --- a/Makefile +++ b/Makefile @@ -6,10 +6,7 @@ reset: patch: powershell "cp ./patches/process.h ./subprojects/libgit2-1.9.0/src/util/process.h" -%.cpp : %.rl - ragel -o $@ $< - -build: lel_parser.cpp +build: meson compile -C builddir config: diff --git a/backend.cpp b/backend.cpp new file mode 100644 index 0000000..8330a6a --- /dev/null +++ b/backend.cpp @@ -0,0 +1,66 @@ +#include "backend.hpp" +#include "shaders.hpp" +#include "sound.hpp" +#include "textures.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={1, 4, 2}, + .DARK_DARK={9, 29, 16}, + .DARK_MID={14, 50, 26}, + .DARK_LIGHT={0, 109, 44}, + .MID={63, 171, 92}, + .LIGHT_DARK={161, 217, 155}, + .LIGHT_MID={199, 233, 192}, + .LIGHT_LIGHT={229, 245, 224}, + .WHITE={255, 255, 255}, + .TRANSPARENT = sf::Color::Transparent + }; + + theme.PADDING = 3; + theme.BORDER_PX = 1; + theme.TEXT_SIZE = 40; + theme.LABEL_SIZE = 40; + 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 = "assets/text.ttf"; + + 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/color.hpp b/color.hpp deleted file mode 100644 index 2998737..0000000 --- a/color.hpp +++ /dev/null @@ -1,15 +0,0 @@ -#pragma once -#include - -namespace ColorValue { - const sf::Color BLACK{1, 4, 2}; - const sf::Color DARK_DARK{9, 29, 16}; - const sf::Color DARK_MID{14, 50, 26}; - const sf::Color DARK_LIGHT{0, 109, 44}; - const sf::Color MID{63, 171, 92}; - const sf::Color LIGHT_DARK{161, 217, 155}; - const sf::Color LIGHT_MID{199, 233, 192}; - const sf::Color LIGHT_LIGHT{229, 245, 224}; - const sf::Color WHITE{255, 255, 255}; - const sf::Color TRANSPARENT = sf::Color::Transparent; -} diff --git a/constants.hpp b/constants.hpp index cff894f..14e06e5 100644 --- a/constants.hpp +++ b/constants.hpp @@ -1,7 +1,6 @@ #pragma once #include -#include "color.hpp" #include constexpr const int SCREEN_WIDTH= 1920 / 2; @@ -10,15 +9,6 @@ constexpr const int SCREEN_HEIGHT= 1080 / 2; constexpr const bool VSYNC=false; constexpr const int FRAME_LIMIT=60; -constexpr const int GUECS_PADDING = 3; -constexpr const int GUECS_BORDER_PX = 1; -constexpr const int GUECS_FONT_SIZE = 40; -const sf::Color GUECS_FILL_COLOR = ColorValue::DARK_DARK; -const sf::Color GUECS_TEXT_COLOR = ColorValue::LIGHT_LIGHT; -const sf::Color GUECS_BG_COLOR = ColorValue::BLACK; -const sf::Color GUECS_BORDER_COLOR = ColorValue::MID; -constexpr const char *FONT_FILE_NAME="assets/text.ttf"; - #ifdef NDEBUG constexpr const bool DEBUG_BUILD=false; #else diff --git a/dbc.hpp b/dbc.hpp index 87e4fb0..d090503 100644 --- a/dbc.hpp +++ b/dbc.hpp @@ -5,9 +5,10 @@ #include #include -using std::string; namespace dbc { + using std::string; + class Error { public: const string message; diff --git a/game_engine.cpp b/game_engine.cpp index 2dffd65..10f4563 100644 --- a/game_engine.cpp +++ b/game_engine.cpp @@ -12,8 +12,10 @@ const auto ERROR = fmt::emphasis::bold | fg(fmt::color::red); using namespace std; -GameEngine::GameEngine(int hp) : starting_hp(hp) { - hit_points = max_hp(); +GameEngine::GameEngine(int hp, int max_hp) : + hit_points(hp), starting_hp(max_hp) +{ + } int GameEngine::determine_damage(string &type) { diff --git a/game_engine.hpp b/game_engine.hpp index e0d5d5b..e88d2ab 100644 --- a/game_engine.hpp +++ b/game_engine.hpp @@ -31,8 +31,8 @@ class GameEngine : DeadSimpleFSM { }; public: - int starting_hp = 0; int hit_points = 0; + int starting_hp = 0; int hits_taken = 0; int rounds = 0; int deaths = 0; @@ -40,7 +40,7 @@ class GameEngine : DeadSimpleFSM { float hp_bonus = 1.0f; bool free_death = false; - GameEngine(int hp); + GameEngine(int hp, int max_hp); // FOUND BUG: I was accidentally copying this and shouldn't have been GameEngine(GameEngine &g) = delete; diff --git a/guecs.cpp b/guecs.cpp deleted file mode 100644 index 9ba642b..0000000 --- a/guecs.cpp +++ /dev/null @@ -1,316 +0,0 @@ -#include "guecs.hpp" -#include "shaders.hpp" -#include "sound.hpp" -#include "config.hpp" - -namespace guecs { - - 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(std::wstring& new_content) { - content = new_content; - text->setString(content); - } - - 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 Sound::play(bool hover) { - if(!hover) { - sound::play(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; - } - - UI::UI() { - $font = make_shared(Config::path_to(FONT_FILE_NAME)); - } - - void UI::position(int x, int y, int width, int height) { - $parser.position(x, y, width, height); - } - - void UI::layout(std::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); - $world.set(ent, cell); - } - } - - DinkyECS::Entity UI::init_entity(std::string name) { - auto entity = $world.entity(); - // this lets you look up an entity by name - $name_ents.insert_or_assign(name, entity); - // this makes it easier to get the name during querying - $world.set(entity, {name}); - return entity; - } - - DinkyECS::Entity UI::entity(std::string name) { - dbc::check($name_ents.contains(name), - fmt::format("GUECS entity {} does not exist. Forgot to init_entity?", name)); - return $name_ents.at(name); - } - - void UI::init() { - if($world.has_the()) { - auto& bg = $world.get_the(); - bg.init(); - } - - $world.query([](auto, auto& bg) { - bg.init(); - }); - - $world.query([](auto, auto& cell, auto& rect) { - rect.init(cell); - }); - - $world.query([](auto, auto& cell, auto& effect) { - effect.init(cell); - }); - - $world.query([](auto, auto& bg, auto &) { - bg.shape->setFillColor(ColorValue::BLACK); - }); - - $world.query([](auto, auto &cell, auto& meter) { - meter.init(cell); - }); - - $world.query([this](auto, auto& cell, auto& text) { - text.init(cell, $font); - }); - - $world.query([this](auto, auto& cell, auto& text) { - text.init(cell, $font); - }); - - $world.query([&](auto, auto &cell, auto &sprite) { - sprite.init(cell); - }); - } - - void UI::debug_layout(sf::RenderWindow& window) { - $world.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($world.has_the()) { - auto& bg = $world.get_the(); - window.draw(*bg.shape); - } - - $world.query([&](auto, auto& shader) { - if(shader.$active) shader.step(); - }); - - $world.query([&](auto ent, auto& rect) { - render_helper(window, ent, true, rect.shape); - }); - - $world.query([&](auto ent, auto& cell, const auto &meter) { - int pad = meter.bar.padding * 2; - float level = std::clamp(meter.percent, 0.0f, 1.0f) * float(cell.w - pad); - // ZED: this 6 is a border width, make it a thing - meter.bar.shape->setSize({std::max(level, 0.0f), float(cell.h - pad)}); - render_helper(window, ent, true, meter.bar.shape); - }); - - $world.query([&](auto ent, auto& sprite) { - render_helper(window, ent, false, sprite.sprite); - }); - - $world.query