Camera is now using Animate2 and it's mostly working, but there's a few more refactors needed.

This commit is contained in:
Zed A. Shaw 2026-02-20 00:15:19 -05:00
parent 46cc21ec7b
commit 364f66bffb
14 changed files with 106 additions and 62 deletions

View file

@ -50,6 +50,23 @@ namespace animate2 {
sprite.setTextureRect(rect); sprite.setTextureRect(rect);
} }
void Animate2::motion(sf::View& view_out, sf::Vector2f pos, sf::Vector2f size) {
dbc::check(size.x > 1.0f && size.y > 1.0f, "motion size must be above 1.0 since it's not a ratio");
dbc::check(transform.flipped == false, "transform must be false, has no effect on View");
sf::Vector2f scale{transform.min_x, transform.min_y};
transform.apply(sequence, pos, scale);
view_out.setCenter(pos);
if(transform.scaled) {
view_out.setSize({size.x * scale.x, size.y * scale.y});
} else {
view_out.setSize(size);
}
}
void Animate2::apply_effect(std::shared_ptr<sf::Shader> effect) { void Animate2::apply_effect(std::shared_ptr<sf::Shader> effect) {
dbc::check(effect != nullptr, "can't apply null effect"); dbc::check(effect != nullptr, "can't apply null effect");
effect->setUniform("u_time", sequence.timer.getElapsedTime().asSeconds()); effect->setUniform("u_time", sequence.timer.getElapsedTime().asSeconds());
@ -70,6 +87,12 @@ namespace animate2 {
} }
} }
/* REFACTOR: I believe this is wrong still. If ::commit() determines number of ticks+alpha since last
* render then update needs to be called 1/tick. The Timer will keep track of alpha as the error
* between commit calls, so this function only really needs to care about ticks. But, I'm still
* calling getElapsedTime() when I already did that in commit(), so should I just ignore that and assume
* elapsed is DELTA, or use elapsed here?
*/
void Animate2::update() { void Animate2::update() {
dbc::check(playing, "attempt to update animation that's not playing"); dbc::check(playing, "attempt to update animation that's not playing");
dbc::check(sequence.frame_count == sequence.frames.size(), "frame_count doesn't match frame.size()"); dbc::check(sequence.frame_count == sequence.frames.size(), "frame_count doesn't match frame.size()");
@ -105,8 +128,8 @@ namespace animate2 {
dbc::check(sequence.current < sequence.frame_count, "onLoop fail: current frame out of frames.size()"); dbc::check(sequence.current < sequence.frame_count, "onLoop fail: current frame out of frames.size()");
} }
void Animate2::motion(sf::Sprite& sprite, sf::Vector2f pos, sf::Vector2f scale) { void Animate2::motion(sf::Transformable& sprite, sf::Vector2f pos, sf::Vector2f scale) {
transform.lerp(sequence, pos, scale); transform.apply(sequence, pos, scale);
if(transform.flipped) { if(transform.flipped) {
scale.x *= -1; scale.x *= -1;
@ -166,11 +189,11 @@ namespace animate2 {
return {int(tick_count), alpha}; return {int(tick_count), alpha};
} }
void Transform::lerp(Sequence& seq, sf::Vector2f& pos_out, sf::Vector2f& scale_out) { void Transform::apply(Sequence& seq, sf::Vector2f& pos_out, sf::Vector2f& scale_out) {
// float dt = 1 - std::powf(ease_rate, seq.subframe + 0.0001); // float dt = 1 - std::powf(ease_rate, seq.subframe + 0.0001);
float tick = easing_func(seq.subframe); float tick = easing_func(seq.subframe);
motion_func(*this, pos_out, scale_out, tick); motion_func(*this, pos_out, scale_out, tick, relative);
// fmt::println("sub: {}, tick: {}, tr: {},{}; pos: {},{}; scale: {},{}", // fmt::println("sub: {}, tick: {}, tr: {},{}; pos: {},{}; scale: {},{}",
// seq.subframe, tick, min_y, max_y, pos_out.x, pos_out.y, // seq.subframe, tick, min_y, max_y, pos_out.x, pos_out.y,
@ -206,10 +229,6 @@ namespace animate2 {
transform.motion_func = ease2::get_motion(transform.motion); transform.motion_func = ease2::get_motion(transform.motion);
} }
void Animate2::apply(sf::View& view_out, sf::Vector2f pos, sf::Vector2f size) {
}
Animate2 load(const std::string &file, const std::string &anim_name) { Animate2 load(const std::string &file, const std::string &anim_name) {
using nlohmann::json; using nlohmann::json;
std::ifstream infile(file); std::ifstream infile(file);

View file

@ -57,6 +57,7 @@ namespace animate2 {
bool flipped{false}; bool flipped{false};
float ease_rate{0.5f}; float ease_rate{0.5f};
bool scaled{false}; bool scaled{false};
bool relative{false};
// handled by onLoop // handled by onLoop
bool toggled{false}; bool toggled{false};
@ -70,7 +71,7 @@ namespace animate2 {
std::shared_ptr<sf::Shader> shader{nullptr}; std::shared_ptr<sf::Shader> shader{nullptr};
void lerp(Sequence& seq, sf::Vector2f& pos_out, sf::Vector2f& scale_out); void apply(Sequence& seq, sf::Vector2f& pos_out, sf::Vector2f& scale_out);
}; };
/* Gets the number of times it looped, and returns if it should stop. */ /* Gets the number of times it looped, and returns if it should stop. */
@ -118,10 +119,10 @@ namespace animate2 {
bool has_form(const std::string& as_form); bool has_form(const std::string& as_form);
void set_form(const std::string& form); void set_form(const std::string& form);
void apply(sf::Sprite& sprite); void apply(sf::Sprite& sprite);
void apply(sf::View& view_out, sf::Vector2f pos, sf::Vector2f size);
void apply_effect(std::shared_ptr<sf::Shader> effect); void apply_effect(std::shared_ptr<sf::Shader> effect);
void update(); void update();
void motion(sf::Sprite& sprite, sf::Vector2f pos, sf::Vector2f scale); void motion(sf::Transformable& sprite, sf::Vector2f pos, sf::Vector2f scale);
void motion(sf::View& view_out, sf::Vector2f pos, sf::Vector2f scale);
std::pair<int, double> commit(); std::pair<int, double> commit();
}; };
@ -130,6 +131,6 @@ namespace animate2 {
ENROLL_COMPONENT(Sheet, frames, frame_width, frame_height); ENROLL_COMPONENT(Sheet, frames, frame_width, frame_height);
ENROLL_COMPONENT(Sequence, frames, durations); ENROLL_COMPONENT(Sequence, frames, durations);
ENROLL_COMPONENT(Transform, min_x, min_y, max_x, max_y, ENROLL_COMPONENT(Transform, min_x, min_y, max_x, max_y,
flipped, ease_rate, scaled, toggled, looped, easing, motion); flipped, ease_rate, scaled, relative, toggled, looped, easing, motion);
ENROLL_COMPONENT(Animate2, sheet, sequences, transforms, forms, sounds); ENROLL_COMPONENT(Animate2, sheet, sequences, transforms, forms, sounds);
} }

View file

@ -252,10 +252,11 @@ namespace animation {
void configure(DinkyECS::World& world, DinkyECS::Entity entity) { void configure(DinkyECS::World& world, DinkyECS::Entity entity) {
auto sprite = world.get_if<Sprite>(entity); auto sprite = world.get_if<Sprite>(entity);
if(sprite != nullptr && animation::has(sprite->name)) { if(sprite != nullptr && animation::has(sprite->name)) {
world.set<Animation>(entity, animation::load(sprite->name)); world.set<Animation>(entity, animation::load(sprite->name));
}
} }
}
void step_animation(DinkyECS::World& world, DinkyECS::Entity entity, sf::Vector2f& scale_out, sf::Vector2f& pos_out, sf::IntRect& rect_out) { void step_animation(DinkyECS::World& world, DinkyECS::Entity entity, sf::Vector2f& scale_out, sf::Vector2f& pos_out, sf::IntRect& rect_out) {
if(auto animation = world.get_if<components::Animation>(entity)) { if(auto animation = world.get_if<components::Animation>(entity)) {

View file

@ -21,6 +21,7 @@
"scaled": true, "scaled": true,
"toggled": false, "toggled": false,
"looped": true, "looped": true,
"relative": false,
"easing": "in_out_back", "easing": "in_out_back",
"motion": "move_rush" "motion": "move_rush"
}, },
@ -34,8 +35,9 @@
"scaled": true, "scaled": true,
"toggled": false, "toggled": false,
"looped": false, "looped": false,
"relative": true,
"easing": "normal_dist", "easing": "normal_dist",
"motion": "move_shake" "motion": "move_slide"
}, },
"breathe": { "breathe": {
"min_x": 0, "min_x": 0,
@ -47,6 +49,7 @@
"scaled": false, "scaled": false,
"toggled": false, "toggled": false,
"looped": true, "looped": true,
"relative": true,
"easing": "sine", "easing": "sine",
"motion": "move_bounce" "motion": "move_bounce"
} }
@ -84,6 +87,7 @@
"scaled": false, "scaled": false,
"toggled": false, "toggled": false,
"looped": true, "looped": true,
"relative": true,
"easing": "none", "easing": "none",
"motion": "move_none" "motion": "move_none"
} }
@ -119,6 +123,7 @@
"scaled": true, "scaled": true,
"toggled": false, "toggled": false,
"looped": true, "looped": true,
"relative": false,
"easing": "sine", "easing": "sine",
"motion": "move_none" "motion": "move_none"
} }

View file

@ -22,8 +22,9 @@
"ease_rate": 5.0, "ease_rate": 5.0,
"scaled": false, "scaled": false,
"toggled": false, "toggled": false,
"looped": false, "looped": true,
"easing": "linear", "easing": "linear",
"relative": false,
"motion": "move_slide" "motion": "move_slide"
}, },
"shake": { "shake": {
@ -35,7 +36,8 @@
"ease_rate": 5.0, "ease_rate": 5.0,
"scaled": false, "scaled": false,
"toggled": false, "toggled": false,
"looped": false, "looped": true,
"relative": true,
"easing": "normal_dist", "easing": "normal_dist",
"motion": "move_shake" "motion": "move_shake"
}, },
@ -48,8 +50,9 @@
"ease_rate": 3.0, "ease_rate": 3.0,
"scaled": false, "scaled": false,
"toggled": false, "toggled": false,
"looped": false, "looped": true,
"easing": "sine", "easing": "sine",
"relative": true,
"motion": "move_rush" "motion": "move_rush"
}, },
"bounce": { "bounce": {
@ -61,7 +64,8 @@
"ease_rate": 3.0, "ease_rate": 3.0,
"scaled": false, "scaled": false,
"toggled": false, "toggled": false,
"looped": false, "looped": true,
"relative": true,
"easing": "in_out_back", "easing": "in_out_back",
"motion": "move_bounce" "motion": "move_bounce"
} }

View file

@ -1,5 +1,4 @@
#include "camera.hpp" #include "camera.hpp"
#include <fmt/core.h>
#include <unordered_map> #include <unordered_map>
#include "components.hpp" #include "components.hpp"
#include "config.hpp" #include "config.hpp"
@ -72,13 +71,12 @@ namespace cinematic {
going_to.x = clamp(x, camera_bounds.position.x, camera_bounds.size.x); going_to.x = clamp(x, camera_bounds.position.x, camera_bounds.size.x);
going_to.y = clamp(y, camera_bounds.position.y, camera_bounds.size.y); going_to.y = clamp(y, camera_bounds.position.y, camera_bounds.size.y);
// BUG: annoying special case if(!anim.transform.relative) {
//if(anim.motion == ease::SLIDE) { anim.transform.min_x = aimed_at.x;
// anim.min_x = aimed_at.x; anim.transform.min_y = aimed_at.y;
// anim.min_y = aimed_at.y; anim.transform.max_x = going_to.x;
// anim.max_x = going_to.x; anim.transform.max_y = going_to.y;
// anim.max_y = going_to.y; }
//}
} }
void Camera::reset(sf::RenderTexture& target) { void Camera::reset(sf::RenderTexture& target) {
@ -94,11 +92,17 @@ namespace cinematic {
void Camera::render(sf::RenderTexture& target) { void Camera::render(sf::RenderTexture& target) {
if(anim.playing) { if(anim.playing) {
anim.apply(view, going_to, size); anim.motion(view, going_to, size);
target.setView(view); target.setView(view);
} }
} }
void Camera::update() {
// REFACTOR: there's no connection between anim.commit() and anim.update()
auto [ticks, alpha] = anim.commit();
if(anim.playing) anim.update();
}
bool Camera::playing() { bool Camera::playing() {
return anim.playing; return anim.playing;
} }

View file

@ -20,6 +20,7 @@ namespace cinematic {
void position(float x, float y); void position(float x, float y);
void move(float x, float y); void move(float x, float y);
bool playing(); bool playing();
void update();
void render(sf::RenderTexture& target); void render(sf::RenderTexture& target);
void play(); void play();
void style(const std::string &name); void style(const std::string &name);

View file

@ -60,47 +60,47 @@ namespace ease2 {
return Random::normal(0.5f, 0.1f); return Random::normal(0.5f, 0.1f);
} }
void move_shake(Transform &tr, sf::Vector2f& pos_out, sf::Vector2f& scale_out, float tick) { void move_shake(Transform &tr, sf::Vector2f& pos_out, sf::Vector2f& scale_out, float tick, bool relative) {
pos_out.x += std::lerp(tr.min_x, tr.max_x, tick); pos_out.x = std::lerp(tr.min_x, tr.max_x, tick) + (pos_out.x * relative);
} }
void move_bounce(Transform &tr, sf::Vector2f& pos_out, sf::Vector2f& scale_out, float tick) { void move_bounce(Transform &tr, sf::Vector2f& pos_out, sf::Vector2f& scale_out, float tick, bool relative) {
pos_out.y -= std::lerp(tr.min_y, tr.max_y, tick); pos_out.y = std::lerp(tr.min_y, tr.max_y, tick) + (pos_out.y * relative);
} }
void move_rush(Transform &tr, sf::Vector2f& pos_out, sf::Vector2f& scale_out, float tick) { void move_rush(Transform &tr, sf::Vector2f& pos_out, sf::Vector2f& scale_out, float tick, bool relative) {
scale_out.x = std::lerp(tr.min_x, tr.max_x, tick); scale_out.x = std::lerp(tr.min_x, tr.max_x, tick) + (scale_out.x * relative);
scale_out.y = std::lerp(tr.min_y, tr.max_y, tick); scale_out.y = std::lerp(tr.min_y, tr.max_y, tick) + (scale_out.y * relative);
pos_out.y = pos_out.y - (pos_out.y * scale_out.y - pos_out.y); pos_out.y = pos_out.y - (pos_out.y * scale_out.y - pos_out.y) + (pos_out.y * relative);
} }
void scale_squeeze(Transform &tr, sf::Vector2f& pos_out, sf::Vector2f& scale_out, float tick) { void scale_squeeze(Transform &tr, sf::Vector2f& pos_out, sf::Vector2f& scale_out, float tick, bool relative) {
scale_out.x = std::lerp(tr.min_x, tr.max_x, tick); scale_out.x = std::lerp(tr.min_x, tr.max_x, tick) + (scale_out.x * relative);
} }
void scale_squash(Transform &tr, sf::Vector2f& pos_out, sf::Vector2f& scale_out, float tick) { void scale_squash(Transform &tr, sf::Vector2f& pos_out, sf::Vector2f& scale_out, float tick, bool relative) {
scale_out.y = std::lerp(tr.min_y, tr.max_y, tick); scale_out.y = std::lerp(tr.min_y, tr.max_y, tick) + (scale_out.y * relative);
} }
void scale_stretch(Transform &tr, sf::Vector2f& pos_out, sf::Vector2f& scale_out, float tick) { void scale_stretch(Transform &tr, sf::Vector2f& pos_out, sf::Vector2f& scale_out, float tick, bool relative) {
scale_out.x = std::lerp(tr.min_x, tr.max_x, tick); scale_out.x = std::lerp(tr.min_x, tr.max_x, tick) + (scale_out.x * relative);
} }
void scale_grow(Transform &tr, sf::Vector2f& pos_out, sf::Vector2f& scale_out, float tick) { void scale_grow(Transform &tr, sf::Vector2f& pos_out, sf::Vector2f& scale_out, float tick, bool relative) {
scale_out.y = std::lerp(tr.min_y, tr.max_y, tick); scale_out.y = std::lerp(tr.min_y, tr.max_y, tick) + (scale_out.y * relative);
} }
void move_slide(Transform &tr, sf::Vector2f& pos_out, sf::Vector2f& scale_out, float tick) { void move_slide(Transform &tr, sf::Vector2f& pos_out, sf::Vector2f& scale_out, float tick, bool relative) {
pos_out.x += std::lerp(tr.min_x, tr.max_x, tick); pos_out.x = std::lerp(tr.min_x, tr.max_x, tick) + (pos_out.x * relative);
pos_out.y += std::lerp(tr.min_y, tr.max_y, tick); pos_out.y = std::lerp(tr.min_y, tr.max_y, tick) + (pos_out.y * relative);
} }
void move_none(Transform &tr, sf::Vector2f& pos_out, sf::Vector2f& scale_out, float tick) { void move_none(Transform &tr, sf::Vector2f& pos_out, sf::Vector2f& scale_out, float tick, bool relative) {
} }
void scale_only(Transform &tr, sf::Vector2f& pos_out, sf::Vector2f& scale_out, float tick) { void scale_only(Transform &tr, sf::Vector2f& pos_out, sf::Vector2f& scale_out, float tick, bool relative) {
scale_out.x = std::lerp(scale_out.x * tr.min_x, scale_out.x * tr.max_x, tick); scale_out.x = std::lerp(scale_out.x * tr.min_x, scale_out.x * tr.max_x, tick) + (scale_out.x * relative);
scale_out.y = std::lerp(scale_out.y * tr.min_y, scale_out.y * tr.max_y, tick); scale_out.y = std::lerp(scale_out.y * tr.min_y, scale_out.y * tr.max_y, tick) + (scale_out.y * relative);
} }
std::unordered_map<std::string, EaseFunc> map_of_easings{ std::unordered_map<std::string, EaseFunc> map_of_easings{

View file

@ -7,7 +7,7 @@ namespace animate2 {
namespace ease2 { namespace ease2 {
using EaseFunc = std::function<double(double)>; using EaseFunc = std::function<double(double)>;
using MotionFunc = std::function<void(animate2::Transform &tr, sf::Vector2f& pos_out, sf::Vector2f& scale_out, float tick)>; using MotionFunc = std::function<void(animate2::Transform &tr, sf::Vector2f& pos_out, sf::Vector2f& scale_out, float tick, bool relative)>;
EaseFunc get_easing(const std::string& name); EaseFunc get_easing(const std::string& name);
MotionFunc get_motion(const std::string& name); MotionFunc get_motion(const std::string& name);
@ -19,14 +19,14 @@ namespace ease2 {
double random(double tick); double random(double tick);
double normal_dist(double tick); double normal_dist(double tick);
void move_bounce(animate2::Transform &tr, sf::Vector2f& pos_out, sf::Vector2f& scale_out, float tick); void move_bounce(animate2::Transform &tr, sf::Vector2f& pos_out, sf::Vector2f& scale_out, float tick, bool relative);
void move_rush(animate2::Transform &tr, sf::Vector2f& pos_out, sf::Vector2f& scale_out, float tick); void move_rush(animate2::Transform &tr, sf::Vector2f& pos_out, sf::Vector2f& scale_out, float tick, bool relative);
void scale_squeeze(animate2::Transform &tr, sf::Vector2f& pos_out, sf::Vector2f& scale_out, float tick); void scale_squeeze(animate2::Transform &tr, sf::Vector2f& pos_out, sf::Vector2f& scale_out, float tick, bool relative);
void scale_squash(animate2::Transform &tr, sf::Vector2f& pos_out, sf::Vector2f& scale_out, float tick); void scale_squash(animate2::Transform &tr, sf::Vector2f& pos_out, sf::Vector2f& scale_out, float tick, bool relative);
void scale_stretch(animate2::Transform &tr, sf::Vector2f& pos_out, sf::Vector2f& scale_out, float tick); void scale_stretch(animate2::Transform &tr, sf::Vector2f& pos_out, sf::Vector2f& scale_out, float tick, bool relative);
void scale_grow(animate2::Transform &tr, sf::Vector2f& pos_out, sf::Vector2f& scale_out, float tick); void scale_grow(animate2::Transform &tr, sf::Vector2f& pos_out, sf::Vector2f& scale_out, float tick, bool relative);
void move_slide(animate2::Transform &tr, sf::Vector2f& pos_out, sf::Vector2f& scale_out, float tick); void move_slide(animate2::Transform &tr, sf::Vector2f& pos_out, sf::Vector2f& scale_out, float tick, bool relative);
void move_none(animate2::Transform &tr, sf::Vector2f& pos_out, sf::Vector2f& scale_out, float tick); void move_none(animate2::Transform &tr, sf::Vector2f& pos_out, sf::Vector2f& scale_out, float tick, bool relative);
void scale_only(animate2::Transform &tr, sf::Vector2f& pos_out, sf::Vector2f& scale_out, float tick); void scale_only(animate2::Transform &tr, sf::Vector2f& pos_out, sf::Vector2f& scale_out, float tick, bool relative);
void move_shake(animate2::Transform &tr, sf::Vector2f& pos_out, sf::Vector2f& scale_out, float tick); void move_shake(animate2::Transform &tr, sf::Vector2f& pos_out, sf::Vector2f& scale_out, float tick, bool relative);
} }

View file

@ -190,6 +190,7 @@ namespace scene {
void Engine::tick() { void Engine::tick() {
for(auto& actor : $actors) { for(auto& actor : $actors) {
dbc::log("IDIOT! you need to use the ticks to all update ticks number of times");
auto [ticks, alpha] = actor.anim.commit(); auto [ticks, alpha] = actor.anim.commit();
} }
} }

View file

@ -61,6 +61,10 @@ namespace storyboard {
return sf::Time(out); return sf::Time(out);
} }
void UI::update() {
$camera.update();
}
void UI::track_audio() { void UI::track_audio() {
auto& beat = $story.beats[cur_beat % $story.beats.size()]; auto& beat = $story.beats[cur_beat % $story.beats.size()];
auto track_head = $audio->getPlayingOffset(); auto track_head = $audio->getPlayingOffset();

View file

@ -22,6 +22,7 @@ namespace storyboard {
UI(const std::string& story_name); UI(const std::string& story_name);
void init(); void init();
void update();
void render(sf::RenderWindow &window); void render(sf::RenderWindow &window);
bool mouse(float x, float y, guecs::Modifiers mods); bool mouse(float x, float y, guecs::Modifiers mods);
void zoom(const std::string &cell_name); void zoom(const std::string &cell_name);

View file

@ -50,6 +50,7 @@ int main(int, char*[]) {
dbc::check(main->$world == world, "GameDB::current_world doesn't match boss fight world."); dbc::check(main->$world == world, "GameDB::current_world doesn't match boss fight world.");
while(!main->in_state(boss::State::END)) { while(!main->in_state(boss::State::END)) {
dbc::log("IDIOT! make this an update.");
main->$ui.$arena.tick(); main->$ui.$arena.tick();
main->render(window); main->render(window);

View file

@ -30,6 +30,9 @@ int main(int, char*[]) {
main.init(); main.init();
while(main.playing()) { while(main.playing()) {
main.update();
main.render(window);
while(const auto ev = window.pollEvent()) { while(const auto ev = window.pollEvent()) {
auto gui_ev = router.process_event(ev); auto gui_ev = router.process_event(ev);
auto mouse_pos = window.mapPixelToCoords(router.position); auto mouse_pos = window.mapPixelToCoords(router.position);
@ -41,7 +44,6 @@ int main(int, char*[]) {
} }
} }
main.render(window);
window.display(); window.display();
} }