Tile Maps

Function CreateTileSet(img_id, tile_w, tile_h)

Return a new Tileset

Parameters:

  • img_id - A image to use as a tile sheet.
  • tile-w - The width of each tile.
  • tile_h - The height of each tile.
isFullScreen = false 
vSync = true
winWidth = 640
winHeight = 480
canViewPortX = 0
canViewPortY = 0

OpenWindow("TestTileset", winWidth, winHeight, isFullScreen, vSync)
mCanvas = OpenCanvas(winWidth, winHeight, canViewPortX, canViewPortY, winWidth, winHeight, 1)
Canvas(mCanvas)

mAtlas = LoadImage("testTileSet.png") 'Load in our atlas or tile set image

mTileSet = CreateTileSet(mAtlas, 24, 24) 'Create a tile set and assign it a holder to access

mTileMap = CreateTileMap(mTileSet, 20, 20) 'Create a tile map with our tile set holder "mTileSet"

'Now we have a blacnk tilemap to fill with tiles, we now use the mAtlas image to fill in the map
'First we will assign a grass tile across our entire tile map. lets grab a grass tile to fill in.

FillTile(mTileMap, 166, 0, 0, 20, 20) 'The tile number goes by starting at the top left corner to the right edge of the image,
'then wraps around and comntinues the count, the image is 13 wide (0-12) so 166 would be
'2 in from the end of the last row of images on the tileset.

SetTile(mTileMap, 15, 0, 0) 'With the map filled with grass tiles we can change a single tile, to a little guy in the
'top left corner.

'Before we begin our main loop, we need to make some variables for holding some important info
mOffsetX = 0
mOffsetY = 0 'Offset values for where the tile is to be drawn from, used for scrolling.
mViewportX = 640
mViewportY = 480 'How much of the tile map is to be drawn on the canvas

While Not Key(K_ESCAPE)
ClearCanvas()

DrawTileMap(mTileMap, 0, 0, mViewportX, mViewportY, mOffsetX, mOffsetY)

If Key(K_UP) Then
mOffsetY = mOffsetY - 0.5
ElseIf Key(K_DOWN) Then
mOffsetY = mOffsetY + 0.5
ElseIf Key(K_LEFT) Then
mOffsetX = mOffsetX - 0.5
ElseIf Key(K_RIGHT) Then
mOffsetX = mOffsetX + 0.5
End If

Update()
Wend

'As you can see this replaces the grass tile with a character tile, if we wanted to place the character or any other object or image ontop of the tile
'we would DrawImage() an image not using the tilemap.


Sub SetTileAnimationLength(tileset, base_tile, num_frames)

Sets the number of frames in a tile’s animation


Function GetTileAnimationLength(tileset, base_tile)

Return the number of frames in a tile’s animation


Sub SetTileAnimationFrame(tileset, base_tile, anim_frame, tile)

Sets a frame of animation for a tile


Function GetTileAnimationFrame(tileset, base_tile, anim_frame)

Returns the tile set at a specified frame in a tiles animation


Sub SetTileAnimationSpeed(tileset, base_tile, speed)

Sets the speed of a tile’s animation


Function GetTileAnimationSpeed(tileset, base_tile)

Returns the fps of a tile’s animation


Function CreateTileMap(tileset, widthInTiles, heightInTiles)

Returns a new tile map.

Parameters:

  • tileset - A tileset Id. ( Tilesets are created with CreateTileSet() )
  • widthInTiles - The number of tiles wide ( Note: Not the pixel width but number of tiles )
  • widthInTiles - The number of tiles high ( Note: Not the pixel height but number of tiles )

See also

CreateTileSet()


Sub SetTileMapSize(tilemap, widthInTiles, heightInTiles)

Resize a tile map


Sub GetTileMapSize(tilemap, ByRef widthInTiles, ByRef heightInTiles)

Gets the size of a tile map


Sub SetTile(tilemap, tile, x, y)

Places a tile on a tilemap


Function GetTile(tilemap, x, y)

Returns the tile at a specified location on a tilemap Note: (x, y) is a tile position, not an actual coordinate on the map


Sub FillTile(tilemap, tile, x, y, widthInTiles, heightInTiles)

Fills an area of a tile map with a specified tile


Sub DrawTileMap(tilemap, x, y, w, h, offset_x, offset_y)

Draws a tile map Note: Can only be drawn on a regular 2D canvas


Sub DeleteTileSet(tileset)

Sub DeleteTileMap(tilemap)

Function TileSetExists(tileset)

Returns true if the given tileset is an active tileset in the program

See also

TileMapExists()


Function TileMapExists(tilemap)

Returns true if the given tilemap is an active tilemap in the program

See also

TileSetExists()