Gamedev Framework (gf)  0.1.0
A C++11 framework for 2D games
Public Member Functions | List of all members
gf::Sprite Class Reference

A drawable representation of a texture, with its own transformations, color, etc. More...

#include <gf/Sprite.h>

Inheritance diagram for gf::Sprite:
Inheritance graph
[legend]

Public Member Functions

 Sprite ()
 Default constructor. More...
 
 Sprite (const Texture &texture)
 Construct the sprite from a source texture. More...
 
 Sprite (const Texture &texture, const RectF &textureRect)
 Construct the sprite from a sub-rectangle of a source texture. More...
 
void setTexture (const Texture &texture, bool resetRect=false)
 Change the source texture of the sprite. More...
 
const TexturegetTexture () const
 Get the source texture of the sprite. More...
 
void unsetTexture ()
 Unset the source texture of the sprite. More...
 
void setTextureRect (const RectF &rect)
 Set the sub-rectangle of the texture that the sprite will display. More...
 
const RectFgetTextureRect () const
 Get the sub-rectangle of the texture displayed by the sprite. More...
 
void setColor (const Color4f &color)
 Set the global color of the sprite. More...
 
const Color4fgetColor () const
 Get the global color of the sprite. More...
 
RectF getLocalBounds () const
 Get the local bounding rectangle of the entity. More...
 
void setAnchor (Anchor anchor)
 Set the anchor origin of the entity. More...
 
VertexBuffer commitGeometry () const
 Create a buffer with the current geometry. More...
 
virtual void draw (RenderTarget &target, RenderStates states) override
 Draw the object to a render target. More...
 
- Public Member Functions inherited from gf::Transformable
 Transformable ()
 Default constructor. More...
 
void setOrigin (Vector2f origin)
 Set the local origin of the object. More...
 
Vector2f getOrigin () const
 Get the local origin of the object. More...
 
void setPosition (Vector2f position)
 Set the position of the object. More...
 
Vector2f getPosition () const
 Get the position of the object. More...
 
void move (Vector2f offset)
 Move the object by a given offset. More...
 
void setRotation (float angle)
 Set the orientation of the object. More...
 
float getRotation () const
 Get the orientation of the object. More...
 
void rotate (float angle)
 Rotate the object. More...
 
void setScale (Vector2f factors)
 Set the scale factors of the object. More...
 
void setScale (float factor)
 Set the scale factor of the object. More...
 
Vector2f getScale () const
 Get the current scale of the object. More...
 
void scale (Vector2f factors)
 Scale the object. More...
 
void scale (float factor)
 Scale the object. More...
 
Matrix3f getTransform () const
 Get the combined transform of the object. More...
 
Matrix3f getInverseTransform () const
 Get the inverse of the combined transform of the object. More...
 
- Public Member Functions inherited from gf::Drawable
virtual ~Drawable ()
 Virtual desctructor. More...
 

Additional Inherited Members

- Protected Member Functions inherited from gf::Transformable
void setOriginFromAnchorAndBounds (Anchor anchor, const RectF &bounds)
 Set the origin from an anchor and bounds. More...
 

Detailed Description

A drawable representation of a texture, with its own transformations, color, etc.

gf::Sprite is a drawable class that allows to easily display a texture (or a part of it) on a render target.

It inherits all the functions from gf::Transformable: position, rotation, scale, origin. It also adds sprite-specific properties such as the texture to use, the part of it to display, and some convenience functions to change the overall color of the sprite, or to get its bounding rectangle.

gf::Sprite works in combination with the gf::Texture class, which loads and provides the pixel data of a given texture.

The separation of gf::Sprite and gf::Texture allows more flexibility and better performances: indeed a gf::Texture is a heavy resource, and any operation on it is slow (often too slow for real-time applications). On the other side, a gf::Sprite is a lightweight object which can use the pixel data of a gf::Texture and draw it with its own transformation/color/blending attributes.

It is important to note that the gf::Sprite instance doesn't copy the texture that it uses, it only keeps a reference to it. Thus, a gf::Texture must not be destroyed while it is used by a gf::Sprite (i.e. never write a function that uses a local gf::Texture instance for creating a sprite).

Usage example:

// Declare and load a texture
gf::Texture texture;
texture.loadFromFile("texture.png");
// Create a sprite
gf::Sprite sprite;
sprite.setTexture(texture);
sprite.setTextureRect({ 0.1f, 0.1f, 0.5f, 0.3f });
sprite.setColor({ 1.0f, 1.0f, 1.0f, 0.8f });
sprite.setPosition({ 100.0f, 25.0f });
// Draw it
renderer.draw(sprite);
See also
gf::Texture, gf::Transformable

Constructor & Destructor Documentation

gf::Sprite::Sprite ( )

Default constructor.

Creates an empty sprite with no source texture.

gf::Sprite::Sprite ( const Texture texture)

Construct the sprite from a source texture.

Parameters
textureSource texture
See also
setTexture()
gf::Sprite::Sprite ( const Texture texture,
const RectF textureRect 
)

Construct the sprite from a sub-rectangle of a source texture.

Parameters
textureSource texture
textureRectSub-rectangle of the texture to assign to the sprite
See also
setTexture(), setTextureRect()

Member Function Documentation

VertexBuffer gf::Sprite::commitGeometry ( ) const

Create a buffer with the current geometry.

The geometry is uploaded in the graphics memory so that it's faster to draw.

Returns
A buffer with the current geometry
virtual void gf::Sprite::draw ( RenderTarget target,
RenderStates  states 
)
overridevirtual

Draw the object to a render target.

This is a pure virtual function that has to be implemented by the derived class to define how the drawable should be drawn.

Parameters
targetRender target to draw to
statesCurrent render states

Implements gf::Drawable.

const Color4f& gf::Sprite::getColor ( ) const

Get the global color of the sprite.

Returns
Global color of the sprite
See also
setColor()
RectF gf::Sprite::getLocalBounds ( ) const

Get the local bounding rectangle of the entity.

The returned rectangle is in local coordinates, which means that it ignores the transformations (translation, rotation, scale, ...) that are applied to the entity. In other words, this function returns the bounds of the entity in the entity's coordinate system.

Returns
Local bounding rectangle of the entity
const Texture* gf::Sprite::getTexture ( ) const
inline

Get the source texture of the sprite.

If the sprite has no source texture, a nullptr pointer is returned. The returned pointer is const, which means that you can't modify the texture when you retrieve it with this function.

Returns
Pointer to the sprite's texture
See also
setTexture()
const RectF& gf::Sprite::getTextureRect ( ) const
inline

Get the sub-rectangle of the texture displayed by the sprite.

Returns
Texture rectangle of the sprite in texture coordinates
See also
setTextureRect()
void gf::Sprite::setAnchor ( Anchor  anchor)

Set the anchor origin of the entity.

Compute the origin of the entity based on the local bounds and the specified anchor. Internally, this function calls Transformable::setOrigin().

Parameters
anchorThe anchor of the entity
See also
getLocalBounds(), Transformable::setOrigin()
void gf::Sprite::setColor ( const Color4f color)

Set the global color of the sprite.

This color is modulated (multiplied) with the sprite's texture. It can be used to colorize the sprite, or change its global opacity.

By default, the sprite's color is opaque white.

Parameters
colorNew color of the sprite
See also
getColor()
void gf::Sprite::setTexture ( const Texture texture,
bool  resetRect = false 
)

Change the source texture of the sprite.

The texture must exist as long as the sprite uses it. Indeed, the sprite doesn't store its own copy of the texture, but rather keeps a pointer to the one that you passed to this function. If the source texture is destroyed and the sprite tries to use it, the behavior is undefined.

If resetRect is true, the texture rect property of the sprite is automatically adjusted to the size of the new texture. If it is false, the texture rect is left unchanged.

Parameters
textureNew texture
resetRectShould the texture rect be reset to the size of the new texture?
See also
getTexture(), setTextureRect()
void gf::Sprite::setTextureRect ( const RectF rect)

Set the sub-rectangle of the texture that the sprite will display.

The texture rect is useful when you don't want to display the whole texture, but rather a part of it. By default, the texture rect covers the entire texture.

The rectangle is given in texture coordinates, meaning that \((0,0)\) is the top left corner and \((1,1)\) is the bottom right corner.

Parameters
rectRectangle defining the region of the texture to display
See also
getTextureRect(), setTexture()
void gf::Sprite::unsetTexture ( )

Unset the source texture of the sprite.

After a call to this function, the sprite has no source texture.

See also
setTexture()