This guide helps you migrate code from Tweeny 3.x to 4.x. While the core concepts remain the same, version 4 introduces significant API changes centered around a builder pattern and a new event system.
Overview of Changes
The main changes in Tweeny 4.x are:
- Builder Pattern: tweeny::from now returns a builder; you must call build() to create a tween
- Immutable Tweens: Once built, keyframes, durations, and easings cannot be changed
- New Event System: Callbacks are now registered via on() with event types instead of onStep()/onSeek()
- Type System Changes: Duration and step parameters are now strongly typed (uint32_t and int32_t)
- Return Value Changes: Multi-value tweens now return tuples instead of arrays
- New peek() Method: Query values without changing tween state
- Direction API Removed: forward()/backward() removed; use negative steps instead
The Builder Pattern
Basic Usage
Tweeny 3.x:
tweeny_builder< true, FirstValue, RemainingValues... > to(const FirstValue &firstComponent, const RemainingValues &... remainingComponents) &
Adds a target keyframe to the tween.
Definition tweeny.h:109
tweeny_builder & during(FrameCountsType... frame_counts)
Specifies per-component frame durations for the last keyframe segment.
Definition tweeny.h:245
tweeny_builder< false, FirstValue, RemainingValues... > from(FirstValue firstComponent, RemainingValues... remainingComponents)
Creates a new tween builder starting from the specified value(s).
Definition tweeny.h:347
Tweeny 4.x:
The key difference: you must explicitly call build() to create the tween. The builder is reusable:
auto tween1 = builder.build();
builder.to(200).during(120U);
auto tween2 = builder.build();
Compile-Time Safety
Tweeny 4.x enforces that you call to() at least once before building:
Tweeny 3.x: (would create invalid tween)
Tweeny 4.x: (compilation error)
Type System Changes
Duration Types
Tweeny 3.x: Durations could be any unsigned integer type
Tweeny 4.x: Durations must be uint32_t (use the U suffix)
Step Types
Tweeny 3.x: step() accepted floats (percentage) or integers (duration)
tween.step(0.5f);
tween.step(10);
Tweeny 4.x: step() only accepts int32_t (duration), no percentage mode
tween.step(10);
tween.step(-5);
Seek Types
Tweeny 3.x: seek() accepted floats (percentage) or integers (absolute position)
tween.seek(0.5f);
tween.seek(500);
Tweeny 4.x: seek() only accepts uint32_t (absolute frame position)
Return Values
Tweeny 3.x: Multi-value tweens returned std::array
std::array<int, 2> values = tween.step(10);
int x = values[0];
int y = values[1];
Tweeny 4.x: Multi-value tweens return tuples (use structured bindings)
auto [x, y] = tween.step(10);
Direction Changes
Tweeny 3.x: Used forward() and backward() to control direction
tween.backward();
tween.step(10);
tween.forward();
tween.step(10);
Tweeny 4.x: Use signed integers with step()
tween.step(-10);
tween.step(10);
The peek() Method
Tweeny 4.x introduces peek() to query values without mutating state.
Tweeny 3.x: No direct equivalent; you had to step and track state manually
tween.seek(50);
int value = tween.step(0);
Tweeny 4.x: Use peek() for non-mutating queries
tween.seek(50U);
int current = tween.peek();
int preview = tween.peek(75U);
Event System Changes
The callback system has been completely redesigned around an event-based architecture.
Registration
Tweeny 3.x: Used onStep() and onSeek() methods
tween.onStep([](int x, int y) {
printf("Position: (%d, %d)\n", x, y);
return false;
});
tween.onStep([](auto& t) {
return false;
});
tween.onStep([](
auto& t,
int x,
int y) {
return false;
});
tween.onSeek([](
int x,
int y) {
return false;
});
A tween represents an animation between keyframes.
Definition tween.h:68
Tweeny 4.x: Use on() with event type tags
auto [x, y] = t.peek();
printf("Position: (%d, %d)\n", x, y);
});
});
});
auto on(detail::event::step_t, Callback &&cb) -> void
Registers a callback for step() events.
constexpr detail::event::seek_t seek
Event triggered after each seek() call.
Definition event.h:111
constexpr detail::event::jump_t jump
Event triggered after each jump() call.
Definition event.h:139
@ ok
Continue receiving events.
Definition detail/event.h:108
constexpr detail::event::step_t step
Event triggered after each step() call.
Definition event.h:83
Callback Return Values
Tweeny 3.x: Returned bool
return true;
return false;
Tweeny 4.x: Returns event::response enum
@ unsubscribe
Unsubscribe after this callback.
Definition detail/event.h:116
New Event Types
Tweeny 4.x introduces several new event types:
printf("Animation finished!\n");
});
printf("Tween updated to: %d\n", t.peek());
});
printf("Entering keyframe %zu\n", evt.key_frame);
});
printf("Leaving keyframe %zu\n", evt.key_frame);
});
constexpr detail::event::complete_t complete
Event triggered when the animation is at completion.
Definition event.h:174
constexpr detail::event::update_t update
Event triggered whenever the tween position changes.
Definition event.h:280
constexpr detail::event::keyframeEnter_t keyframeEnter
Event triggered when entering a new keyframe segment.
Definition event.h:210
constexpr detail::event::keyframeLeave_t keyframeLeave
Event triggered when leaving a keyframe segment.
Definition event.h:246
Callback Signature Changes
Tweeny 3.x callbacks could receive interpolated values directly. Tweeny 4.x callbacks always receive a tween reference; use peek() to get values:
Tweeny 3.x:
tween.onStep([](int x, int y) {
printf("x=%d, y=%d\n", x, y);
return false;
});
Tweeny 4.x:
auto [x, y] = t.peek();
printf("x=%d, y=%d\n", x, y);
});
Migration Checklist
Use this checklist to migrate your code:
- Add .build() calls
- Find all tweeny::from(...) chains
- Add .build() at the end to create the tween
- Update duration literals
- Change during(100) to during(100U)
- Ensure all duration values use uint32_t
- Update step() calls
- Remove percentage-based stepping (convert to frame counts)
- Use negative values for backward stepping instead of backward()
- Update seek() calls
- Remove percentage-based seeking (calculate frame from percentage manually)
- Add U suffix to all seek values: seek(500) → seek(500U)
- Replace array destructuring with tuple destructuring
- Change std::array<T, N> v = tween.step(...) to auto [v1, v2, ...] = tween.step(...)
- Update callbacks
- Replace onStep(callback) with on(event::step, callback)
- Replace onSeek(callback) with on(event::seek, callback)
- Change callback signatures to accept auto& t parameter
- Use t.peek() to get values inside callbacks
- Change return true/false to return event::response::unsubscribe/ok
- Remove forward()/backward() calls
- Replace with signed step values
- Use peek() for non-mutating queries
- Replace step(0) patterns with peek()
- Use peek(frame) to preview values at different positions
- Consider new event types
Complete Migration Example
Tweeny 3.x:
#include "tweeny.h"
tween.onStep([](int x, int y, int alpha) {
draw_sprite(x, y, alpha);
return false;
});
tween.onSeek([](int x, int y, int alpha) {
printf("Seeked to: %d, %d, %d\n", x, y, alpha);
return false;
});
render();
}
render();
}
auto step(int32_t frames) -> tween_value_t
Advances or rewinds the animation by a frame delta.
auto progress() const -> float
Returns the animation completion percentage.
constexpr detail::backOutEasing backOut
Easing function that overshoots the target before settling back.
Definition easing.h:131
Tweeny 4.x:
#include <tweeny/tweeny.h>
.build();
auto [x, y, alpha] = t.peek();
draw_sprite(x, y, alpha);
});
auto [x, y, alpha] = t.peek();
printf("Seeked to: %d, %d, %d\n", x, y, alpha);
});
printf("Animation complete!\n");
});
render();
}
render();
}
Done!
For detailed information on the new API, see the Tweeny Manual.