e2d::graphics::Tileset Class Reference

Inheritance diagram for e2d::graphics::Tileset:


List of all members.

Detailed Description

A tileset is a collection of images exactly of the same size, called tiles. This class loads an image full of tiles, separates them (logically, not phisically), and lets you blit them separately.

For the tileset to recognize an image as containing tiles, this image must be composed of tiles exactly the same size (say, 32x16 pixels), distributed in any number of lines and columns, with no "extra space" between tiles (i.e., pixels that are neither tiles nor a border/grid).

After loading, each tile will be given a number. This number starts from zero as being the most upper-left tile of the image, and then increasing one by one from left to rigth, and then from top to bottom.

You can also have "logical" tilesets. These special tilesets will not load their images into memory, and are useful for creating logical layers (for example, collision layers). You can set tile properties as usual.

When loading a tileset from a xml file, the <tileset> tag must follow the rules described below:

  <tileset name="MyTileset" tilewidth="24" tileheight="24" margin="1" spacing="1" logical="false">
    <image source="MyImage.png" trans="FF00FF"/>
    <tile id="0">
        <property name="type" value="grass"/>
        <property name="height" value="3"/>

The attribute name is the name that is going to be used to store the tileset in the ResourceMap. Attributes tilewidth and tileheight are the dimensions of each tile; margin (optional) is the size in pixels of a border around the image; and spacing (also optional) is the size in pixels of the spacing between the tiles. The attribute logical is optional, and indicates a logical tileset if set to "true". In tag <image>, source is the path to the image filename (relative to your executable file); trans is a RGB hexadecimal value for a color that will be considered transparent (not blitted).

Tag <tile> is optional, you can have as many as there are tiles in your tileset. This tag is created by Tiled to store the <properties> tag. For each tile, you can have as many properties as you want, specified in the <property> tag. They have attributes name and value. You can retrieve these properties using the GetProperty() method.

Attribute logical is not a feature of Tiled. If you want a logical tileset you will have to edit the xml file by yourself.
As an option, the tileset could be contained in a file of its own, and just referenced from the map or the sprite file. The tag would then look like this:

  <tileset source="mytileset.tsx"/>

Where source is a path to a xml file that shall contain the tileset tag as previously described. The path is always relative to your executable file.

If you load a tileset using a xml file, it will be automatically added to (or recovered from) the ResourceMap.

Public Member Functions

void Blit (int tile_index, Rect *position, SDL_Surface *destination)
 Blits given tile at given position to the destination.
int GetFirstGid (void)
 Returns the firstgid index.
ImageGetImage (void)
 Returns the tileset image.
int GetNumberOfTiles (void)
 Returns the number of tiles available.
string GetProperty (int tile_index, string property)
 Gets a property of a tile.
int GetTileHeight (void)
 Returns the tile height, in pixels.
vector< SDL_Rect > * GetTileRefs (void)
 Gets the tile dimension mappings.
int GetTileWidth (void)
 Returns the tile width, in pixels.
bool IsLogical (void)
 Tells if this tileset is a logical tileset.
void Load (const char *filename, int tile_width, int tile_height, int border=0, int spacing=0)
 Loads the tileset from an image file.
void Load (xml_node<> *node)
 Loads the tileset from a xml tag.
void Optimize ()
 Optimizes blitting.

Static Public Member Functions

static TilesetLoad (const char *filename)
 Loads the tileset from a xml file.

Member Function Documentation

void e2d::graphics::Tileset::Blit ( int  tile_index,
Rect position,
SDL_Surface *  destination 

Blits given tile at given position to the destination

tile_index The tile you want to blit. If negative, no blitting occurs.
position Position of the destination surface where the upper-left corner of the tile will be blitted.
destination Pointer to an SDL_Surface where the tile will be blitted (may be the Video::framebuffer or another image's surface).
e2d::core::Exception if tile_number is greater than the number of tiles available in this tileset.

int e2d::graphics::Tileset::GetFirstGid ( void   ) 

"firstgid" is a marker of Tiled to control multiple tilesets in a single map.


Image * e2d::graphics::Tileset::GetImage ( void   ) 

A pointer to this tileset's image

int e2d::graphics::Tileset::GetNumberOfTiles ( void   ) 

Number of tiles available

string e2d::graphics::Tileset::GetProperty ( int  tile_index,
string  property 

Properties are optionally defined on the xml file for each tile. You can retrieve them with this method.

tile_index Tile from which you want to retrieve the property.
property Name of the property from which you want the value
Value of the named property for the given tile.

int e2d::graphics::Tileset::GetTileHeight ( void   ) 

Tile height, in pixels

vector< SDL_Rect > * e2d::graphics::Tileset::GetTileRefs ( void   ) 

a pointer to the vector of clip rects correspondent to the tiles of this tileset.

int e2d::graphics::Tileset::GetTileWidth ( void   ) 

Tile width, in pixels

bool e2d::graphics::Tileset::IsLogical ( void   ) 

true if this is a logical tileset, false otherwise

void e2d::graphics::Tileset::Load ( const char *  filename,
int  tile_width,
int  tile_height,
int  border = 0,
int  spacing = 0 

Loads an image and logically separates it into tiles.

filename Path to the image file.
tile_width Width of each tile in the image.
tile_height Height of each tile in the image.
border size in pixels of a border around the image.
spacing size in pixels of the spacing between the tiles.
e2d::core::Exception If the image loading failed.

void e2d::graphics::Tileset::Load ( xml_node<> *  node  ) 

Reads the data from the <tileset> tag of a map created with Tiled.

node xml tag (xml_node is part of RapidXML).
e2d::core::Exception if element is either null or not a <tileset> tag.

Tileset * e2d::graphics::Tileset::Load ( const char *  filename  )  [static]

Tiled can export tilesets to files with extension *.tsx. Easy2D can read these files with this method. When loading a tileset from a xml file, it will be automatically added to (or recovered from) the ResourceMap.

filename Name of the *.tsx file containing tileset data.
instance of the loaded tileset.

void e2d::graphics::Tileset::Optimize (  ) 

Calls Image::Optimize on this tileset's image.

Generated on Tue Dec 8 10:32:45 2009 for Easy2D by  doxygen 1.5.6