Refactor the animate2 and then start working on the motion feature.

This commit is contained in:
Zed A. Shaw 2026-01-21 13:13:58 -05:00
parent 992b8f0e0a
commit 60c405b1fc
9 changed files with 408 additions and 290 deletions

81
animate2.hpp Normal file
View file

@ -0,0 +1,81 @@
#pragma once
#include <memory>
#include <chrono>
#include "easings.hpp"
#include <SFML/Graphics/Rect.hpp>
#include <SFML/Graphics/Sprite.hpp>
#include <SFML/System/Clock.hpp>
#include <SFML/System/Time.hpp>
#include <functional>
namespace animate2 {
struct Sheet {
int width{0};
int height{0};
int frame_width{0};
int frame_height{0};
};
struct Sequence {
std::vector<int> frames{};
std::vector<std::chrono::milliseconds> durations{};
size_t current{0};
int loop_count{0};
size_t frame_count{frames.size()};
sf::Clock timer{};
};
struct Transform {
// how to know when a transform ends?
float min_x{1.0f};
float min_y{1.0f};
float max_x{1.0f};
float max_y{1.0f};
bool simple{true};
bool flipped{false};
float ease_rate{0.5f};
bool scaled{false};
bool stationary{false};
// these can handled by the onLoop, same as ganim8 does it
bool toggled{false};
bool looped{false};
std::shared_ptr<sf::Shader> shader{nullptr};
// change to using a callback function for these
ease::Style easing = ease::IN_OUT_BACK;
ease::Motion motion = ease::RUSH;
float twitching(Sequence& seq);
void lerp(Sequence& seq, sf::Vector2f& scale_out, sf::Vector2f& pos_out);
};
/* Gets the number of times it looped, and returns if it should stop. */
using OnLoopHandler = std::function<bool(Sequence& seq, Transform& tr)>;
using OnFrameHandler = std::function<void()>;
inline bool DefaultOnLoop(Sequence& seq, Transform& tr) {
seq.current = 0;
return tr.looped;
}
class Animate2 {
public:
Sheet $sheet;
Sequence $sequence;
Transform $transform;
OnFrameHandler onFrame = nullptr;
std::vector<sf::IntRect> $frame_rects{calc_frames()};
OnLoopHandler onLoop = DefaultOnLoop;
bool playing = false;
std::vector<sf::IntRect> calc_frames();
void play();
void stop();
void apply(sf::Sprite& sprite);
void update_frame();
void update();
void motion(sf::Vector2f& pos, sf::Vector2f& scale);
};
}