IMHO the (mis-)use of an image is still the simpler solution. When using a tiles array instead of pointers (a silly idea, I confess), if there are not more than 255 different tiles, the colour depth can be reduced to 8 bits. That means a memory usage of 11kB for a 100 x 100 fields board.
Furthermore the whole board can be saved with a simple BSAVE.
Code: Select all
#Include Once "fbgfx.bi"
#Include Once "file.bi"
Type tTile
kind As String
End Type
Dim As Integer x, y, display_x, display_y
Dim As String g
Dim As FB.image Ptr map, maptemp
Dim Shared As ULong mapwidth = 50, mapheight = 50
Dim Shared As UShort mapbpp = 8
Declare Sub display(viewx As ULong, viewy As ULong, map As FB.image Ptr, tile() As tTile)
'create an example array of tiles
Dim tile(8) As tTile
tile(1).kind = "village"
tile(2).kind = "town"
tile(3).kind = "forest"
tile(4).kind = "street"
tile(5).kind = "abbey"
tile(6).kind = "swamp"
tile(7).kind = "castle"
tile(8).kind = "lake"
ScreenRes 900, 900, 32
map = ImageCreate(mapwidth, mapheight, 0, mapbpp) 'create default map
'randomly place 20 tiles
Randomize
Dim As Integer tile_x, tile_y
For x As Integer = 1 To 20
tile_x = Int(Rnd * 10)
tile_y = Int(Rnd * 10)
Do Until Point(tile_x, tile_y, map) = 0 'find an empty pixel
tile_x = Int(Rnd * 10)
tile_y = Int(Rnd * 10)
Loop
Dim As UByte kindOfTile = Int(Rnd * UBound(tile) + 1) 'pick a random tile
PSet map, (tile_x, tile_y), kindOfTile 'write the array index of the tile to the map
Next
Do
ImageInfo map, mapwidth, mapheight
Select Case InKey
Case Chr(255,77) 'arrow right
If display_x >= mapwidth - 10 Then 'add one column at the right side
maptemp = ImageCreate(mapwidth + 1, mapheight, 0, mapbpp)
Put maptemp, (0, 0), map, (0, 0) - (mapwidth - 1, mapheight - 1), PSet
ImageDestroy map
map = maptemp
maptemp = 0
EndIf
display_x += 1
Case Chr(255,75) 'arrow left
If display_x > 0 Then
display_x -= 1
Else 'add one column at the left side
maptemp = ImageCreate(mapwidth + 1, mapheight, 0, mapbpp)
Put maptemp, (1, 0), map, (0, 0) - (mapwidth - 1, mapheight - 1), PSet
ImageDestroy map
map = maptemp
maptemp = 0
EndIf
Case Chr(255,80) 'arrow down
If display_y >= mapheight - 10 Then 'add one row at the bottom
maptemp = ImageCreate(mapwidth, mapheight + 1, 0, mapbpp)
Put maptemp, (0, 0), map, (0, 0) - (mapwidth - 1, mapheight - 1), PSet
ImageDestroy map
map = maptemp
maptemp = 0
EndIf
display_y += 1
Case Chr(255,72) 'arrow up
If display_y > 0 Then
display_y -= 1
Else 'add one row at the top
maptemp = ImageCreate(mapwidth, mapheight + 1, 0, mapbpp)
Put maptemp, (0, 1), map, (0, 0) - (mapwidth - 1, mapheight - 1), PSet
ImageDestroy map
map = maptemp
maptemp = 0
EndIf
Case "s" 'save map
BSave("gameboard.bmp", map)
Case "l" 'load map
If FileExists("gameboard.bmp") Then
ImageDestroy map
map = 0
'get the map parameters
Open "gameboard.bmp" For Binary As #1
g = Input(18, #1) 'skip 18 bytes
mapwidth = Cvi(Input(4, #1))
mapheight = Abs(Cvi(Input(4, #1)))
g = Input(2, #1) 'skip 2 bytes
mapbpp = CVShort(Input(2, #1))
Close #1
map = ImageCreate(mapwidth, mapheight, 0, mapbpp) 'create map
BLoad("gameboard.bmp", map)
display_x = 0
display_y = 0
EndIf
Case " "
Exit Do
End Select
ImageInfo map, mapwidth, mapheight
display(display_x, display_y, map, tile())
Sleep 1
Loop
'BSave("gameboard.bmp", map)
ImageDestroy map
Sub display(viewx As ULong, viewy As ULong, map As FB.image Ptr, tile() As tTile)
Dim As Integer x, y, scx, scy
'create view section, the piece of the map that is displayed on the screen (10 x 10 tiles)
Dim As FB.image Ptr viewsection = ImageCreate(10, 10, 0, mapbpp)
Dim As UByte index
Dim As String text
'copy the section to be displayed to the buffer
Get map, (viewx, viewy) - (viewx + 9, viewy + 9), viewsection
ScreenLock
Cls
For y = 0 To 9
For x = 0 To 9
Line (x * 90 + 5, y * 90 + 5) - ((x + 1) * 90 - 5, (y + 1) * 90 - 5), RGB(255, 255, 255), b
index = Point(x, y, viewsection)
Draw String(x * 90 + 10, y * 90 + 10), "x=" & viewx + x & " y=" & viewy + y, RGB(255, 255, 255)
If index Then
text = tile(index).kind
Else
'text = "empty"
text = ""
EndIf
Draw String(x * 90 + 10, y * 90 + 30), text, RGB(255, 255, 255)
Next
Next
ScreenUnLock
ImageDestroy viewsection
End Sub