Ok, I came up with this code, does the job flawlessly, but I'm sure it's not the best way to achieve it.
This might be interesting for those who like to play with 2d and tile based maps, and everyone in general as I think this might be useful in 3d apps as well.
gosub defTypes
gosub declareVariables
gosub screenSetup
gosub loadData
gosub createImages
do
gosub Controls
gosub ScrollScreen
gosub DisplayPlayer
sync
loop
screenSetup:
sync on
sync rate 60
backdrop off
set image colorkey 255,0,255
set display mode 512, 480, 16
return
defTypes:
type Tile
x1 as integer
x2 as integer
y1 as integer
y2 as integer
image as integer
mask as integer
special as integer
endtype
type Jugador
x1 as integer
x2 as integer
y1 as integer
y2 as integer
image as integer
PosX as integer
SpeedX
endtype
return
declareVariables:
TilesPerScreenX = 16
TilesPerScreenY = 6
TileSizeX = 32
TileSizeY = 32
NumOfTilesX = 56
NumOfTilesY = 7
yOffset = 8
player as Jugador
dim TileMap(NumOfTilesX-1, NumOfTilesY-1) as Tile
player.SpeedX = 5
player.image = 100
player.x1 = 320
player.y1 = 320
player.y2 = player.y1 + 64
return
createImages:
load image "road2.bmp", 1, 1
load image "grass2.bmp", 2, 1
load image "fence2.bmp",3,1
load image "player.bmp", 100,1
return
loadData:
restore lvl
for y = 0 to NumOfTilesY - 1
for x = 0 to NumOfTilesX - 1
read TileMap(x,y).image
TileMap(x,y).x1 = x * TilesizeX
TileMap(x,y).x2 = (x*32) + TileSizeX
TileMap(x,y).y1 = (y+yOffset) * TileSizeY
TileMap(x,y).y2 = ((y+yOffset) + 32) + TileSizeY
next x
next y
return
Controls:
if leftkey() = 1 and delay => 2
`if player.x1 =< 192 then dec player.PosX, player.SpeedX
`if player.x1 > 192 then dec player.x1, player.SpeedX
dec player.PosX, player.SpeedX
delay = 0
endif
if rightkey() = 1 and delay => 2
`if player.x1 => 320 then inc player.PosX, player.SpeedX
`if player.x1 < 320 then inc player.x1, player.SpeedX
inc player.PosX, player.SpeedX
delay = 0
endif
inc delay
return
ScrollScreen:
if TileNumX = 40 `if TileNumX = the end of the map...
TileNumX = 0 ` then reset the player coordinates so the player starts at the beginning of the map
TempX = 0
player.PosX = 0
Temp1 = 0
endif
if player.PosX < 0 `if TileNumX < the start of the map...
TileNumX = 50 `then set the player coordinates to the end of the map
TempX = 0
player.PosX = 1270
endif
TileNumX = player.PosX / TileSizeX
TempX = player.PosX - (TileNumX * TileSizeX)
if TilesPerScreenX + TileNumX >= NumOfTilesX then Temp1 = 1
for y = 0 to TilesPerScreenY - Temp2
for x = 0 to TilesPerScreenX - Temp1
paste image TileMap(x + TileNumX,y + TileNumY).image, TileMap(x,y).x1 - Tempx, TileMap(x,y).y1, 0
next x
next y
return
DisplayPlayer:
sprite 1, player.x1, player.y2, player.image
return
lvl:
data 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2
data 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2
data 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2
data 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2
data 3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3
data 2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,3,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
data 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2
By the way, the media consists of 3 images of 32*32 pixels each, and an image of 32*64.