diff --git a/animate2.cpp b/animate2.cpp index 70f8ecc..b4fcfbc 100644 --- a/animate2.cpp +++ b/animate2.cpp @@ -50,10 +50,16 @@ namespace animate2 { sprite.setTextureRect(rect); } + void Animate2::apply_effect(std::shared_ptr effect) { + dbc::check(effect != nullptr, "can't apply null effect"); + effect->setUniform("u_time", sequence.timer.getElapsedTime().asSeconds()); + sf::Vector2f u_resolution{float(sheet.frame_width), float(sheet.frame_height)}; + effect->setUniform("u_resolution", u_resolution); + } + void Animate2::play_sound() { // BUG: this can be optimized way better if(sounds.contains(form_name)) { - fmt::println("Playing sound for {}", form_name); for(auto& [at_frame, sound_name] : sounds.at(form_name)) { if(sequence.current == at_frame) { sound::play(sound_name); diff --git a/animate2.hpp b/animate2.hpp index 52f93b8..33061d6 100644 --- a/animate2.hpp +++ b/animate2.hpp @@ -3,6 +3,7 @@ #include #include #include +#include #include #include #include @@ -116,6 +117,7 @@ namespace animate2 { bool has_form(const std::string& as_form); void set_form(const std::string& form); void apply(sf::Sprite& sprite); + void apply_effect(std::shared_ptr effect); void update_frame(); void update(); void motion(sf::Sprite& sprite, sf::Vector2f pos, sf::Vector2f scale); diff --git a/scene.cpp b/scene.cpp index 4d0e6d9..5a2ca70 100644 --- a/scene.cpp +++ b/scene.cpp @@ -76,7 +76,6 @@ namespace scene { void Engine::apply_effect(const std::string& actor, const std::string& shader) { auto& element = actor_config(actor); element.effect = shaders::get(shader); - } void Engine::attach_text(const std::string& actor, const std::string& text) { diff --git a/tools/animator.cpp b/tools/animator.cpp index e094256..99872d7 100644 --- a/tools/animator.cpp +++ b/tools/animator.cpp @@ -38,11 +38,10 @@ namespace animator { $window.setPosition({0,0}); $ui.init($sprite_name, $background, new_size.x, new_size.y); - $sprite = $ui.get_sprite(); // need to keep these around - $pos = $sprite->getPosition(); - $scale = $sprite->getScale(); + $pos = $ui.sprite->getPosition(); + $scale = $ui.sprite->getScale(); if(YES_SYNC) { $window.setVerticalSyncEnabled(VSYNC); @@ -95,6 +94,9 @@ namespace animator { $ui.update_status($anim); state(State::START); break; + case Event::TEST_SHADER: + $ui.effect = $ui.effect == nullptr ? shaders::get("flame") : nullptr; + break; default: state(State::START); } @@ -129,8 +131,9 @@ namespace animator { void FSM::run_animation() { if($anim.playing) { $anim.update(); - $anim.apply(*$sprite); - $anim.motion(*$sprite, $pos, $scale); + $anim.apply(*$ui.sprite); + if($ui.effect != nullptr) $anim.apply_effect($ui.effect); + $anim.motion(*$ui.sprite, $pos, $scale); } } @@ -209,6 +212,8 @@ namespace animator { } else if($router.scancode == KEY::M) { $mute = !$mute; sound::mute($mute); + } else if($router.scancode == KEY::S) { + event(Event::TEST_SHADER); } break; case MOUSE_CLICK: @@ -246,9 +251,13 @@ namespace animator { } auto viewer = $ui.entity("viewer"); - $ui.set(viewer, { sprite_name, 0, false}); - + // BUG: this is some jank bullshit but it works + $ui.set(viewer, {sprite_name, 0, false}); $ui.init(); + sprite = $ui.get(viewer).sprite; + dbc::check(sprite != nullptr, "failed to initialize $ui.sprite"); + + $ui.remove(viewer); $overlay.position(0, 0, width/4, height/4); $overlay.layout( @@ -264,6 +273,7 @@ namespace animator { void UI::render(sf::RenderWindow& window, bool debug) { $ui.render(window); + window.draw(*sprite, effect.get()); $overlay.render(window); if(debug) { diff --git a/tools/animator.hpp b/tools/animator.hpp index 0765974..6e894d7 100644 --- a/tools/animator.hpp +++ b/tools/animator.hpp @@ -18,6 +18,7 @@ namespace animator { PLAY_STOP=__LINE__, NEXT_FORM=__LINE__, PREV_FORM=__LINE__, + TEST_SHADER=__LINE__, RELOAD=__LINE__, }; @@ -26,6 +27,8 @@ namespace animator { struct UI { guecs::UI $ui; guecs::UI $overlay; + std::shared_ptr sprite = nullptr; + std::shared_ptr effect = nullptr; bool $initialized_this_sucks_ass = false; void button(const std::string& name, std::function cb); @@ -44,7 +47,6 @@ namespace animator { sf::RenderWindow $window; sf::Vector2f $pos{0,0}; sf::Vector2f $scale{0,0}; - std::shared_ptr $sprite = nullptr; animate2::Animate2 $anim; std::string $sprite_name=""; std::string $anim_name="";