I didn't want to hijack Clonkex's thread, and my
previous thread is too old to post anymore.
I've added a list of new commands, mostly just basic access commands that Clonkex had. If you need Tiled's property object, you'll need to keep using his version, for now. The major changes in mine are support for all encoding types; xml, csv, base64(uncompressed). Compression is still a little ways off, and even I did manage to write it, I'm not sure how fast it would be in Tier 1.
Object layers and objects are now supported.
Features:
Supports encoding: xml, csv, base64(uncompressed)
Supported map types: orthogonal, isometric, staggered isometric
Supports tileset images which use margin/spacing
Supports image layers
Layer alpha and visibility
Set the map viewport area (Define area of screen where map can only be visible)
Supports tile flipping
Support for TSX tilesets (external tileset xml)
Compression (gzip, zlib)
Objects
Properties
Terrain
Updated March 14, 2014
//////////////////////////////////////////////////////////////////////
// Title: TMX Loader
// Author: Phaelax
// Date: May 6, 2013
//
// Commands:
// void = tmx_Init()
// void = tmx_LoadMap(string filename)
// void = tmx_DeleteMap()
// void = tmx_DrawGridTile(int tileIndex, int red, int green, int blue)
// void = tmx_SetMapPosition(float x, float y)
// void = tmx_SetMapVisible(boolean)
// void = tmx_SetLayerVisible(bool)
// void = tmx_SetLayerAlpha(int layer, int value)
// int = tmx_GetLayerAlpha(int layer)
// int = tmx_GetLayerVisible(int layer)
// void = tmx_SetViewport(int x1, int y1, int x2, int y2)
// int = tmx_GetTileIndex(int layer, int worldX, int worldY)
// int = tmx_GetTileSpriteId(int layer, x, y)
// int = tmx_PickScreenTileImageFrame(int layer, x, y)
// int = tmx_GetMapWidth()
// int = tmx_GetMapHeight()
// int = tmx_GetMapTileWidth()
// int = tmx_GetMapTileHeight()
// float = tmx_GetMapPositionX()
// float = tmx_GetMapPositionY()
// int = tmx_GetLayerTotal()
// String = tmx_GetTilesetName(int tilesetId)
// String = tmx_GetTilesetImageSource(id tilesetId)
// int = tmx_GetTileWorldX(int tileIndex)
// int = tmx_GetTileWorldY(int tileIndex)
// int = tmx_GetTileX(int tileIndex)
// int = tmx_GetTileY(int tileIndex)
// int = tmx_PickScreenTileSpriteId(int x, int y)
// void = tmx_SetObjectVisible(int objectId, bool)
// int = tmx_GetObjectSprite(int objectId)
// void = tmx_SetObjectPosition(int objectId, float x, float y)
// float = tmx_GetObjectPositionX(int objectId)
// float = tmx_GetObjectPositionY(int objectId)
// int = tmx_PickScreenTileAbsoluteId(int layer, int x, int y)
// int = tmx_PickScreenTileLocalId(int layer, int x, int y)
// int = tmx_GetTileLocalId(tileIndex)
// int = tmx_GetTileLayerId(tileIndex)
//
//
//
//
// void = tmx_UpdateMap()
//
//
// Update: Feb 27, 2014
//
// - Added base64 encoding support
// - Added support for layer opacity and visible attributes
// - Added support for tileset attributes spacing and margin
// - Added support for horizontal/vertical tile flipping
// - Changed tmx_SetMapPosition() to use floats instead of integers
// - Added commands: tmx_SetViewport(), tmx_SetLayerVisible(), tmx_SetLayerAlpha()
// tmx_GetTileSpriteId(), tmx_GetTileImageFrame(), tmx_GetMapWidth(),
// tmx_GetMapHeight(), tmx_GetMapTileWidth(), tmx_GetMapTileHeight(),
// tmx_GetMapPositionX(), tmx_GetMapPositionY(), tmx_GetTilesetName(),
// tmx_GetTilesetImageSource(), tmx_GetLayerTotal(), tmx_GetTileWorldX(),
// tmx_GetTileWorldY(), tmx_GetTileX(), tmx_GetTileY()
//
// - TMX_Tilesets[] array is no longer a fixed size and will increase as needed.
//
//
// Update: Feb 28, 2014
//
// - Added CSV encoding support
//
//
// Update: March 2, 2014
//
// - Added support for image layers.
// - Added tmx_GetAbsoluteTileSpriteId()
// - Fixed bug with layer depth
// - Tileset images and image layers now loaded outside of tmx_LoadMap() function
// - Added commands: tmx_GetLayerVisible(), tmx_GetLayerAlpha()
//
//
// Update: March 2, 2014
//
// - Added support for object layers and objects
// - Added tmx_SetObjectVisible(), tmx_GetObjectSprite(), tmx_SetObjectPosition(),
// tmx_GetObjectPositionX(), tmx_GetObjectPositionY(), tmx_UpdateMap()
// - Not all objects have sprites associated with them. To see the physical boundaries of these
// objects you must call tmx_UpdateMap() in your game loop.
// - Map position now resets back to [0,0] after new map is loaded
//
//
// Update: March 12, 2014
//
// - Fixed a bug in tmx_GetTileSpriteId()
// - Sprites are no longer created for tiles that have a GID value of 0
//
//
// Update: March 13, 2014
//
// - Fixed bug in layer visiblility functions
//
//
// Update: March 14, 2014
//
// - Bug fixes
// - Renamed tmx_GetAbsoluteTileSpriteId() to tmx_PickScreenTileSpriteId()
// - Renamed tmx_GetTileImageFrame() to tmx_PickScreenTileImageFrame()
// - Added tmx_PickScreenTileAbsoluteId(), tmx_PickScreenTileLocalId(), tmx_GetTileLocalId()
// tmx_GetTileLayerId()
//
//////////////////////////////////////////////////////////////////////
#include "base64.agc"
#CONSTANT TMX_ORIENTATION_ORTHO = 1
#CONSTANT TMX_ORIENTATION_ISO = 2
#CONSTANT TMX_ORIENTATION_STAG = 3
#CONSTANT TMX_FLIPPED_HORIZONTALLY = 0x80000000
#CONSTANT TMX_FLIPPED_VERTICALLY = 0x40000000
#CONSTANT TMX_FLIPPED_DIAGONALLY = 0x20000000
#CONSTANT TMX_CLEAR_FLAGS = 0x1FFFFFFF
#CONSTANT TMX_LAYER_TILE = 1
#CONSTANT TMX_LAYER_IMAGE = 2
#CONSTANT TMX_LAYER_OBJECT = 3
Type TMX_Map_Data_Properties
mapWidth as integer
mapHeight as integer
pixelMapWidth as integer
pixelMapHeight as integer
tileWidth as integer
tileHeight as integer
orientation as integer
tileWidth2 as integer
tileHeight2 as integer
offsetX as float
offsetY as float
bgColor as integer
EndType
Type TMX_Tile
tilesetIndex as integer
sprite as integer
gid as integer
flipHorizontal as integer
flipVertical as integer
flipDiagonal as integer
EndType
Type TMX_Tileset
firstGid as integer
lastGid as integer
tileWidth as integer
tileHeight as integer
name as string
imgWidth as integer
imgHeight as integer
source as string
sprite as integer
offsetX as integer
offsetY as integer
spacing as integer
margin as integer
EndType
Type TMX_ImageLayer
source as string
sprite as integer
EndType
Type TMX_Layer
name as string
x as integer ` Deprecated
y as integer ` Deprecated
width as integer ` Deprecated
height as integer ` Deprecated
alpha as integer
visible as integer
imageLayer as TMX_ImageLayer
layerType as integer
color as integer
EndType
Type TMX_Object
name as string
oType as string
x as float
y as float
width as integer
height as integer
rotation as float
gid as integer
visible as integer
tilesetIndex as integer
sprite as integer
layerId as integer
EndType
//////////////////////////////////////////////////
// Initializes the TMX engine variables
//////////////////////////////////////////////////
function tmx_Init()
Global TMX_Map as TMX_Map_Data_Properties
Global _TMX_TilesetCount = 0
Global _TMX_LayerCount = 0
Global _TMX_ObjectCount = 0
global fileCount = 0
dim TMX_Tilesets[_TMX_TilesetCount] as TMX_Tileset
dim TMX_Map_Layers[_TMX_LayerCount] as TMX_Layer
endfunction
//////////////////////////////////////////////////
// Draws an outline around tile 't'
//////////////////////////////////////////////////
function tmx_DrawGridTile(t, r, g, b)
if TMX_Map.orientation = TMX_ORIENTATION_ORTHO then t = tmx_DrawOrthoGridTile(t, r, g, b)
if TMX_Map.orientation = TMX_ORIENTATION_ISO then t = tmx_DrawIsoGridTile(t, r, g, b)
if TMX_Map.orientation = TMX_ORIENTATION_STAG then t = tmx_DrawStagGridTile(t, r, g, b)
endfunction t
//////////////////////////////////////////////////
//
//////////////////////////////////////////////////
function tmx_DrawOrthoGridTile(t, r, g, b)
t = t - 1
ty = floor(t / TMX_Map.mapWidth)
tx = t - ty*TMX_Map.mapWidth
x = tx*TMX_Map.tileWidth + TMX_Map.offsetX
y = ty*TMX_Map.tileHeight + TMX_Map.offsetY
drawLine(x, y, x+TMX_Map.tileWidth, y, r,g,b)
drawLine(x, y+TMX_Map.tileHeight, x+TMX_Map.tileWidth, y+TMX_Map.tileHeight, r,g,b)
drawLine(x, y, x, y+TMX_Map.tileHeight, r,g,b)
drawLine(x+TMX_Map.tileWidth, y, x+TMX_Map.tileWidth, y+TMX_Map.tileHeight, r,g,b)
endfunction t
//////////////////////////////////////////////////
//
//////////////////////////////////////////////////
function tmx_DrawIsoGridTile(t, r, g, b)
t = t - 1
ty = floor(t / TMX_Map.mapWidth)
tx = t - ty*TMX_Map.mapWidth
tw = TMX_Map.tileWidth / 2
th = TMX_Map.tileHeight / 2
x = (tx-ty-1)*tw + TMX_Map.offsetX
y = (tx+ty-1)*th + TMX_Map.offsetY
drawLine(x, y+TMX_Map.tileHeight, x+tw, y+th, r,g,b)
drawLine(x+TMX_Map.tileWidth, y+TMX_Map.tileHeight, x+tw, y+TMX_Map.tileHeight+th, r,g,b)
drawLine(x+tw, y+th, x+TMX_Map.tileWidth, y+TMX_Map.tileHeight, r,g,b)
drawLine(x, y+TMX_Map.tileHeight, x+tw, y+TMX_Map.tileHeight+th, r,g,b)
endfunction t
//////////////////////////////////////////////////
//
//////////////////////////////////////////////////
function tmx_DrawStagGridTile(t, r, g, b)
t = t - 1
ty = floor(t / TMX_Map.mapWidth)
tx = t - ty*TMX_Map.mapWidth
tw = TMX_Map.tileWidth / 2
th = TMX_Map.tileHeight / 2
if ty mod 2 = 1 then shiftX = tw
x = tx*TMX_Map.tileWidth + shiftX + TMX_Map.offsetX
y = ty*th + TMX_Map.offsetY
drawLine(x, y+TMX_Map.tileHeight, x+tw, y+th, r,g,b)
drawLine(x+TMX_Map.tileWidth, y+TMX_Map.tileHeight, x+tw, y+TMX_Map.tileHeight+th, r,g,b)
drawLine(x+tw, y+th, x+TMX_Map.tileWidth, y+TMX_Map.tileHeight, r,g,b)
drawLine(x, y+TMX_Map.tileHeight, x+tw, y+TMX_Map.tileHeight+th, r,g,b)
endfunction t
//////////////////////////////////////////////////
// Returns the tile number under screen coordinates
// [x,y] in relation to the specified layer number
// This is an absolute tile ID, meaning it is unique
// to that tile on that layer. A tile at [12,4]
// will have the same "local" index on all layers,
// but have a different id globally for each layer.
//
// Optionally, you can specify 0 for the layer and
// it will return the tile ID for the top most
// visible layer, regardless of layer type or if a
// sprite is even present at that layer.
//////////////////////////////////////////////////
function tmx_PickScreenTileAbsoluteId(layer, x, y)
if x >= TMX_Map.offsetX and x <= TMX_Map.offsetX + TMX_Map.pixelMapWidth
if y >= TMX_Map.offsetY and y <= TMX_Map.offsetY + TMX_Map.pixelMapHeight
x = x - TMX_Map.offsetX
y = y - TMX_Map.offsetY
if TMX_Map.orientation = TMX_ORIENTATION_ORTHO then t = tmx_GetOrthoTile(x, y)
if TMX_Map.orientation = TMX_ORIENTATION_ISO then t = tmx_GetIsoTile(x, y)
if TMX_Map.orientation = TMX_ORIENTATION_STAG then t = tmx_GetStagTile(x, y)
if layer = 0
for layer = tmx_GetLayerTotal() to 1 step -1
if TMX_Map_Layers[layer].visible = 1 then exit
next layer
endif
t = t + (layer-1) * TMX_Map.mapWidth*TMX_Map.mapHeight
exitfunction t
endif
endif
endfunction 0
//////////////////////////////////////////////////
// Returns the tile number under screen coordinates
// [x,y] in relation to the specified layer number.
// Returns a local tile ID, meaning the tile ID
// returned at [x,y] is the same for any layer.
//////////////////////////////////////////////////
function tmx_PickScreenTileLocalId(layer, x, y)
t = tmx_PickScreenTileAbsoluteId(layer, x, y)
t = tmx_GetTileLocalId(t)
endfunction t
//////////////////////////////////////////////////
// Returns the sprite ID at screen coordinates [x,y]
// of the specified layer id. If layer ID is
// specified, it disregards the layer's current
// visibility state. If layer is 0, it will
// return the top most visible sprite, ignoring
// layers which are not currently visible.
// If the layer is an image layer, it will only
// return the sprite ID if [x,y] falls on top of it.
//////////////////////////////////////////////////
function tmx_PickScreenTileSpriteId(layer, x, y)
if layer > 0
s = tmx_GetTileSpriteIdFromPoint(layer, x, y)
else
for layer = tmx_GetLayerTotal() to 1 step -1
if TMX_Map_Layers[layer].visible = 1
s = tmx_GetTileSpriteIdFromPoint(layer, x, y)
if s > 0 then exitfunction s
endif
next layer
endif
endfunction s
//////////////////////////////////////////////////
// Returns the sprite associated with this absolute
// tile ID. If tileID belongs to a layer that is an
// Image Layer, then that sprite will be returned
// despite what the local tile id coordinates are.
//////////////////////////////////////////////////
function tmx_GetTileSpriteId(tileId)
layer = tmx_GetTileLayerId(tileId)
tile = tmx_GetTileLocalId(tileId)
s = 0
if TMX_Map_Layers[layer].layerType = TMX_LAYER_TILE then s = TMX_Map[layer, tile].sprite
if TMX_Map_Layers[layer].layerType = TMX_LAYER_IMAGE then s = TMX_Map_Layers[layer].imageLayer.sprite
endfunction s
//////////////////////////////////////////////////
// Returns the sprite ID associated with the tile
// Requires a valid layer ID. This function is
// designed to be used in this library internally
// by tmx_PickScreenTileSpriteId()
//////////////////////////////////////////////////
function tmx_GetTileSpriteIdFromPoint(layer, x, y)
if TMX_Map_Layers[layer].layerType = TMX_LAYER_TILE
t = tmx_PickScreenTileLocalId(layer, x, y)
s = TMX_Map[layer, t].sprite
exitfunction s
endif
if TMX_Map_Layers[layer].layerType = TMX_LAYER_IMAGE
s = TMX_Map_Layers[layer].imageLayer.sprite
sx = getSpriteX(s)
sy = getSpriteY(s)
w = getSpriteWidth(s)
h = getSpriteHeight(s)
if x >= sx and x <= sx+w and y >= sy and y <= sy+h then exitfunction s
endif
endfunction 0
//////////////////////////////////////////////////
// Returns the sprite's animation frame
//////////////////////////////////////////////////
function tmx_PickScreenTileImageFrame(layer, x, y)
s = tmx_PickScreenTileSpriteId(layer, x, y)
f = getSpriteCurrentFrame(s)
endfunction f
//////////////////////////////////////////////////
//
//////////////////////////////////////////////////
function tmx_GetTileLayerId(tileIndex)
L = (tileIndex-1) / (TMX_Map.mapWidth * TMX_Map.mapHeight) + 1
endfunction L
//////////////////////////////////////////////////
//
//////////////////////////////////////////////////
function tmx_GetTileLocalId(tileIndex)
L = (tileIndex-1) %% (TMX_Map.mapWidth * TMX_Map.mapHeight) + 1
endfunction L
//////////////////////////////////////////////////
//
//////////////////////////////////////////////////
function tmx_GetOrthoTile(x, y)
tx = x / TMX_Map.tileWidth
ty = y / TMX_Map.tileHeight
t = tx + ty*TMX_Map.mapWidth + 1
endfunction t
//////////////////////////////////////////////////
//
//////////////////////////////////////////////////
function tmx_GetIsoTile(x, y)
a = TMX_Map.tileWidth*y
b = TMX_Map.tileHeight*x
c = TMX_Map.tileWidth*TMX_Map.tileHeight
tx = (a + b) / c
ty = (a - b) / c
t = tx + ty*TMX_Map.mapWidth + 1
endfunction t
//////////////////////////////////////////////////
//
//////////////////////////////////////////////////
function tmx_GetStagTile(x, y)
th = TMX_Map.tileHeight/2
dec y, th
ratio# = TMX_Map.tileHeight / (tmx_Map.tileWidth+0.0)
// Coordinates of grid-aligned tile
tx = floor(x / TMX_Map.tileWidth)
ty = floor(y / TMX_Map.tileHeight) * 2
// relative position of the base square of the grid-aligned tile
rx# = x - tx * TMX_Map.tileWidth
ry# = y - (ty / 2) * TMX_Map.tileHeight
if th - rx#*ratio# > ry#
if ty mod 2
dec ty
else
dec tx : dec ty
endif
endif
if -th + rx#*ratio# > ry#
if ty mod 2
inc tx : dec ty
else
dec ty
endif
endif
if th + rx#*ratio# < ry#
if ty mod 2
inc ty
else
dec tx : inc ty
endif
endif
if th*3 - rx#*ratio# < ry#
if ty mod 2
inc tx : inc ty
else
inc ty
endif
endif
t = tx + ty*TMX_Map.mapWidth + 1
endfunction t
//////////////////////////////////////////////////
// Position the map in world space
//////////////////////////////////////////////////
function tmx_SetMapPosition(ox, oy)
if TMX_Map.orientation = TMX_ORIENTATION_ORTHO then tmx_PositionOrthoMap(ox, oy)
if TMX_Map.orientation = TMX_ORIENTATION_ISO then tmx_PositionIsoMap(ox, oy)
if TMX_Map.orientation = TMX_ORIENTATION_STAG then tmx_PositionStagMap(ox, oy)
TMX_Map.offsetX = ox
TMX_Map.offsetY = oy
for i = 1 to _TMX_ObjectCount
if TMX_Objects[i].gid > 0
setSpritePosition(TMX_Objects[i].sprite, TMX_Objects[i].x+ox, TMX_Objects[i].y+oy)
endif
next i
endfunction
//////////////////////////////////////////////////
// Positions an object relative to the map
//////////////////////////////////////////////////
function tmx_SetObjectPosition(objectId, x, y)
TMX_Objects[objectId].x = x
TMX_Objects[objectId].y = y
if TMX_Objects[objectId].gid > 0 then setSpritePosition(TMX_Objects[objectId].sprite, x+TMX_Map.offsetX, y+TMX_Map.offsetY)
endfunction
//////////////////////////////////////////////////
//
//////////////////////////////////////////////////
function tmx_GetObjectPositionX(objectId)
x = TMX_Objects[objectId].x
endfunction x
//////////////////////////////////////////////////
//
//////////////////////////////////////////////////
function tmx_GetObjectPositionY(objectId)
y = TMX_Objects[objectId].y
endfunction y
//////////////////////////////////////////////////
// Position the map in world space using an
// orthogonal layout
//////////////////////////////////////////////////
function tmx_PositionOrthoMap(ox, oy)
for layer = 1 to tmx_GetLayerTotal()
if TMX_Map_Layers[layer].layerType = TMX_LAYER_TILE
for y = 0 to TMX_Map.mapHeight-1
for x = 0 to TMX_Map.mapWidth-1
t = x + y*TMX_Map.mapWidth + 1
if TMX_Map[layer,t].tilesetIndex > 0
tx = x*TMX_Map.tileWidth
ty = y*TMX_Map.tileHeight
sx = tx + ox + TMX_Tilesets[TMX_Map[layer, t].tilesetIndex].offsetX
sy = ty + oy + TMX_Tilesets[TMX_Map[layer, t].tilesetIndex].offsetY
setSpritePosition(TMX_Map[layer, t].sprite, sx, sy)
endif
next x
next y
else
if TMX_Map_Layers[layer].layerType = TMX_LAYER_IMAGE then setSpritePosition(TMX_Map_Layers[layer].imageLayer.sprite, ox, oy)
endif
next layer
endfunction
//////////////////////////////////////////////////
// Position the map in world space using an
// isometric layout
//////////////////////////////////////////////////
function tmx_PositionIsoMap(ox, oy)
tw = TMX_Map.tileWidth / 2
th = TMX_Map.tileHeight / 2
for layer = 1 to tmx_GetLayerTotal()
if TMX_Map_Layers[layer].layerType = TMX_LAYER_TILE
for y = 0 to TMX_Map.mapHeight-1
for x = 0 to TMX_Map.mapWidth-1
t = x + y*TMX_Map.mapWidth + 1
if TMX_Map[layer,t].tilesetIndex > 0
tx = (x-y-1)*tw
ty = (x+y-1)*th
setSpritePosition(TMX_Map[layer, t].sprite, tx+ox, ty+oy)
endif
next x
next y
else
if TMX_Map_Layers[layer].layerType = TMX_LAYER_IMAGE then setSpritePosition(TMX_Map_Layers[layer].imageLayer.sprite, ox, oy)
endif
next layer
endfunction
//////////////////////////////////////////////////
// Position the map in world space using an
// isometric staggered layout
//////////////////////////////////////////////////
function tmx_PositionStagMap(ox, oy)
tw = TMX_Map.tileWidth / 2
th = TMX_Map.tileHeight / 2
for layer = 1 to tmx_GetLayerTotal()
if TMX_Map_Layers[layer].layerType = TMX_LAYER_TILE
for y = 0 to TMX_Map.mapHeight-1
shiftX = 0
if y mod 2 = 1 then shiftX = tw
for x = 0 to TMX_Map.mapWidth-1
t = x + y*TMX_Map.mapWidth + 1
if TMX_Map[layer,t].tilesetIndex > 0
tx = x*TMX_Map.tileWidth + shiftX
ty = y*th
setSpritePosition(TMX_Map[layer, t].sprite, tx+ox, ty+oy)
endif
next x
next y
else
if TMX_Map_Layers[layer].layerType = TMX_LAYER_IMAGE then setSpritePosition(TMX_Map_Layers[layer].imageLayer.sprite, ox, oy)
endif
next layer
endfunction
//////////////////////////////////////////////////
// Sets the map's visibility ON or OFF
//////////////////////////////////////////////////
function tmx_SetMapVisible(bool)
for layer = 1 to tmx_GetLayerTotal()
tmx_SetLayerVisible(layer, bool)
next layer
endfunction
//////////////////////////////////////////////////
// Sets a map layer's visibility ON or OFF
//////////////////////////////////////////////////
function tmx_SetLayerVisible(layer, bool)
if TMX_Map_Layers[layer].layerType = TMX_LAYER_IMAGE
if TMX_Map_Layers[layer].imageLayer.sprite > 0 then setSpriteVisible(TMX_Map_Layers[layer].imageLayer.sprite, bool)
exitfunction
endif
if TMX_Map_Layers[layer].layerType = TMX_LAYER_OBJECT
TMX_Map_Layers[layer].visible = bool
endif
if TMX_Map_Layers[layer].layerType = TMX_LAYER_TILE
TMX_Map_Layers[layer].visible = bool
for y = 0 to TMX_Map.mapHeight-1
for x = 0 to TMX_Map.mapWidth-1
t = x + y*TMX_Map.mapWidth + 1
if TMX_Map[layer,t].tilesetIndex > 0 then setSpriteVisible(TMX_Map[layer, t].sprite, bool)
next x
next y
endif
for i = 1 to _TMX_ObjectCount
if TMX_Objects[i].layerId = layer
if TMX_Objects[i].gid > 0
setSpriteVisible(TMX_Objects[i].sprite, bool)
endif
TMX_Objects[i].visible = bool
endif
next i
endfunction
//////////////////////////////////////////////////
// Creates sprites and lays out the map layers
// for an orthogonal map
//////////////////////////////////////////////////
function tmx_SetObjectVisible(objectId, bool)
TMX_Objects[objectId].visible = bool
if TMX_Objects[objectId].gid > 0 then setSpriteVisible(TMX_Objects[objectId].sprite, bool)
endfunction
//////////////////////////////////////////////////
// Creates sprites and lays out the map layers
// for an orthogonal map
//////////////////////////////////////////////////
function tmx_SetLayerAlpha(layer, value)
if TMX_Map_Layers[layer].layerType = TMX_LAYER_IMAGE
if TMX_Map_Layers[layer].imageLayer.sprite > 0 then setSpriteColorAlpha(TMX_Map_Layers[layer].imageLayer.sprite, value)
exitfunction
endif
if TMX_Map_Layers[layer].layerType = TMX_LAYER_OBJECT
TMX_Map_Layers[layer].alpha = value
endif
for y = 0 to TMX_Map.mapHeight-1
for x = 0 to TMX_Map.mapWidth-1
t = x + y*TMX_Map.mapWidth + 1
if TMX_Map[layer,t].tilesetIndex > 0 then setSpriteColorAlpha(TMX_Map[layer, t].sprite, value)
next x
next y
for i = 1 to _TMX_ObjectCount
if TMX_Objects[i].gid > 0 and TMX_Objects[i].layerId = layer
setSpriteColorAlpha(TMX_Objects[i].sprite, value)
endif
next i
endfunction
//////////////////////////////////////////////////
// Loades tileset images
//////////////////////////////////////////////////
function tmx_LoadTilesets()
for i = 1 to _TMX_TilesetCount
if TMX_Tilesets[i].spacing > 0 or TMX_Tilesets[i].margin > 0
img = getTrimmedImage(TMX_Tilesets[i].source, TMX_Tilesets[i].tileWidth, TMX_Tilesets[i].tileHeight, TMX_Tilesets[i].margin, TMX_Tilesets[i].spacing)
imgWidth = TMX_Tilesets[i].imgWidth
imgHeight = TMX_Tilesets[i].imgHeight
else
img = loadImage(TMX_Tilesets[i].source)
endif
TMX_Tilesets[i].lastGid = floor(TMX_Tilesets[i].imgWidth / (TMX_Tilesets[i].tileWidth+0.0)) * floor(TMX_Tilesets[i].imgHeight / (TMX_Tilesets[i].tileHeight+0.0)) + TMX_Tilesets[i].firstGid - 1
TMX_Tilesets[i].sprite = createSprite(img)
tileCount = (TMX_Tilesets[i].imgWidth / TMX_Tilesets[i].tileWidth) * (TMX_Tilesets[i].imgHeight / TMX_Tilesets[i].tileHeight)
setSpriteAnimation(TMX_Tilesets[i].sprite, TMX_Tilesets[i].tileWidth, TMX_Tilesets[i].tileHeight, tileCount)
setSpriteVisible(TMX_Tilesets[i].sprite, 0)
next i
endfunction
//////////////////////////////////////////////////
// Creates sprites and lays out the map layers
// for an orthogonal map
//////////////////////////////////////////////////
function tmx_BuildOrthogonalMap()
for layer = 1 to tmx_GetLayerTotal()
if TMX_Map_Layers[layer].layerType = TMX_LAYER_TILE
for y = 0 to TMX_Map.mapHeight-1
for x = 0 to TMX_Map.mapWidth-1
t = x + y*TMX_Map.mapWidth + 1
TMX_Map[layer, t].sprite = 0
if TMX_Map[layer,t].tilesetIndex > 0 and TMX_Map[layer,t].gid > 0
TMX_Map[layer, t].sprite = cloneSprite(TMX_Tilesets[TMX_Map[layer,t].tilesetIndex].sprite)
tx = x*TMX_Map.tileWidth
ty = y*TMX_Map.tileHeight
setSpritePosition(TMX_Map[layer, t].sprite, tx, ty)
setSpriteColorAlpha(TMX_Map[layer, t].sprite, TMX_Map_Layers[layer].alpha)
setSpriteDepth(TMX_Map[layer, t].sprite, 10-layer)
setSpriteFrame(TMX_Map[layer, t].sprite, TMX_Map[layer, t].gid)
setSpriteVisible(TMX_Map[layer, t].sprite, TMX_Map_Layers[layer].visible)
setSpriteFlip(TMX_Map[layer, t].sprite, TMX_Map[layer, t].flipHorizontal, TMX_Map[layer, t].FlipVertical)
endif
next x
next y
elseif TMX_Map_Layers[layer].layerType = TMX_LAYER_IMAGE
if TMX_Map_Layers[layer].imageLayer.source <> ""
TMX_Map_Layers[_TMX_LayerCount].imageLayer.sprite = createSprite(loadImage(TMX_Map_Layers[layer].imageLayer.source))
if TMX_Map_Layers[layer].imageLayer.sprite > 0
setSpriteColorAlpha(TMX_Map_Layers[layer].imageLayer.sprite, TMX_Map_Layers[layer].alpha)
setSpriteDepth(TMX_Map_Layers[layer].imageLayer.sprite, 10-layer)
endif
endif
elseif TMX_Map_Layers[layer].layerType = TMX_LAYER_OBJECT
// do nothing
endif
next layer
endfunction
//////////////////////////////////////////////////
// Creates sprites and lays out the map layers
// for an orthogonal map
//////////////////////////////////////////////////
function tmx_BuildIsometricMap()
depth = (TMX_Map.mapWidth * TMX_Map.mapHeight) * tmx_GetLayerTotal() + 100
tw = TMX_Map.tileWidth / 2
th = TMX_Map.tileHeight / 2
for layer = 1 to tmx_GetLayerTotal()
if TMX_Map_Layers[layer].layerType = TMX_LAYER_TILE
depth = (TMX_Map.mapWidth * TMX_Map.mapHeight) + 100
for y = 0 to TMX_Map.mapHeight-1
for x = 0 to TMX_Map.mapWidth-1
t = x + y*TMX_Map.mapWidth + 1
if TMX_Map[layer,t].tilesetIndex > 0 and TMX_Map[layer,t].gid > 0
TMX_Map[layer, t].sprite = cloneSprite(TMX_Tilesets[TMX_Map[layer,t].tilesetIndex].sprite)
tx = (x-y-1)*tw
ty = (x+y-1)*th
setSpritePosition(TMX_Map[layer, t].sprite, tx, ty)
setSpriteColorAlpha(TMX_Map[layer, t].sprite, TMX_Map_Layers[layer].alpha)
setSpriteDepth(TMX_Map[layer, t].sprite, depth)
setSpriteFrame(TMX_Map[layer, t].sprite, TMX_Map[layer, t].gid)
setSpriteVisible(TMX_Map[layer, t].sprite, TMX_Map_Layers[layer].visible)
setSpriteFlip(TMX_Map[layer, t].sprite, TMX_Map[layer, t].flipHorizontal, TMX_Map[layer, t].FlipVertical)
else
TMX_Map[layer, t].sprite = 0
endif
dec depth
next x
next y
else
// Image layers are not tiled and simply display a single image
if TMX_Map_Layers[layer].layerType = TMX_LAYER_IMAGE
if TMX_Map_Layers[layer].imageLayer.source <> ""
TMX_Map_Layers[_TMX_LayerCount].imageLayer.sprite = createSprite(loadImage(TMX_Map_Layers[layer].imageLayer.source))
if TMX_Map_Layers[layer].imageLayer.sprite > 0
setSpriteColorAlpha(TMX_Map_Layers[layer].imageLayer.sprite, TMX_Map_Layers[layer].alpha)
setSpriteDepth(TMX_Map_Layers[layer].imageLayer.sprite, depth)
endif
endif
endif
endif
next layer
endfunction
//////////////////////////////////////////////////
// Creates sprites and lays out the map layers
// for an orthogonal map
//////////////////////////////////////////////////
function tmx_BuildStaggeredMap()
tw = TMX_Map.tileWidth / 2
th = TMX_Map.tileHeight / 2
for layer = 1 to tmx_GetLayerTotal()
if TMX_Map_Layers[layer].layerType = TMX_LAYER_TILE
for y = 0 to TMX_Map.mapHeight-1
shiftX = 0
if y mod 2 = 1 then shiftX = tw
for x = 0 to TMX_Map.mapWidth-1
t = x + y*TMX_Map.mapWidth + 1
if TMX_Map[layer,t].tilesetIndex > 0 and TMX_Map[layer,t].gid > 0
TMX_Map[layer, t].sprite = cloneSprite(TMX_Tilesets[TMX_Map[layer,t].tilesetIndex].sprite)
tx = x*TMX_Map.tileWidth + shiftX
ty = y*th
setSpritePosition(TMX_Map[layer, t].sprite, tx, ty)
setSpriteColorAlpha(TMX_Map[layer, t].sprite, TMX_Map_Layers[layer].alpha)
setSpriteDepth(TMX_Map[layer, t].sprite, 10+j)
setSpriteFrame(TMX_Map[layer, t].sprite, TMX_Map[layer, t].gid)
setSpriteVisible(TMX_Map[layer, t].sprite, TMX_Map_Layers[layer].visible)
setSpriteFlip(TMX_Map[layer, t].sprite, TMX_Map[layer, t].flipHorizontal, TMX_Map[layer, t].FlipVertical)
else
TMX_Map[layer, t].sprite = 0
endif
next x
next y
else
// Image layers are not tiled and simply display a single image
if TMX_Map_Layers[layer].layerType = TMX_LAYER_IMAGE
if TMX_Map_Layers[layer].imageLayer.source <> ""
TMX_Map_Layers[_TMX_LayerCount].imageLayer.sprite = createSprite(loadImage(TMX_Map_Layers[layer].imageLayer.source))
if TMX_Map_Layers[layer].imageLayer.sprite > 0
setSpriteColorAlpha(TMX_Map_Layers[layer].imageLayer.sprite, TMX_Map_Layers[layer].alpha)
setSpriteDepth(TMX_Map_Layers[layer].imageLayer.sprite, depth)
endif
endif
endif
endif
next layer
endfunction
//////////////////////////////////////////////////
// Finds which tileset this gid belongs to
//////////////////////////////////////////////////
function tmx_GetTilesetIndex(gid)
for i = 1 to _TMX_TilesetCount
if gid >= TMX_Tilesets[i].firstGid and gid <= TMX_Tilesets[i].lastGid then exitfunction i
next i
endfunction 0
//////////////////////////////////////////////////
// Load an XML-based TMX map file
//////////////////////////////////////////////////
function tmx_LoadMap(file$)
`f2 = openToWrite("thing.txt")
F = openToRead(file$)
layerId = 0
repeat
s$ = xml_StripLeadingSpaces$(readline(F))
tag$ = xml_GetTagName$(s$)
if tag$ = "map"
attributes$ = xml_GetUnparsedAttributeString$(s$, tag$)
orientation$ = xml_GetAttribute$(attributes$, "orientation")
TMX_Map.mapWidth = val(xml_GetAttribute$(attributes$, "width"))
TMX_Map.mapHeight = val(xml_GetAttribute$(attributes$, "height"))
TMX_Map.tileWidth = val(xml_GetAttribute$(attributes$, "tilewidth"))
TMX_Map.tileHeight = val(xml_GetAttribute$(attributes$, "tileheight"))
TMX_Map.pixelMapWidth = TMX_Map.mapWidth * TMX_Map.tileWidth
TMX_Map.pixelMapHeight = TMX_Map.mapHeight * TMX_Map.tileHeight
TMX_Map.bgColor = val(mid(xml_GetAttribute$(attributes$, "backgroundcolor"), 2, 6), 16)
if orientation$ = "orthogonal" then TMX_Map.orientation = TMX_ORIENTATION_ORTHO
if orientation$ = "isometric" then TMX_Map.orientation = TMX_ORIENTATION_ISO
if orientation$ = "staggered" then TMX_Map.orientation = TMX_ORIENTATION_STAG
// Create a map with a max of 5 layers
dim TMX_Map[5, TMX_Map.mapWidth*TMX_Map.mapHeight] as TMX_Tile
endif
// New tileset detected
if tag$ = "tileset"
attributes$ = xml_GetUnparsedAttributeString$(s$, tag$)
inc _TMX_TilesetCount
dim TMX_Tilesets[_TMX_TilesetCount] as TMX_Tileset
TMX_Tilesets[_TMX_TilesetCount].firstGid = val(xml_GetAttribute$(attributes$, "firstgid"))
TMX_Tilesets[_TMX_TilesetCount].tileWidth = val(xml_GetAttribute$(attributes$, "tilewidth"))
TMX_Tilesets[_TMX_TilesetCount].tileHeight = val(xml_GetAttribute$(attributes$, "tileheight"))
TMX_Tilesets[_TMX_TilesetCount].name = xml_GetAttribute$(attributes$, "name")
TMX_Tilesets[_TMX_TilesetCount].spacing = val(xml_GetAttribute$(attributes$, "spacing"))
TMX_Tilesets[_TMX_TilesetCount].margin = val(xml_GetAttribute$(attributes$, "margin"))
endif
// Tileset pixel offset
if tag$ = "tileoffset"
attributes$ = xml_GetUnparsedAttributeString$(s$, tag$)
TMX_Tilesets[_TMX_TilesetCount].offsetX = val(xml_GetAttribute$(attributes$, "x"))
TMX_Tilesets[_TMX_TilesetCount].offsetY = val(xml_GetAttribute$(attributes$, "y"))
endif
// Image detected, load it and set its animation to make up the tiles
if tag$ = "image"
attributes$ = xml_GetUnparsedAttributeString$(s$, tag$)
TMX_Tilesets[_TMX_TilesetCount].source = xml_GetAttribute$(attributes$, "source")
imgWidth = val(xml_GetAttribute$(attributes$, "width"))
imgHeight = val(xml_GetAttribute$(attributes$, "height"))
if TMX_Tilesets[_TMX_TilesetCount].spacing > 0 or TMX_Tilesets[_TMX_TilesetCount].margin > 0
tileCountX = (TMX_Tilesets[_TMX_TilesetCount].imgWidth - TMX_Tilesets[_TMX_TilesetCount].margin) / (TMX_Tilesets[_TMX_TilesetCount].tileWidth + TMX_Tilesets[_TMX_TilesetCount].spacing)
tileCountY = (TMX_Tilesets[_TMX_TilesetCount].imgHeight - TMX_Tilesets[_TMX_TilesetCount].margin) / (TMX_Tilesets[_TMX_TilesetCount].tileHeight + TMX_Tilesets[_TMX_TilesetCount].spacing)
imgWidth = TMX_Tilesets[_TMX_TilesetCount].tileWidth * tileCountX
imgHeight = TMX_Tilesets[_TMX_TilesetCount].tileHeight * tileCountY
endif
TMX_Tilesets[_TMX_TilesetCount].imgWidth = imgWidth
TMX_Tilesets[_TMX_TilesetCount].imgHeight = imgHeight
TMX_Tilesets[_TMX_TilesetCount].lastGid = floor(TMX_Tilesets[_TMX_TilesetCount].imgWidth / (TMX_Tilesets[_TMX_TilesetCount].tileWidth+0.0)) * floor(TMX_Tilesets[_TMX_TilesetCount].imgHeight / (TMX_Tilesets[_TMX_TilesetCount].tileHeight+0.0)) + TMX_Tilesets[_TMX_TilesetCount].firstGid - 1
endif
// New layer detected
if tag$ = "layer"
inc _TMX_LayerCount
dim TMX_Map_Layers[_TMX_LayerCount] as TMX_LAYER
TMX_Map_Layers[_TMX_LayerCount].layerType = TMX_LAYER_TILE
tileNumber = 0
attributes$ = xml_GetUnparsedAttributeString$(s$, tag$)
opacity$ = xml_GetAttribute$(attributes$, "opacity")
isVisible$ = xml_GetAttribute$(attributes$, "visible")
width$ = xml_GetAttribute$(attributes$, "width")
height$ = xml_GetAttribute$(attributes$, "height")
// Layer's name
TMX_Map_Layers[_TMX_LayerCount].name = xml_GetAttribute$(attributes$, "name")
// Layer's offset position
TMX_Map_Layers[_TMX_LayerCount].x = val(xml_GetAttribute$(attributes$, "x"))
TMX_Map_Layers[_TMX_LayerCount].y = val(xml_GetAttribute$(attributes$, "y"))
// If opacity not set, default to 255
if opacity$ = "" then opacity$ = "1"
TMX_Map_Layers[_TMX_LayerCount].alpha = val(opacity$) * 255
// If visible not set, default to true
if isVisible$ = "" : TMX_Map_Layers[_TMX_LayerCount].visible = 1 : else : TMX_Map_Layers[_TMX_LayerCount].visible = val(isVisible$) : endif
// If width or height are not set, default to map dimensions
if width$ = "" : TMX_Map_Layers[_TMX_LayerCount].width = TMX_Map.mapWidth : else : TMX_Map_Layers[_TMX_LayerCount].width = val(width$) : endif
if height$ = "" : TMX_Map_Layers[_TMX_LayerCount].height = TMX_Map.mapHeight : else : TMX_Map_Layers[_TMX_LayerCount].height = val(height$) : endif
endif
// An object group is really just another map layer
if tag$ = "objectgroup"
inc _TMX_LayerCount
dim TMX_Map_Layers[_TMX_LayerCount] as TMX_LAYER
TMX_Map_Layers[_TMX_LayerCount].layerType = TMX_LAYER_OBJECT
TMX_Map_Layers[_TMX_LayerCount].imageLayer.source = ""
attributes$ = xml_GetUnparsedAttributeString$(s$, tag$)
opacity$ = xml_GetAttribute$(attributes$, "opacity")
isVisible$ = xml_GetAttribute$(attributes$, "visible")
// Layer's name
TMX_Map_Layers[_TMX_LayerCount].name = xml_GetAttribute$(attributes$, "name")
// Color to display objects in this group
TMX_Map_Layers[_TMX_LayerCount].color = val(mid(xml_GetAttribute$(attributes$, "color"), 2, 6), 16)
// Layer's offset position
TMX_Map_Layers[_TMX_LayerCount].x = 0
TMX_Map_Layers[_TMX_LayerCount].y = 0
// If opacity not set, default to 255
if opacity$ = "" then opacity$ = "1"
TMX_Map_Layers[_TMX_LayerCount].alpha = val(opacity$) * 255
// If visible not set, default to true
if isVisible$ = "" : TMX_Map_Layers[_TMX_LayerCount].visible = 1 : else : TMX_Map_Layers[_TMX_LayerCount].visible = val(isVisible$) : endif
while tag$ <> "/objectgroup"
s$ = xml_StripLeadingSpaces$(readline(F))
tag$ = xml_GetTagName$(s$)
if tag$ = "object"
inc _TMX_ObjectCount
dim TMX_Objects[_TMX_ObjectCount] as TMX_OBJECT
attributes$ = xml_GetUnparsedAttributeString$(s$, tag$)
TMX_Objects[_TMX_ObjectCount].name = xml_GetAttribute$(attributes$, "name")
TMX_Objects[_TMX_ObjectCount].oType = xml_GetAttribute$(attributes$, "type")
TMX_Objects[_TMX_ObjectCount].x = val(xml_GetAttribute$(attributes$, "x"))
TMX_Objects[_TMX_ObjectCount].y = val(xml_GetAttribute$(attributes$, "y"))
TMX_Objects[_TMX_ObjectCount].width = val(xml_GetAttribute$(attributes$, "width"))
TMX_Objects[_TMX_ObjectCount].height = val(xml_GetAttribute$(attributes$, "height"))
TMX_Objects[_TMX_ObjectCount].rotation = val(xml_GetAttribute$(attributes$, "rotation"))
TMX_Objects[_TMX_ObjectCount].gid = val(xml_GetAttribute$(attributes$, "gid"))
TMX_Objects[_TMX_ObjectCount].layerId = _TMX_LayerCount
// If visible not set, default to the parent value
isVisible$ = xml_GetAttribute$(attributes$, "visible")
if isVisible$ = "" : TMX_Objects[_TMX_ObjectCount].visible = TMX_Map_Layers[_TMX_LayerCount].visible : else : TMX_Objects[_TMX_ObjectCount].visible = val(isVisible$) : endif
if TMX_Objects[_TMX_ObjectCount].gid > 0
tsi = tmx_GetTilesetIndex(TMX_Objects[_TMX_ObjectCount].gid)
TMX_Objects[_TMX_ObjectCount].gid = TMX_Objects[_TMX_ObjectCount].gid - TMX_Tilesets[tsi].firstGid + 1
TMX_Objects[_TMX_ObjectCount].tilesetIndex = tsi
endif
endif
endwhile
endif
// New image layer
if tag$ = "imagelayer"
inc _TMX_LayerCount
dim TMX_Map_Layers[_TMX_LayerCount] as TMX_LAYER
TMX_Map_Layers[_TMX_LayerCount].layerType = TMX_LAYER_IMAGE
attributes$ = xml_GetUnparsedAttributeString$(s$, tag$)
opacity$ = xml_GetAttribute$(attributes$, "opacity")
isVisible$ = xml_GetAttribute$(attributes$, "visible")
// Layer's name
TMX_Map_Layers[_TMX_LayerCount].name = xml_GetAttribute$(attributes$, "name")
// Layer's offset position
TMX_Map_Layers[_TMX_LayerCount].x = 0
TMX_Map_Layers[_TMX_LayerCount].y = 0
// If opacity not set, default to 255
if opacity$ = "" then opacity$ = "1"
TMX_Map_Layers[_TMX_LayerCount].alpha = val(opacity$) * 255
// If visible not set, default to true
if isVisible$ = "" : TMX_Map_Layers[_TMX_LayerCount].visible = 1 : else : TMX_Map_Layers[_TMX_LayerCount].visible = val(isVisible$) : endif
// Meaningless in an image layer
`TMX_Map_Layers[_TMX_LayerCount].width = TMX_Map.mapWidth
`TMX_Map_Layers[_TMX_LayerCount].height = TMX_Map.mapHeight
while tag$ <> "/imagelayer"
s$ = xml_StripLeadingSpaces$(readline(F))
tag$ = xml_GetTagName$(s$)
if tag$ = "image"
attributes$ = xml_GetUnparsedAttributeString$(s$, tag$)
TMX_Map_Layers[_TMX_LayerCount].imageLayer.source = xml_GetAttribute$(attributes$, "source")
endif
endwhile
endif
// Only supports Base64 uncompressed [2-6-2014]
if tag$ = "data"
attributes$ = xml_GetUnparsedAttributeString$(s$, tag$)
encoding$ = xml_GetAttribute$(attributes$, "encoding")
// Base64 encoding
if encoding$ = "base64"
data$ = xml_GetTagInnerContent$(F, "data")
dataSize = decode64ToByteArray(data$)
for i = 1 to dataSize step 4
gid = B64[i] || B64[i+1]<<8 || B64[i+2]<<16 || B64[i+3]<<24
inc tileNumber
checkAndSetGID(_TMX_LayerCount, tileNumber, gid)
next i
// free up memory
undim B64[]
// CSV encoding
elseif encoding$ = "csv"
while tag$ <> "/data"
s$ = xml_StripLeadingSpaces$(readline(F))
tag$ = xml_GetTagName$(s$)
n = countStringTokens(s$, ",")
for i = 1 to n
inc tileNumber
gid = val(getStringToken(s$, ",", i))
checkAndSetGID(_TMX_LayerCount, tileNumber, gid)
next i
endwhile
// Default, no encoding
else
while tag$ <> "/data"
s$ = xml_StripLeadingSpaces$(readline(F))
tag$ = xml_GetTagName$(s$)
if tag$ = "tile"
inc tileNumber
attributes$ = xml_GetUnparsedAttributeString$(s$, tag$)
gid = val(xml_GetAttribute$(attributes$, "gid"))
`checkAndSetGID(_TMX_LayerCount, tileNumber, gid)
if gid = 0
TMX_Map[_TMX_LayerCount, tileNumber].gid = 0
TMX_Map[_TMX_LayerCount, tileNumber].tilesetIndex = 0
else
tsi = tmx_GetTilesetIndex(gid)
TMX_Map[_TMX_LayerCount, tileNumber].gid = gid - TMX_Tilesets[tsi].firstGid + 1
TMX_Map[_TMX_LayerCount, tileNumber].tilesetIndex = tsi
endif
if _TMX_LayerCount = 3
r$ = str(gid) + " - "+str(TMX_Map[_TMX_LayerCount, tileNumber].gid )
`writeLine(f2, r$)
endif
endif
endwhile
endif
endif
// Tiles from inside a pair of tileset tags, contains terrain info
if tag$ = "tile"
// not yet implemented
endif
until fileEOF(F) = 1
closeFile(F)
`closeFile(f2)
// Set background color
r = (TMX_Map.bgColor && 16711680) >> 16
g = (TMX_Map.bgColor && 65280) >> 8
b = TMX_Map.bgColor && 255
setClearColor(r, g, b)
// Load tileset images
tmx_LoadTilesets()
// Build objects
tmx_BuildObjects()
if TMX_Map.orientation = TMX_ORIENTATION_ORTHO then tmx_BuildOrthogonalMap()
if TMX_Map.orientation = TMX_ORIENTATION_ISO then tmx_BuildIsometricMap()
if TMX_Map.orientation = TMX_ORIENTATION_STAG then tmx_BuildStaggeredMap()
endfunction
//////////////////////////////////////////////////
//
//////////////////////////////////////////////////
function tmx_BuildObjects()
for i = 1 to _TMX_ObjectCount
if TMX_Objects[i].gid > 0
TMX_Objects[i].sprite = cloneSprite(TMX_Tilesets[TMX_Objects[i].tilesetIndex].sprite)
setSpritePosition(TMX_Objects[i].sprite, TMX_Objects[i].x, TMX_Objects[i].y)
setSpriteAngle(TMX_Objects[i].sprite, TMX_Objects[i].rotation)
setSpriteColorAlpha(TMX_Objects[i].sprite, TMX_Map_Layers[TMX_Objects[i].layerId].alpha)
setSpriteVisible(TMX_Objects[i].sprite, TMX_Objects[i].visible)
endif
next i
endfunction
//////////////////////////////////////////////////
// Checks the GID for tile flipping flags, then
// clears the flags before finally adding it to
// the map
//////////////////////////////////////////////////
function checkAndSetGID(layerId, tileId, gid)
if gid = 0
TMX_Map[layerId, tileId].gid = 0
TMX_Map[layerId, tileId].tilesetIndex = 0
exitfunction
endif
// Check for tile flipping
TMX_Map[layerId, tileId].flipHorizontal = gid && TMX_FLIPPED_HORIZONTALLY
TMX_Map[layerId, tileId].flipVertical = gid && TMX_FLIPPED_VERTICALLY
TMX_Map[layerId, tileId].flipDiagonal = gid && TMX_FLIPPED_DIAGONALLY
// Clear flags from gid
gid = gid && TMX_CLEAR_FLAGS
tsi = tmx_GetTilesetIndex(gid)
TMX_Map[layerId, tileId].gid = gid - TMX_Tilesets[tsi].firstGid + 1
TMX_Map[layerId, tileId].tilesetIndex = tsi
endfunction
//////////////////////////////////////////////////
// Sets the visible area for the map. Default is
// the entire screen.
//////////////////////////////////////////////////
function tmx_SetViewport(x1, y1, x2, y2)
count = TMX_Map.mapWidth * TMX_Map.mapHeight
for layer = 1 to tmx_GetLayerTotal()
for i = 1 to count
setSpriteScissor(TMX_Map[layer, i].sprite, x1, y1, x2, y2)
next i
next layer
endfunction
//////////////////////////////////////////////////
//
//////////////////////////////////////////////////
function tmx_GetTilesetName(tilesetId)
s$ = TMX_Tilesets[i].name
endfunction s$
//////////////////////////////////////////////////
//
//////////////////////////////////////////////////
function tmx_GetTilesetImageSource(tilesetId)
s$ = TMX_Tilesets[i].source
endfunction s$
//////////////////////////////////////////////////
//
//////////////////////////////////////////////////
function tmx_UpdateMap()
for i = 1 to _TMX_ObjectCount
if TMX_Objects[i].visible = 1
x = TMX_Objects[i].x + TMX_Map.offsetX
y = TMX_Objects[i].y + TMX_Map.offsetY
tmx_DrawBox(x, y, x+TMX_Objects[i].width, y+TMX_Objects[i].height, 255,0,0)
endif
next i
endfunction
//////////////////////////////////////////////////
// Returns the value of an attribute found in a
// string created by getUnparsedAttributeString$()
//////////////////////////////////////////////////
function tmx_DeleteMap()
count = TMX_Map.mapWidth * TMX_Map.mapHeight
for layer = 1 to tmx_GetLayerTotal()
if TMX_Map_Layers[layer].layerType = TMX_LAYER_TILE
for i = 1 to count
if getSpriteExists(TMX_Map[layer, i].sprite) = 1 then deleteSprite(TMX_Map[layer, i].sprite)
next i
elseif TMX_Map_Layers[layer].layerType = TMX_LAYER_IMAGE
if getSpriteExists(TMX_Map_Layers[layer].imageLayer.sprite) = 1 then deleteSprite(TMX_Map_Layers[layer].imageLayer.sprite)
endif
next layer
for i = 1 to _TMX_TilesetCount
deleteImage(getSpriteImageId(TMX_Tilesets[i].sprite))
deleteSprite(TMX_Tilesets[i].sprite)
next i
_TMX_TilesetCount = 0
_TMX_LayerCount = 0
undim TMX_Map[]
undim TMX_Map_Layers[]
undim TMX_Tilesets[]
TMX_Map.mapWidth = 0
TMX_Map.mapHeight = 0
TMX_Map.tileWidth = 0
TMX_Map.tileHeight = 0
TMX_Map.orientation = 0
TMX_Map.tileWidth2 = 0
TMX_Map.tileHeight2 = 0
TMX_Map.offsetX = 0
TMX_Map.offsetY = 0
TMX_Map.bgColor = 0
endfunction
//////////////////////////////////////////////////
// Returns the value of an attribute found in a
// string created by getUnparsedAttributeString$()
//////////////////////////////////////////////////
function xml_GetAttribute$(attributes$, attname$)
attLength = len(attname$)
L = len(attributes$)
for i = 1 to L - attLength
if mid(attributes$, i, attLength) = attname$ then exit
next i
v = i+attLength+1
temp$ = str(v)
opening = 1
for i = v to L
if mid(attributes$, i, 1) = chr(34)
if open = 1
closing = i-1
exit
endif
if open = 0
open = 1
opening = i+1
endif
endif
next i
value$ = mid(attributes$, opening, closing-opening+1)
endfunction value$
//////////////////////////////////////////////////
// Returns the content between the currently open
// tag and it's closing tag. Must specify name of
// currently opened tag.
//////////////////////////////////////////////////
function xml_GetTagInnerContent$(fileId, tag$)
tag$ = "/"+tag$
text$ = ""
s$ = xml_StripLeadingSpaces$(readline(fileId))
t$ = xml_GetTagName$(s$)
if t$ = tag$ then exitfunction text$
text$ = s$
while t$ <> tag$
s$ = xml_StripLeadingSpaces$(readline(fileId))
t$ = xml_GetTagName$(s$)
if t$ <> tag$ then text$ = s$+"\r\n" + s$
endwhile
endfunction text$
//////////////////////////////////////////////////
// Returns a string stripped of its tag
//////////////////////////////////////////////////
function xml_GetUnparsedAttributeString$(text$, tag$)
text$ = mid(text$, 3+len(tag$), -1)
endfunction text$
//////////////////////////////////////////////////
// Returns first tag name found in string
//////////////////////////////////////////////////
function xml_GetTagName$(text$)
L = len(text$)
for i = 2 to L
c = asc(mid(text$, i, 1))
if c = 62 or c = 32 then exit
next i
text$ = lower(mid(text$, 2, i-2))
endfunction text$
//////////////////////////////////////////////////
// Returns a string stripped of leading spaces
//////////////////////////////////////////////////
function xml_StripLeadingSpaces$(text$)
s = 1
for i = 1 to len(text$)
if mid(text$, i, 1) <> " "
s = i
exit
endif
next i
text$ = mid(text$, s, -1)
endfunction text$
//////////////////////////////////////////////////
// Some tileset images can having spacing and/or
// margins between the tiles. AGK's animated
// sprites cannot accommodate for these variables
// and so the image must be restructured,
// stripping out the spacing and margin.
// Returns the image ID.
//////////////////////////////////////////////////
function getTrimmedImage(filename$, tileWidth, tileHeight, margin, spacing)
img = loadImage(filename$)
m1 = createMemblockFromImage(img)
imgWidth = getImageWidth(img)
imgHeight = getImageHeight(img)
tileCountX = (imgWidth-margin) / (tileWidth + spacing)
tileCountY = (imgHeight-margin) / (tileHeight + spacing)
// Don't need image anymore, it's in the memblock
deleteImage(img)
// Loop through all tiles
for ty = 0 to tileCountY-1
for tx = 0 to tileCountX-1
// Find starting position of this tile
rows = ((ty*(tileHeight+spacing))+margin)
cols = tx*(tileWidth+spacing) + margin
base = (rows*imgWidth)*4 + cols*4 + 12
// Tile pixels
for y = 0 to tileHeight-1
for x = 0 to tileWidth-1
pos1 = base + (y*imgWidth + x)*4
junk = (ty*spacing+margin)*imgWidth*4 + (tx*spacing+margin)*4
pos2 = pos1 - junk
setMemblockInt(m1, pos2, getMemblockInt(m1, pos1))
setMemblockInt(m1, pos2, getMemblockInt(m1, pos1))
setMemblockInt(m1, pos2, getMemblockInt(m1, pos1))
next x
next y
next tx
next ty
// Dimensions of new image stripped of margin and spacing
newWidth = tileCountX*tileWidth
newHeight = tileCountY*tileHeight
createImageFromMemblock(img, m1)
img1 = copyImage(img, 0, 0, newWidth, newHeight)
deleteMemblock(m1)
endfunction img1
//////////////////////////////////////////////////
//
//////////////////////////////////////////////////
function tmx_GetMapWidth()
endfunction TMX_Map.mapWidth
//////////////////////////////////////////////////
//
//////////////////////////////////////////////////
function tmx_GetMapHeight()
endfunction TMX_Map.mapHeight
//////////////////////////////////////////////////
//
//////////////////////////////////////////////////
function tmx_GetMapTileWidth()
endfunction TMX_Map.tileWidth
//////////////////////////////////////////////////
//
//////////////////////////////////////////////////
function tmx_GetMapTileHeight()
endfunction TMX_Map.tileHeight
//////////////////////////////////////////////////
//
//////////////////////////////////////////////////
function tmx_GetMapPositionX()
endfunction TMX_Map.offsetX
//////////////////////////////////////////////////
//
//////////////////////////////////////////////////
function tmx_GetMapPositionY()
endfunction TMX_Map.offsetY
//////////////////////////////////////////////////
// Returns number of map layers
//////////////////////////////////////////////////
function tmx_GetLayerTotal()
endfunction _TMX_LayerCount
//////////////////////////////////////////////////
// X-coordinate local to the map. Map position
// does not change this value.
//////////////////////////////////////////////////
function tmx_GetTileX(t)
t = t - 1
ty = floor(t / TMX_Map.mapWidth)
tx = t - ty*TMX_Map.mapWidth
x = tx*TMX_Map.tileWidth
endfunction x
//////////////////////////////////////////////////
// Y-coordinate local to the map. Map position
// does not change this value.
//////////////////////////////////////////////////
function tmx_GetTileY(t)
t = t - 1
ty = floor(t / TMX_Map.mapWidth)
tx = t - ty*TMX_Map.mapWidth
y = ty*TMX_Map.tileHeight
endfunction y
//////////////////////////////////////////////////
// World X position of specified tile
//////////////////////////////////////////////////
function tmx_GetTileWorldX(t)
x = tmx_GetTileX(t) + TMX_Map.offsetX
endfunction x
//////////////////////////////////////////////////
// World Y position of specified tile
//////////////////////////////////////////////////
function tmx_GetTileWorldY(t)
y = tmx_GetTileY(t) + TMX_Map.offsetY
endfunction y
//////////////////////////////////////////////////
//
//////////////////////////////////////////////////
function tmx_GetLayerAlpha(layer)
a = TMX_Map_Layers[layer].alpha
endfunction a
//////////////////////////////////////////////////
//
//////////////////////////////////////////////////
function tmx_GetLayerVisible(layer)
v = TMX_Map_Layers[layer].visible
endfunction v
//////////////////////////////////////////////////
//
//////////////////////////////////////////////////
function tmx_GetObjectSprite(objectId)
s = 0
if getSpriteExists(TMX_Objects[objectId].sprite) = 1 then s = TMX_Objects[objectId].sprite
endfunction s
//////////////////////////////////////////////////
// Draws a box outline
//////////////////////////////////////////////////
function tmx_DrawBox(x1, y1, x2, y2, r,g,b)
drawLine(x1, y1, x2, y1, r,g,b)
drawLine(x1, y2, x2, y2, r,g,b)
drawLine(x1, y1, x1, y2, r,g,b)
drawLine(x2, y1, x2, y2, r,g,b)
endfunction