TurtleBrains  0.3.5
High quality, portable, C++ framework for rapid 2D game development.
TurtleBrains::Graphics::SpriteManager Class Reference

#include <tb_sprite_manager.hpp>

Inheritance diagram for TurtleBrains::Graphics::SpriteManager:
TurtleBrains::Core::Noncopyable

Public Member Functions

 SpriteManager (void)
 
 ~SpriteManager (void)
 
bool LoadSpriteSheetFromFile (const tbCore::tbString &spriteSheetName, const tbCore::tbString &spriteSheetFile)
 
bool AddSpriteSheet (const tbCore::tbString &spriteSheetName, const SpriteSheet &spriteSheet)
 
TextureHandle GetSpriteSheetTextureHandle (const tbCore::tbString &spriteSheetName) const
 
SpriteFrame GetSpriteFrame (const tbCore::tbString &spriteSheetName, const tbCore::tbString &spriteName) const
 
Sprite GetSpriteFromFile (const tbCore::tbString &textureFile, const PixelSpace &frameX=0, const PixelSpace &frameY=0, const PixelSpace &frameWidth=0, const PixelSpace &frameHeight=0) const
 
Sprite GetSprite (const tbCore::tbString &spriteSheetName, const tbCore::tbString &spriteName) const
 
AnimatedSprite GetAnimatedSprite (const tbCore::tbString &spriteSheetName, const tbCore::tbString &spriteName) const
 
void AddAnimationSequences (const tbCore::tbString &spriteSheetName, const tbCore::tbString &spriteName, AnimatedSprite &animatedSprite) const
 

Detailed Description

The SpriteManager holds each of the SpriteSheets and all the Sprites and Animations that have been loaded into the manager. It provides an easy method of create a Sprite or AnimatedSprite object from data.

Constructor & Destructor Documentation

TurtleBrains::Graphics::SpriteManager::SpriteManager ( void  )

The SpriteManager should be treated like a singleton object and should only ever be accessed through the tbGraphics::theSpriteManager 'global' variable.

TurtleBrains::Graphics::SpriteManager::~SpriteManager ( void  )

The SpriteManager should be treated like a singleton object and should only ever be accessed through the tbGraphics::theSpriteManager 'global' variable.

Member Function Documentation

void TurtleBrains::Graphics::SpriteManager::AddAnimationSequences ( const tbCore::tbString spriteSheetName,
const tbCore::tbString spriteName,
AnimatedSprite animatedSprite 
) const

Adds all the AnimationSequences found on the spriteName to the animatedSprite provided.

Parameters
spriteSheetNameA name to a SpriteSheet as loaded during a call to LoadSpriteSheetFromFile() or LoadSpriteSheetFromData(). If no object is found with the sheet name, an error condition will be triggered.
spriteNameA name to the sprite data on the specified SpriteSheet which was loaded. If no sprite data is found with the provided spriteName and error condition will be triggered.
animatedSpriteThe AnimatedSprite object to add the sequences to so they can be played back.
bool TurtleBrains::Graphics::SpriteManager::AddSpriteSheet ( const tbCore::tbString spriteSheetName,
const SpriteSheet spriteSheet 
)

Adds the spriteSheet to the manager, allowing GetSpriteFrame() and such to be used with the spriteSheetName provided for it. Returns true if successfully added, or false if there was a problem such as a sprite sheet with the name provided was already existing.

Parameters
spriteSheetNameA name unique to this spriteSheet to refer to it. If a sprite sheet with this name already exists an error condition will be triggered and false returned.
spriteSheetThe sheet of sprites to add to the manager to be referenced using GetSpriteFrame(), GetSprite() or GetAnimatedSprite().
AnimatedSprite TurtleBrains::Graphics::SpriteManager::GetAnimatedSprite ( const tbCore::tbString spriteSheetName,
const tbCore::tbString spriteName 
) const

Behaves identical to GetSprite() except an AnimatedSprite object is returned with AnimationSequences added to the AnimatedSprite, ready for action. An error condition is triggered if the sprite sheet is not found or the sprite data is not found. This can be called even if the sprite data does not contain any animation sequences, but as it is expected the sequences will be added upon initialization.

Parameters
spriteSheetNameA name to a SpriteSheet as loaded during a call to LoadSpriteSheetFromFile() or LoadSpriteSheetFromData(). If no object is found with the sheet name, an error condition will be triggered.
spriteNameA name to the sprite data on the specified SpriteSheet which was loaded. If no sprite data is found with the provided spriteName and error condition will be triggered.
Sprite TurtleBrains::Graphics::SpriteManager::GetSprite ( const tbCore::tbString spriteSheetName,
const tbCore::tbString spriteName 
) const

Creates a Sprite object from the data provided on the SpriteSheet for the given spriteName. If no SpriteSheet is found with the spriteSheetName, or if no sprite data is found with the spriteName an error condition will be triggered.

Parameters
spriteSheetNameA name to a SpriteSheet as loaded during a call to LoadSpriteSheetFromFile() or LoadSpriteSheetFromData(). If no object is found with the sheet name, an error condition will be triggered.
spriteNameA name to the sprite data on the specified SpriteSheet which was loaded. If no sprite data is found with the provided spriteName and error condition will be triggered.
SpriteFrame TurtleBrains::Graphics::SpriteManager::GetSpriteFrame ( const tbCore::tbString spriteSheetName,
const tbCore::tbString spriteName 
) const

Creates just the SpriteFrame data provided on the SpriteSheet for the given spriteName. If no SpriteSheet is found with the spriteSheetName, or if no sprite data is found with the spriteName an error condition will be triggered.

Parameters
spriteSheetNameA name to a SpriteSheet as loaded during a call to LoadSpriteSheetFromFile() or LoadSpriteSheetFromData(). If no object is found with the sheet name, an error condition will be triggered.
spriteNameA name to the sprite data on the specified SpriteSheet which was loaded. If no sprite data is found with the provided spriteName and error condition will be triggered.
Sprite TurtleBrains::Graphics::SpriteManager::GetSpriteFromFile ( const tbCore::tbString textureFile,
const PixelSpace frameX = 0,
const PixelSpace frameY = 0,
const PixelSpace frameWidth = 0,
const PixelSpace frameHeight = 0 
) const

By default this will creates a sprite that is the entire width and height of the texture loaded from the textureFile provided. If the file is unable to load from disk an error condition will be triggered. The remaining parameters are optional to setup a frame of specified location and size.

Parameters
textureFileThe filepath of the image to load using theTextureManager. Will use an already loaded texture if it has been loaded previously, otherwise loads the file (or triggers an error condition if the texture file cannot be loaded.
frameXAn optional parameter to set the horizontal location of the frame on the texture, in pixels.
frameYAn optional parameter to set the vertical location of the frame on the texture, in pixels.
frameWidthAn option parameter to set the width in pixels of the sprites frame.
frameHeightAn option parameter to set the height in pixels of the sprites frame.
Note
This can trigger an error condition if the texture cannot be loaded.
If any of the optional frame parameters are used, all must be set.
This may be deprecated in favor of the Sprite() ctor version.
TextureHandle TurtleBrains::Graphics::SpriteManager::GetSpriteSheetTextureHandle ( const tbCore::tbString spriteSheetName) const

Finds the sprite sheet previously loaded with spriteSheetName and returns the associated texture handle.

Parameters
spriteSheetNameThe name of the SpriteSheet to retrieve the texture handle of.
Note
If no SpriteSheet with the name spriteSheetName is found, and error condition will be triggered.
bool TurtleBrains::Graphics::SpriteManager::LoadSpriteSheetFromFile ( const tbCore::tbString spriteSheetName,
const tbCore::tbString spriteSheetFile 
)

Loads a json formatted file and processes it into a DynamicStructure which is then processed with LoadSpriteSheetFromData(). If the file was not found or loaded an error condition is triggered. If the json data did not contain all required information about a sprite or animation sequence an error condition will be triggered from within LoadSpriteSheetFromData().

Parameters
spriteSheetNameThe name of the SpriteSheet where all the sprite and animation data will be stored.
spriteSheetFileThe path to the json formatted file that contains the sprites and animation data to load.

Ex. SpriteSheet:

{ "texture":"pathToTexture", "sprite_width":18, "sprite_height":18, "sprite_maps":[ { "name":"map16x16", "width":16, "height":16, "x":0, "y":0, "padding_x":1, "padding_y":1 }, ], "sprites":[ { "name":"mapped_sprite_1", "sprite_map":"map16x16", "map_index":5 }, { "name":"mapped_sprite_2", "sprite_map":"map16x16", "map_row":5, "map_column":2 }, { "name":"anim_global", "x":0, "y":0, "width":128, "height":128, "animations":[ "idle", "jump", "death" ] }, { "name":"anim_local_names", "x":0, "y":0, "width":128, "height":128, "animations":[ { "name":"sequence_name", "sequence":[ "sprite_frame_1", "sprite_frame_2" ... ] }, ] }, ], "animations":[ { "name":"mapped_anim", "sequence": { "sprite_map":"map16x16", "frames":[0, 1, 2] } }, { "name":"mapped_anim", "sequence": { "sprite_map":"map16x16", "startIndex": 2, "frameCount":5 } }, { "name":"sequence_name", "sequence":[ "sprite_frame_1", "sprite_frame_2" ... ] }, { "name":"sequence_map", "sequence" : { "startIndex":0, "frameCount":3, "width":32, "height":32, "locationX":0, "locationY":0 } }, { "name":"sequence_mapc", "sequence" : { "width":32, "height":32, "x":0, "y":0, "frames":[ 0, 1, 5, 3 ] } } ] }

"width" from within a sequence (top-level "frame") is optional and will default to width of sprite frame. "width" of sprite frame is optional and will default to sprite_width of the sheet. "sprite_width" of the sheet is optional and will default to the full texture width. repeat the same for height.

TODO: TurtleBrains: Graphics: Verify this-> "x" / "y" within a sequence is optional and will default to sprite frame. x/y of sprite frame is optional and will default to 0