]> git.localhorst.tv Git - l2e.git/blob - src/graphics/Animation.h
removed stupid file headers that eclipse put in
[l2e.git] / src / graphics / Animation.h
1 #ifndef GRAPHICS_ANIMATION_H_
2 #define GRAPHICS_ANIMATION_H_
3
4 #include "Sprite.h"
5 #include "../app/Application.h"
6 #include "../app/State.h"
7 #include "../app/Timer.h"
8 #include "../loader/fwd.h"
9 #include "../geometry/Vector.h"
10
11 #include <memory>
12 #include <SDL.h>
13
14 namespace graphics {
15
16 class Animation {
17
18 public:
19         static const int TYPE_ID = 401;
20
21 public:
22         Animation()
23         : sprite(0), frameTime(0), repeat(false) { }
24         Animation(const Sprite *sprite, int frameTime, bool repeat = false)
25         : sprite(sprite), frameTime(frameTime), repeat(repeat) { }
26         virtual ~Animation() { };
27
28 public:
29         const Sprite *GetSprite() const { return sprite; }
30         int FrameTime() const { return frameTime; }
31         bool Repeat() const { return repeat; }
32
33 public:
34         void SetSprite(const Sprite *s) { sprite = s; }
35         void SetFrameTime(int t) { frameTime = t; }
36         void SetRepeat(bool r) { repeat = r; }
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         static void CreateTypeDescription();
45
46 protected:
47         static void AddFields(loader::TypeDescription &, const Animation &, std::ptrdiff_t offset);
48
49 private:
50         const Sprite *sprite;
51         int frameTime;
52         bool repeat;
53
54 };
55
56
57 class AnimationRunner {
58
59 public:
60         explicit AnimationRunner(const Animation *a = 0, int colOffset = 0, int rowOffset = 0)
61         : animation(a), sprite(0), frameShift(0), colOffset(colOffset), rowOffset(rowOffset) { }
62
63 public:
64         bool Valid() const { return animation; }
65         void Clear() { animation = 0; timer = app::Timer<Uint32>(); }
66
67         void Start(app::State &ctrl) {
68                 timer = ctrl.GraphicsTimers().StartInterval(animation->FrameTime());
69         }
70         void Start(app::Application &ctrl) {
71                 timer = ctrl.GlobalTimers().StartInterval(animation->FrameTime());
72         }
73         void Stop() {
74                 timer = app::Timer<Uint32>();
75         }
76         bool Started() const {
77                 return timer.Started();
78         }
79         bool Running() const {
80                 return timer.Running() && (animation->Repeat() || timer.Iteration() < animation->NumFrames());
81         }
82         bool Finished() const {
83                 return Started() && !Running();
84         }
85         bool JustFinished() const {
86                 return timer.JustHit() && timer.Iteration() == animation->NumFrames();
87         }
88
89         const app::Timer<Uint32> &GetTimer() { return timer; }
90
91         void SetFrameShift(int offset) { frameShift = offset; }
92         int FrameShift() const { return frameShift; }
93         void SetColOffset(int offset) { colOffset = offset; }
94         int ColOffset() const { return colOffset; }
95         void SetRowOffset(int offset) { rowOffset = offset; }
96         int RowOffset() const { return rowOffset; }
97
98         void ChangeAnimation(const Animation *a) { animation = a; }
99         const Animation *GetAnimation() const { return animation; }
100
101         void ChangeSprite(const Sprite *s) { sprite = s; }
102         const Sprite *GetSprite() const { return sprite ? sprite : animation->GetSprite(); }
103
104         void Draw(SDL_Surface *dest, geometry::Vector<int> position) const {
105                 GetSprite()->Draw(dest, position + animation->Offset(Frame()), animation->Col(Frame()) + ColOffset(), animation->Row(Frame()) + RowOffset());
106         }
107         void DrawTopRight(SDL_Surface *dest, geometry::Vector<int> position) const {
108                 geometry::Vector<int> offset(-GetSprite()->Width(), 0);
109                 Draw(dest, position + offset);
110         }
111         void DrawCenter(SDL_Surface *dest, geometry::Vector<int> position) const {
112                 Draw(dest, position - (GetSprite()->Size() / 2));
113         }
114         void DrawCenterBottom(SDL_Surface *dest, geometry::Vector<int> position) const {
115                 geometry::Vector<int> offset(-GetSprite()->Width() / 2, -GetSprite()->Height());
116                 Draw(dest, position + offset);
117         }
118
119         int Frame() const { return Running() ? ((timer.Iteration() + frameShift) % animation->NumFrames()) : 0; }
120
121 private:
122         const Animation *animation;
123         const graphics::Sprite *sprite;
124         app::Timer<Uint32> timer;
125         int frameShift;
126         int colOffset;
127         int rowOffset;
128
129 };
130
131 }
132
133 #endif /* GRAPHICS_SIMPLEANIMATION_H_ */