How is this done?

Miscellaneous Forums/General Discussion/How is this done?

Hi Dude! I know your pain when it comes to tilemaps. I pulled my hair out untill I figured out how they worked.

Here is a small tilemap editor to help you get started. Press space bar to change the tile. Left mouse button to place a tile.

'########################################
'###   TileMap Editor Example for IKG   #
'###   Author - Amon                    #
'###   Version 0.3                      #
'###   Date 21st/August/2006            #
'########################################

SuperStrict



Graphics 800,600,32,60

Global tiles:TImage = LoadAnimImage("tileset.png",32,32,0,7)

Global TileSize:Int = 32 ' The size of our tiles

Global MapWidth:Int = 800/TileSize 'The width of our Map Array. Divide the screenwidth by the tileSize to get max tiles on screens width
Global MapHeight:Int = 600/TileSize ' Same as Above but divide by Screen Height

Global MapArray:Int[MapWidth,MapHeight] 'Setup our TileMAp Array which holds the tiles.

Global MX:Int, MY:Int ' MouseX & MouseY

Global CurrentTile:Int = 0 ' Holds the current tile number

For Local xiter:Int = 0 To MapWidth-1
	For Local yiter:Int = 0 To MapHeight-1
		MapArray[xiter,yiter] = 0 ' fill our Map Array with a number to respresent a blank tile. I would have the first tile in the anim
	Next						  ' as just a black square.
Next


While Not KeyHit(KEY_ESCAPE)

	Cls
	
		MX = MouseX()/TileSize*TileSize ' Doing this simple math (Dividing the mouse X&Y position by the size of our tile (32 and then
		MY = MouseY()/TileSize*TileSize ' Multiplying again gives you a grid like movement for the mouse position.
		
		ChangeTile()
		PlaceTile()
		DrawMap()
		
		DrawImage tiles,MX,MY,CurrentTile
		
	
	Flip
	
Wend

'#######################################
'# DrawMap Function					   #
'# Returns = None                      #
'# Draws our map to screen             #
'#######################################
Function DrawMap()
		
	For Local x:Int = 0 To MapWidth-1 
		For Local y:Int = 0 To MapHeight-1
			DrawImage tiles,x*TileSize,y*TileSize,MapArray[x,y] 
		Next
	Next
	
End Function

'######################################
'#Place Tile Function                 #
'#Returns = none                      #
'#Places our tiles on screen          #
'######################################
Function PlaceTile()

	If MouseDown(MOUSE_LEFT)
		MapArray[ MouseX()/TileSize , MouseY()/TileSize ] = CurrentTile 'If you notice we are dividing the mouse positions by the
																		'tilesize INSIDE the map array. This gives us the correct 
	EndIf																'location within the array to place the tile.
	
End Function

'######################################
'#Change the current Tile             #
'#Returns = None                      #
'#Changes the current tile            #
'######################################
Function ChangeTile()

	If KeyHit(KEY_SPACE)
		CurrentTile:+1
	EndIf
	
	If CurrentTile >6 Then CurrentTile = 0
	
End Function





And the image to go with it is below.



Let me know if you have any problems or need something explained. :)