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