Skip to content
modkitv0.2

tilemap.h

#include <modkit/tilemap.h>15 functions · 4 structs · 2 enums · 7 typedefs · 2 macros

Tile map rendering, autotiling and Tiled import (opt-in).

An OPT-IN package (MODKIT_ENABLE_TILEMAP, default OFF) that requires MODKIT_ENABLE_SPRITE. When disabled every symbol here still links and returns an invalid handle, false, or MK_ERROR_UNSUPPORTED. A tilemap is a fixed grid of layers over a single tileset texture. Tiles are baked into per-chunk static vertex buffers and rebuilt only when edited, so static maps cost nothing per frame and edited ones only pay for the chunks that changed. Drawing goes through an open canvas, which keeps painter order with the rest of your 2D content. Typical use:

Functions

mk_autotile_maskfunction

uint32_t mk_autotile_mask(const bool neighbours[MK_NEIGHBOUR_COUNT], mk_autotile_kind kind)

Canonical variant index for a neighbourhood.

Pure function of the neighbour bits, exposed so callers can drive their own tile selection without owning a tilemap. Returns < 16 for EDGE_16 and < 47 for BLOB_47.

ParameterTypeDescription
neighboursconst boolOccupancy in mk_neighbour order.
kindmk_autotile_kindAutotile rule used to canonicalize the mask

Returns The resulting value.

mk_tilemap_autotilefunction

mk_result mk_tilemap_autotile(mk_tilemap_t map, uint32_t layer, const mk_terrain_desc *terrain)

Rewrite every non-empty tile of a layer to the terrain variant that fits.

ParameterTypeDescription
mapmk_tilemap_tValue for map.
layeruint32_tValue for layer.
terrainconst mk_terrain_desc *Value for terrain.

Returns MK_SUCCESS or an error result.

mk_tilemap_clear_layerfunction

void mk_tilemap_clear_layer(mk_tilemap_t map, uint32_t layer)

Clear the layer for the tilemap.

ParameterTypeDescription
mapmk_tilemap_tValue for map.
layeruint32_tValue for layer.

mk_tilemap_createfunction

mk_tilemap_t mk_tilemap_create(const mk_tilemap_desc *desc)

Create tilemap.

ParameterTypeDescription
descconst mk_tilemap_desc *Configuration descriptor.

Returns The resulting handle or value.

mk_tilemap_destroyfunction

void mk_tilemap_destroy(mk_tilemap_t map)

Destroy the tilemap.

ParameterTypeDescription
mapmk_tilemap_tValue for map.

mk_tilemap_drawfunction

uint32_t mk_tilemap_draw(mk_tilemap_t map, mk_canvas_t canvas, const mk_camera2d *camera)

Draw every visible layer into an open canvas.

Must be called between mk_begin_pass()/mk_end_pass() on a painter-ordered 2D pass, with the canvas in drawing mode. Pass the camera to cull off-screen chunks, or NULL to draw them all.

ParameterTypeDescription
mapmk_tilemap_tValue for map.
canvasmk_canvas_tValue for canvas.
cameraconst mk_camera2d *Value for camera.

Returns Number of chunks submitted (0 when fully culled or unavailable).

mk_tilemap_get_tilefunction

mk_tile_t mk_tilemap_get_tile(mk_tilemap_t map, uint32_t layer, uint32_t x, uint32_t y)

Get tile from the tilemap.

ParameterTypeDescription
mapmk_tilemap_tValue for map.
layeruint32_tValue for layer.
xuint32_tValue for x.
yuint32_tValue for y.

Returns The resulting handle or value.

mk_tilemap_heightfunction

uint32_t mk_tilemap_height(mk_tilemap_t map)

Perform the tilemap height operation.

ParameterTypeDescription
mapmk_tilemap_tValue for map.

Returns The resulting value.

mk_tilemap_is_validfunction

bool mk_tilemap_is_valid(mk_tilemap_t map)

Test whether the tilemap is valid.

ParameterTypeDescription
mapmk_tilemap_tValue for map.

Returns True when the condition holds.

mk_tilemap_layer_countfunction

uint32_t mk_tilemap_layer_count(mk_tilemap_t map)

Perform the tilemap layer count operation.

ParameterTypeDescription
mapmk_tilemap_tValue for map.

Returns The resulting value.

mk_tilemap_layer_visiblefunction

bool mk_tilemap_layer_visible(mk_tilemap_t map, uint32_t layer)

Perform the tilemap layer visible operation.

ParameterTypeDescription
mapmk_tilemap_tValue for map.
layeruint32_tValue for layer.

Returns True when the operation succeeds.

mk_tilemap_load_tiledfunction

mk_tilemap_t mk_tilemap_load_tiled(const char *tmj_path, const mk_tileset_desc *tileset_override)

Load an orthogonal map from Tiled's JSON export (.tmj).

Handles tile layers with array or CSV data, embedded and external (.tsj) tilesets, and the flip bits Tiled packs into each GID. Returns MK_TILEMAP_INVALID for infinite maps, isometric/hexagonal orientations, and base64/compressed layer data rather than misreading them.

ParameterTypeDescription
tmj_pathconst char *Path to the .tmj file.
tileset_overrideconst mk_tileset_desc *Use this tileset instead of the one named in the file; pass NULL to resolve the file's own tileset image relative to the .tmj.

Returns The resulting handle or value.

mk_tilemap_set_layer_visiblefunction

void mk_tilemap_set_layer_visible(mk_tilemap_t map, uint32_t layer, bool visible)

Set layer visible on the tilemap.

ParameterTypeDescription
mapmk_tilemap_tValue for map.
layeruint32_tValue for layer.
visibleboolValue for visible.

mk_tilemap_set_tilefunction

mk_result mk_tilemap_set_tile(mk_tilemap_t map, uint32_t layer, uint32_t x, uint32_t y, mk_tile_t tile)

Set tile on the tilemap.

ParameterTypeDescription
mapmk_tilemap_tValue for map.
layeruint32_tValue for layer.
xuint32_tValue for x.
yuint32_tValue for y.
tilemk_tile_tValue for tile.

Returns MK_SUCCESS or an error result.

mk_tilemap_widthfunction

uint32_t mk_tilemap_width(mk_tilemap_t map)

Perform the tilemap width operation.

ParameterTypeDescription
mapmk_tilemap_tValue for map.

Returns The resulting value.

Structs

mk_terrain_descstruct

Data for terrain desc.

FieldTypeDescription
kindmk_autotile_kindThe kind.
variantsconst uint32_t *Tile indices, indexed by the canonical mask.
variant_countuint32_t16 for EDGE_16, 47 for BLOB_47.
solid_minuint32_tTiles in [solid_min, solid_max] count as terrain.
solid_maxuint32_t0 => any non-empty tile counts.
out_of_bounds_solidboolTreat off-map as terrain (usual for ground).

mk_tilestruct

One cell of a layer.

index is 1-based; 0 means empty.

FieldTypeDescription
indexuint32_tThe index.
flipuint8_tBitmask of mk_flip_t.

mk_tilemap_descstruct

Data for tilemap desc.

FieldTypeDescription
widthuint32_tMap width in tiles.
heightuint32_tMap height in tiles.
layer_countuint32_t0 => 1.
tilesetmk_tileset_descThe tileset.
chunk_sizeuint16_tTiles per chunk edge.
tile_world_wfloatWorld units per tile.
tile_world_hfloatThe tile world h.

mk_tileset_descstruct

How a tileset texture is cut into tiles.

FieldTypeDescription
texturemk_texture_tThe texture.
tile_wuint16_tTile width in pixels.
tile_huint16_tTile height in pixels.
marginuint16_tBorder around the whole grid, in pixels.
spacinguint16_tGap between adjacent tiles, in pixels.
columnsuint16_t0 = derive from texture width.

Enums

mk_autotile_kindenum

Which neighbour bits a terrain set is indexed by.

ValueDescription
MK_AUTOTILE_EDGE_164 edge neighbours -> 16 variants.
MK_AUTOTILE_BLOB_478 neighbours reduced to 47 variants.

mk_neighbourenum

Neighbour order for mk_autotile_mask(), clockwise from north.

ValueDescription
MK_NEIGHBOUR_NSelects n.
MK_NEIGHBOUR_NESelects ne.
MK_NEIGHBOUR_ESelects e.
MK_NEIGHBOUR_SESelects se.
MK_NEIGHBOUR_SSelects s.
MK_NEIGHBOUR_SWSelects sw.
MK_NEIGHBOUR_WSelects w.
MK_NEIGHBOUR_NWSelects nw.
MK_NEIGHBOUR_COUNTSelects count.

Typedefs

mk_autotile_kindtypedef

typedef enum mk_autotile_kind mk_autotile_kind

Which neighbour bits a terrain set is indexed by.

mk_neighbourtypedef

typedef enum mk_neighbour mk_neighbour

Neighbour order for mk_autotile_mask(), clockwise from north.

mk_terrain_desctypedef

typedef struct mk_terrain_desc mk_terrain_desc

Data for terrain desc.

mk_tile_ttypedef

typedef struct mk_tile mk_tile_t

One cell of a layer.

index is 1-based; 0 means empty.

mk_tilemap_desctypedef

typedef struct mk_tilemap_desc mk_tilemap_desc

Data for tilemap desc.

mk_tilemap_ttypedef

typedef mk_tilemap_handle_t mk_tilemap_t

Type used for tilemap.

mk_tileset_desctypedef

typedef struct mk_tileset_desc mk_tileset_desc

How a tileset texture is cut into tiles.

Macros

MK_TILE_EMPTYdefine

MK_TILE_EMPTY

Constant for tile empty.

MK_TILEMAP_INVALIDdefine

MK_TILEMAP_INVALID

Invalid sentinel for tilemap.