]> git.localhorst.tv Git - l2e.git/blob - src/graphics/Animation.h
expose timing information in Animation
[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), colOffset(0), rowOffset(0), repeat(false) { }
28         Animation(const Sprite *sprite, int frameTime, bool repeat = false)
29         : sprite(sprite), frameTime(frameTime), colOffset(0), rowOffset(0), repeat(repeat) { }
30         virtual ~Animation() { };
31
32 public:
33         void Start(app::State &ctrl) {
34                 timer = ctrl.GraphicsTimers().StartInterval(frameTime);
35         }
36         void Start(app::Application &ctrl) {
37                 timer = ctrl.GlobalTimers().StartInterval(frameTime);
38         }
39         void Stop() {
40                 timer = app::Timer<Uint32>();
41         }
42         bool Running() const {
43                 return timer.Running() && (repeat || timer.Iteration() < NumFrames());
44         }
45
46         const app::Timer<Uint32> &GetTimer() { return timer; }
47
48         void SetColOffset(int offset) { colOffset = offset; }
49         int ColOffset() const { return colOffset; }
50         void SetRowOffset(int offset) { rowOffset = offset; }
51         int RowOffset() const { return rowOffset; }
52
53         const Sprite *GetSprite() const { return sprite; }
54         void ChangeSprite(const Sprite *s) { sprite = s; }
55
56         virtual void Draw(SDL_Surface *dest, geometry::Point<int> position) const {
57                 sprite->Draw(dest, position, Col() + ColOffset(), Row() + RowOffset());
58         }
59         void DrawTopRight(SDL_Surface *dest, geometry::Point<int> position) const {
60                 geometry::Vector<int> offset(-sprite->Width(), 0);
61                 Draw(dest, position + offset);
62         }
63         void DrawCenter(SDL_Surface *dest, geometry::Point<int> position) const {
64                 geometry::Vector<int> offset(-sprite->Width() / 2, -sprite->Height() / 2);
65                 Draw(dest, position + offset);
66         }
67         void DrawCenterBottom(SDL_Surface *dest, geometry::Point<int> position) const {
68                 geometry::Vector<int> offset(-sprite->Width() / 2, -sprite->Height());
69                 Draw(dest, position + offset);
70         }
71
72         int Frame() const { return Running() ? (timer.Iteration() % NumFrames()) : 0; }
73
74 protected:
75         virtual int Col() const = 0;
76         virtual int Row() const = 0;
77         virtual int NumFrames() const = 0;
78
79 private:
80         const Sprite *sprite;
81         app::Timer<Uint32> timer;
82         int frameTime;
83         int colOffset;
84         int rowOffset;
85         bool repeat;
86
87 };
88
89 }
90
91 #endif /* GRAPHICS_SIMPLEANIMATION_H_ */