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"> <properties> <property name="type" value="grass"/> <property name="height" value="3"/> </properties> </tile> </tileset>
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.
<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.
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. | |
Image * | GetImage (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 Tileset * | Load (const char *filename) |
Loads the tileset from a xml file. |
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 | ) |
int e2d::graphics::Tileset::GetNumberOfTiles | ( | void | ) |
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 |
int e2d::graphics::Tileset::GetTileHeight | ( | void | ) |
vector< SDL_Rect > * e2d::graphics::Tileset::GetTileRefs | ( | void | ) |
int e2d::graphics::Tileset::GetTileWidth | ( | void | ) |
bool e2d::graphics::Tileset::IsLogical | ( | void | ) |
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. |
void e2d::graphics::Tileset::Optimize | ( | ) |
Calls Image::Optimize on this tileset's image.