-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnimation.cpp
More file actions
123 lines (102 loc) · 2.53 KB
/
Copy pathAnimation.cpp
File metadata and controls
123 lines (102 loc) · 2.53 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include "Animation.h"
#include <iostream>
#include "ResourceLocator.h"
Animation::Animation(sf::Texture* texture, int frame_width, int frame_height, int ms_between_frames, bool looping, int repeat_from_frame)
{
if(texture == NULL)
{
std::cerr << "Warning, null texture passed to Animation" << std::endl;
}
m_Texture = texture;
m_FrameWidth = frame_width;
m_FrameHeight = frame_height;
std::cout << m_FrameWidth << "x" << m_FrameHeight;
m_Looping = looping;
m_CurrentFrame = 0;
m_MsBetweenFrames = ms_between_frames;
m_Active = true;
m_TotalFrames = texture->getSize().x / m_FrameWidth;
std::cout << " frames: " << m_TotalFrames <<std::endl;
m_RepeatFromFrame = repeat_from_frame;
}
// copy ctor! :D
Animation::Animation(const Animation& animation)
{
m_Sprite = NULL;
m_Active = true;
m_CurrentFrame = 0;
m_Texture = animation.getTexture();
m_MsBetweenFrames = animation.getMsBetweenFrames();
m_FrameHeight = animation.getFrameSize().y;
m_FrameWidth = animation.getFrameSize().x;
m_Looping = animation.isLooping();
m_RepeatFromFrame = animation.getRepeatFromFrame();
m_TotalFrames = m_Texture->getSize().x / m_FrameWidth;
}
Animation::~Animation()
{
}
void Animation::update(float delta_ms)
{
if(m_Active)
{
if(m_Timer.getElapsedTime().asMilliseconds() >= m_MsBetweenFrames)
{
m_Timer.restart();
m_CurrentFrame++;
if(m_CurrentFrame == m_TotalFrames)
{
if(m_Looping == false)
{
m_Active = false;
}
m_CurrentFrame = m_RepeatFromFrame;
}
}
}
}
void Animation::render()
{
sf::IntRect texture_rect;
texture_rect.left = m_CurrentFrame * m_FrameWidth;
texture_rect.top = 0;
texture_rect.width = m_FrameWidth;
texture_rect.height = m_FrameHeight;
m_Sprite->setTextureRect(texture_rect);
ResourceLocator::getDrawSurface()->draw(*m_Sprite);
}
void Animation::setTexture(sf::Texture* texture)
{
m_Texture = texture;
}
sf::Texture* Animation::getTexture() const
{
return m_Texture;
}
sf::Vector2i Animation::getFrameSize() const
{
return sf::Vector2i(m_FrameWidth, m_FrameHeight);
}
int Animation::getMsBetweenFrames() const
{
return m_MsBetweenFrames;
}
int Animation::getRepeatFromFrame() const
{
return m_RepeatFromFrame;
}
bool Animation::isLooping() const
{
return m_Looping;
}
void Animation::reset()
{
m_Timer.restart();
m_CurrentFrame = 0;
m_Active = true;
}
void Animation::setSprite(sf::Sprite* sprite)
{
m_Sprite = sprite;
sprite->setTexture(*m_Texture);
}