TurtleBrains  0.2.1
High quality, portable, C++ API for native application and game development.
tb_sprite_map.h
1 
9 #ifndef _TurtleBrains_SpriteMap_h_
10 #define _TurtleBrains_SpriteMap_h_
11 
12 #include "tb_sprite.h"
13 
14 namespace TurtleBrains
15 {
16  namespace Graphics
17  {
18 
23  class SpriteMap// : public Graphic
24  {
25  public:
40  SpriteMap(const TextureHandle& textureHandle, const PixelSpace& frameWidth, const PixelSpace& frameHeight,
41  const PixelSpace& spacingX = 0, const PixelSpace& spacingY = 0, const PixelSpace& offsetX = 0, const PixelSpace& offsetY = 0);
42 
47  virtual ~SpriteMap(void);
48 
52  SpriteFrame GetSpriteFrameAtIndex(const size_t& frameIndex) const;
53 
57  SpriteFrame GetSpriteFrameAtLocation(const size_t& frameColumn, const size_t& frameRow) const;
58 
62  size_t GetIndexCount(void) const;
63 
67  size_t GetColumnCount(void) const;
68 
72  size_t GetRowCount(void) const;
73 
77  PixelSpace GetFrameWidth(void) const;
78 
82  PixelSpace GetFrameHeight(void) const;
83 
87  TextureHandle GetTextureHandle(void) const;
88 
92  void RenderSpritesByIndex(const tbCore::uint16* const spriteArray, const size_t& columnCount, const size_t& rowCount,
93  const tbMath::Vector2& position = tbMath::Vector2::kZero) const;
94 
95  protected:
96 
97  private:
98  TextureHandle mTextureHandle;
99  PixelSpace mFrameWidth;
100  PixelSpace mFrameHeight;
101  PixelSpace mSpacingX;
102  PixelSpace mSpacingY;
103  PixelSpace mOffsetX;
104  PixelSpace mOffsetY;
105  };
106 
107 
108 // ///
109 // /// @details This is an ordered sequence of SpriteFrame objects that make up an animation for the AnimatedSprite to
110 // /// then flip from frame to frame.
111 // ///
112 // /// @note May eventually contain desired flip rate, default looping / pingpong values etc.
113 // ///
114 // class AnimationSequence
115 // {
116 // public:
117 //
118 // ///
119 // /// @details Creates an empty AnimationSequence with no frames. Frames must then be added to the sequence in the
120 // /// order desired by calling AddFrame().
121 // ///
122 // /// @param textureHandle All frames added to the AnimationSequence must be on this texture.
123 // ///
124 // /// @note This will trigger an error condition it textureHandle is kInvalidTexture.
125 // ///
126 // explicit AnimationSequence(const TextureHandle& textureHandle);
127 //
128 // ///
129 // /// @details Creates an AnimationSequence with a custom sequence of mapped frames by breaking down the texture into
130 // /// columns and rows based on width/height of the frame and texture, then uses the frames parameter as a
131 // /// sequence of indices into that space. A 512x256 texture, with a 32x32 frame will have 16 columns and 8 rows.
132 // /// The index 0 is for the top-left of the texture, the maximum index is (columns * rows) - 1 for the bottom-right
133 // /// most frame of the texture.
134 // ///
135 // /// @param textureHandle All frames added to the AnimationSequence must be on this texture.
136 // /// @param frameWidth The width of each frame of animation, in pixels, and for the mapped texture columns.
137 // /// @param frameHeight The height of each frame of animation, in pixels, and for the mapped texture rows.
138 // /// @param frames The customized sequence of indices on the mapped texture columns by rows, starting with top-left
139 // /// ending at the bottom-right.
140 // /// @param locationX An optional horizontal offset to the first mapped frame, if used the first row is shifted
141 // /// right horizontally by this amount in pixels.
142 // /// @param locationY An optional vertical offset to the first mapped frame, if used the first column is shifted
143 // /// down vertically by this amount in pixels.
144 // ///
145 // /// @note This will trigger an error condition it textureHandle is kInvalidTexture.
146 // ///
147 // AnimationSequence(const TextureHandle& textureHandle, const PixelSpace& frameWidth, const PixelSpace& frameHeight,
148 // const std::vector<size_t> frames, const PixelSpace& locationX = 0, const PixelSpace& locationY = 0);
149 //
150 // ///
151 // /// @details Creates an AnimationSequence with an ordered sequence of mapped frames by breaking down the texture into
152 // /// columns and rows based on width/height of the frame and texture, then uses the frames parameter as a
153 // /// sequence of indices into that space. A 512x256 texture, with a 32x32 frame will have 16 columns and 8 rows.
154 // /// The index 0 is for the top-left of the texture, the maximum index is (columns * rows) - 1 for the bottom-right
155 // /// most frame of the texture.
156 // ///
157 // /// @param textureHandle All frames added to the AnimationSequence must be on this texture.
158 // /// @param frameWidth The width of each frame of animation, in pixels, and for the mapped texture columns.
159 // /// @param frameHeight The height of each frame of animation, in pixels, and for the mapped texture rows.
160 // /// @param startFrameIndex The first frame of the ordered sequence.
161 // /// @param frameCount The number of frames in the sequence, the final frame index will be startFrame + frameCount.
162 // /// @param locationX An optional horizontal offset to the first mapped frame, if used the first row is shifted
163 // /// right horizontally by this amount in pixels.
164 // /// @param locationY An optional vertical offset to the first mapped frame, if used the first column is shifted
165 // /// down vertically by this amount in pixels.
166 // ///
167 // /// @note This will trigger an error condition it textureHandle is kInvalidTexture.
168 // ///
169 // AnimationSequence(const TextureHandle& textureHandle, const PixelSpace& frameWidth, const PixelSpace& frameHeight,
170 // const size_t& startFrameIndex, const size_t& frameCount, const PixelSpace& locationX = 0,
171 // const PixelSpace& locationY = 0);
172 //
173 // ///
174 // /// @details Cleans up after the AnimationSequence by removing the sequence of SpriteFrame objects from memory.
175 // ///
176 // ~AnimationSequence(void);
177 //
178 // ///
179 // /// @details Adds a single custom frame to the end of the sequence. Each frame will be played in the order added
180 // /// to the sequence. Call AddFrame(); in the order desired. It is possible to add the same frame multiple
181 // /// times as well for any desired effects.
182 // ///
183 // /// @param frame The SpriteFrame object of the next frame in the animation sequence.
184 // ///
185 // void AddFrame(const SpriteFrame& frame);
186 //
187 // ///
188 // /// @details Builds a SpriteFrame to add to the end of the sequence. Each frame will be played in the order added
189 // /// to the sequence. Call AddFrame(); in the order desired. It is possible to add the same frame multiple
190 // /// times as well for any desired effects.
191 // ///
192 // /// @param frameX The horizontal pixel location of the left edge of the sprite frame, 0 is the left edge of texture.
193 // /// @param frameY The vertical pixel location of the top edge of the sprite frame, 0 is the top edge of texture.
194 // /// @param frameWidth The width in pixels of the sprite frame.
195 // /// @param frameHeight The height in pixels of the sprite frame.
196 // ///
197 // void AddFrame(const PixelSpace& frameX, const PixelSpace& frameY, const PixelSpace& frameWidth, const PixelSpace& frameHeight);
198 //
199 // ///
200 // /// @details Returns the number of frames in the AnimationSequence.
201 // ///
202 // size_t GetFrameCount(void) const;
203 //
204 // ///
205 // /// @details Returns the SpriteFrame at the frameIndex or triggers an error condition if frameIndex is out-of-range.
206 // ///
207 // /// @param frameIndex The index of the SpriteFrame to return, must be in the range: (0 <= frameIndex < GetFrameCount()).
208 // ///
209 // const SpriteFrame& GetFrame(const size_t& frameIndex) const;
210 //
211 // private:
212 // //Animation Size: 36 (bytes per frame) * frames + 8 (bytes minimum for this)
213 // //This could greatly reduce size by hold SpriteFrame references, but added cost of complexity and easy usage.
214 // TextureHandle mTexture;
215 // std::vector<SpriteFrame> mAnimationFrames;
216 // };
217 
218 
219 // ///
220 // /// @details The AnimatedSprite is a Sprite that contains several AnimationSequences that can be played back to
221 // /// flip the frames of the sprite to playback an animation.
222 // ///
223 // class AnimatedSprite : public Sprite
224 // {
225 // public:
226 // ///
227 // /// @details The default amount of time per frame, in seconds by the animation system. Currently at 30fps
228 // /// in (TurtleBrains v0.2.0). This may become an integer type that represents the time in milliseconds.
229 // ///
230 // /// @note This may become a time in milliseconds with a maximum of 100 frames per second and a minimum of
231 // /// 1 frame per second, although this is yet to be decided.
232 // ///
233 // static const float kDefaultTimePerFrame;
234 //
235 // ///
236 // /// @details Creates an AnimatedSprite with a given sprite frame, all frames of the sequences added to the
237 // /// AnimatedSprite must have the same texture handle as this frame or an error condition will be triggered.
238 // ///
239 // /// @param spriteFrame the initial frame of the Sprite containing the TextureHandle that all other frames
240 // /// must have in order to be used.
241 // ///
242 // explicit AnimatedSprite(const SpriteFrame& spriteFrame);
243 //
244 // ///
245 // /// @details Copy constructor to create an animated sprite by copying all sequences from the other object.
246 // ///
247 // /// @param other The AnimatedSprite to copy and mimic.
248 // ///
249 // AnimatedSprite(const AnimatedSprite& other);
250 //
251 // ///
252 // /// @details Assignment operator for the AnimatedSprite, this will clear any current animation and sequences,
253 // /// copy all the sequences from the other object and set the current frame and timers to that other object.
254 // ///
255 // /// @param other The AnimatedSprite to copy and mimic.
256 // ///
257 // AnimatedSprite& operator=(const AnimatedSprite& other);
258 //
259 // ///
260 // /// @details Destructs the AnimatedSprite object cleaning up the sequences of animations that had been added.
261 // ///
262 // virtual ~AnimatedSprite(void);
263 //
264 // ///
265 // /// @details Adds a set of frames to the AnimatedSprite so it can be played as an animation using PlayAnimation().
266 // /// It is expected that no AnimationSequence has been added with sequenceName and that the sequence has no more
267 // /// than 256 frames of animation or an error condition will be triggered.
268 // ///
269 // /// @param sequenceName A name for the animation sequence added so it can be reference later in PlayAnimation
270 // /// using the same name. Must not be an empty string or an error condition will occur, must be different
271 // /// than any other sequence that has been added to the animated sprite or an error condition will be triggered.
272 // /// @param sequence The sequence of frames that represents the animation.
273 // ///
274 // void AddSequence(const tbCore::tbString& sequenceName, const AnimationSequence& sequence);
275 //
276 // ///
277 // /// @details Creates an AnimationSequence with the parameters given, literally calling the appropriate
278 // /// constructor. This may be removed from the interface in future versions as it is redundant now.
279 // /// Should favor the use of loading json Sprite Sheets in the SpriteManager with animation and sprite data.
280 // ///
281 // /// @param sequenceName A name for the animation sequence added so it can be reference later in PlayAnimation
282 // /// using the same name. Must not be an empty string or an error condition will occur, must be different
283 // /// than any other sequence that has been added to the animated sprite or an error condition will be triggered.
284 // /// @param frameWidth is the width of the frame in pixels, must be greater than 0.
285 // /// @param frameHeight is the height of the frame in in pixels, must be greater than 0.
286 // /// @param frames is an ordered container of the frames in the sequences, { 0, 1, 5, 3 }.
287 // /// @param locationX is an optional horizontal offset for the mapped sequence, at least one column (frameWidth)
288 // /// must exist to the right of locationX: (0 <= locationX <= textureWidth - frameWidth) or error is triggered.
289 // /// @param locationY is an optional vertical offset for the mapped sequence, at least one row (frameHeight) must
290 // /// exist under the locationY: (0 <= locationY <= textureHeight - frameHeight) or an error will be triggered.
291 // ///
292 // void AddMappedSequence(const tbCore::tbString& sequenceName, const PixelSpace& frameWidth, const PixelSpace& frameHeight,
293 // const std::vector<size_t> frames, const PixelSpace& locationX = 0, const PixelSpace& locationY = 0);
294 //
295 // ///
296 // /// @details Creates an AnimationSequence with the parameters given, literally calling the appropriate
297 // /// constructor. This may be removed from the interface in future versions as it is redundant now.
298 // /// Should favor the use of loading json Sprite Sheets in the SpriteManager with animation and sprite data.
299 // ///
300 // /// @param sequenceName A name for the animation sequence added so it can be reference later in PlayAnimation
301 // /// using the same name. Must not be an empty string or an error condition will occur, must be different
302 // /// than any other sequence that has been added to the animated sprite or an error condition will be triggered.
303 // /// @param frameWidth is the width of the frame in pixels, must be greater than 0.
304 // /// @param frameHeight is the height of the frame in in pixels, must be greater than 0.
305 // /// @param startFrameIndex The first frame of the ordered sequence.
306 // /// @param frameCount The number of frames in the sequence, the final frame index will be startFrame + frameCount.
307 // /// @param locationX is an optional horizontal offset for the mapped sequence, at least one column (frameWidth)
308 // /// must exist to the right of locationX: (0 <= locationX <= textureWidth - frameWidth) or error is triggered.
309 // /// @param locationY is an optional vertical offset for the mapped sequence, at least one row (frameHeight) must
310 // /// exist under the locationY: (0 <= locationY <= textureHeight - frameHeight) or an error will be triggered.
311 // ///
312 // void AddMappedSequence(const tbCore::tbString& sequenceName, const PixelSpace& frameWidth, const PixelSpace& frameHeight,
313 // const size_t& startFrameIndex, const size_t& frameCount, const PixelSpace& locationX = 0, const PixelSpace& locationY = 0);
314 //
315 // ///
316 // /// @details Sets the current sequence to the one found by the sequenceName, the sprite is immediately changed to
317 // /// the first frame of the sequence, or last if played backwards, and the animation begins the frame timers.
318 // ///
319 // /// @param sequenceName The name of the sequence to play, as it was added in AddSequence().
320 // /// @param isLooping Set to true to play a looping animation until either StopAnimation() is called, or another
321 // /// animation is started with PlayAnimation().
322 // /// @param isForward Set to true to play the animation forward, or false to start from the last frame and play
323 // /// backward until the first frame is reached.
324 // /// @param timePerFrame Sets the minimum amount of time that a frame should be displayed for before changing to
325 // /// the next frame of the animation. This may be changing to milliseconds in the future, but currently the
326 // /// value is in seconds where 1.25 is one and a quarter seconds per frame.
327 // ///
328 // /// @note timePerFrame may become a time in milliseconds with a maximum of 100 frames per second and a minimum of
329 // /// 1 frame per second, although this is yet to be decided.
330 // ///
331 // void PlayAnimation(const tbCore::tbString& sequenceName, const bool isLooping, const bool isForward = true,
332 // const float timePerFrame = kDefaultTimePerFrame);
333 //
334 // ///
335 // /// @details Sets the current sequence to the one found by the sequenceName, the sprite is immediately changed to
336 // /// the first frame of the sequence, or last if played backwards, and the animation begins the frame timers.
337 // ///
338 // /// @param sequenceName The name of the sequence to play, as it was added in AddSequence().
339 // ///
340 // void PlayAnimation(const tbCore::tbString& sequenceName);
341 //
342 // ///
343 // /// @details Stops the animation on the current frame and stops the frame timers from being processed further. No
344 // /// frames will be switched until PlayAnimation is called again to begin playing.
345 // ///
346 // /// @note The implementation details may change in the future pending the addition of a Pause(), if Pause() is
347 // /// introduced, Stop() will stop the animation and set the frame to the first frame of animation. This is yet
348 // /// to be decided. TODO: TIM: Implementation: Figure out this detail before TurtleBrains public release.
349 // ///
350 // void StopAnimation(void);
351 //
352 // ///
353 // /// @details Check if the animation is currently playing or if it has reached the end and stopped, or been stopped
354 // /// with a call to StopAnimation(). This will always return true for a looping animation!
355 // ///
356 // bool IsAnimationPlaying(void) const;
357 //
358 // protected:
359 //
360 // ///
361 // /// @details If the animation is paying this will update the frame timers and swap to the next frame if the timer
362 // /// has exceeded the maximum time allowed per frame.
363 // ///
364 // virtual void OnUpdate(const float deltaTime) override;
365 //
366 // private:
367 // typedef std::map<tbCore::tbString, AnimationSequence> AnimationContainer;
368 // AnimationContainer mAnimationSequences;
369 //
370 // const AnimationSequence* mCurrentSequence;
371 // size_t mCurrentFrameIndex;
372 // float mTimePerFrame;
373 // float mFrameTimer;
374 // bool mIsPlaying;
375 // bool mIsLooping;
376 // bool mIsForward;
377 //
378 // //The above could become the following for implementation to save space if it ever becomes a concern, will aim
379 // //for mobile at some point with a few, reasonable? restrictions: Animation speed must be 1fps or faster and no
380 // //animation can contain more than 256 frames.
381 // //
382 // // 4 bits: playing, looping, forward, pingpong
383 // // 8 bits: currentFrameIndex (0-255)
384 // // 10 bits: timePerFrame (milliseconds)
385 // // 10 bits: frameTimer (milliseconds)
386 // };
387 
388  }; /* namespace Graphics */
389 }; /* namespace TurtleBrains */
390 
392 
393 #endif /* _TurtleBrains_SpriteMap_h_ */
Definition: tb_vector.h:48
PixelSpace GetFrameHeight(void) const
SpriteFrame GetSpriteFrameAtIndex(const size_t &frameIndex) const
Give the GameScene and Entities something to display, Text, Sprites and AnimatedSprites help bring th...
TextureHandle GetTextureHandle(void) const
size_t GetIndexCount(void) const
Definition: tb_sprite.h:30
uint16_t uint16
Unsigned integer with a size of 16 bits. Supports values from 0 to 65535.
Definition: tb_types.h:25
unsigned int TextureHandle
Definition: tb_texture_manager.h:41
static const Vector2 kZero
Definition: tb_vector.h:54
Contains all functions, classes and helpers related to game/application development written by Tim "B...
Definition: tb_application_dialog.h:21
void RenderSpritesByIndex(const tbCore::uint16 *const spriteArray, const size_t &columnCount, const size_t &rowCount, const tbMath::Vector2 &position=tbMath::Vector2::kZero) const
PixelSpace GetFrameWidth(void) const
tbCore::uint16 PixelSpace
Definition: tb_texture_manager.h:33
Definition: tb_sprite_map.h:23
SpriteMap(const TextureHandle &textureHandle, const PixelSpace &frameWidth, const PixelSpace &frameHeight, const PixelSpace &spacingX=0, const PixelSpace &spacingY=0, const PixelSpace &offsetX=0, const PixelSpace &offsetY=0)
SpriteFrame GetSpriteFrameAtLocation(const size_t &frameColumn, const size_t &frameRow) const
size_t GetColumnCount(void) const
size_t GetRowCount(void) const