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