-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSheetAnimation.cpp
More file actions
109 lines (86 loc) · 1.85 KB
/
Copy pathSheetAnimation.cpp
File metadata and controls
109 lines (86 loc) · 1.85 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
#include "SheetAnimation.h"
#include "Shared.h"
#include "TextureManager.h"
#include <SFML/Graphics/RenderWindow.hpp>
#include <sstream>
#include <fstream>
SheetAnimation::SheetAnimation(Shared* shared, const std::string& filename, sf::Vector2f spriteSize)
: m_shared(shared), m_spriteSize(spriteSize)
{
m_sheet = m_shared->m_texMgr->getTexture(filename);
m_currentFrame = 0;
m_totalFrames = m_sheet->getSize().x / m_sheet->getSize().y;
std::ifstream in("frame.txt");
std::string frameLength;
std::getline(in, frameLength);
std::stringstream ss(frameLength);
ss >> m_frameLength;
in.close();
m_currentFrameElapsedTime = 0.f;
m_play = m_loop = false;
m_sprite.setTexture(*m_sheet);
crop();
}
void SheetAnimation::update(float dT)
{
if (!m_play)
{
return;
}
m_currentFrameElapsedTime += dT;
if (m_currentFrameElapsedTime >= m_frameLength)
{
++m_currentFrame;
if (m_currentFrame >= m_totalFrames)
{
m_currentFrame = m_totalFrames - 1;
if (!m_loop)
{
m_play = false;
}
}
m_currentFrameElapsedTime = 0.f;
}
}
void SheetAnimation::setPosition(sf::Vector2f newPos)
{
m_sprite.setPosition(newPos);
}
void SheetAnimation::move(sf::Vector2f offset)
{
m_sprite.move(offset);
}
void SheetAnimation::setPlay(bool play)
{
m_play = play;
}
void SheetAnimation::setLooping(bool looping)
{
m_loop = looping;
}
void SheetAnimation::setFrame(int frame)
{
m_currentFrame = frame;
}
void SheetAnimation::crop()
{
sf::IntRect rect = { (int)m_spriteSize.x * m_currentFrame, 0, (int)m_spriteSize.x, (int)m_spriteSize.y };
m_sprite.setTextureRect(rect);
}
void SheetAnimation::draw(sf::RenderWindow& window)
{
crop();
window.draw(m_sprite);
}
sf::Sprite& SheetAnimation::getSprite()
{
return m_sprite;
}
bool SheetAnimation::isPlaying() const
{
return m_play;
}
int SheetAnimation::getCurrentFrame() const
{
return m_currentFrame;
}