Most of the transforms kind of work now need to hook in the new timer.

This commit is contained in:
Zed A. Shaw 2026-01-27 23:39:09 -05:00
parent 20d7612f1c
commit 7f14a39edf
6 changed files with 47 additions and 27 deletions

View file

@ -78,6 +78,7 @@ namespace animate2 {
void Timer::start() {
clock.start();
prev_time = clock.getElapsedTime().asSeconds();
}
void Timer::reset() {
@ -86,25 +87,34 @@ namespace animate2 {
void Timer::restart() {
clock.restart();
prev_time = clock.getElapsedTime().asSeconds();
}
sf::Time Timer::getElapsedTime() {
return clock.getElapsedTime();
}
void Timer::begin() {
prev_time = clock.getElapsedTime().asSeconds();
}
std::pair<int, double> Timer::commit() {
// determine frame duration based on previous time
current_time = clock.getElapsedTime().asSeconds();
frame_duration = current_time - prev_time;
// update prev_time for the next call
prev_time = current_time;
// update accumulator, retaining previous errors
accumulator += frame_duration;
// find the tick count based on DELTA
double tick_count = accumulator / DELTA;
// alpha is then the remainder/fractional part
double alpha = modf(tick_count, &tick_count);
accumulator -= tick_count * DELTA;
// reduce accumulator by the number of DELTAS
accumulator -= tick_count * DELTA;
// that leaves the remaining errors for next loop
// return the number of even DELTA ticks and the alpha
return {int(tick_count), alpha};
}

View file

@ -26,7 +26,6 @@ namespace animate2 {
double alpha = 0.0;
sf::Clock clock{};
void begin();
std::pair<int, double> commit();
void start();
void reset();

View file

@ -15,7 +15,7 @@ namespace ease2 {
return (std::sin(x) + 1.0) / 2.0;
}
double out_circ(double x) {
double out_circle(double x) {
return std::sqrt(1.0f - ((x - 1.0f) * (x - 1.0f)));
}
@ -46,11 +46,11 @@ namespace ease2 {
: (std::pow(2.0 * x - 2.0, 2.0) * ((c2 + 1.0) * (x * 2.0 - 2.0) + c2) + 2.0) / 2.0;
}
float random(float tick) {
double random(double tick) {
return Random::uniform_real(0.0001f, 1.0f);
}
float normal_dist(float tick) {
double normal_dist(double tick) {
return Random::normal(0.5f, 0.1f);
}
@ -66,7 +66,6 @@ namespace ease2 {
scale_out.x = std::lerp(tr.min_x, tr.max_x, tick);
scale_out.y = std::lerp(tr.min_y, tr.max_y, tick);
pos_out.y = pos_out.y - (pos_out.y * scale_out.y - pos_out.y);
fmt::println("RUSH pos={},{}; scale={},{}; tic={}", pos_out.x, pos_out.y, scale_out.x, scale_out.y, tick);
}
void scale_squeeze(Transform &tr, sf::Vector2f& pos_out, sf::Vector2f& scale_out, float tick) {
@ -77,7 +76,7 @@ namespace ease2 {
scale_out.y *= std::lerp(tr.min_y, tr.max_y, tick);
}
void scale_strech(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) {
scale_out.x = std::lerp(tr.min_x, tr.max_x, tick);
}
@ -86,8 +85,8 @@ namespace ease2 {
}
void move_slide(Transform &tr, sf::Vector2f& pos_out, sf::Vector2f& scale_out, float tick) {
pos_out.x = std::lerp(tr.min_x, tr.max_x, tick);
pos_out.y = std::lerp(tr.min_y, tr.max_y, tick);
pos_out.x += std::lerp(tr.min_x, tr.max_x, tick);
pos_out.y += std::lerp(tr.min_y, tr.max_y, tick);
}
void move_none(Transform &tr, sf::Vector2f& pos_out, sf::Vector2f& scale_out, float tick) {

View file

@ -10,7 +10,7 @@ namespace ease2 {
using MoveFunc = std::function<void(animate2::Transform &tr, sf::Vector2f& pos_out, sf::Vector2f& scale_out, float tick)>;
double sine(double x);
double out_circ(double x);
double out_circle(double x);
double out_bounce(double x);
double in_out_back(double x);
double random(double tick);
@ -20,9 +20,10 @@ namespace ease2 {
void move_rush(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);
void scale_squash(animate2::Transform &tr, sf::Vector2f& pos_out, sf::Vector2f& scale_out, float tick);
void scale_strech(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);
void scale_grow(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);
void move_none(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);
void move_shake(animate2::Transform &tr, sf::Vector2f& pos_out, sf::Vector2f& scale_out, float tick);
}

View file

@ -196,9 +196,9 @@ TEST_CASE("confirm transition changes work", "[animation-new]") {
TEST_CASE("playing with delta time", "[animation-new]") {
animate2::Timer timer;
timer.start();
for(int i = 0; i < 20; i++) {
timer.begin();
FAKE_RENDER();
auto [tick_count, alpha] = timer.commit();
fmt::println("tick: {}, alpha: {}", tick_count, alpha);

View file

@ -55,6 +55,11 @@ namespace animator {
bool mouse(float x, float y, guecs::Modifiers mods) {
return $ui.mouse(x, y, mods);
}
std::shared_ptr<sf::Sprite> get_sprite() {
auto viewer = $ui.entity("viewer");
return $ui.get<guecs::Sprite>(viewer).sprite;
}
};
enum class State {
@ -156,10 +161,10 @@ animate2::Sequence sequence{
};
animate2::Transform transform{
.min_x{0.6f},
.min_y{0.6f},
.max_x{0.8f},
.max_y{0.8f},
.min_x{-40.0f},
.min_y{-40.0f},
.max_x{40.0f},
.max_y{40.0f},
.simple{false},
.flipped{false},
.ease_rate{0.5f},
@ -168,8 +173,8 @@ animate2::Transform transform{
.stationary{true},
.toggled{false},
.looped{true},
.easing = ease2::in_out_back,
.motion = ease2::move_rush,
.easing = ease2::out_circle,
.motion = ease2::move_bounce,
};
int main(int argc, char* argv[]) {
@ -194,13 +199,19 @@ int main(int argc, char* argv[]) {
timer.start();
anim.play();
auto viewer = main.$ui.$ui.entity("viewer");
auto& sprite = main.$ui.$ui.get<guecs::Sprite>(viewer).sprite;
auto sprite = main.$ui.get_sprite();
sf::Vector2f pos = sprite->getPosition();
sf::Vector2f scale = sprite->getScale();
while(main.active()) {
timer.begin();
anim.apply(*sprite);
sf::Vector2f new_pos = pos;
sf::Vector2f new_scale = scale;
anim.motion(new_pos, new_scale);
sprite->setPosition(new_pos);
sprite->setScale(new_scale);
main.render();
auto [ticks, alpha] = timer.commit();
@ -211,8 +222,8 @@ int main(int argc, char* argv[]) {
anim.update();
}
// do something with alpha....
main.handle_keyboard_mouse();
}