-
Notifications
You must be signed in to change notification settings - Fork 5
/
animationbuffer.cpp
73 lines (64 loc) · 1.96 KB
/
animationbuffer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include "animationbuffer.h"
AnimationBuffer::AnimationBuffer()
{
timer = new QTimer(this);
connect(timer,SIGNAL(timeout()),this,SLOT(UpdateAnimations()));
timer->start(ANIMATION_INTERVAL);
}
AnimationBuffer::~AnimationBuffer()
{
timer->stop();
this->clear();
}
void AnimationBuffer::setTileBuffer(TileBuffer *buf)
{
Tiles = buf;
}
QPixmap *AnimationBuffer::getFramePixmap(int AnimationIndex)
{
if (AnimationIndex >= Animations.size()) return NULL;
if (Animations[AnimationIndex].Frames.size() <= 0 ) return NULL;
return Tiles->getTilePixmapAt( Animations[AnimationIndex].Frames[
abs( Animations[AnimationIndex].CurrentFrame)]
);
}
QPixmap *AnimationBuffer::getFirstFramePixmap(int AnimationIndex)
{
if (AnimationIndex >= Animations.size()) return NULL;
if (Animations[AnimationIndex].Frames.size() <= 0 ) return NULL;
return Tiles->getTilePixmapAt( Animations[AnimationIndex].Frames[0]);
}
void AnimationBuffer::syncAllAnimations()
{
for (int i=0; i<Animations.size(); i++)
{
Animations[i].delay = 0;
Animations[i].CurrentFrame = 0;
}
}
void AnimationBuffer::clear()
{
Animations.clear();
}
void AnimationBuffer::UpdateAnimations()
{
bool hasSomethingChanged = false;
for (int i=0; i<Animations.size(); i++)
{
if (Animations[i].Fps <= 0) continue;
Animations[i].delay++;
if (Animations[i].delay >= (1000/ANIMATION_INTERVAL)/(Animations[i].Fps))
{
Animations[i].delay = 0;
Animations[i].CurrentFrame ++;
if (Animations[i].CurrentFrame >= Animations[i].Frames.size())
{
if (Animations[i].isPingPong)
Animations[i].CurrentFrame = - Animations[i].Frames.size()+2; else
Animations[i].CurrentFrame = 0;
}
hasSomethingChanged = true;
}
}
if (hasSomethingChanged) emit onAnimationUpdate();
}