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), colOffset(0), rowOffset(0), repeat(false) { }
28 Animation(const Sprite *sprite, int frameTime, bool repeat = false)
29 : sprite(sprite), frameTime(frameTime), colOffset(0), rowOffset(0), repeat(repeat) { }
30 virtual ~Animation() { };
33 void Start(app::State &ctrl) {
34 timer = ctrl.GraphicsTimers().StartInterval(frameTime);
36 void Start(app::Application &ctrl) {
37 timer = ctrl.GlobalTimers().StartInterval(frameTime);
40 timer = app::Timer<Uint32>();
42 bool Started() const {
43 return timer.Started();
45 bool Running() const {
46 return timer.Running() && (repeat || timer.Iteration() < NumFrames());
48 bool Finished() const {
49 return Started() && !Running();
51 bool JustFinished() const {
52 return timer.JustHit() && timer.Iteration() == NumFrames();
55 const app::Timer<Uint32> &GetTimer() { return timer; }
57 void SetColOffset(int offset) { colOffset = offset; }
58 int ColOffset() const { return colOffset; }
59 void SetRowOffset(int offset) { rowOffset = offset; }
60 int RowOffset() const { return rowOffset; }
62 const Sprite *GetSprite() const { return sprite; }
63 void ChangeSprite(const Sprite *s) { sprite = s; }
65 virtual void Draw(SDL_Surface *dest, geometry::Point<int> position) const {
66 sprite->Draw(dest, position, Col() + ColOffset(), Row() + RowOffset());
68 void DrawTopRight(SDL_Surface *dest, geometry::Point<int> position) const {
69 geometry::Vector<int> offset(-sprite->Width(), 0);
70 Draw(dest, position + offset);
72 void DrawCenter(SDL_Surface *dest, geometry::Point<int> position) const {
73 geometry::Vector<int> offset(-sprite->Width() / 2, -sprite->Height() / 2);
74 Draw(dest, position + offset);
76 void DrawCenterBottom(SDL_Surface *dest, geometry::Point<int> position) const {
77 geometry::Vector<int> offset(-sprite->Width() / 2, -sprite->Height());
78 Draw(dest, position + offset);
81 int Frame() const { return Running() ? (timer.Iteration() % NumFrames()) : 0; }
84 virtual int Col() const = 0;
85 virtual int Row() const = 0;
86 virtual int NumFrames() const = 0;
90 app::Timer<Uint32> timer;
100 #endif /* GRAPHICS_SIMPLEANIMATION_H_ */