jade-engine  0.0
JadeEngine::Sprite Class Reference

#include <Sprite.h>

Inheritance diagram for JadeEngine::Sprite:
JadeEngine::IGameObject JadeEngine::BoxSprite JadeEngine::TextSprite

Public Member Functions

 Sprite (const SpriteParams &params)
 
 Sprite (const ObjectLayer layer, std::shared_ptr< Texture > texture, const int32_t z)
 
void Render (SDL_Renderer *renderer) override
 
void Clean () override
 
void Tint (const SDL_Color &tintColor)
 
void SetAlpha (const float alpha)
 
void MakeTextureUnique ()
 
virtual float GetAlpha () const
 
void SetRotation (const double angle)
 
virtual bool HitTest (const int32_t x, const int32_t y) const
 
virtual bool HasHitTest () const
 
void SetSampling (const TextureSampling sampling)
 
void SetSpriteSheetSprite (const std::string &sprite)
 
void SetSpriteSheetMask (const Rectangle &mask)
 
const std::string & GetTextureName () const
 
- Public Member Functions inherited from JadeEngine::IGameObject
 IGameObject ()
 
virtual void Update ()
 
virtual LoadState Load (SDL_Renderer *renderer)
 
LoadState GetLoadState () const
 
void SetLoadState (const LoadState newState)
 
bool IsShown () const
 
virtual void Show (const bool show)
 
int32_t GetZ () const
 
void Destroy ()
 
bool DestructionWanted () const
 

Protected Attributes

std::shared_ptr< Texture_textureDescription
 
const detail::SpriteSheetDescription * _spriteSheetDescription
 
SDL_Texture * _texture
 
bool _spriteSheetMasked
 
Rectangle _spriteSheetMask
 
std::string _textureName
 
bool _rotated
 
float _alpha
 
double _rotationAngle
 
ObjectLayer _layer
 
- Protected Attributes inherited from JadeEngine::IGameObject
LoadState _loadState
 
bool _shown
 
int32_t _z
 

Additional Inherited Members

- Public Attributes inherited from JadeEngine::IGameObject
const std::shared_ptr< Transformtransform
 

Detailed Description

Game object that renders a texture on screen.

See also
SpriteParams

Constructor & Destructor Documentation

◆ Sprite() [1/2]

JadeEngine::Sprite::Sprite ( const SpriteParams params)

Constructor for Sprite that accepts SpriteParams.

Warning
One should use Game::Create function to create Sprites instead of constructing the object directly.
See also
Game::Create, SpriteParams

◆ Sprite() [2/2]

JadeEngine::Sprite::Sprite ( const ObjectLayer  layer,
std::shared_ptr< Texture texture,
const int32_t  z 
)

Utility constructor for Sprites where their texture is already known and created elsewhere.

Warning
One should use Game::Create function to create Sprites instead of constructing the object directly.

Used internally by the Jade Engine and most likely of limited use otherwise.

Member Function Documentation

◆ Clean()

void JadeEngine::Sprite::Clean ( )
overridevirtual

Triggered when the game object is about to be destroyed or reset.

All resources belonging solely to the game object, such as textures, should be released at this point. The game object instance itself may or may not be destroyed after this callback.

Reimplemented from JadeEngine::IGameObject.

◆ GetAlpha()

float JadeEngine::Sprite::GetAlpha ( ) const
virtual

Return the current transparency alpha value.

◆ GetTextureName()

const std::string & JadeEngine::Sprite::GetTextureName ( ) const

Return the texture name that the sprite is rendering. In the case of sprite-sheet sprite it is the texture name of one of the packed textures.

See also
SpriteParams::spriteSheet

◆ HasHitTest()

bool JadeEngine::Sprite::HasHitTest ( ) const
virtual

Return whether the sprite's texture was initialized with hit map.

See also
GameInitParamsTextureEntry::generateHitMap, Sprite::HitTest

Reimplemented in JadeEngine::BoxSprite.

◆ HitTest()

bool JadeEngine::Sprite::HitTest ( const int32_t  x,
const int32_t  y 
) const
virtual

Perform a hit test, returning whether a pixel of the sprite is not empty (not completely transparent, alpha > 0).

Precondition
The texture used by the sprite was initialized with hit map. You can use HasHitTest() to confirm so.
See also
GameInitParamsTextureEntry::generateHitMap, Sprite::HasHitTest
Returns
True if the pixel is opaque, false if completely transparent.

◆ MakeTextureUnique()

void JadeEngine::Sprite::MakeTextureUnique ( )

Create a deep-copy of the Sprite texture that is not shared with other Sprites using the same texture.

This operation is necessary for Sprite::Tint and Sprite::SetAlpha and will be called automatically on their first call unless already called by the user.

Warning
This is potentially expensive operation as it deep-copying the texture using Game::CopyTexture.
See also
Sprite::Tint, Sprite::SetAlpha, Game::CopyTexture

◆ Render()

void JadeEngine::Sprite::Render ( SDL_Renderer *  renderer)
overridevirtual

Triggered every frame while the scene that owns this game object is active, the game object was successfully loaded and the game object is shown.

The trigger order is the following: IScene::PreUpdate -> IGameObject::Load -> IGameObject::Update -> IScene::Update -> IGameObject::Load -> IGameObject::Render.

Parameters
rendererThe engine's SDL2 renderer that should be used to render the game object.
See also
IGameObject::Update, LoadState, IGameObject::IsShown

Reimplemented from JadeEngine::IGameObject.

Reimplemented in JadeEngine::TextSprite.

◆ SetAlpha()

void JadeEngine::Sprite::SetAlpha ( const float  alpha)

Changed the transparency of the sprite.

Warning
The first time Sprite is tinted or its transparency changed MakeTextureUnique is called which is potentially expensive operation. Consider calling MakeTextureUnique explicitly during initialization to avoid flickering.
Sprite-sheet Sprites are not recommended to be used with SetAlpha.
Parameters
alphaTransparency alpha value to set. 0 will make the sprite completely invisible, 1 fully opaque. It will be clamped to [0.0f, 1.0f] range.
See also
Sprite::MakeTextureUnique

◆ SetRotation()

void JadeEngine::Sprite::SetRotation ( const double  angle)

Set the rotation the sprite will rendered with.

The rotation center will be the center of the sprite, i.e. GetWidth() / 2 and GetHeight() / 2. This only affects the rendering of the sprite and not its transform.

Parameters
angleRotational angle in degrees in a clockwise direction.

◆ SetSampling()

void JadeEngine::Sprite::SetSampling ( const TextureSampling  sampling)

Change the sampling of the texture.

Warning
Changing sampling requires a deep copy of the texture which is potentially expensive operation.
See also
TextureSampling, GameInitParamsTextureEntry::sampling, GameInitParamsSpriteSheetEntry::sampling

◆ SetSpriteSheetMask()

void JadeEngine::Sprite::SetSpriteSheetMask ( const Rectangle &  mask)

Change the what rectangle of the sprite-sheet is used for rendering the sprite.

As sprite-sheets are collections of texture it is recommended to use Sprite::SetSpriteSheetSprite instead of directly specifying the rectangle.

Precondition
The sprite was initialized with sprite-sheet.
See also
SpriteParams::spriteSheet

◆ SetSpriteSheetSprite()

void JadeEngine::Sprite::SetSpriteSheetSprite ( const std::string &  sprite)

Change the sprite's texture to a different texture that is part of the same sprite-sheet.

Useful for simple frame-based animations of sprites. Nothing happens if the new texture is not found.

Precondition
The sprite was initialized with sprite-sheet.
Parameters
spriteName of the new texture as defined by the sprite-sheet. Usually original file name of the texture before packing into sprite-sheet.
See also
SpriteParams::spriteSheet

◆ Tint()

void JadeEngine::Sprite::Tint ( const SDL_Color &  tintColor)

Tint the sprite with a single color.

Warning
The first time Sprite is tinted or its transparency changed MakeTextureUnique is called which is potentially expensive operation. Consider calling MakeTextureUnique explicitly during initialization to avoid flickering.
Sprite-sheet Sprites are not recommended to be used with SetAlpha.
Parameters
tintColorRGBA 0-255 color that will be used to multiply each pixel of the sprite. Alpha component is ignored.
See also
Sprite::MakeTextureUnique

The documentation for this class was generated from the following files: