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);
}
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) {
dbc::check(effect != nullptr, "can't apply null effect");
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() {
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()");
@ -105,8 +128,8 @@ namespace animate2 {
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) {
transform.lerp(sequence, pos, scale);
void Animate2::motion(sf::Transformable& sprite, sf::Vector2f pos, sf::Vector2f scale) {
transform.apply(sequence, pos, scale);
if(transform.flipped) {
scale.x *= -1;
@ -166,11 +189,11 @@ namespace animate2 {
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 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: {},{}",
// 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);
}
void Animate2::apply(sf::View& view_out, sf::Vector2f pos, sf::Vector2f size) {
}
Animate2 load(const std::string &file, const std::string &anim_name) {
using nlohmann::json;
std::ifstream infile(file);