4 #include "../app/Application.h"
5 #include "../app/State.h"
6 #include "../loader/Interpreter.h"
7 #include "../loader/TypeDescription.h"
11 using loader::FieldDescription;
12 using loader::Interpreter;
13 using loader::TypeDescription;
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));
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"));
30 void AnimationRunner::Start(app::State &ctrl) {
32 timer = ctrl.GraphicsTimers().StartInterval(animation->FrameTime());
35 void AnimationRunner::Start(app::Application &ctrl) {
37 timer = ctrl.GlobalTimers().StartInterval(animation->FrameTime());
40 void AnimationRunner::Stop() {
41 timer = app::Timer<Uint32>();
44 bool AnimationRunner::Started() const {
45 return timer.Started();
48 bool AnimationRunner::Running() const {
51 && (animation->Repeat()
52 || timer.Iteration() < animation->NumFrames());
55 bool AnimationRunner::Finished() const {
56 return Started() && !Running();
59 bool AnimationRunner::JustFinished() const {
62 && timer.Iteration() == animation->NumFrames();
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());
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);
78 void AnimationRunner::DrawCenter(SDL_Surface *dest, math::Vector<int> position) const {
79 Draw(dest, position - (GetSprite()->Size() / 2));
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);
87 int AnimationRunner::Frame() const {
89 ? ((timer.Iteration() + frameShift) % animation->NumFrames())