News:

PlayBASIC2DLL V0.99 Revision I Commercial Edition released! - Convert PlayBASIC programs to super fast Machine Code. 

Main Menu

Copying Tile Data With Wrapping

Started by kevin, January 29, 2007, 11:20:17 PM

Previous topic - Next topic

kevin

  This example shows a concept used in some scrolling 2D games, where the game engine scrolled/copied the tile data to create the illusion of an infinite scrolling plain. This example just shows how the base concept of copying the tiles (with wrapping) works.

 This approach was necessary in the days of old basically (On Computers with Character Map screens ie. C64), but not today.  So naturally in PlayBasic you wouldn't actually do it this way (See Maps & Camera), but you could if you wanted to I guess.


PlayBASIC Code: [Select]
; Array to hold the world map
Dim WorldMap(128,128)


; Size in pixels of a tile
TileWidth=48
TileHeight=48

; Calc
X=GetScreenWidth()/TileWidth
Y=GetScreenHeight()/TileHeight
Dim ScreenMap(X,Y)

; Fill the World Map will colour data
FillMap(WorldMap())

; Copy the WorldMap to Screen Map at Cord 0,0
CopyMap(WorldMap(),ScreenMap(),0,0)



; Manin loop
Do
; Clear the Screen to Black RGB(0,0,0)
Cls 0


; Check if the user moved the camera
Moved=false
if LeftKey() Then Dec Xpos : Moved=true
if RightKey() Then Inc Xpos : Moved=true
if upKey() Then Dec Ypos : Moved=true
if downKey() Then Inc Ypos : Moved=true

; Copy the new section of the worldbuffer to the screenbuffer
; if the user moved.
if Moved then CopyMap(WorldMap(),ScreenMap(),Xpos,Ypos)


; render the Map
ShowMap(SCreenMap(),0,0,TileWidth,TileHeight)

print "Position:"+StR$(Xpos)+","+str$(Ypos)
Sync
loop




Function CopyMap(SrcMap(),DestMap(),Xpos,Ypos)

; get src array size
Width =getArrayElements(Srcmap(),1)
Height =getArrayElements(Srcmap(),2)

; Wrap Starting Coords
Xpos=mod(Xpos,Width)
Ypos=mod(Ypos,Height)
if Xpos<0 then Xpos=Width+Xpos
if Ypos<0 then Ypos=Height+Ypos

; Do Blit
SrcY=Ypos
For ylp=0 to GetArrayElements(Destmap(),2)
For Xlp=0 to GetArrayElements(Destmap(),1)
; copy the SRC to DEST map. (Wrap the SRCX coord)
DestMap(xlp,ylp)=SrcMap(Mod(Xpos+Xlp,Width),SrcY)
Next
; wrap the SRC Y coords
SrcY=Mod(Ypos+Ylp,Height)
Next

EndFunction



Function FillMap(Map())
For ylp=0 to GetArrayElements(map(),2)
For Xlp=0 to GetArrayElements(map(),1)
Map(xlp,ylp)=rgb(100,Ylp*2,Xlp*2)
Next
Next
EndFunction



Function ShowMap(Map(),Xpos,Ypos,TileWidth,TileHeight)
For ylp=0 to GetArrayElements(map(),2)
For Xlp=0 to GetArrayElements(map(),1)
x=Xpos+(TileWidth*Xlp)
boxc X,Ypos,X+TileWidth,Ypos+Tileheight,true,Map(Xlp,ylp)
Next
Ypos=Ypos+TileHeight
Next
EndFunction





Ian Price

Cheers Kevin, that's actually pretty useful.

I've not delved into the world of maps/cameras in PB, I've always used my own methods, however, I will one day.
I came. I saw. I played some Nintendo.

kevin

#2

Wrap Scrolling Example


QuoteI've not delved into the world of maps/cameras in PB, I've always used my own methods, however, I will one day//

    Well, you can't teach an old dog new tricks, least of all programmers !  :)