From f3f2e90cd28ba505c0f7b18c85463d9e78039ee0 Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Thu, 8 May 2025 01:12:12 -0400 Subject: [PATCH] [BROKEN] This build is totally broken. DONOT USE. --- demos/calc.cpp | 12 +++++------ include/constants.hpp | 16 --------------- include/guecs.hpp | 20 ++++++++++++++++++ include/sfml/backend.hpp | 16 +++++++++++++++ include/sfml/components.hpp | 9 ++++++++- include/{ => sfml}/config.hpp | 0 meson.build | 30 ++++++++++++++------------- src/sfml/backend.cpp | 38 +++++++++++++++++++++++++++++++++++ src/sfml/components.cpp | 26 +++++++++++++----------- src/{ => sfml}/config.cpp | 2 +- src/sfml/shaders.cpp | 3 +-- src/sfml/sound.cpp | 2 +- src/sfml/textures.cpp | 3 +-- tests/guecs.cpp | 23 --------------------- wraps/sfml.wrap | 4 ++-- 15 files changed, 123 insertions(+), 81 deletions(-) delete mode 100644 include/constants.hpp create mode 100644 include/sfml/backend.hpp rename include/{ => sfml}/config.hpp (100%) create mode 100644 src/sfml/backend.cpp rename src/{ => sfml}/config.cpp (97%) diff --git a/demos/calc.cpp b/demos/calc.cpp index 89bde5a..66031d2 100644 --- a/demos/calc.cpp +++ b/demos/calc.cpp @@ -1,14 +1,13 @@ +#include "sfml/backend.hpp" #include "sfml/components.hpp" -#include "sfml/sound.hpp" -#include "sfml/shaders.hpp" -#include "sfml/textures.hpp" #include "guecs.hpp" -#include "constants.hpp" #include #include constexpr const int WINDOW_WIDTH=300; constexpr const int WINDOW_HEIGHT=400; +constexpr const int FRAME_LIMIT=60; +constexpr const bool VSYNC=true; using std::string, std::wstring; @@ -193,9 +192,8 @@ struct CalculatorUI { }; int main() { - sound::init(); - shaders::init(); - textures::init(); + sfml::Backend backend; + guecs::init(&backend); sf::RenderWindow window(sf::VideoMode({WINDOW_WIDTH, WINDOW_HEIGHT}), "LEL-GUECS Calculator"); window.setFramerateLimit(FRAME_LIMIT); diff --git a/include/constants.hpp b/include/constants.hpp deleted file mode 100644 index da3898a..0000000 --- a/include/constants.hpp +++ /dev/null @@ -1,16 +0,0 @@ -#pragma once - -#include -#include - -constexpr const int SCREEN_WIDTH=1280; -constexpr const int SCREEN_HEIGHT=720; - -constexpr const bool VSYNC=false; -constexpr const int FRAME_LIMIT=60; - -#ifdef NDEBUG -constexpr const bool DEBUG_BUILD=false; -#else -constexpr const bool DEBUG_BUILD=true; -#endif diff --git a/include/guecs.hpp b/include/guecs.hpp index fc560c2..c0a61e8 100644 --- a/include/guecs.hpp +++ b/include/guecs.hpp @@ -38,6 +38,26 @@ namespace guecs { string name; }; + struct SpriteTexture { + std::shared_ptr sprite = nullptr; + std::shared_ptr texture = nullptr; + }; + + class Backend { + public: + virtual SpriteTexture texture_get(const string& name) = 0; + + virtual void sound_play(const string& name) = 0; + + virtual void sound_stop(const string& name) = 0; + + virtual std::shared_ptr shader_get(const std::string& name) = 0; + + virtual bool shader_updated() = 0; + }; + + void init(Backend* backend); + class UI { public: Entity MAIN = 0; diff --git a/include/sfml/backend.hpp b/include/sfml/backend.hpp new file mode 100644 index 0000000..63549ac --- /dev/null +++ b/include/sfml/backend.hpp @@ -0,0 +1,16 @@ +#include "guecs.hpp" + +namespace sfml { + 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(); + }; +} diff --git a/include/sfml/components.hpp b/include/sfml/components.hpp index fe890f8..6e86ffa 100644 --- a/include/sfml/components.hpp +++ b/include/sfml/components.hpp @@ -1,10 +1,11 @@ #pragma once +#include + #include "dbc.hpp" #include "sfml/color.hpp" #include "lel.hpp" #include #include -#include #include #include @@ -48,6 +49,12 @@ namespace guecs { }; struct Sprite { + // either you set a filename here, + // or some kind of config, + // or a callback that does the loading, + // or a virtual function and you subclass + // or there's a static config function you call once, + // that's passed an object with all the necessary gear string name; int padding = PADDING; std::shared_ptr sprite = nullptr; diff --git a/include/config.hpp b/include/sfml/config.hpp similarity index 100% rename from include/config.hpp rename to include/sfml/config.hpp diff --git a/meson.build b/meson.build index 38518ce..e4926e3 100644 --- a/meson.build +++ b/meson.build @@ -69,14 +69,18 @@ dependencies += [ ] sources = [ - 'src/config.cpp', 'src/dbc.cpp', 'src/guecs.cpp', 'src/lel.cpp', + 'src/sfml/components.cpp', +] + +sfml_impl = [ + 'src/sfml/config.cpp', + 'src/sfml/backend.cpp', 'src/sfml/shaders.cpp', 'src/sfml/sound.cpp', 'src/sfml/textures.cpp', - 'src/sfml/components.cpp', ] lel_guecs_inc = include_directories('include') @@ -94,11 +98,9 @@ lel_guecs_dep = declare_dependency( include_directories: lel_guecs_inc) executable('runtests', [ + 'src/sfml/config.cpp', 'tests/lel.cpp', 'tests/guecs.cpp', - 'tests/shaders.cpp', - 'tests/sound.cpp', - 'tests/textures.cpp', ], cpp_args: cpp_args, link_args: link_args, @@ -107,12 +109,12 @@ executable('runtests', [ link_with: [lel_guecs_lib], dependencies: dependencies + [catch2]) -executable('calc', [ - 'demos/calc.cpp', - ], - cpp_args: cpp_args, - link_args: link_args, - override_options: exe_defaults, - include_directories: lel_guecs_inc, - link_with: [lel_guecs_lib], - dependencies: dependencies) +#executable('calc', sfml_impl + [ +# 'demos/calc.cpp', +# ], +# cpp_args: cpp_args, +# link_args: link_args, +# override_options: exe_defaults, +# include_directories: lel_guecs_inc, +# link_with: [lel_guecs_lib], +# dependencies: dependencies) diff --git a/src/sfml/backend.cpp b/src/sfml/backend.cpp new file mode 100644 index 0000000..049ecc1 --- /dev/null +++ b/src/sfml/backend.cpp @@ -0,0 +1,38 @@ +#include "sfml/backend.hpp" +#include "sfml/shaders.hpp" +#include "sfml/sound.hpp" +#include "sfml/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; + } + } +} diff --git a/src/sfml/components.cpp b/src/sfml/components.cpp index 3a72919..5e917e6 100644 --- a/src/sfml/components.cpp +++ b/src/sfml/components.cpp @@ -1,11 +1,15 @@ #include "guecs.hpp" -#include "sfml/shaders.hpp" -#include "sfml/sound.hpp" -#include "sfml/textures.hpp" +#include "sfml/backend.hpp" namespace guecs { + static Backend* BACKEND = nullptr; + using std::make_shared; + void init(Backend* backend) { + BACKEND = backend; + } + 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; @@ -32,14 +36,14 @@ namespace guecs { void Sprite::update(const string& new_name) { if(new_name != name) { name = new_name; - auto sprite_texture = textures::get(name); + auto sprite_texture = BACKEND->texture_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); + auto sprite_texture = BACKEND->texture_get(name); sprite = make_shared( *sprite_texture.texture, @@ -78,13 +82,13 @@ namespace guecs { void Sound::play(bool hover) { if(!hover) { - sound::play(on_click); + BACKEND->sound_play(on_click); } } void Sound::stop(bool hover) { if(!hover) { - sound::stop(on_click); + BACKEND->sound_stop(on_click); } } @@ -96,8 +100,7 @@ namespace guecs { } void Effect::init(lel::Cell &cell) { - $shader_version = shaders::version(); - $shader = shaders::get(name); + $shader = BACKEND->shader_get(name); $shader->setUniform("u_resolution", sf::Vector2f({float(cell.w), float(cell.h)})); $clock = std::make_shared(); } @@ -122,9 +125,8 @@ namespace guecs { } shared_ptr Effect::checkout_ptr() { - if(shaders::updated($shader_version)) { - $shader = shaders::get(name); - $shader_version = shaders::version(); + if(BACKEND->shader_updated()) { + $shader = BACKEND->shader_get(name); } return $shader; diff --git a/src/config.cpp b/src/sfml/config.cpp similarity index 97% rename from src/config.cpp rename to src/sfml/config.cpp index ea3ef62..21a73cf 100644 --- a/src/config.cpp +++ b/src/sfml/config.cpp @@ -1,4 +1,4 @@ -#include "config.hpp" +#include "sfml/config.hpp" #include "dbc.hpp" #include diff --git a/src/sfml/shaders.cpp b/src/sfml/shaders.cpp index f58b567..e2c6c76 100644 --- a/src/sfml/shaders.cpp +++ b/src/sfml/shaders.cpp @@ -2,8 +2,7 @@ #include #include "dbc.hpp" #include -#include "config.hpp" -#include "constants.hpp" +#include "sfml/config.hpp" #include namespace shaders { diff --git a/src/sfml/sound.cpp b/src/sfml/sound.cpp index bc5deb9..f3f558c 100644 --- a/src/sfml/sound.cpp +++ b/src/sfml/sound.cpp @@ -1,7 +1,7 @@ #include "sfml/sound.hpp" #include "dbc.hpp" #include -#include "config.hpp" +#include "sfml/config.hpp" namespace sound { static SoundManager SMGR; diff --git a/src/sfml/textures.cpp b/src/sfml/textures.cpp index 7e8544a..48ef7df 100644 --- a/src/sfml/textures.cpp +++ b/src/sfml/textures.cpp @@ -2,8 +2,7 @@ #include #include "dbc.hpp" #include -#include "config.hpp" -#include "constants.hpp" +#include "sfml/config.hpp" #include namespace textures { diff --git a/tests/guecs.cpp b/tests/guecs.cpp index 016971a..60b7e3a 100644 --- a/tests/guecs.cpp +++ b/tests/guecs.cpp @@ -1,32 +1,9 @@ #include #include -#include "constants.hpp" #include "guecs.hpp" -#include "sfml/textures.hpp" #include using namespace guecs; TEST_CASE("prototype one gui", "[ecs-gui]") { - guecs::UI gui; - textures::init(); - - gui.position(0, 0, 1000, 500); - gui.layout("[test1|test2|test3][test4|_|test5]"); - - for(auto& [name, cell] : gui.cells()) { - auto button = gui.entity(name); - gui.set(button, cell); - gui.set(button, {}); - gui.set(button, {}); - gui.set(button, {L"whatever"}); - } - - gui.init(); - - // at this point it's mostly ready but I'd need to render it to a window real quick - sf::RenderWindow window; - window.setSize({SCREEN_WIDTH, SCREEN_HEIGHT}); - gui.render(window); - window.display(); } diff --git a/wraps/sfml.wrap b/wraps/sfml.wrap index 577ed1e..cf9cf25 100644 --- a/wraps/sfml.wrap +++ b/wraps/sfml.wrap @@ -1,7 +1,7 @@ [wrap-git] -directory=SFML-3.0.0 +directory=SFML-3.0.1 url=https://github.com/SFML/SFML.git -revision=3.0.0 +revision=3.0.1 depth=1 method=cmake