4 * Created on: Aug 11, 2012
8 #ifndef GRAPHICS_ANIMATION_H_
9 #define GRAPHICS_ANIMATION_H_
12 #include "../app/Application.h"
13 #include "../app/State.h"
14 #include "../app/Timer.h"
15 #include "../geometry/Vector.h"
25 : sprite(0), frameTime(0), repeat(false) { }
26 Animation(const Sprite *sprite, int frameTime, bool repeat = false)
27 : sprite(sprite), frameTime(frameTime), repeat(repeat) { }
28 virtual ~Animation() { };
32 const Sprite *GetSprite() const { return sprite; }
33 int FrameTime() const { return frameTime; }
34 bool Repeat() const { return repeat; }
37 virtual int NumFrames() const = 0;
38 virtual int Col(int frame) const = 0;
39 virtual int Row(int frame) const = 0;
40 virtual geometry::Vector<int> Offset(int frame) const { return geometry::Vector<int>(); }
50 class AnimationRunner {
53 explicit AnimationRunner(const Animation *a = 0, int colOffset = 0, int rowOffset = 0)
54 : animation(a), sprite(a ? a->GetSprite() : 0), colOffset(colOffset), rowOffset(rowOffset) { }
57 bool Valid() const { return animation; }
58 void Clear() { animation = 0; timer = app::Timer<Uint32>(); }
60 void Start(app::State &ctrl) {
61 timer = ctrl.GraphicsTimers().StartInterval(animation->FrameTime());
63 void Start(app::Application &ctrl) {
64 timer = ctrl.GlobalTimers().StartInterval(animation->FrameTime());
67 timer = app::Timer<Uint32>();
69 bool Started() const {
70 return timer.Started();
72 bool Running() const {
73 return timer.Running() && (animation->Repeat() || timer.Iteration() < animation->NumFrames());
75 bool Finished() const {
76 return Started() && !Running();
78 bool JustFinished() const {
79 return timer.JustHit() && timer.Iteration() == animation->NumFrames();
82 const app::Timer<Uint32> &GetTimer() { return timer; }
84 void SetColOffset(int offset) { colOffset = offset; }
85 int ColOffset() const { return colOffset; }
86 void SetRowOffset(int offset) { rowOffset = offset; }
87 int RowOffset() const { return rowOffset; }
89 void ChangeSprite(const Sprite *s) { sprite = s; }
90 const Sprite *GetSprite() const { return sprite; }
92 void Draw(SDL_Surface *dest, geometry::Vector<int> position) const {
93 sprite->Draw(dest, position + animation->Offset(Frame()), animation->Col(Frame()) + ColOffset(), animation->Row(Frame()) + RowOffset());
95 void DrawTopRight(SDL_Surface *dest, geometry::Vector<int> position) const {
96 geometry::Vector<int> offset(-sprite->Width(), 0);
97 Draw(dest, position + offset);
99 void DrawCenter(SDL_Surface *dest, geometry::Vector<int> position) const {
100 Draw(dest, position - (sprite->Size() / 2));
102 void DrawCenterBottom(SDL_Surface *dest, geometry::Vector<int> position) const {
103 geometry::Vector<int> offset(-sprite->Width() / 2, -sprite->Height());
104 Draw(dest, position + offset);
107 int Frame() const { return Running() ? (timer.Iteration() % animation->NumFrames()) : 0; }
110 const Animation *animation;
111 const graphics::Sprite *sprite;
112 app::Timer<Uint32> timer;
120 #endif /* GRAPHICS_SIMPLEANIMATION_H_ */