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