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/operators.h"
16 #include "../geometry/Point.h"
17 #include "../geometry/Vector.h"
27 : sprite(0), frameTime(0), repeat(false) { }
28 Animation(const Sprite *sprite, int frameTime, bool repeat = false)
29 : sprite(sprite), frameTime(frameTime), repeat(repeat) { }
30 virtual ~Animation() { };
34 const Sprite *GetSprite() const { return sprite; }
35 int FrameTime() const { return frameTime; }
36 bool Repeat() const { return repeat; }
39 virtual int NumFrames() const = 0;
40 virtual int Col(int frame) const = 0;
41 virtual int Row(int frame) const = 0;
42 virtual geometry::Vector<int> Offset(int frame) const { return geometry::Vector<int>(); }
52 class AnimationRunner {
55 explicit AnimationRunner(const Animation *a = 0, int colOffset = 0, int rowOffset = 0)
56 : animation(a), sprite(a ? a->GetSprite() : 0), colOffset(colOffset), rowOffset(rowOffset) { }
59 bool Valid() const { return animation; }
60 void Clear() { animation = 0; timer = app::Timer<Uint32>(); }
62 void Start(app::State &ctrl) {
63 timer = ctrl.GraphicsTimers().StartInterval(animation->FrameTime());
65 void Start(app::Application &ctrl) {
66 timer = ctrl.GlobalTimers().StartInterval(animation->FrameTime());
69 timer = app::Timer<Uint32>();
71 bool Started() const {
72 return timer.Started();
74 bool Running() const {
75 return timer.Running() && (animation->Repeat() || timer.Iteration() < animation->NumFrames());
77 bool Finished() const {
78 return Started() && !Running();
80 bool JustFinished() const {
81 return timer.JustHit() && timer.Iteration() == animation->NumFrames();
84 const app::Timer<Uint32> &GetTimer() { return timer; }
86 void SetColOffset(int offset) { colOffset = offset; }
87 int ColOffset() const { return colOffset; }
88 void SetRowOffset(int offset) { rowOffset = offset; }
89 int RowOffset() const { return rowOffset; }
91 void ChangeSprite(const Sprite *s) { sprite = s; }
92 const Sprite *GetSprite() const { return sprite; }
94 void Draw(SDL_Surface *dest, geometry::Point<int> position) const {
95 sprite->Draw(dest, position + animation->Offset(Frame()), animation->Col(Frame()) + ColOffset(), animation->Row(Frame()) + RowOffset());
97 void DrawTopRight(SDL_Surface *dest, geometry::Point<int> position) const {
98 geometry::Vector<int> offset(-sprite->Width(), 0);
99 Draw(dest, position + offset);
101 void DrawCenter(SDL_Surface *dest, geometry::Point<int> position) const {
102 geometry::Vector<int> offset(-sprite->Width() / 2, -sprite->Height() / 2);
103 Draw(dest, position + offset);
105 void DrawCenterBottom(SDL_Surface *dest, geometry::Point<int> position) const {
106 geometry::Vector<int> offset(-sprite->Width() / 2, -sprite->Height());
107 Draw(dest, position + offset);
110 int Frame() const { return Running() ? (timer.Iteration() % animation->NumFrames()) : 0; }
113 const Animation *animation;
114 const graphics::Sprite *sprite;
115 app::Timer<Uint32> timer;
123 #endif /* GRAPHICS_SIMPLEANIMATION_H_ */