-
Notifications
You must be signed in to change notification settings - Fork 3
darnit_tilemap.h
Tilemaps have a few properties with tile ID worth knowing about. Before a tile ID from the map is used to draw a tile, it is first logically AND'ed with a bitmask to mask out upper bits. LDMZ puts collision data in bit 16 and up. After masking these bits out, the ID is divided with a number specified at tilemap creation. If the division yields no reminder, the tile is not drawn, otherwise it is.
A tilemaps tile ID's can be modified directly. However, coordinates are cached, so after such modifications, the cache has to be flushed for changes to take effect. This can be done manually by calling d_tilemap_recalc. It also happens automatically every time a map is moved so that a tile previously visible is no longer visible, as only the visible tiles are cached.
See the article on isometric tilemaps for information on isometric tilemaps.
typedef struct {
int w;
int h;
unsigned int *data;
void *render;
} DARNIT_TILEMAP;- w - The width in tiles of the tilemap
- h - Yjr height in tiles of the tilemap
- data - The tile IDs. To calculate the position in the array for a tile, use i = (x + w * y)
- render - Internal, don't use
Functions
- DARNIT_TILEMAP *d_tilemap_new(unsigned int invisibility_divider, DARNIT_TILESHEET *tilesheet, unsigned int mask, int w, int h);
- DARNIT_TILEMAP *d_tilemap_isom_new(unsigned int invisibility_divider, DARNIT_TILESHEET *tilesheet, unsigned int mask, int w, int h, int tile_h);
- DARNIT_TILEMAP *d_tilemap_load(const char *fname, unsigned int invisibility_divider, DARNIT_TILESHEET *tilesheet, unsigned int mask);
- DARNIT_TILEMAP *d_tilemap_isom_load(const char *fname, unsigned int invisibility_divider, DARNIT_TILESHEET *tilesheet, unsigned int mask, int tile_h);
- void d_tilemap_recalc(DARNIT_TILEMAP *tilemap);
- int d_tilemap_sprite_add(DARNIT_TILEMAP *tilemap, DARNIT_SPRITE *sprite);
- int d_tilemap_sprite_delete(DARNIT_TILEMAP *tilemap, DARNIT_SPRITE *sprite);
- void d_tilemap_camera_move(DARNIT_TILEMAP *tilemap, int cam_x, int cam_y);
- void d_tilemap_draw(DARNIT_TILEMAP *tilemap);
- DARNIT_TILEMAP *d_tilemap_free(DARNIT_TILEMAP *tilemap);
- void d_tilemap_screen_to_iso(DARNIT_TILEMAP *tm, int x, int y, int *tile_x, int *tile_y);
- void d_tilemap_iso_to_screen(DARNIT_TILEMAP *tm, int tile_x, int tile_y, int *x, int *y);
DARNIT_TILEMAP *d_tilemap_new(unsigned int invisibility_divider, DARNIT_TILESHEET *tilesheet, unsigned int mask, int w, int h);
Creates a new, blank, orthogonal tilemap.
Arguments
- invisibility_divider - The tile ID is divided with this number. If the tile ID is evenly divisable with this number, the tile will be invisible. If set to 0, all tiles will be visible.
- tilesheet - The tilesheet to use with the tilemap. A tilemap can only have one tilesheet
- mask - The tile ID is AND'ed with this value before it's divided and used. Default is 0xFFF. Not using all the bits for tile ID means other information can be put in the upper bit. LDMZ puts collision data in bit 16 and up.
- w - The width of the tilemap in tiles
- h - The height of the tilemap in tiles
Return value
Returns NULL on failure, everything else is a valid DARNIT_TILEMAP.
DARNIT_TILEMAP *d_tilemap_isom_new(unsigned int invisibility_divider, DARNIT_TILESHEET *tilesheet, unsigned int mask, int w, int h, int tile_h);
Creates a new, blank, isometric tilemap.
Arguments
- invisibility_divider - The tile ID is divided with this number. If the tile ID is evenly divisable with this number, the tile will be invisible. If set to 0, all tiles will be visible.
- tilesheet - The tilesheet to use with the tilemap. A tilemap can only have one tilesheet
- mask - The tile ID is AND'ed with this value before it's divided and used. Default is 0xFFF. Not using all the bits for tile ID means other information can be put in the upper bit. LDMZ puts collision data in bit 16 and up.
- w - The width of the tilemap in tiles
- h - The height of the tilemap in tiles
- tile_h - The height of the tiles on the map. This height is usually lower than the tile height on the tilesheet for isometric maps.
Return value
Returns NULL on failure, everything else is a valid DARNIT_TILEMAP.
DARNIT_TILEMAP *d_tilemap_load(const char *fname, unsigned int invisibility_divider, DARNIT_TILESHEET *tilesheet, unsigned int mask);
Creates an othogonal tilemap with the dimensions from a supplied image file (eg. PNG,) setting the tile ID for the tiles from the corresponding color value for the pixel at that coordinate in the image file.
Arguments
- fname - File name of the image file to load
- invisibility_divider - The tile ID is divided with this number. If the tile ID is evenly divisable with this number, the tile will be invisible. If set to 0, all tiles will be visible.
- tilesheet - The tilesheet to use with the tilemap. A tilemap can only have one tilesheet
- mask - The tile ID is AND'ed with this value before it's divided and used. Default is 0xFFF. Not using all the bits for tile ID means other information can be put in the upper bit. LDMZ puts collision data in bit 16 and up.
Return value
Returns NULL on failure, everything else is a valid DARNIT_TILEMAP.
DARNIT_TILEMAP *d_tilemap_isom_load(const char *fname, unsigned int invisibility_divider, DARNIT_TILESHEET *tilesheet, unsigned int mask, int tile_h);
Creates an isometric tilemap with the dimensions from a supplied image file (eg. PNG,) setting the tile ID for the tiles from the corresponding color value for the pixel at that coordinate in the image file.
Arguments
- fname - File name of the image file to load
- invisibility_divider - The tile ID is divided with this number. If the tile ID is evenly divisable with this number, the tile will be invisible. If set to 0, all tiles will be visible.
- tilesheet - The tilesheet to use with the tilemap. A tilemap can only have one tilesheet
- mask - The tile ID is AND'ed with this value before it's divided and used. Default is 0xFFF. Not using all the bits for tile ID means other information can be put in the upper bit. LDMZ puts collision data in bit 16 and up.
- tile_h - The height of the tiles on the map. This height is usually lower than the tile height on the tilesheet for isometric maps.
Return value
Returns NULL on failure, everything else is a valid DARNIT_TILEMAP.
void d_tilemap_recalc(DARNIT_TILEMAP *tilemap);
Flushes the coordinate cache used for rendering a tilemap. This must be done for changes in the tile data to be visible.
Arguments
- tilemap - The tilemap to flush the coordinate cache for
Return value None.
int d_tilemap_sprite_add(DARNIT_TILEMAP *tilemap, DARNIT_SPRITE *sprite);
Adds a sprite to the tilemaps list of sprites to render as the tilemap is rendered. Using this feature only really makes sense for isometric maps, when a part of the map can cover a sprite.
Arguments
- tilemap - The tilemap to add a sprite to
- sprite - The sprite to add to the tilemap
Return value
Returns -1 on failure. 0 or greater indicates success.
int d_tilemap_sprite_delete(DARNIT_TILEMAP *tilemap, DARNIT_SPRITE *sprite);
Deletes a sprite from the tilemap.
Arguments
- tilemap - The tilemap to delete a sprite from
- sprite - The sprite to delete
Return value
Returns -1 if the sprite was not found in the tilemap. 0 or greater means that the sprite was found and removed.
void d_tilemap_camera_move(DARNIT_TILEMAP *tilemap, int cam_x, int cam_y);
Moves the camera for the tilemap to coordinates x, y, making that area of the tilemap visible.
Arguments
- tilemap - The tilemap to move the camera for
- cam_x - The X-coordinate for the camera in pixels
- cam_y - The Y-coordinate for the camera in pixels
Return value None.
void d_tilemap_draw(DARNIT_TILEMAP *tilemap);
Draws the tilemap.
Arguments
- tilemap - The tilemap to draw
Return value None.
DARNIT_TILEMAP *d_tilemap_free(DARNIT_TILEMAP *tilemap);
Frees a tilemap and releases all of its resources. It does not free any sprites added to it., and does not free the tilesheet it was assigned.
Arguments
- tilemap - The tilemap to free
Return value
Returns NULL for compact pointer clearing.
void d_tilemap_screen_to_iso(DARNIT_TILEMAP *tm, int x, int y, int *tile_x, int *tile_y);
Translates an orthogonal pixel coordinate to the tile coordinate in the tilemap. This function only makes sense when using isometric maps.
Arguments
- tm - The tilemap to translate a coordinate to
- x - The orthogonal X-coordinate to translate
- y - The orthogonal Y-coordinate to translate
- tile_w - The isometric tile X-coordinate
- tile_h - The isometric tile Y-coordinate
Return value None.
void d_tilemap_iso_to_screen(DARNIT_TILEMAP *tm, int tile_x, int tile_y, int *x, int *y);
Translates an isometric tile coordinate to an orthogonal pixel coordinate
Arguments
- tm - The tilemap to translate a coordinate from
- tile_w - The tile X-coordinate to translate
- tile_h - The tile Y-coordinate to translate
- x - The orthogonal translated X coordinate
- y - The orthogonal translated Y coordinate