GetSpriteRect Example (Dirty Rectangles / Selective Map Refreshing)

Started by kevin, July 27, 2013, 09:05:32 PM

Previous topic - Next topic

kevin

  GetSpriteRect Example (Dirty Rectangles / Selective Map Refreshing)

     This is a variation of the selective map refreshing examples, the main difference is that this version draws a sprite scene and uses the GetSpriteRect function to query the sprites bounding box volume.  This saves us have to manually query the sprites possibly rotated and scaled verts.    Not only that we can make the normally fixed cost of rendering a frame more efficient, since the display refresh only has to draw sections of the backdrop and not the whole thing.        
     

     Requires PlayBASIC V1.64O Beta 19 or higher

PlayBASIC Code: [Select]
   loadfont  "arial",1,16,0,8


// -----------------------------------------------------------
// Size the Map blocks we'll use.
// -----------------------------------------------------------
constant TileWidth =32
constant TileHeight =32

// -----------------------------------------------------------
// Make the Backdrop image. This buffer is also used for the 'FX surface'
// -----------------------------------------------------------
BackDropFX =MakeBackdropImage()


// -----------------------------------------------------------
// Make a blob image to represent the sprites in the scene
// -----------------------------------------------------------
Blob= MakeBlobImage(64,64)


// -----------------------------------------------------------
// Define our RECT structure to hold the bounds of each sprite
// -----------------------------------------------------------

Type tRect
x1,y1
x2,y2
EndType

// -----------------------------------------------------------
// Define a type to simulate some characters in the scene.
// -----------------------------------------------------------
Type tAlien
Sprite
speed#
direction#
Rect as tRect
EndType

// ------------------------------------------------------
// Dim a list called Character to hold the objects
// ------------------------------------------------------
Dim Character as TAlien list


// ------------------------------------------------------
// Set the clip zone of the object
// ------------------------------------------------------
clipSize=64
ViewportX1=-clipSize
ViewportX2=GetSCreenWidth()+clipSize
ViewportY1=-clipSize
ViewportY2=GetSCreenHeight()+clipSize


// ------------------------------------------------------
// MAKE MAP / LEVEL / BLOCKS from our backdrop picture
// ------------------------------------------------------

Map,OriginalLevel,RefreshLevel=Make_Map_FRom_Image(BackDropFX)




;*=-----------------------------------------------------------------------------=*
; >> Main Loop <<
;*=-----------------------------------------------------------------------------=*


Screen=NewIMage(GetScreenWidth(),GetScreenHeight(),2)


Do

// Add new charcter to scene if the space key is being pressed
if Spacekey() or LastTime<Timer()

LastTime=Timer()+50
Add_Random_Character(Blob)

endif


// Process and render to the objects in the character list
RenderToImage Screen


// Draw the refresh level to our FX image screen
drawmap Map,RefreshLevel,0,0

// reset the refresh level
ClearLevel Map,RefreshLevel,0


// Process our list of characters
For each Character()

ThisSprite=Character.Sprite
Speed# =Character.speed#
Angle# =Character.direction#

moveSprite ThisSprite,Cosradius(angle#,speed#),SinRadius(angle#,speed#)

; Check if the character is inside the screen ?
if RectHitSprite(ViewportX1,ViewportY1,ViewportX2,ViewportY2,ThisSprite)=false
deleteSprite Sprite
Character=null
continue
endif

// Get the rect around this sprite...
if GetSpriteRect(ThisSprite,Character.Rect)
// Convert Pixels coordinates to Map Tile positions
x1=Character.Rect.x1/TileWidth
y1=Character.Rect.y1/TileHeight
x2=Character.Rect.x2/TileWidth
y2=Character.Rect.y2/TileHeight
CopyLevel Map,OriginalLevel,x1,y1,x2+1,y2+1,Map,RefreshLevel,TilePad+x1,TilePad+y1
endif
Next


// Draw the Sprite List to the FX buffer image
DrawAllSprites


// Direct rendering to the PB display
rendertoscreen

// Draw the current state of the Backdrop to the SCREEN
Drawimage Screen,0,0,false

// Draw the info to the screen for the viewer
text 0, 0,"Refresh %:"+CalcRefreshPercentage(Map,RefreshLevel)
text 0,20,"Fps:"+str$(Fps())

Sync
loop




;*=-----------------------------------------------------------------------------=*
; >> Add Random Character <<
;*=-----------------------------------------------------------------------------=*
;
Login required to view complete source code



  Video

   


   

 Related Articles:

     * Selective Tile Map Refreshing