GameKit  0.0.1a
C++ gamedev tools
Sprite.cpp
Go to the documentation of this file.
1 /*
2  * =====================================================================================
3  *
4  * Filename: Sprite.cpp
5  *
6  * Description:
7  *
8  * Created: 17/01/2018 19:48:08
9  *
10  * Author: Quentin Bazin, <quent42340@gmail.com>
11  *
12  * =====================================================================================
13  */
14 #include "gk/graphics/Sprite.hpp"
15 #include "gk/core/Exception.hpp"
16 
17 namespace gk {
18 
19 Sprite::Sprite(const std::string &textureName, u16 frameWidth, u16 frameHeight, bool isAnimated) {
20  load(textureName, frameWidth, frameHeight, isAnimated);
21 }
22 
23 void Sprite::load(const Sprite &sprite) {
24  Image::load(sprite);
25 
26  m_animations = sprite.m_animations;
27 
30 
31  m_frameWidth = sprite.m_frameWidth;
33 
34  m_isAnimated = sprite.m_isAnimated;
35 
37 }
38 
39 void Sprite::load(const std::string &textureName, u16 frameWidth, u16 frameHeight, bool isAnimated) {
40  Image::load(textureName);
41 
44 
45  setPosRect(0, 0, frameWidth, frameHeight);
46 
47  setCurrentFrame(0);
48 
50 }
51 
53  if (m_animations.size() > 0 && m_isAnimated) {
54  if(m_currentAnimation >= m_animations.size()) {
55  throw EXCEPTION("Trying to play inexistant animation:", m_currentAnimation, "| Animations:", m_animations.size());
56  }
57 
59 
61  }
62 }
63 
65  u16 frameX = (currentFrame % (Image::width() / m_frameWidth)) * m_frameWidth;
66  u16 frameY = (currentFrame / (Image::width() / m_frameWidth)) * m_frameHeight;
67 
68  setClipRect(frameX, frameY, m_frameWidth, m_frameHeight);
69 
71 }
72 
74  if (m_previousAnimation != currentAnimation)
76 
79 }
80 
81 } // namespace gk
82 
void load(const Image &image)
Definition: Image.cpp:33
u16 frameWidth() const
Definition: Sprite.hpp:36
void setClipRect(float x, float y, u16 width, u16 height)
Definition: Image.cpp:61
u16 m_previousAnimation
Definition: Sprite.hpp:58
void load(const Sprite &sprite)
Definition: Sprite.cpp:23
unsigned short u16
Definition: IntTypes.hpp:22
u16 m_currentAnimation
Definition: Sprite.hpp:57
bool m_isAnimated
Definition: Sprite.hpp:63
#define EXCEPTION(args...)
Definition: Exception.hpp:22
void updateAnimations()
Definition: Sprite.cpp:52
void setCurrentAnimation(u16 currentAnimation)
Definition: Sprite.cpp:73
u16 m_currentFrame
Definition: Sprite.hpp:56
u16 m_frameHeight
Definition: Sprite.hpp:61
u16 m_frameWidth
Definition: Sprite.hpp:60
std::vector< SpriteAnimation > m_animations
Definition: Sprite.hpp:54
bool isAnimated() const
Definition: Sprite.hpp:50
SpriteAnimation & currentAnimation()
Definition: Sprite.hpp:41
u16 frameHeight() const
Definition: Sprite.hpp:37
u16 currentFrame() const
Definition: Sprite.hpp:34
u16 width() const
Definition: Image.hpp:41
void setCurrentFrame(u16 currentFrame)
Definition: Sprite.cpp:64
Sprite()=default
void setPosRect(float x, float y, u16 width, u16 height)
Definition: Image.cpp:70