Tilemap library

A library for tilemap based games.

File:
Tilemap.lib
Version:
1.0
Author:
Marcus Johansson

Contents

Details
Constants
Sub routines


Details

You can, and should, use this library with maps created in TM Editor. It takes care of rendering and collision handling for many types of 2D games.


Constants

Array indexing

Name
Description
TM_X
X coordinate.
TM_Y
Y coordinate.
TM_FLAG
Flag.

Collision info

Name
Description
TM_LEFT
Left.
TM_RIGHT
Right.
TM_UP
Up.
TM_DOWN
Down.


Subroutines

Map

Name
Parameters
Brief description
function
TM_LoadMap
filename$
Load map.
function
TM_GetLoaderFlags[][]
Get loader flags.
function
TM_MapWidth
Get map width.
function
TM_MapHeight
Get map height.
function
TM_WorldWidth
Get world width.
function
TM_WorldHeight
Get world height.
procedure
TM_InitMap
w, h
Init map.
procedure
TM_SetImage
img
Set image.
procedure
TM_SetObstacle
cel, value
Make cel an obstacle.
procedure
TM_SetOnlyDown
cel, value
Make cel become an obstacle from top only.
procedure
TM_SetBorder
l, r, t, b
Set map border collision behavior.
procedure
TM_SetCel
x, y, cel
Set cel.
procedure
TM_SetFlag
x, y, flag
Set flag.
function
TM_GetCel
x, y
Get cel.
function
TM_GetCelAt
x, y
Get cel.
function
TM_GetFlag
x, y
Get game flag.
function
TM_GetFlagAt
x, y
Get game flag.
procedure
TM_SetView
x, y, w, h
Set view.
procedure
TM_SetCamera
x, y
Set camera position.
function
TM_Obstacle
cel
Return true if image cel in map image is an obstacle.
function
TM_ToTile[]
x, y
Convert from world to map coordinates.
function
TM_GetImage
Get map image.
function
TM_CameraX
Get camera x position in world.
function
TM_CameraY
Get camera y position in world.
procedure
TM_Render
Render.

Coordinate conversion

Name
Parameters
Brief description
function
TM_ToWorld[]
x, y
Convert from screen coordinates to world.
function
TM_ToWorldF#[]
x#, y#
Convert from floating point screen coordinates to world.
function
TM_ToWorldX
x
Convert from x screen coordinate to world.
function
TM_ToWorldXF#
x#
Convert from floating point x screen coordinate to world.
function
TM_ToWorldY
y
Convert from y screen coordinate to world.
function
TM_ToWorldYF#
y#
Convert from floating point y screen coordinate to world.
function
TM_ToScreen[]
x, y
Convert from world coordinates to screen.
function
TM_ToScreenF#[]
x#, y#
Convert from floating point world coordinates to screen.
function
TM_ToScreenX
x
Convert x world coordinate to screen.
function
TM_ToScreenXF#
x#
Convert floating point x world coordinate to screen.
function
TM_ToScreenY
y
Convert y world coordinate to screen.
function
TM_ToScreenYF#
y#
Convert floating point y world coordinate to screen.
function
TM_Visible
img, x, y
Return true if image at world coordinates is visible.

Collision handling

Name
Parameters
Brief description
function
TM_Move[]
img, imgx, imgy, dx, dy
Try move image.
function
TM_MoveRect[]
rectx, recty, w, h, dx, dy
Try move rectangle.
function
TM_MoveF#[]
img, imgx#, imgy#, dx#, dy#
Try move image.
function
TM_GetCollisionInfo[]
Get collision information.
function
TM_CollisionLeft
Returns true if last collision occured leftwards.
function
TM_CollisionRight
Returns true if last collision occuered rightwards.
function
TM_CollisionDown
Returns true if last collision occured downwards.
function
TM_CollisionUp
Returns true if last collision occured upwards.
function
TM_ImageCol
img1, x1, y1, img2, x2, y2
Return true if two images are overlapping.


Subroutine documentation

function TM_LoadMap ( filename$ )

Load a map created with TM Editor.

Parameter
Mode
Description
filename
In
Filename.

Return value
True on success.

[ Back ]


function TM_GetLoaderFlags[][] ( )

This function returns the loader flags from the last loaded map. You can access a loader flag and its position with [n][TM_FLAG], [n][TM_X] and [n][TM_Y].

Return value

[ Back ]


function TM_MapWidth ( )

This function returns the width of the map in tiles.

Return value
Map width.

[ Back ]


function TM_MapHeight ( )

This function returns the height of the map in tiles.

Return value
Map height.

[ Back ]


function TM_WorldWidth ( )

Get world width.

Return value
World width.

[ Back ]


function TM_WorldHeight ( )

Get world height.

Return value
World height.

[ Back ]


procedure TM_InitMap ( w, h )

Init a map with specified size. This procedure is only of use if you're not using TM Editor.

Parameter
Mode
Description
w
In
Width of map in tiles.
h
In
Height of map in tiles.

[ Back ]


procedure TM_SetImage ( img )

Set image for map to use. This function is only useful if you're not using TM Editor.

Parameter
Mode
Description
img
In
Image identifier.

[ Back ]


procedure TM_SetObstacle ( cel, value )

Make cel in map image become an obstacle. This can be set in TM Editor.

Parameter
Mode
Description
cel
In
Cel index in map image.
value
In
True or false.

[ Back ]


procedure TM_SetOnlyDown ( cel, value )

It is common in platform games that some tiles can only be collided with when approached from the top. To make a certain cel in the map image behave like that, you can use this procedure. @value True or false.

Parameter
Mode
Description
cel
In
Cel index in map image.

[ Back ]


procedure TM_SetBorder ( l, r, t, b )

When an object reaches the border of a map it can either continue moving or be stopped. This procedure sets the behavior for the four borders.

Parameter
Mode
Description
l
In
True if objects should be able to collide with left border.
r
In
True if objects should be able to collide with right border.
t
In
True if objects should be able to collide with top border.
b
In
True if objects should be able to collide with bottom border.

[ Back ]


procedure TM_SetCel ( x, y, cel )

Set cel in map image to use at a map position.

Parameter
Mode
Description
x
In
X map coordinate.
y
In
Y map coordinate.
cel
In
Image cel in map image.

[ Back ]


procedure TM_SetFlag ( x, y, flag )

Set game flag at a map position.

Parameter
Mode
Description
x
In
X map coordinate.
y
In
Y map coordinate.
flag
In
Game flag.

[ Back ]


function TM_GetCel ( x, y )

Get cel index in map image at map position.

Parameter
Mode
Description
x
In
X map coordinate.
y
In
Y map coordinate.

Return value
Cel index.

[ Back ]


function TM_GetCelAt ( x, y )

Get cel index in map image at world position.

Parameter
Mode
Description
x
In
X world coordinate.
y
In
Y world coordinate.

Return value
Cel index.

[ Back ]


function TM_GetFlag ( x, y )

Get game flag at map position.

Parameter
Mode
Description
x
In
X map coordinate.
y
In
Y map coordinate.

Return value
Game flag.

[ Back ]


function TM_GetFlagAt ( x, y )

Get game flag at world position.

Parameter
Mode
Description
x
In
X world coordinate.
y
In
Y world coordinate.

Return value
Game flag.

[ Back ]


procedure TM_SetView ( x, y, w, h )

Set render view and size. If nothing is showing when you call 'TM_Render', you've probably forgot to call this procedure.

Parameter
Mode
Description
x
In
X screen coordinate.
y
In
Y screen coordinate.
w
In
Screen width.
h
In
Screen height.

[ Back ]


procedure TM_SetCamera ( x, y )

Set the top left corner of the camera.

Parameter
Mode
Description
x
In
X world coordinate.
y
In
Y world coordinate.

[ Back ]


function TM_Obstacle ( cel )

Return true if image cel in map image is an obstacle.

Parameter
Mode
Description
cel
In
Cel index in map image.

Return value
True if cel is an obstacle.

[ Back ]


function TM_ToTile[] ( x, y )

Convert from world to map coordinates.

Parameter
Mode
Description
x
In
X world coordinate.
y
In
Y world coordinate.

Return value
Array with map coordinates accessed with [TM_X] and [TM_Y].

[ Back ]


function TM_GetImage ( )

Get map image.

Return value
Image id of map image.

[ Back ]


function TM_CameraX ( )

Get camera x position in world.

Return value
Camera x position.

[ Back ]


function TM_CameraY ( )

Get camera y position in world.

Return value
Camera y position.

[ Back ]


procedure TM_Render ( )

Render.

[ Back ]


function TM_ToWorld[] ( x, y )

Convert from screen coordinates to world.

Parameter
Mode
Description
x
In
X screen coordinate.
y
In
Y screen coordinate.

Return value
World coordinates accessed as [TM_X] and [TM_Y].

[ Back ]


function TM_ToWorldF#[] ( x#, y# )

Convert from floating point screen coordinates to world.

Parameter
Mode
Description
x
In
X screen coordinate.
y
In
Y screen coordinate.

Return value
World coordinates accessed as [TM_X] and [TM_Y].

[ Back ]


function TM_ToWorldX ( x )

Convert from x screen coordinate to world.

Parameter
Mode
Description
x
In
X screen coordinate.

Return value

[ Back ]


function TM_ToWorldXF# ( x# )

Convert from floating point x screen coordinate to world.

Parameter
Mode
Description
x
In
X screen coordinate.

Return value
X world coordinate.

[ Back ]


function TM_ToWorldY ( y )

Convert from y screen coordinate to world.

Parameter
Mode
Description
y
In
Y screen coordinate.

Return value
Y world coordinate.

[ Back ]


function TM_ToWorldYF# ( y# )

Convert from floating point y screen coordinate to world.

Parameter
Mode
Description
y
In
Y screen coordinate.

Return value
Y world coordinate.

[ Back ]


function TM_ToScreen[] ( x, y )

Convert from world coordinates to screen.

Parameter
Mode
Description
x
In
X world coordinate.
y
In
Y world coordinate.

Return value
Screen coordinates accessed as [TM_X] and [TM_Y].

[ Back ]


function TM_ToScreenF#[] ( x#, y# )

Convert from floating point world coordinates to screen.

Parameter
Mode
Description
x
In
X world coordinate.
y
In
Y world coordinate.

Return value
Screen coordinates accessed as [TM_X] and [TM_Y].

[ Back ]


function TM_ToScreenX ( x )

Convert x world coordinate to screen.

Parameter
Mode
Description
x
In
X world coordinate.

Return value
X screen coordinate.

[ Back ]


function TM_ToScreenXF# ( x# )

Convert floating point x world coordinate to screen.

Parameter
Mode
Description
x
In
X world coordinate.

Return value
X screen coordinate.

[ Back ]


function TM_ToScreenY ( y )

Convert y world coordinate to screen.

Parameter
Mode
Description
y
In
Y world coordinate.

Return value
Y screen coordinate.

[ Back ]


function TM_ToScreenYF# ( y# )

Convert floating point y world coordinate to screen.

Parameter
Mode
Description
y
In
Y world coordinate.

Return value
Y screen coordinate.

[ Back ]


function TM_Visible ( img, x, y )

Return true if image at world coordinates is visible.

Parameter
Mode
Description
img
In
Image identifier.
x
In
X coordinate of image.
y
In
Y coordinate of image.

Return value
True if image is visible.

[ Back ]


function TM_Move[] ( img, imgx, imgy, dx, dy )

This function returns an array containing the new coordinates of an image at specified position after trying to move it. The new coordinates are accessed with [TM_X] and [TM_Y]. If no collision occured, the coordinates are '(imgx + dx, imgy + dy)'.

Parameter
Mode
Description
img
In
Image identifier.
imgx
In
Current x world coordinate.
imgy
In
Current y world coordinate.

Return value
New world coordinates accessed with [TM_X] and [TM_Y].

[ Back ]


function TM_MoveRect[] ( rectx, recty, w, h, dx, dy )

This function returns an array containing the new coordinates of a rectangle at specified position after trying to move it. The new coordinates are accessed with [TM_X] and [TM_Y]. If no collision occured, the coordinates are '(rectx + dx, recty + dy)'.

Parameter
Mode
Description
rectx
In
Current x world coordinate.
recty
In
Current y world coordinate.
w
In
Rectangle width.
h
In
Rectangle height.

Return value
New world coordinates accessed with [TM_X] and [TM_Y].

[ Back ]


function TM_MoveF#[] ( img, imgx#, imgy#, dx#, dy# )

This function returns an array containing the new coordinates of an image at specified floating point position after trying to move it. The new coordinates are accessed with [TM_X] and [TM_Y]. If no collision occured, the coordinates are '(imgx + dx, imgy + dy)'.

Parameter
Mode
Description
img
In
Image identifier.
imgx
In
Current x world coordinate.
imgy
In
Current y world coordinate.

Return value
New world coordinates accessed with [TM_X] and [TM_Y].

[ Back ]


function TM_GetCollisionInfo[] ( )

Get information about any collision that occured during the last call to any of the collision functions. You can check the values at the indexes TM_LEFT, TM_RIGHT, TM_UP and TM_DOWN to see in what direction the collision happened. If, for example, [TM_RIGHT] is true, the object that was moved collided with something on its right.

Return value
Collision information.

[ Back ]


function TM_CollisionLeft ( )

Returns true if last collision occured leftwards.

Return value
True if last collision occured leftwards.

[ Back ]


function TM_CollisionRight ( )

Returns true if last collision occuered rightwards.

Return value
True if last collision occured rightwards.

[ Back ]


function TM_CollisionDown ( )

Returns true if last collision occured downwards.

Return value
True if last collision occured downwards.

[ Back ]


function TM_CollisionUp ( )

Returns true if last collision occured upwards.

Return value
True if last collision occured upwards.

[ Back ]


function TM_ImageCol ( img1, x1, y1, img2, x2, y2 )

Return true if two images are overlapping.

Parameter
Mode
Description
img1
In
First image's identifier.
x1
In
X coordinate of first image.
y1
In
Y coordinate of first image.
img2
In
Second image's identifier.
x2
In
X coordinate of second image.
y2
In
Y coordinate of second image.

Return value
True if images are overlapping.

[ Back ]


Generated with NLDoc 20140627.