Most of the transforms kind of work now need to hook in the new timer.
This commit is contained in:
parent
20d7612f1c
commit
7f14a39edf
6 changed files with 47 additions and 27 deletions
20
animate2.cpp
20
animate2.cpp
|
|
@ -78,6 +78,7 @@ namespace animate2 {
|
||||||
|
|
||||||
void Timer::start() {
|
void Timer::start() {
|
||||||
clock.start();
|
clock.start();
|
||||||
|
prev_time = clock.getElapsedTime().asSeconds();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Timer::reset() {
|
void Timer::reset() {
|
||||||
|
|
@ -86,25 +87,34 @@ namespace animate2 {
|
||||||
|
|
||||||
void Timer::restart() {
|
void Timer::restart() {
|
||||||
clock.restart();
|
clock.restart();
|
||||||
|
prev_time = clock.getElapsedTime().asSeconds();
|
||||||
}
|
}
|
||||||
|
|
||||||
sf::Time Timer::getElapsedTime() {
|
sf::Time Timer::getElapsedTime() {
|
||||||
return clock.getElapsedTime();
|
return clock.getElapsedTime();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Timer::begin() {
|
|
||||||
prev_time = clock.getElapsedTime().asSeconds();
|
|
||||||
}
|
|
||||||
|
|
||||||
std::pair<int, double> Timer::commit() {
|
std::pair<int, double> Timer::commit() {
|
||||||
|
// determine frame duration based on previous time
|
||||||
current_time = clock.getElapsedTime().asSeconds();
|
current_time = clock.getElapsedTime().asSeconds();
|
||||||
frame_duration = current_time - prev_time;
|
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;
|
accumulator += frame_duration;
|
||||||
|
|
||||||
|
// find the tick count based on DELTA
|
||||||
double tick_count = accumulator / DELTA;
|
double tick_count = accumulator / DELTA;
|
||||||
|
// alpha is then the remainder/fractional part
|
||||||
double alpha = modf(tick_count, &tick_count);
|
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};
|
return {int(tick_count), alpha};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -26,7 +26,6 @@ namespace animate2 {
|
||||||
double alpha = 0.0;
|
double alpha = 0.0;
|
||||||
sf::Clock clock{};
|
sf::Clock clock{};
|
||||||
|
|
||||||
void begin();
|
|
||||||
std::pair<int, double> commit();
|
std::pair<int, double> commit();
|
||||||
void start();
|
void start();
|
||||||
void reset();
|
void reset();
|
||||||
|
|
|
||||||
13
ease2.cpp
13
ease2.cpp
|
|
@ -15,7 +15,7 @@ namespace ease2 {
|
||||||
return (std::sin(x) + 1.0) / 2.0;
|
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)));
|
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;
|
: (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);
|
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);
|
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.x = std::lerp(tr.min_x, tr.max_x, tick);
|
||||||
scale_out.y = std::lerp(tr.min_y, tr.max_y, 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);
|
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) {
|
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);
|
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);
|
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) {
|
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.x += std::lerp(tr.min_x, tr.max_x, tick);
|
||||||
pos_out.y = std::lerp(tr.min_y, tr.max_y, 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) {
|
void move_none(Transform &tr, sf::Vector2f& pos_out, sf::Vector2f& scale_out, float tick) {
|
||||||
|
|
|
||||||
|
|
@ -10,7 +10,7 @@ namespace ease2 {
|
||||||
using MoveFunc = std::function<void(animate2::Transform &tr, sf::Vector2f& pos_out, sf::Vector2f& scale_out, float tick)>;
|
using MoveFunc = std::function<void(animate2::Transform &tr, sf::Vector2f& pos_out, sf::Vector2f& scale_out, float tick)>;
|
||||||
|
|
||||||
double sine(double x);
|
double sine(double x);
|
||||||
double out_circ(double x);
|
double out_circle(double x);
|
||||||
double out_bounce(double x);
|
double out_bounce(double x);
|
||||||
double in_out_back(double x);
|
double in_out_back(double x);
|
||||||
double random(double tick);
|
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 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_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_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 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_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 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 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);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -196,9 +196,9 @@ TEST_CASE("confirm transition changes work", "[animation-new]") {
|
||||||
|
|
||||||
TEST_CASE("playing with delta time", "[animation-new]") {
|
TEST_CASE("playing with delta time", "[animation-new]") {
|
||||||
animate2::Timer timer;
|
animate2::Timer timer;
|
||||||
|
timer.start();
|
||||||
|
|
||||||
for(int i = 0; i < 20; i++) {
|
for(int i = 0; i < 20; i++) {
|
||||||
timer.begin();
|
|
||||||
FAKE_RENDER();
|
FAKE_RENDER();
|
||||||
auto [tick_count, alpha] = timer.commit();
|
auto [tick_count, alpha] = timer.commit();
|
||||||
fmt::println("tick: {}, alpha: {}", tick_count, alpha);
|
fmt::println("tick: {}, alpha: {}", tick_count, alpha);
|
||||||
|
|
|
||||||
|
|
@ -55,6 +55,11 @@ namespace animator {
|
||||||
bool mouse(float x, float y, guecs::Modifiers mods) {
|
bool mouse(float x, float y, guecs::Modifiers mods) {
|
||||||
return $ui.mouse(x, y, 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 {
|
enum class State {
|
||||||
|
|
@ -156,10 +161,10 @@ animate2::Sequence sequence{
|
||||||
};
|
};
|
||||||
|
|
||||||
animate2::Transform transform{
|
animate2::Transform transform{
|
||||||
.min_x{0.6f},
|
.min_x{-40.0f},
|
||||||
.min_y{0.6f},
|
.min_y{-40.0f},
|
||||||
.max_x{0.8f},
|
.max_x{40.0f},
|
||||||
.max_y{0.8f},
|
.max_y{40.0f},
|
||||||
.simple{false},
|
.simple{false},
|
||||||
.flipped{false},
|
.flipped{false},
|
||||||
.ease_rate{0.5f},
|
.ease_rate{0.5f},
|
||||||
|
|
@ -168,8 +173,8 @@ animate2::Transform transform{
|
||||||
.stationary{true},
|
.stationary{true},
|
||||||
.toggled{false},
|
.toggled{false},
|
||||||
.looped{true},
|
.looped{true},
|
||||||
.easing = ease2::in_out_back,
|
.easing = ease2::out_circle,
|
||||||
.motion = ease2::move_rush,
|
.motion = ease2::move_bounce,
|
||||||
};
|
};
|
||||||
|
|
||||||
int main(int argc, char* argv[]) {
|
int main(int argc, char* argv[]) {
|
||||||
|
|
@ -194,13 +199,19 @@ int main(int argc, char* argv[]) {
|
||||||
timer.start();
|
timer.start();
|
||||||
anim.play();
|
anim.play();
|
||||||
|
|
||||||
auto viewer = main.$ui.$ui.entity("viewer");
|
auto sprite = main.$ui.get_sprite();
|
||||||
auto& sprite = main.$ui.$ui.get<guecs::Sprite>(viewer).sprite;
|
sf::Vector2f pos = sprite->getPosition();
|
||||||
|
sf::Vector2f scale = sprite->getScale();
|
||||||
|
|
||||||
while(main.active()) {
|
while(main.active()) {
|
||||||
timer.begin();
|
|
||||||
|
|
||||||
anim.apply(*sprite);
|
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();
|
main.render();
|
||||||
|
|
||||||
auto [ticks, alpha] = timer.commit();
|
auto [ticks, alpha] = timer.commit();
|
||||||
|
|
@ -211,8 +222,8 @@ int main(int argc, char* argv[]) {
|
||||||
anim.update();
|
anim.update();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// do something with alpha....
|
// do something with alpha....
|
||||||
|
|
||||||
main.handle_keyboard_mouse();
|
main.handle_keyboard_mouse();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue