]> git.localhorst.tv Git - l2e.git/blob - src/graphics/Animation.cpp
added "profile" build configuration
[l2e.git] / src / graphics / Animation.cpp
1 #include "Animation.h"
2
3 #include "Sprite.h"
4 #include "../app/Application.h"
5 #include "../app/State.h"
6 #include "../loader/Interpreter.h"
7 #include "../loader/TypeDescription.h"
8
9 #include <cassert>
10
11 using loader::FieldDescription;
12 using loader::Interpreter;
13 using loader::TypeDescription;
14
15 namespace graphics {
16
17 void Animation::CreateTypeDescription() {
18         TypeDescription &td(TypeDescription::Create(TYPE_ID, "Animation"));
19         td.SetDescription("Abstract base type for animations.");
20         td.SetSize(sizeof(Animation));
21 }
22
23 void Animation::AddFields(TypeDescription &td, const Animation &a, std::ptrdiff_t offset) {
24         td.AddField("sprite", FieldDescription(((char *)&a.sprite) - ((char *)&a) - offset, Sprite::TYPE_ID).SetReferenced().SetDescription("the sprite used for cutting out frames"));
25         td.AddField("frametime", FieldDescription(((char *)&a.frameTime) - ((char *)&a) - offset, Interpreter::NUMBER_ID).SetDescription("duration of a frame in miliseconds"));
26         td.AddField("repeat", FieldDescription(((char *)&a.repeat) - ((char *)&a) - offset, Interpreter::BOOLEAN_ID).SetDescription("whether the animation should start over at the beginning after reaching the last frame"));
27 }
28
29
30 void AnimationRunner::Start(app::State &ctrl) {
31         assert(animation);
32         timer = ctrl.GraphicsTimers().StartInterval(animation->FrameTime());
33 }
34
35 void AnimationRunner::Start(app::Application &ctrl) {
36         assert(animation);
37         timer = ctrl.GlobalTimers().StartInterval(animation->FrameTime());
38 }
39
40 void AnimationRunner::Stop() {
41         timer = app::Timer<Uint32>();
42 }
43
44 bool AnimationRunner::Started() const {
45         return timer.Started();
46 }
47
48 bool AnimationRunner::Running() const {
49         return animation
50                         && timer.Running()
51                         && (animation->Repeat()
52                                         || timer.Iteration() < animation->NumFrames());
53 }
54
55 bool AnimationRunner::Finished() const {
56         return Started() && !Running();
57 }
58
59 bool AnimationRunner::JustFinished() const {
60         return animation
61                         && timer.JustHit()
62                         && timer.Iteration() == animation->NumFrames();
63 }
64
65
66 void AnimationRunner::Draw(SDL_Surface *dest, math::Vector<int> position) const {
67         GetSprite()->Draw(dest,
68                         position + animation->Offset(Frame()),
69                         animation->Col(Frame()) + ColOffset(),
70                         animation->Row(Frame()) + RowOffset());
71 }
72
73 void AnimationRunner::DrawTopRight(SDL_Surface *dest, math::Vector<int> position) const {
74         math::Vector<int> offset(-GetSprite()->Width(), 0);
75         Draw(dest, position + offset);
76 }
77
78 void AnimationRunner::DrawCenter(SDL_Surface *dest, math::Vector<int> position) const {
79         Draw(dest, position - (GetSprite()->Size() / 2));
80 }
81
82 void AnimationRunner::DrawCenterBottom(SDL_Surface *dest, math::Vector<int> position) const {
83         math::Vector<int> offset(-GetSprite()->Width() / 2, -GetSprite()->Height());
84         Draw(dest, position + offset);
85 }
86
87 int AnimationRunner::Frame() const {
88         return Running()
89                         ? ((timer.Iteration() + frameShift) % animation->NumFrames())
90                         : 0;
91 }
92
93
94 }