]> git.localhorst.tv Git - l2e.git/blob - src/graphics/Animation.h
yet more stuff for AnimationRunner
[l2e.git] / src / graphics / Animation.h
1 /*
2  *  Animation.h
3  *
4  *  Created on: Aug 11, 2012
5  *      Author: holy
6  */
7
8 #ifndef GRAPHICS_ANIMATION_H_
9 #define GRAPHICS_ANIMATION_H_
10
11 #include "Sprite.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"
18
19 #include <SDL.h>
20
21 namespace graphics {
22
23 class Animation {
24
25 public:
26         Animation()
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() { };
31
32 public:
33
34         const Sprite *GetSprite() const { return sprite; }
35         int FrameTime() const { return frameTime; }
36         bool Repeat() const { return repeat; }
37
38 public:
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>(); }
43
44 private:
45         const Sprite *sprite;
46         int frameTime;
47         bool repeat;
48
49 };
50
51
52 class AnimationRunner {
53
54 public:
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) { }
57
58 public:
59         bool Valid() const { return animation; }
60         void Clear() { animation = 0; timer = app::Timer<Uint32>(); }
61
62         void Start(app::State &ctrl) {
63                 timer = ctrl.GraphicsTimers().StartInterval(animation->FrameTime());
64         }
65         void Start(app::Application &ctrl) {
66                 timer = ctrl.GlobalTimers().StartInterval(animation->FrameTime());
67         }
68         void Stop() {
69                 timer = app::Timer<Uint32>();
70         }
71         bool Started() const {
72                 return timer.Started();
73         }
74         bool Running() const {
75                 return timer.Running() && (animation->Repeat() || timer.Iteration() < animation->NumFrames());
76         }
77         bool Finished() const {
78                 return Started() && !Running();
79         }
80         bool JustFinished() const {
81                 return timer.JustHit() && timer.Iteration() == animation->NumFrames();
82         }
83
84         const app::Timer<Uint32> &GetTimer() { return timer; }
85
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; }
90
91         void ChangeSprite(const Sprite *s) { sprite = s; }
92         const Sprite *GetSprite() const { return sprite; }
93
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());
96         }
97         void DrawTopRight(SDL_Surface *dest, geometry::Point<int> position) const {
98                 geometry::Vector<int> offset(-sprite->Width(), 0);
99                 Draw(dest, position + offset);
100         }
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);
104         }
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);
108         }
109
110         int Frame() const { return Running() ? (timer.Iteration() % animation->NumFrames()) : 0; }
111
112 private:
113         const Animation *animation;
114         const graphics::Sprite *sprite;
115         app::Timer<Uint32> timer;
116         int colOffset;
117         int rowOffset;
118
119 };
120
121 }
122
123 #endif /* GRAPHICS_SIMPLEANIMATION_H_ */