Gamedev Framework (gf)  0.3.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...
 
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...
 
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:

1 <TextureAtlas imagePath="bricks.png">
2  <SubTexture name="brickBlack01" x="718" y="500" width="32" height="32"/>
3  <SubTexture name="brickBlack02" x="716" y="66" width="64" height="32"/>
4  ...
5 </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

gf::TextureAtlas::TextureAtlas ( )
inline

Default constructor.

Member Function Documentation

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
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()
const Texture* gf::TextureAtlas::getTexture ( ) const
inline

Get the source texture of the atlas.

If the atlas 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 atlas' texture
See also
setTexture()
const Path& gf::TextureAtlas::getTexturePath ( ) const
inline

Get the texture path.

Returns
The path to the texture
See also
setTexturePath()
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()
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
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()
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()
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()