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