Can now apply a shader using the animation's timer but not sure if there should be shaders on animations or outside or both.

This commit is contained in:
Zed A. Shaw 2026-02-09 13:58:28 -05:00
parent 0d481a5ab7
commit 80f100f223
5 changed files with 29 additions and 10 deletions

View file

@ -50,10 +50,16 @@ namespace animate2 {
sprite.setTextureRect(rect);
}
void Animate2::apply_effect(std::shared_ptr<sf::Shader> 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);

View file

@ -3,6 +3,7 @@
#include <chrono>
#include <SFML/Graphics/Rect.hpp>
#include <SFML/Graphics/Sprite.hpp>
#include <SFML/Graphics/Shader.hpp>
#include <SFML/System/Clock.hpp>
#include <SFML/System/Time.hpp>
#include <functional>
@ -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<sf::Shader> effect);
void update_frame();
void update();
void motion(sf::Sprite& sprite, sf::Vector2f pos, sf::Vector2f scale);

View file

@ -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) {

View file

@ -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<guecs::Sprite>(viewer, { sprite_name, 0, false});
// BUG: this is some jank bullshit but it works
$ui.set<guecs::Sprite>(viewer, {sprite_name, 0, false});
$ui.init();
sprite = $ui.get<guecs::Sprite>(viewer).sprite;
dbc::check(sprite != nullptr, "failed to initialize $ui.sprite");
$ui.remove<guecs::Sprite>(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) {

View file

@ -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<sf::Sprite> sprite = nullptr;
std::shared_ptr<sf::Shader> effect = nullptr;
bool $initialized_this_sucks_ass = false;
void button(const std::string& name, std::function<void(guecs::Modifiers mods)> cb);
@ -44,7 +47,6 @@ namespace animator {
sf::RenderWindow $window;
sf::Vector2f $pos{0,0};
sf::Vector2f $scale{0,0};
std::shared_ptr<sf::Sprite> $sprite = nullptr;
animate2::Animate2 $anim;
std::string $sprite_name="";
std::string $anim_name="";