From 458bf7e25e8204c3240b3d746e6456824f15144c Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Fri, 13 Feb 2026 23:30:57 -0500 Subject: [PATCH] Scene now renders correctly, but I need to pull out the json in config_scene_element and make a component that's loaded. --- scene.cpp | 41 +++++++++++++++++------------------------ scene.hpp | 2 +- 2 files changed, 18 insertions(+), 25 deletions(-) diff --git a/scene.cpp b/scene.cpp index 91a5374..8c4e8d0 100644 --- a/scene.cpp +++ b/scene.cpp @@ -8,7 +8,7 @@ const bool DEBUG=false; namespace scene { - Element Engine::config_scene_element(nlohmann::json& config, bool and_play, bool duped) { + Element Engine::config_scene_element(nlohmann::json& config, bool duped) { std::string sprite_name = config["sprite"]; auto st = textures::get_sprite(sprite_name, duped); @@ -20,7 +20,7 @@ namespace scene { // BUG: put the .json file to load as a default/optional arg auto anim = animate2::load("./assets/animate2.json", sprite_name); - if(and_play) anim.play(); + anim.play(); anim.transform.flipped = flipped; @@ -38,7 +38,7 @@ namespace scene { $scene(scene) { for(auto& config : $scene.actors) { - auto element = config_scene_element(config, false, false); + auto element = config_scene_element(config, false); dbc::check(!$actor_name_ids.contains(element.name), fmt::format("actors key {} already exists", element.name)); $actors.push_back(element); @@ -46,7 +46,7 @@ namespace scene { } for(auto& fixture : $scene.fixtures) { - auto element = config_scene_element(fixture, true, true); + auto element = config_scene_element(fixture, true); $fixtures.push_back(element); } @@ -125,28 +125,20 @@ namespace scene { if(!config.anim.playing) config.anim.play(); } - void Engine::play_animations() { - for(auto& fixture : $fixtures) { - if(fixture.anim.playing) { - fixture.anim.update(); - fixture.anim.motion(*fixture.st.sprite, fixture.pos, fixture.scale); - fixture.anim.apply(*fixture.st.sprite); - } else { - fixture.effect = nullptr; + inline void this_is_stupid_refactor(std::vector& elements) { + for(auto& element : elements) { + if(element.anim.playing) { + element.anim.update(); + element.anim.motion(*element.st.sprite, element.pos, element.scale); + element.anim.apply(*element.st.sprite); + if(element.effect != nullptr) element.anim.apply_effect(element.effect); } } + } - for(auto& actor : $actors) { - if(actor.anim.playing) { - actor.anim.update(); - actor.anim.motion(*actor.st.sprite, actor.pos, actor.scale); - actor.anim.apply(*actor.st.sprite); - // REFACTOR: actor.anim.apply(actor.text, actor.pos); - // if(actor.effect != nullptr) actor.anim.apply_effect(actor.effect); - } else { - actor.effect = nullptr; - } - } + void Engine::play_animations() { + this_is_stupid_refactor($fixtures); + this_is_stupid_refactor($actors); } sf::Vector2f Engine::position_sprite(textures::SpriteTexture& st, const std::string& cell_name, sf::Vector2f scale, bool at_mid, float x_diff, float y_diff) { @@ -179,9 +171,10 @@ namespace scene { void Engine::set_end_cb(std::function cb) { for(auto& actor : $actors) { - actor.anim.onLoop = [=](auto& seq, auto& tr) -> bool { + actor.anim.onLoop = [&,cb](auto& seq, auto& tr) -> bool { seq.current = tr.toggled ? seq.frame_count - 1 : 0; cb(); + actor.effect = nullptr; return tr.looped; }; } diff --git a/scene.hpp b/scene.hpp index 1be1321..9bfd281 100644 --- a/scene.hpp +++ b/scene.hpp @@ -42,7 +42,7 @@ namespace scene { void render(sf::RenderTexture& view); bool mouse(float x, float y, guecs::Modifiers mods); void attach_text(const std::string& actor, const std::string& text); - Element config_scene_element(nlohmann::json& config, bool and_play, bool duped); + Element config_scene_element(nlohmann::json& config, bool duped); sf::Vector2f position_sprite(textures::SpriteTexture& st, const std::string& cell_name, sf::Vector2f scale, bool at_mid, float x_diff=0.0f, float y_diff=0.0f);