TurtleBrains  0.3.5
High quality, portable, C++ framework for rapid 2D game development.
tb_sprite.hpp
1 
9 #ifndef TurtleBrains_Sprite_hpp
10 #define TurtleBrains_Sprite_hpp
11 
12 #include <turtle_brains/graphics/tb_graphic.hpp>
13 #include <turtle_brains/graphics/tb_texture_manager.hpp>
14 #include <turtle_brains/graphics/tb_color.hpp>
15 
16 namespace TurtleBrains::Graphics
17 {
18 
27  struct SpriteFrame
28  { //TODO: TIM: Planning: These may turn into PixelSpace values, (int), that is what they are, but they are used as float.
29  float x;
30  float y;
31  float w;
32  float h;
38 
39  //Optimize: TurtleBrains: It might be worth doing this for both optimizing for texture lookups and
40  //convenience/optimization if using a texture packing tool that rotates sprites and cuts transparency away from the sprite.
41  //PixelSpace mTextureWidth;
42  //PixelSpace mTextureHeight; //32bits
43  //rotation byte/ (2bits) //0, 90, 180, 270
44  //32bits offsets, 32bits size change
45 
46 
50  PixelSpace GetPixelTop(void) const;
51 
55  PixelSpace GetPixelLeft(void) const;
56 
61 
66 
71 
76 
80  TexelSpace GetTexelTop(void) const;
81 
85  TexelSpace GetTexelLeft(void) const;
86 
91 
96 
101 
106 
112  static SpriteFrame CreateWith(const TextureHandle& textureHandle);
113 
125  static SpriteFrame CreateWith(const TextureHandle& textureHandle, const PixelSpace& frameX, const PixelSpace& frameY,
126  const PixelSpace& frameWidth, const PixelSpace& frameHeight);
127  };
128 
134  class Sprite : public Graphic
135  {
136  public:
141  explicit Sprite(const SpriteFrame& spriteFrame);
142 
155  explicit Sprite(const String& textureFile, const PixelSpace& frameX = 0, const PixelSpace& frameY = 0,
156  const PixelSpace& frameWidth = 0, const PixelSpace& frameHeight = 0);
157 
170  explicit Sprite(const TextureHandle& textureHandle, const PixelSpace& frameX = 0, const PixelSpace& frameY = 0,
171  const PixelSpace& frameWidth = 0, const PixelSpace& frameHeight = 0);
172 
183  Sprite(const String& spriteSheetName, const String& spriteName);
184 
191  Sprite(const Sprite& other);
192 
196  virtual ~Sprite(void);
197 
204  Sprite& operator=(const Sprite& other);
205 
209  virtual PixelSpace GetPixelWidth(void) const override;
210 
214  virtual PixelSpace GetPixelHeight(void) const override;
215 
223  void SetFlippedHorizontally(bool isFlippedHorizontally);
224 
232  void SetFlippedVertically(bool isFlippedVertically);
233 
237  bool IsFlippedHorizontally(void) const;
238 
242  bool IsFlippedVertically(void) const;
243 
244  protected:
251  virtual void OnRender(void) const override;
252 
260  void SetSpriteFrame(const SpriteFrame& spriteFrame);
261 
265  const SpriteFrame& GetSpriteFrame(void) const;
266 
267  private:
268  SpriteFrame mSpriteFrame;
269  bool mIsFlippedHorizontally;
270  bool mIsFlippedVertically;
271  };
272 
273 }; /* namespace TurtleBrains::Graphics */
274 
276 
277 #endif /* TurtleBrains_Sprite_hpp */
Definition: tb_graphic.hpp:50
Definition: tb_sprite.hpp:135
void SetSpriteFrame(const SpriteFrame &spriteFrame)
Sprite(const TextureHandle &textureHandle, const PixelSpace &frameX=0, const PixelSpace &frameY=0, const PixelSpace &frameWidth=0, const PixelSpace &frameHeight=0)
void SetFlippedVertically(bool isFlippedVertically)
Sprite(const Sprite &other)
const SpriteFrame & GetSpriteFrame(void) const
Sprite & operator=(const Sprite &other)
void SetFlippedHorizontally(bool isFlippedHorizontally)
Sprite(const String &textureFile, const PixelSpace &frameX=0, const PixelSpace &frameY=0, const PixelSpace &frameWidth=0, const PixelSpace &frameHeight=0)
virtual PixelSpace GetPixelWidth(void) const override
Sprite(const String &spriteSheetName, const String &spriteName)
bool IsFlippedHorizontally(void) const
virtual PixelSpace GetPixelHeight(void) const override
bool IsFlippedVertically(void) const
Sprite(const SpriteFrame &spriteFrame)
virtual void OnRender(void) const override
Give the GameScene and Entities something to display, Text, Sprites and AnimatedSprites help bring th...
float TexelSpace
Definition: tb_texture_manager.hpp:28
tbCore::uint16 PixelSpace
Definition: tb_texture_manager.hpp:34
Definition: tb_sprite.hpp:28
TexelSpace uvy
Definition: tb_sprite.hpp:34
TexelSpace GetTexelWidth(void) const
TexelSpace GetTexelTop(void) const
PixelSpace GetPixelTop(void) const
PixelSpace GetPixelHeight(void) const
TextureHandle mTexture
Definition: tb_sprite.hpp:37
static SpriteFrame CreateWith(const TextureHandle &textureHandle)
TexelSpace GetTexelRight(void) const
PixelSpace GetPixelBottom(void) const
TexelSpace uvh
Definition: tb_sprite.hpp:36
TexelSpace uvw
Definition: tb_sprite.hpp:35
TexelSpace GetTexelLeft(void) const
PixelSpace GetPixelWidth(void) const
TexelSpace GetTexelBottom(void) const
float x
Definition: tb_sprite.hpp:29
float h
Definition: tb_sprite.hpp:32
TexelSpace uvx
Definition: tb_sprite.hpp:33
PixelSpace GetPixelLeft(void) const
float y
Definition: tb_sprite.hpp:30
float w
Definition: tb_sprite.hpp:31
PixelSpace GetPixelRight(void) const
static SpriteFrame CreateWith(const TextureHandle &textureHandle, const PixelSpace &frameX, const PixelSpace &frameY, const PixelSpace &frameWidth, const PixelSpace &frameHeight)
TexelSpace GetTexelHeight(void) const