News:

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

Main Menu

Basic Tiling Engine using map array

Started by LemonWizard, January 17, 2009, 11:28:42 PM

Previous topic - Next topic

LemonWizard

Again, the same old tricks work in PB ^_^ So I'm pleased.
And I really prefer this type of drawing routine.

PlayBASIC Code: [Select]
Drawnmap:
tiles=5
dim colors(tiles,3)
dim screens(tiles)
for temp=1 to tiles
screens(temp)=newimage(32,32)
a=rnd(255)
b=rnd(255)
c=rnd(255)
ink RGB (a, b, c)
rendertoimage screens(temp)
box 0,0,32,32, 1
sync
next temp


dim map(20,20)
rendertoscreen
for tempx=1 to 20
for tempy=1 to 20
tile=rnd(tiles)
if tile <1 then tile=1
map(tempx,tempy)=tile
drawimage screens(tile), tempx*32, tempy*32, 0
sync
next
next
Goto Drawnmap






kevin

#1
 There's a few problems with this example.  

  * You're not using Sync correctly.  In this program there really should only be one sync.  I get the impression,  you feel sync performs the drawing, it doesn't.  Sync flips the Screens back buffer to the front buffer.

    When we draw to the screen,  we're drawing to an invisible second copy of the screen, called the back buffer.  What you're meant to do is draw everything to backbuffer, then use Sync to show this completed image the user.   So the user only ever sees the screen is drawn state.


  * The other problem, is a logic error that makes program leak image resources.    If leave it running long enough it'd crash, one way or another.

     The problem here is how the program is restarted.  If you follow it through,  you'll notice that on the first time through the program,  we create a bunch of images, then render them as grid.  But then jump back to the start and do this again.   However,  on the second (and third etc etc ) time through, we're creating another bunch of images, without removing/replacing the existing one's.  So every time it loops, it'll leak more image resources.     You can validate this yourself by running the program is debug mode (F7) and looking at the Images in use counter under the state tab.  
     
   
PlayBASIC Code: [Select]
   tiles=5
dim colors(tiles,3)
dim screens(tiles)

for temp=1 to tiles
screens(temp)=newimage(32,32)
rendertoimage screens(temp)
boxc 0,0,32,32, 1,RndRGB()
next temp
rendertoscreen


dim map(20,20)
Do
for tempx=1 to 20
for tempy=1 to 20
tile=rndrange(1,tiles)
map(tempx,tempy)=tile
drawimage screens(tile), tempx*32, tempy*32, 0
next
next
sync
loop






Using a Map
PlayBASIC Code: [Select]
   tiles=5

Map=newMap(1)
CreateMapGFX Map,32,32,Tiles,Rgb(255,0,255)

for temp=0 to tiles-1
boxc 0,0,32,32, 1,RndRGB()
GetMapBlk Map,Temp-1,0,0
next temp

Level=NewLevel(Map,20,20)
Do

Cls 0
for tempy=0 to 20
for tempx=0 to 20
POkeLevelTile Map,Level,tempX,TempY,rndrange(1,tiles)
next
next
drawmap Map,Level,32,32

sync
loop