Here we go... Store the tiles in a single Sequence and work out each tile's position from x and y (y * mapWidth + x). Each time you load a map, clear it and fill it again, so it fits whatever size that map is.
Here's a full example you can run. It makes two test maps of different sizes (8 x 6 and 40 x 30), loads one, and draws it. Press SPACE to switch maps and ESC to quit. The window title shows the size of the loaded map.
The map files start with the width and height, followed by one Int per tile, row by row. Change LoadMap to match your own file format.
; Tilemaps of any size, using a Sequence
; Press SPACE to switch between a small and a big map, ESC to quit.
Import "BBRuntime64.decls"
Const KEY_ESCAPE:Int = $1B
Const KEY_SPACE:Int = $20
Const SCREEN_WIDTH:Int = 800
Const SCREEN_HEIGHT:Int = 600
; The current map
Global mapWidth:Int = 0
Global mapHeight:Int = 0
Global mapTiles:Sequence Of Int
; Load a map file: width, height, then one Int per tile (row by row)
Function LoadMap(filename:String)
Local file:Int = fileReadFile(filename)
mapWidth = fileReadInt(file)
mapHeight = fileReadInt(file)
SeqClear mapTiles
For i:Int = 0 To mapWidth * mapHeight - 1
SeqAdd mapTiles, fileReadInt(file)
Next
fileCloseFile(file)
EndFunction
Function GetTile:Int(x:Int, y:Int)
Return SeqGet(mapTiles, y * mapWidth + x)
EndFunction
Function SetTile(x:Int, y:Int, tile:Int)
SeqSet mapTiles, y * mapWidth + x, tile
EndFunction
; Make a test map file, so the example has something to load.
; Tiles: 0 = grass, 1 = wall, 2 = water
Function MakeTestMap(filename:String, w:Int, h:Int)
Local file:Int = fileWriteFile(filename)
fileWriteInt(file, w)
fileWriteInt(file, h)
For y:Int = 0 To h - 1
For x:Int = 0 To w - 1
Local tile:Int = 0
If x = 0 Or y = 0 Or x = w - 1 Or y = h - 1 Then
tile = 1
ElseIf (x * 7 + y * 3) Mod 5 = 0 Then
tile = 2
EndIf
fileWriteInt(file, tile)
Next
Next
fileCloseFile(file)
EndFunction
; Draw the whole map, with the tiles sized to fit the screen
Function DrawMap()
Local tileSize:Int = SCREEN_WIDTH / mapWidth
If SCREEN_HEIGHT / mapHeight < tileSize Then
tileSize = SCREEN_HEIGHT / mapHeight
EndIf
For y:Int = 0 To mapHeight - 1
For x:Int = 0 To mapWidth - 1
Select GetTile(x, y)
Case 0
b2dSetColor(40, 160, 60)
Exit
Case 1
b2dSetColor(120, 120, 120)
Exit
Case 2
b2dSetColor(40, 90, 200)
Exit
EndSelect
b2dDrawRect(x * tileSize, y * tileSize, tileSize - 1, tileSize - 1, 1)
Next
Next
EndFunction
Function Main()
b2dGraphics(SCREEN_WIDTH, SCREEN_HEIGHT, 1)
b2dSetClsColor(0, 0, 0)
MakeTestMap("small.map", 8, 6)
MakeTestMap("big.map", 40, 30)
Local mapName:String = "small.map"
LoadMap(mapName)
SetTile(1, 1, 2) ; change a tile after loading
Local running:Int = True
While running
sysUpdateEvents()
If inpIsKeyDown(KEY_ESCAPE) Then
running = False
EndIf
If inpIsKeyHit(KEY_SPACE) Then
If mapName = "small.map" Then
mapName = "big.map"
Else
mapName = "small.map"
EndIf
LoadMap(mapName)
EndIf
sysSetWindowTitle(mapName & ": " & ToString(mapWidth) & " x " & ToString(mapHeight) & " tiles (" & ToString(SeqLength(mapTiles)) & ") - SPACE to switch")
b2dCls()
DrawMap()
b2dFlip()
Wend
b2dEnd()
Return False
EndFunction
Just to note... I'm currently implementing runtime functions to handle Tiled tilemaps, it's in the Windows version at this moment in time, still testing when I can, but it will be ported to the other targets when I get to it.
So far, this is what is in there:
; Tilemaps
tmCreate%(widthInTiles%,heightInTiles%,tileW%,tileH%):"tmCreate"
tmFree(map%):"tmFree"
tmGetWidth%(map%):"tmGetWidth"
tmGetHeight%(map%):"tmGetHeight"
tmGetTileWidth%(map%):"tmGetTileWidth"
tmGetTileHeight%(map%):"tmGetTileHeight"
tmAddLayer%(map%,name$,tileset%):"tmAddLayer"
tmGetLayer%(map%,name$):"tmGetLayer"
tmLayerCount%(map%):"tmLayerCount"
tmTilesetCreate%(image%,tileW%,tileH%,padding%):"tmTilesetCreate"
tmTilesetCreateBlank%(count%,tileW%,tileH%):"tmTilesetCreateBlank"
tmTilesetFree(tileset%):"tmTilesetFree"
tmTilesetCount%(tileset%):"tmTilesetCount"
tmSetTileFlags(tileset%,id%,flags%):"tmSetTileFlags"
tmGetTileFlags%(tileset%,id%):"tmGetTileFlags"
tmSetTileValue(tileset%,id%,value%):"tmSetTileValue"
tmGetTileValue%(tileset%,id%):"tmGetTileValue"
tmSetTileAnim(tileset%,id%,firstFrame%,frameCount%,msPerFrame%):"tmSetTileAnim"
tmSetTileMask(tileset%,id%,mask$):"tmSetTileMask"
tmClearTileMask(tileset%,id%):"tmClearTileMask"
tmMasksFromImage%(tileset%):"tmMasksFromImage"
tmSetTile(map%,layer%,x%,y%,id%):"tmSetTile"
tmGetTile%(map%,layer%,x%,y%):"tmGetTile"
tmSetTileFlip(map%,layer%,x%,y%,flip%):"tmSetTileFlip"
tmGetTileFlip%(map%,layer%,x%,y%):"tmGetTileFlip"
tmFill(map%,layer%,x%,y%,w%,h%,id%):"tmFill"
tmSetTerrain(tileset%,terrain%,baseTile%,layout%):"tmSetTerrain"
tmSetTileAuto(map%,layer%,x%,y%,terrain%):"tmSetTileAuto"
tmSetCamera(map%,x%,y%):"tmSetCamera"
tmSetZoom(map%,zoom!):"tmSetZoom"
tmDrawLayer(map%,layer%):"tmDrawLayer"
tmDraw(map%):"tmDraw"
tmDrawLayerRow(map%,layer%,row%):"tmDrawLayerRow"
tmRowCount%(map%):"tmRowCount"
tmRowAt%(map%,worldX%,worldY%):"tmRowAt"
tmSetOrientation(map%,orientation%):"tmSetOrientation"
tmGetOrientation%(map%):"tmGetOrientation"
tmSetStagger(map%,byColumns%,evenOnes%):"tmSetStagger"
tmSetHexSide(map%,side%):"tmSetHexSide"
tmNeighbourX%(map%,x%,y%,direction%):"tmNeighbourX"
tmNeighbourY%(map%,x%,y%,direction%):"tmNeighbourY"
tmTileXAt%(map%,worldX%,worldY%):"tmTileXAt"
tmTileYAt%(map%,worldX%,worldY%):"tmTileYAt"
tmTileCentreX%(map%,x%,y%):"tmTileCentreX"
tmTileCentreY%(map%,x%,y%):"tmTileCentreY"
tmGridToWorldX!(map%,gridX!,gridY!):"tmGridToWorldX"
tmGridToWorldY!(map%,gridX!,gridY!):"tmGridToWorldY"
tmWorldToGridX!(map%,worldX!,worldY!):"tmWorldToGridX"
tmWorldToGridY!(map%,worldX!,worldY!):"tmWorldToGridY"
tmSetLayerParallax(map%,layer%,factorX!,factorY!):"tmSetLayerParallax"
tmSetLayerVisible(map%,layer%,visible%):"tmSetLayerVisible"
tmSetLayerAlpha(map%,layer%,alpha!):"tmSetLayerAlpha"
tmWorldToTileX%(map%,worldX%):"tmWorldToTileX"
tmWorldToTileY%(map%,worldY%):"tmWorldToTileY"
tmTileAt%(map%,layer%,worldX%,worldY%):"tmTileAt"
tmBoxHit%(map%,layer%,x%,y%,w%,h%,flags%):"tmBoxHit"
tmRayCast%(map%,layer%,x1%,y1%,x2%,y2%,flags%):"tmRayCast"
tmHitX%():"tmHitX"
tmHitY%():"tmHitY"
tmHitWorldX%():"tmHitWorldX"
tmHitWorldY%():"tmHitWorldY"
tmHitTile%():"tmHitTile"
tmPixelSolid%(map%,layer%,worldX%,worldY%,flags%):"tmPixelSolid"
tmProbe%(map%,layer%,x!,y!,dirX!,dirY!,from%,reach%,flags%):"tmProbe"
tmHitDistance%():"tmHitDistance"
tmGroundProbe%(map%,layer%,x!,y!,angle!,radius%,foot%,from%,reach%,flags%):"tmGroundProbe"
tmGroundX!():"tmGroundX"
tmGroundY!():"tmGroundY"
tmGroundAngle!():"tmGroundAngle"
tmLoad%(path$):"tmLoad"
tmLoaded%(map%):"tmLoaded"
tmLayerTileset%(map%,layer%):"tmLayerTileset"
tmUseBackground%(map%):"tmUseBackground"
tmObjectCount%(map%,layerName$):"tmObjectCount"
tmObjectName$(map%,layerName$,index%):"tmObjectName"
tmObjectType$(map%,layerName$,index%):"tmObjectType"
tmObjectX%(map%,layerName$,index%):"tmObjectX"
tmObjectY%(map%,layerName$,index%):"tmObjectY"
tmObjectW%(map%,layerName$,index%):"tmObjectW"
tmObjectH%(map%,layerName$,index%):"tmObjectH"
tmObjectProp$(map%,layerName$,index%,key$):"tmObjectProp"
tmObjectTile%(map%,layerName$,index%):"tmObjectTile"
tmObjectRotation!(map%,layerName$,index%):"tmObjectRotation"
I was going to roll my own tilemap editor in the IDE, but to be honest, Tiled is ideal, free and has been around for ages... So, people will know how to use it.
Anyway, there we go! 🙂
Dabzy