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

A collection of sub-texture. More...

#include <gf/TextureAtlas.h>

Public Member Functions

 TextureAtlas ()
 Default constructor. More...
 
bool loadFromFile (const Path &filename)
 Load an atlas from a XML file. More...
 
bool loadFromFile (const Path &filename, ResourceManager &resources)
 Load an atlas from a XML file. More...
 
void setTexturePath (const Path &path)
 Set the texture path. More...
 
const PathgetTexturePath () const
 Get the texture path. More...
 
void setTexture (const Texture &texture)
 Change the source texture of the atlas. More...
 
const TexturegetTexture () const
 Get the source texture of the atlas. More...
 
bool hasTexture () const
 Check if a texture is set. More...
 
void unsetTexture ()
 Unset the source texture of the atlas. More...
 
void addSubTexture (std::string name, const RectU &rect)
 Add a sub-texture to the atlas. More...
 
RectU getSubTexture (const std::string &name) const
 Get the sub-texture rectangle. More...
 
RectF getTextureRect (const std::string &name) const
 Get the texture rectangle in normalized coordinates. More...
 

Detailed Description

A collection of sub-texture.

A texture atlas is a collection of sub-texture that have been packed together in a single texture. The description of the sub-textures is done in a XML file. This description is a name and the texture coordinates in the texture.

Here is an example of a XML file containing an atlas:

<TextureAtlas imagePath="bricks.png">
<SubTexture name="brickBlack01" x="718" y="500" width="32" height="32"/>
<SubTexture name="brickBlack02" x="716" y="66" width="64" height="32"/>
...
</TextureAtlas>

gf::TextureAtlas is able to read the XML file and give the coordinates thanks to the name of the sub-texture, either in pixels or in texture normalized coordinates.

See also
gf::Texture
Texture Atlas (wikipedia)

Constructor & Destructor Documentation

◆ TextureAtlas()

gf::TextureAtlas::TextureAtlas ( )
inline

Default constructor.

Member Function Documentation

◆ addSubTexture()

void gf::TextureAtlas::addSubTexture ( std::string  name,
const RectU rect 
)

Add a sub-texture to the atlas.

Parameters
nameThe name of the sub-texture
rectThe rectangle inside the texture

◆ getSubTexture()

RectU gf::TextureAtlas::getSubTexture ( const std::string &  name) const

Get the sub-texture rectangle.

Parameters
nameThe name of the sub-texture
Returns
The rectangle corresponding to the name
See also
getTextureRect()

◆ getTexture()

const Texture& gf::TextureAtlas::getTexture ( ) const
inline

Get the source texture of the atlas.

The returned reference is const, which means that you can't modify the texture when you retrieve it with this function.

Returns
A reference to the atlas' texture
See also
setTexture()

◆ getTexturePath()

const Path& gf::TextureAtlas::getTexturePath ( ) const
inline

Get the texture path.

Returns
The path to the texture
See also
setTexturePath()

◆ getTextureRect()

RectF gf::TextureAtlas::getTextureRect ( const std::string &  name) const

Get the texture rectangle in normalized coordinates.

Parameters
nameThe name of the sub-texture
Returns
The texture rectangle in normalized coordinates
See also
getSubTexture()

◆ hasTexture()

bool gf::TextureAtlas::hasTexture ( ) const
inline

Check if a texture is set.

Returns
True if a texture is already set
See also
setTexture(), getTexture()

◆ loadFromFile() [1/2]

bool gf::TextureAtlas::loadFromFile ( const Path filename)

Load an atlas from a XML file.

Load the data about the sub-textures from a XML file.

Parameters
filenameThe filename of the XML file
Returns
True if the file has been loaded

◆ loadFromFile() [2/2]

bool gf::TextureAtlas::loadFromFile ( const Path filename,
ResourceManager resources 
)

Load an atlas from a XML file.

Load the data about the sub-textures from a XML file. In addition, load the texture thanks to a resource manager.

Parameters
filenameThe filename of the XML file
resourcesA resource manager
Returns
True if the file has been loaded

◆ setTexture()

void gf::TextureAtlas::setTexture ( const Texture texture)
inline

Change the source texture of the atlas.

The texture must exist as long as the atlas uses it. Indeed, the atlas 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 atlas tries to use it, the behavior is undefined.

Parameters
textureNew texture
See also
getTexture(), getTexturePath()

◆ setTexturePath()

void gf::TextureAtlas::setTexturePath ( const Path path)
inline

Set the texture path.

The texture path comes from the XML file when it is loaded.

Parameters
pathThe path to the texture
See also
getTexturePath()

◆ unsetTexture()

void gf::TextureAtlas::unsetTexture ( )
inline

Unset the source texture of the atlas.

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

See also
setTexture()