Animator now can apply transforms using the timer alpha which mostly works.
This commit is contained in:
parent
7f14a39edf
commit
785d0240da
4 changed files with 29 additions and 31 deletions
21
animate2.cpp
21
animate2.cpp
|
|
@ -71,9 +71,8 @@ namespace animate2 {
|
|||
update_frame();
|
||||
}
|
||||
|
||||
void Animate2::motion(sf::Vector2f& pos, sf::Vector2f& scale) {
|
||||
$transform.twitching($sequence);
|
||||
$transform.lerp($sequence, pos, scale);
|
||||
void Animate2::motion(sf::Vector2f& pos, sf::Vector2f& scale, float alpha) {
|
||||
$transform.lerp($sequence, pos, scale, alpha);
|
||||
}
|
||||
|
||||
void Timer::start() {
|
||||
|
|
@ -106,25 +105,25 @@ namespace animate2 {
|
|||
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);
|
||||
double tick_count = floor(accumulator / DELTA);
|
||||
|
||||
// reduce accumulator by the number of DELTAS
|
||||
accumulator -= tick_count * DELTA;
|
||||
// that leaves the remaining errors for next loop
|
||||
|
||||
// alpha is then what we lerp...but WHY?!
|
||||
alpha = accumulator / DELTA;
|
||||
|
||||
// return the number of even DELTA ticks and the alpha
|
||||
return {int(tick_count), alpha};
|
||||
}
|
||||
|
||||
float Transform::twitching(Sequence& seq) {
|
||||
float tick = 1 - std::powf(ease_rate, seq.subframe + 0.0001);
|
||||
return easing(tick);
|
||||
float Transform::twitching(Sequence& seq, float alpha) {
|
||||
return easing(alpha);
|
||||
}
|
||||
|
||||
void Transform::lerp(Sequence& seq, sf::Vector2f& pos_out, sf::Vector2f& scale_out) {
|
||||
float tick = std::abs(twitching(seq));
|
||||
void Transform::lerp(Sequence& seq, sf::Vector2f& pos_out, sf::Vector2f& scale_out, float alpha) {
|
||||
float tick = std::abs(twitching(seq, alpha));
|
||||
return motion(*this, pos_out, scale_out, tick);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -64,8 +64,8 @@ namespace animate2 {
|
|||
ease2::EaseFunc easing = ease2::in_out_back;
|
||||
ease2::MoveFunc motion = ease2::move_rush;
|
||||
|
||||
float twitching(Sequence& seq);
|
||||
void lerp(Sequence& seq, sf::Vector2f& pos_out, sf::Vector2f& scale_out);
|
||||
float twitching(Sequence& seq, float alph);
|
||||
void lerp(Sequence& seq, sf::Vector2f& pos_out, sf::Vector2f& scale_out, float alpha);
|
||||
};
|
||||
|
||||
/* Gets the number of times it looped, and returns if it should stop. */
|
||||
|
|
@ -94,7 +94,7 @@ namespace animate2 {
|
|||
void apply(sf::Sprite& sprite);
|
||||
void update_frame();
|
||||
void update();
|
||||
void motion(sf::Vector2f& pos_out, sf::Vector2f& scale_out);
|
||||
void motion(sf::Vector2f& pos_out, sf::Vector2f& scale_out, float alpha);
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -180,7 +180,7 @@ TEST_CASE("confirm transition changes work", "[animation-new]") {
|
|||
start = clock.getElapsedTime();
|
||||
|
||||
anim.update();
|
||||
anim.motion(pos, scale);
|
||||
anim.motion(pos, scale, 0.8f);
|
||||
std::this_thread::sleep_for(10ms);
|
||||
fmt::println("POSITION: {},{}; SCALE: {},{}; current: {}; subframe: {}",
|
||||
pos.x, pos.y, scale.x, scale.y, anim.$sequence.current, anim.$sequence.subframe);
|
||||
|
|
|
|||
|
|
@ -161,10 +161,10 @@ animate2::Sequence sequence{
|
|||
};
|
||||
|
||||
animate2::Transform transform{
|
||||
.min_x{-40.0f},
|
||||
.min_y{-40.0f},
|
||||
.max_x{40.0f},
|
||||
.max_y{40.0f},
|
||||
.min_x{-20.0f},
|
||||
.min_y{-20.0f},
|
||||
.max_x{20.0f},
|
||||
.max_y{20.0f},
|
||||
.simple{false},
|
||||
.flipped{false},
|
||||
.ease_rate{0.5f},
|
||||
|
|
@ -173,7 +173,7 @@ animate2::Transform transform{
|
|||
.stationary{true},
|
||||
.toggled{false},
|
||||
.looped{true},
|
||||
.easing = ease2::out_circle,
|
||||
.easing = ease2::out_bounce,
|
||||
.motion = ease2::move_bounce,
|
||||
};
|
||||
|
||||
|
|
@ -204,16 +204,6 @@ int main(int argc, char* argv[]) {
|
|||
sf::Vector2f scale = sprite->getScale();
|
||||
|
||||
while(main.active()) {
|
||||
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();
|
||||
fmt::println("TICK: {}, alpha: {}", ticks, alpha);
|
||||
|
||||
|
|
@ -222,8 +212,17 @@ int main(int argc, char* argv[]) {
|
|||
anim.update();
|
||||
}
|
||||
|
||||
// do something with alpha....
|
||||
anim.apply(*sprite);
|
||||
|
||||
sf::Vector2f new_pos = pos;
|
||||
sf::Vector2f new_scale = scale;
|
||||
anim.motion(new_pos, new_scale, alpha);
|
||||
sprite->setPosition(new_pos);
|
||||
sprite->setScale(new_scale);
|
||||
|
||||
main.render();
|
||||
|
||||
// do something with alpha....
|
||||
main.handle_keyboard_mouse();
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue