-
Notifications
You must be signed in to change notification settings - Fork 0
/
Animation.cpp
67 lines (53 loc) · 1.36 KB
/
Animation.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
#include "Animation.h"
Animation::Animation(){}
Animation::Animation(int left, int top,int sizex, int sizey, float holdt, int n , std::string texname) : currentframe(0), holdtime(holdt), time(0.0f), frame(n), sizex(sizex) , sizey(sizey)
{
pTex=Texturecodex::Acquire(texname);
bounds.height = sizey;
bounds.width = sizex;
bounds.left=left;
bounds.top=top;
if(n==0){frame = ++n;}
////////////////////////////////////////////////////
//Storing all the coordinates positions(x and y) in
//the corresponding x-y vector structure.
////////////////////////////////////////////////////
for(int i=0; i<=n-1; i++)
{
frames[i].left = left + sizex*i;
frames[i].top = top;
frames[i].height = sizey;
frames[i].width = sizex;
}
}
void Animation::draw(sf::RenderTarget &window, int x, int y)
{
sf::Sprite sprite(*pTex);
sprite.setTextureRect(frames[currentframe]);
sprite.setPosition((float)x,(float)y);
window.draw(sprite);
}
void Animation::resetframe()
{
if( ++currentframe >= frame)
{
currentframe = 0;
}
}
void Animation::update(float dt)
{
if(holdtime == 0){resetframe();
}
else {
time += dt;
while( time >= holdtime )
{
time -= holdtime;
resetframe();
}}
}
void Animation::ApplyToSprite( sf::Sprite& s )
{
s.setTexture( *pTex );
s.setTextureRect( frames[currentframe] );
}