The gorious Lord Keith the Rat King can now have easing and motion functions that are defined in json and hot-reloaded.

This commit is contained in:
Zed A. Shaw 2026-02-04 00:59:26 -05:00
parent 383f839fdf
commit 07b2102f59
6 changed files with 69 additions and 18 deletions

View file

@ -33,7 +33,6 @@ namespace animate2 {
playing = true;
sequence.timer.start();
sequence.frame_count = sequence.frames.size();
$frame_rects = calc_frames();
}
void Animate2::stop() {
@ -149,9 +148,9 @@ namespace animate2 {
void Transform::lerp(Sequence& seq, sf::Vector2f& pos_out, sf::Vector2f& scale_out) {
// float dt = 1 - std::powf(ease_rate, seq.subframe + 0.0001);
float tick = easing(seq.subframe);
float tick = easing_func(seq.subframe);
motion(*this, pos_out, scale_out, tick);
motion_func(*this, pos_out, scale_out, tick);
// fmt::println("sub: {}, tick: {}, tr: {},{}; pos: {},{}; scale: {},{}",
// seq.subframe, tick, min_y, max_y, pos_out.x, pos_out.y,
@ -166,6 +165,11 @@ namespace animate2 {
Animate2 anim;
animate2::from_json(data[anim_name], anim);
// BUG: json doesn't like may "fancy" no-constructor bullshit
anim.$frame_rects = anim.calc_frames();
anim.transform.easing_func = ease2::get_easing(anim.transform.easing);
anim.transform.motion_func = ease2::get_motion(anim.transform.motion);
return anim;
}
}

View file

@ -60,11 +60,14 @@ namespace animate2 {
// handled by onLoop
bool toggled{false};
bool looped{false};
std::string easing{"in_out_back"};
std::string motion{"move_rush"};
// change to using a callback function for these
ease2::EaseFunc easing_func{ease2::get_easing(easing)};
ease2::MotionFunc motion_func{ease2::get_motion(motion)};
std::shared_ptr<sf::Shader> shader{nullptr};
// change to using a callback function for these
ease2::EaseFunc easing = ease2::in_out_back;
ease2::MoveFunc motion = ease2::move_rush;
void lerp(Sequence& seq, sf::Vector2f& pos_out, sf::Vector2f& scale_out);
};
@ -108,6 +111,7 @@ namespace animate2 {
ENROLL_COMPONENT(Sheet, frames, frame_width, frame_height);
ENROLL_COMPONENT(Sequence, frames, durations);
ENROLL_COMPONENT(Transform, min_x, min_y, max_x, max_y, simple, flipped, ease_rate, scaled, toggled, looped);
ENROLL_COMPONENT(Transform, min_x, min_y, max_x, max_y, simple,
flipped, ease_rate, scaled, toggled, looped, easing, motion);
ENROLL_COMPONENT(Animate2, sheet, sequence, transform);
}

View file

@ -19,7 +19,9 @@
"ease_rate": 5.0,
"scaled": true,
"toggled": false,
"looped": true
"looped": true,
"easing": "out_circle",
"motion": "scale_grow"
}
},
"rat_king_meth": {
@ -30,19 +32,21 @@
},
"sequence": {
"frames": [0, 1],
"durations": [33, 33]
"durations": [133, 133]
},
"transform": {
"min_x": 0.6,
"min_y": 0.6,
"max_x": 0.8,
"max_y": 0.8,
"min_x": -20.0,
"min_y": -20.0,
"max_x": 20.0,
"max_y": 20.0,
"simple": false,
"flipped": false,
"flipped": true,
"ease_rate": 5.0,
"scaled": true,
"toggled": false,
"looped": true
"looped": true,
"easing": "normal_dist",
"motion": "move_shake"
}
}
}

View file

@ -2,6 +2,8 @@
#include "rand.hpp"
#include "animate2.hpp"
#include <fmt/core.h>
#include <unordered_map>
#include "dbc.hpp"
namespace ease2 {
using namespace animate2;
@ -96,4 +98,39 @@ namespace ease2 {
scale_out.x = std::lerp(scale_out.x * tr.min_x, scale_out.x * tr.max_x, tick);
scale_out.y = std::lerp(scale_out.y * tr.min_y, scale_out.y * tr.max_y, tick);
}
std::unordered_map<std::string, EaseFunc> map_of_easings{
{"sine", sine},
{"out_circle", out_circle},
{"out_bounce", out_bounce},
{"in_out_back", in_out_back},
{"random", random},
{"normal_dist", normal_dist},
};
std::unordered_map<std::string, MotionFunc> map_of_motions{
{"move_bounce", move_bounce},
{"move_rush", move_rush},
{"scale_squeeze", scale_squeeze},
{"scale_squash", scale_squash},
{"scale_stretch", scale_stretch},
{"scale_grow", scale_grow},
{"move_slide", move_slide},
{"move_none", move_none},
{"scale_only", scale_only},
{"move_shake", move_shake},
};
EaseFunc get_easing(const std::string& name) {
dbc::check(map_of_easings.contains(name),
fmt::format("easing name {} does not exist", name));
return map_of_easings.at(name);
}
MotionFunc get_motion(const std::string& name) {
dbc::check(map_of_motions.contains(name),
fmt::format("motion name {} does not exist", name));
return map_of_motions.at(name);
}
}

View file

@ -7,7 +7,10 @@ namespace animate2 {
namespace ease2 {
using EaseFunc = std::function<double(double)>;
using MoveFunc = 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)>;
EaseFunc get_easing(const std::string& name);
MotionFunc get_motion(const std::string& name);
double sine(double x);
double out_circle(double x);

View file

@ -13,10 +13,9 @@
/*
* TODO:
*
* - Setting a background.
* - Options parsing for that.
* - Easing functions from strings in json.
* - Map of animation forms.
* - Bring back shaders.
*/
using namespace std::chrono_literals;