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() { };
31 const Sprite *GetSprite() const { return sprite; }
32 int FrameTime() const { return frameTime; }
33 bool Repeat() const { return repeat; }
36 void SetSprite(const Sprite *s) { sprite = s; }
37 void SetFrameTime(int t) { frameTime = t; }
38 void SetRepeat(bool r) { repeat = r; }
41 virtual int NumFrames() const = 0;
42 virtual int Col(int frame) const = 0;
43 virtual int Row(int frame) const = 0;
44 virtual geometry::Vector<int> Offset(int frame) const { return geometry::Vector<int>(); }
54 class AnimationRunner {
57 explicit AnimationRunner(const Animation *a = 0, int colOffset = 0, int rowOffset = 0)
58 : animation(a), sprite(a ? a->GetSprite() : 0), colOffset(colOffset), rowOffset(rowOffset) { }
61 bool Valid() const { return animation; }
62 void Clear() { animation = 0; timer = app::Timer<Uint32>(); }
64 void Start(app::State &ctrl) {
65 timer = ctrl.GraphicsTimers().StartInterval(animation->FrameTime());
67 void Start(app::Application &ctrl) {
68 timer = ctrl.GlobalTimers().StartInterval(animation->FrameTime());
71 timer = app::Timer<Uint32>();
73 bool Started() const {
74 return timer.Started();
76 bool Running() const {
77 return timer.Running() && (animation->Repeat() || timer.Iteration() < animation->NumFrames());
79 bool Finished() const {
80 return Started() && !Running();
82 bool JustFinished() const {
83 return timer.JustHit() && timer.Iteration() == animation->NumFrames();
86 const app::Timer<Uint32> &GetTimer() { return timer; }
88 void SetColOffset(int offset) { colOffset = offset; }
89 int ColOffset() const { return colOffset; }
90 void SetRowOffset(int offset) { rowOffset = offset; }
91 int RowOffset() const { return rowOffset; }
93 void ChangeSprite(const Sprite *s) { sprite = s; }
94 const Sprite *GetSprite() const { return sprite; }
96 void Draw(SDL_Surface *dest, geometry::Vector<int> position) const {
97 sprite->Draw(dest, position + animation->Offset(Frame()), animation->Col(Frame()) + ColOffset(), animation->Row(Frame()) + RowOffset());
99 void DrawTopRight(SDL_Surface *dest, geometry::Vector<int> position) const {
100 geometry::Vector<int> offset(-sprite->Width(), 0);
101 Draw(dest, position + offset);
103 void DrawCenter(SDL_Surface *dest, geometry::Vector<int> position) const {
104 Draw(dest, position - (sprite->Size() / 2));
106 void DrawCenterBottom(SDL_Surface *dest, geometry::Vector<int> position) const {
107 geometry::Vector<int> offset(-sprite->Width() / 2, -sprite->Height());
108 Draw(dest, position + offset);
111 int Frame() const { return Running() ? (timer.Iteration() % animation->NumFrames()) : 0; }
114 const Animation *animation;
115 const graphics::Sprite *sprite;
116 app::Timer<Uint32> timer;
124 #endif /* GRAPHICS_SIMPLEANIMATION_H_ */