TurtleBrains  0.3.1
High quality, portable, C++ framework for rapid 2D game development.
tb_animated_sprite.h
1 
9 #ifndef _TurtleBrains_AnimatedSprite_h_
10 #define _TurtleBrains_AnimatedSprite_h_
11 
12 #include "tb_sprite.h"
13 #include "tb_sprite_map.h"
14 
15 #include <vector>
16 #include <map>
17 
18 namespace TurtleBrains
19 {
20  namespace Graphics
21  {
22 
30  {
31  public:
32 
41  explicit AnimationSequence(const TextureHandle& textureHandle);
42 
54  AnimationSequence(const SpriteMap& spriteMap, const std::vector<size_t> frames);
55 
67  AnimationSequence(const SpriteMap& spriteMap, const size_t& startFrameIndex, const size_t& frameCount);
68 
72  ~AnimationSequence(void);
73 
81  void AddFrame(const SpriteFrame& frame);
82 
93  void AddFrame(const PixelSpace& frameX, const PixelSpace& frameY, const PixelSpace& frameWidth, const PixelSpace& frameHeight);
94 
98  size_t GetFrameCount(void) const;
99 
105  const SpriteFrame& GetFrame(const size_t& frameIndex) const;
106 
107  private:
108  //Animation Size: 36 (bytes per frame) * frames + 8 (bytes minimum for this)
109  //This could greatly reduce size by hold SpriteFrame references, but added cost of complexity and easy usage.
110  TextureHandle mTexture;
111  std::vector<SpriteFrame> mAnimationFrames;
112  };
113 
114 
119  class AnimatedSprite : public Sprite
120  {
121  public:
129  static const float& kDefaultTimePerFrame;
130 
134  static void SetAnimationFrameRate(const int framesPerSecond);
135 
143  explicit AnimatedSprite(const SpriteFrame& spriteFrame);
144 
145  explicit AnimatedSprite(const tbCore::tbString& textureFile, const PixelSpace& frameX = 0, const PixelSpace& frameY = 0,
146  const PixelSpace& frameWidth = 0, const PixelSpace& frameHeight = 0, const size_t& frameCount = 1);
147 
158  AnimatedSprite(const tbCore::tbString& spriteSheetName, const tbCore::tbString& spriteName);
159 
165  AnimatedSprite(const AnimatedSprite& other);
166 
173  AnimatedSprite& operator=(const AnimatedSprite& other);
174 
178  virtual ~AnimatedSprite(void);
179 
190  void AddSequence(const tbCore::tbString& sequenceName, const AnimationSequence& sequence);
191 
203  void AddMappedSequence(const tbCore::tbString& sequenceName, const SpriteMap& spriteMap, const std::vector<size_t> frames);
204 
217  void AddMappedSequence(const tbCore::tbString& sequenceName, const SpriteMap& spriteMap, const size_t& startFrameIndex,
218  const size_t& frameCount);
219 
236  void PlayAnimation(const tbCore::tbString& sequenceName, const bool isLooping, const bool isForward = true,
237  const float timePerFrame = kDefaultTimePerFrame);
238 
245  void PlayAnimation(const tbCore::tbString& sequenceName);
246 
255  void StopAnimation(void);
256 
261  bool IsAnimationPlaying(void) const;
262 
270  bool IsAnimationPlaying(const tbCore::tbString& sequenceName) const;
271 
278  size_t GetFrameIndex(void) const { return mCurrentFrameIndex; }
279 
280  protected:
281 
286  virtual void OnUpdate(const float deltaTime) override;
287 
288  private:
289  typedef std::map<tbCore::tbString, AnimationSequence> AnimationContainer;
290  AnimationContainer mAnimationSequences;
291 
292  const AnimationSequence* mCurrentSequence;
293  size_t mCurrentFrameIndex;
294  float mTimePerFrame;
295  float mFrameTimer;
296  bool mIsPlaying;
297  bool mIsLooping;
298  bool mIsForward;
299 
300  //The above could become the following for implementation to save space if it ever becomes a concern, will aim
301  //for mobile at some point with a few, reasonable? restrictions: Animation speed must be 1fps or faster and no
302  //animation can contain more than 256 frames.
303  //
304  // 4 bits: playing, looping, forward, pingpong
305  // 8 bits: currentFrameIndex (0-255)
306  // 10 bits: timePerFrame (milliseconds)
307  // 10 bits: frameTimer (milliseconds)
308  };
309 
310  }; /* namespace Graphics */
311 }; /* namespace TurtleBrains */
312 
314 
315 #endif /* _TurtleBrains_AnimatedSprite_h_ */
316 
317 
318 /*
319 
320  The AnimatedSprite was designed for thse following usage patterns:
321 
322  //Essentially just load it up in a table and create an animated sprite. (data driven, most likely use case)
323  void FooSimple(void)
324  //Initialization
325  mPlayerSprite = tbGraphics::theSpriteManager.GetAnimatedSprite("player_sheet", "player");
326 
327  //Usage
328  mPlayerSprite.PlayAnimation("idle"); / LoopAnimation("idle")
329  mPlayerSprite.StopAnimation();
330 
331 
332  //Must support developer creating sprite frames themeselves:
333 
334  void FooMinimal(void)
335  //Initialization
336  mPlayerSprite = tbGraphics::theSpriteManager.GetSprite("player_sheet", "player");
337  AnimationSequence idleSequence();
338  //idleSequence.AddFrame(locationX, locationY, width, height);
339  idleSequence.AddFrame(0, 0, 32, 32);
340  idleSequence.AddFrame(32, 0, 32, 32);
341  idleSequence.AddFrame(64, 0, 32, 32);
342  idleSequence.AddFrame(0, 32, 64, 64); //This method allows size and location differences.
343  mPlayerSprite.AddSequence("idle", idleSequence);
344 
345  AnimationSequence shootSequence;
346  // blah blah, shootSequence.AddFrames()
347  mPlayerSprite.AddSequence("shoot", shootSequence);
348 
349  //Usage
350  mPlayerSprite.PlayAnimation("idle"); / LoopAnimation("idle")
351  mPlayerSprite.StopAnimation();
352 
353 
354 
355  void FooBetterMin(void)
356  //Initialization
357  mPlayerSprite = GetSprite("player_sheet", "player");
358  //Create a sequence of 3 - 32x32 frames starting at frame 0 on the sheet.
359 
360  //Too much effort
361  // AnimationSequence idleSequence();
362  // idleSequence.AddMappedFrames();
363  // mPlayerSprite.AddSequence("idle", idleSequence);
364 
365  mPlayerSprite.AddSequence("idle", tbGraphics::CreateMappedAnimationSequence(32, 32, 0, 3));
366  //mPlayerSprite.AddSequence("shoot", AnimationSequence(32, 32, { 0, 1, 3, 4 }));
367  mPlayerSprite.AddMappedSequence("shoot", 32, 32, { 0, 1, 3, 4});
368 
369  //Usage
370  mPlayerSprite.PlayAnimation("idle"); / LoopAnimation("idle")
371  mPlayerSprite.StopAnimation();
372  */
static void SetAnimationFrameRate(const int framesPerSecond)
void AddMappedSequence(const tbCore::tbString &sequenceName, const SpriteMap &spriteMap, const std::vector< size_t > frames)
AnimatedSprite(const SpriteFrame &spriteFrame)
Give the GameScene and Entities something to display, Text, Sprites and AnimatedSprites help bring th...
virtual void OnUpdate(const float deltaTime) override
Definition: tb_sprite.h:29
void PlayAnimation(const tbCore::tbString &sequenceName, const bool isLooping, const bool isForward=true, const float timePerFrame=kDefaultTimePerFrame)
Here is some information about the primary namespace.
Definition: tb_application_dialog.h:21
Definition: tb_animated_sprite.h:29
static const float & kDefaultTimePerFrame
Definition: tb_animated_sprite.h:129
tbCore::uint32 TextureHandle
Definition: tb_texture_manager.h:43
tbCore::uint16 PixelSpace
Definition: tb_texture_manager.h:35
Definition: tb_sprite_map.h:23
void AddFrame(const SpriteFrame &frame)
AnimationSequence(const TextureHandle &textureHandle)
AnimatedSprite & operator=(const AnimatedSprite &other)
const SpriteFrame & GetFrame(const size_t &frameIndex) const
std::string tbString
Definition: tb_string.h:335
void AddSequence(const tbCore::tbString &sequenceName, const AnimationSequence &sequence)
Definition: tb_animated_sprite.h:119
Definition: tb_sprite.h:136
size_t GetFrameIndex(void) const
Definition: tb_animated_sprite.h:278