diff --git a/include/point.hpp b/include/point.hpp index 2569a37..9560f45 100644 --- a/include/point.hpp +++ b/include/point.hpp @@ -70,7 +70,12 @@ Point operator-(const Point& p1, const Point& p2) return Point(p1.x - p2.x, p1.y - p2.y); } -Point operator*(const Point& p1, float f) +Point operator+(const Point& p1, const Point& p2) +{ + return Point(p1.x + p2.x, p1.y + p2.y); +} + +Point operator*(float f, const Point& p1) { return Point(p1.x*f, p1.y*f); } diff --git a/include/transition.hpp b/include/transition.hpp index 4ab1b5d..937f645 100644 --- a/include/transition.hpp +++ b/include/transition.hpp @@ -1,51 +1,77 @@ #pragma once +#include +#include + +float ratio(float t) +{ + return 1.0f / (1.0f + std::expf(-(10.0f*t - 5.0f))); +} template class Transition { public: + using ChronoPoint = std::chrono::steady_clock::time_point; + Transition() : + m_start_value(), m_current_value(), m_target_value(), + m_start_time(std::chrono::steady_clock::now()), m_speed(0.0f) {} Transition(const T& value, float speed=1.0f) : + m_start_value(value), m_current_value(value), m_target_value(value), + m_start_time(std::chrono::steady_clock::now()), m_speed(speed) {} template explicit Transition(Args&&... args) : - m_current_value(std::forward(args)...), - m_target_value(m_current_value), + m_start_value(std::forward(args)...), + m_current_value(m_start_value), + m_target_value(m_start_value), + m_start_time(std::chrono::steady_clock::now()), m_speed(1.0f) {} - operator const T&() const + operator const T&() { + autoUpdate(); return m_current_value; } - void update(float dt = 0.016f) - { - m_current_value += (m_target_value - m_current_value) * (m_speed * dt); - } - void operator=(const T& value) { + m_start_value = m_current_value; + m_start_time = std::chrono::steady_clock::now(); m_target_value = value; + m_delta = m_target_value - m_start_value; } - void speed(float s) + void setSpeed(float s) { m_speed = s; } private: - T m_current_value; + T m_start_value; T m_target_value; + T m_delta; + T m_current_value; + ChronoPoint m_start_time; float m_speed; + + void autoUpdate() + { + ChronoPoint now(std::chrono::steady_clock::now()); + double t(static_cast(std::chrono::duration_cast(now - m_start_time).count())); + m_current_value = m_start_value + ratio(t * 0.001f * m_speed) * m_delta; + } }; + + diff --git a/src/main.cpp b/src/main.cpp index e7e04a2..1f65458 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -47,13 +47,22 @@ int main() event_manager.addKeyReleasedCallback(sf::Keyboard::S, [&](const sf::Event&) {draw_signal =!draw_signal; }); event_manager.addKeyReleasedCallback(sf::Keyboard::Q, [&](const sf::Event&) {painter.draw_harmonics = !painter.draw_harmonics; }); event_manager.addKeyReleasedCallback(sf::Keyboard::D, [&](const sf::Event&) {painter.draw_arms = !painter.draw_arms; }); - event_manager.addKeyReleasedCallback(sf::Keyboard::Space, [&](const sf::Event&) {slow = !slow; if (slow) { painter.setDt(0.0008); } else { painter.setDt(0.016); }}); + event_manager.addKeyReleasedCallback(sf::Keyboard::Space, [&](const sf::Event&) {slow = !slow; if (slow) { + painter.setDt(0.0008); + zoom = 30.0f; + zoom.setSpeed(0.25f); + focus.setSpeed(8.0f); + } else { + painter.setDt(0.016); + zoom = 1.0f; + zoom.setSpeed(1.0f); + focus = Point(0.0f, 0.0f); + focus.setSpeed(0.5f); + }}); while (window.isOpen()) { painter.update(); - zoom.update(); - focus.update(); current_mouse_pos = sf::Mouse::getPosition(window); event_manager.processEvents(); @@ -70,19 +79,10 @@ int main() tf_in.translate(win_width * 0.5, win_height * 0.5); tf_in.scale(zoom, zoom); tf_in.translate(-toV2f(focus)); + if (slow) { - zoom = 30.0f; - zoom.speed(0.125f); focus = painter.getResult(); - focus.speed(5.0f); - } - else - { - zoom = 1.0f; - zoom.speed(4.0f); - focus = Point(0.0f, 0.0f); - focus.speed(1.0f); } // Draw