TurtleBrains  0.2.1
High quality, portable, C++ API for native application and game development.
TurtleBrains::Graphics::Sprite Class Reference

#include <tb_sprite.h>

Inheritance diagram for TurtleBrains::Graphics::Sprite:
TurtleBrains::Graphics::Graphic TurtleBrains::Graphics::AnimatedSprite

Public Member Functions

 Sprite (const SpriteFrame &spriteFrame)
 
 Sprite (const tbCore::tbString &textureFile, const PixelSpace &frameX=0, const PixelSpace &frameY=0, const PixelSpace &frameWidth=0, const PixelSpace &frameHeight=0)
 
 Sprite (const TextureHandle &textureHandle, const PixelSpace &frameX=0, const PixelSpace &frameY=0, const PixelSpace &frameWidth=0, const PixelSpace &frameHeight=0)
 
 Sprite (const Sprite &other)
 
virtual ~Sprite (void)
 
Spriteoperator= (const Sprite &other)
 
void SetOrigin (const AnchorLocation &anchor)
 
void SetOrigin (const float originX, const float originY)
 
void SetScale (const float scale)
 
void SetScale (const float scaleX, const float scaleY)
 
void SetRotation (const float rotation)
 
- Public Member Functions inherited from TurtleBrains::Graphics::Graphic
 Graphic (void)
 
 Graphic (const Graphic &other)
 
virtual ~Graphic (void)
 
bool IsActive (void) const
 
void SetActive (const bool isActive)
 
void Update (const float deltaTime)
 
bool IsVisible (void) const
 
void SetVisible (const bool isVisible)
 
void Render (void) const
 
const tbMath::Vector2GetPosition (void) const
 
void SetPosition (const float x, const float y)
 
void SetPosition (const tbMath::Vector2 &position)
 
float GetDepth (void) const
 
void SetDepth (const float newDepth)
 
bool IsRelative (void) const
 
virtual void SetColor (const tbGraphics::Color &newColor)
 

Protected Member Functions

virtual void OnRender (void) const override
 
void SetSpriteFrame (const SpriteFrame &spriteFrame)
 
const SpriteFrameGetSpriteFrame (void) const
 
- Protected Member Functions inherited from TurtleBrains::Graphics::Graphic
virtual void OnUpdate (const float deltaTime)
 
const ColorGetColor (void) const
 

Detailed Description

The Sprite is likely the most common Graphic object used when creating a two-dimensional game. The Sprite contains a little information about the Texture and a rectangle on that texture that describes what should be rendered by the Sprite.

Constructor & Destructor Documentation

TurtleBrains::Graphics::Sprite::Sprite ( const SpriteFrame spriteFrame)
explicit

Constructs a Sprite object with the given SpriteFrame which contains the texture and location size of the sprite.

TurtleBrains::Graphics::Sprite::Sprite ( const tbCore::tbString textureFile,
const PixelSpace frameX = 0,
const PixelSpace frameY = 0,
const PixelSpace frameWidth = 0,
const PixelSpace frameHeight = 0 
)
explicit

Construct a sprite from a filepath of a texture with a given frame location and size.

Parameters
textureFileThe filepath of a texture to load into the texture manager, if it isn't already, and use as the texture for the sprites frame.
frameXThe location of the left edge of the sprites frame in pixels from the left edge of the texture.
frameYThe location of the top edge of the sprites frame in pixels from the top edge of the texture.
frameWidthThe width of the sprite frame in pixels, frameWidth + frameX must not exceed texture width, or an error condition will be triggered.
frameHeightThe height of the sprite frame in pixels, frameHeight + frameY must not exceed texture height, or an error condition will be triggered.
TurtleBrains::Graphics::Sprite::Sprite ( const TextureHandle textureHandle,
const PixelSpace frameX = 0,
const PixelSpace frameY = 0,
const PixelSpace frameWidth = 0,
const PixelSpace frameHeight = 0 
)
explicit

Construct a sprite from a handle of an already loaded texture given a frame location and size.

Parameters
textureHandleThe handle of the loaded texture, cannot be kInvalidTexture or an error condition will be triggered.
frameXThe location of the left edge of the sprites frame in pixels from the left edge of the texture.
frameYThe location of the top edge of the sprites frame in pixels from the top edge of the texture.
frameWidthThe width of the sprite frame in pixels, frameWidth + frameX must not exceed texture width, or an error condition will be triggered.
frameHeightThe height of the sprite frame in pixels, frameHeight + frameY must not exceed texture height, or an error condition will be triggered.
TurtleBrains::Graphics::Sprite::Sprite ( const Sprite other)

Construct a sprite from another spriute, copying the sprites frame, color, scaling and other properties.

Parameters
otherThe sprite to copy and mimic.
virtual TurtleBrains::Graphics::Sprite::~Sprite ( void  )
virtual

Destroys a sprite object, which doesn't do any heavy lifting at this time.

Member Function Documentation

const SpriteFrame& TurtleBrains::Graphics::Sprite::GetSpriteFrame ( void  ) const
protected

Returns the current sprite frame to be inspected or used by subclasses.

virtual void TurtleBrains::Graphics::Sprite::OnRender ( void  ) const
overrideprotectedvirtual

Actually performs the rendering of the sprite to the screen with OpenGL.

Note
OnRender() should not be called directly, you should call Render() which will first check if the Graphic is visible before rendering.

Implements TurtleBrains::Graphics::Graphic.

Sprite& TurtleBrains::Graphics::Sprite::operator= ( const Sprite other)

Set a sprite object to be the show the same sprite frame, color, scaling, origin and all other properties copied from the other sprite.

Parameters
otherThe sprite to copy and mimic.
void TurtleBrains::Graphics::Sprite::SetOrigin ( const AnchorLocation anchor)

Sets the origin or a hotspot of the sprite. This is an offset where the sprite will be positioned by and rotated around. The AnchorLocation will describe where the origin offset with a 9 locations, the four corners, and center points of each edge.

void TurtleBrains::Graphics::Sprite::SetOrigin ( const float  originX,
const float  originY 
)
inline

Sets the origin or a hotspot of the sprite. This is an offset where the sprite will be positioned by and rotated around.

Note
Defaults to center, may default to top-left in future versions.
void TurtleBrains::Graphics::Sprite::SetRotation ( const float  rotation)
inline

Set the rotation/orientation of the sprite so it can rotate around the game world and animate things such as windmills, tires, etc.

Parameters
rotationThe angle in degrees of the sprite, positive values rotate clockwise.
void TurtleBrains::Graphics::Sprite::SetScale ( const float  scale)
inline

Changes the scaling of the sprite uniformly on each axis.

Parameters
scaleThe amount of scaling to apply, 1.0 is normal scale, 0.5 is half size and 2.0 is double.
void TurtleBrains::Graphics::Sprite::SetScale ( const float  scaleX,
const float  scaleY 
)
inline

Changes the scaling of the sprite on each axis separately.

Parameters
scaleXThe amount of scaling to apply on the X-axis, 1.0 is normal scale, 0.5 is half size.
scaleYThe amount of scaling to apply on the Y-axis, 1.0 is normal scale, 2.0 is double size.
void TurtleBrains::Graphics::Sprite::SetSpriteFrame ( const SpriteFrame spriteFrame)
protected

Changes the frame of the Sprite so it can flip to a new frame for AnimatedSprite or other effects from subclasses.

Note
The sprite frame textureHandle MUST match the current sprite frame texture handle or an error condition will be triggered.