Main Menu

Selective Tile Map Refreshing

Started by kevin, October 01, 2008, 02:20:02 PM

Previous topic - Next topic

kevin

Selective Tile Map Refreshing

   This is a quick example of a dirty rectangle style refresh using a tile map.  The example allows you to render a 'static' backdrop page with a bunch of the sprites moving over it.  The primary objective of such approaches is to reduce the cost per pixel.   For example,  In order to support Alpha effects we'd normally draw everything to an off screen FX surface.  So in order to the build the frame we'd clear/redraw the backdrop,  draw our sprites over that.  Then draw the whole buffer to the screen.   So every pixel is being rendered at least twice.   This cost adds up, no matter how fast the machine is.

   This kind of process,

PlayBASIC Code: [Select]
    RenderToIMage FXScreen

DrawImage BackDropPicture,0,0,false ; (Redraw the backdrop to reset the scene)

DrawAllSprites

RenderToScreen

DRawIMage FXScreen,0,0,false

Sync



   However when selectively refreshing,  rather than redraw the backdrop to clear the screen each frame,  we can manage what parts of the backdrop picture need to 'redrawn' in order to restore the backdrop image to it's original state (before the sprites we're drawn over it) .   So if there's only one sprite on screen for example,  then we're only refreshing the pixels behind it.  So only those pixels are being drawn twice.   Making it a much more efficient approach for single screen games with a few characters on them.

  There's a tipping point however,  if at some point the entire backdrop needs to be refreshed (Screen fulll of sprites for example),  then our efficiency will doubt be slow the good old copy over loop above.


 Example Written in PB1.64  (Should work  1.63 editions (pre-2008) also though)



   Related Articles:

        * GetSpriteRect Example (Dirty Rectangles / Selective Map Refreshing)



kevin

#1
   Selective Tile Map Refreshing II

      This is a more updated version of the previous example.   The only real difference is that, this one uses built maps where is the original one demonstrates the  hands on method.   Apart from that they're pretty much the same, with just some tweaks for newer editions of PlayBASIC V1.64M and above.


  Video



  This is another short PlayBASIC source code example, this one's demonstrating a selective redrawing method using PlayBASIC Maps. The program attempts to reduce the cost per refresh by not redrawing the entire backdrop picture every frame. It does this by tagging the overdrawn zones that need to be restored using a Map. So basically the backdrop picture is first cut up into an Map representation. In this example the block size is 32x by 32y pixels, giving us a level size of (800/32 by 600/32). The demo uses two versions of the level tile map data. One is the original and the second is a version we're going to draw from. We need two since the redraw version gets cleared and altered each frame.

 When drawing character objects (in this demo those are the circular blobs), we move the object then convert it's 2d space coordinates back into map coordinates. Then copy from this tile/block area from Original Level to the Refresh Level. We repeat this process for all on screen objects. So after processing our characters, the refresh map contains the parts of the backdrop that need to refreshed (reset) back to it's original state before drawing the next frame. So rather than redraw the entire backdrop picture every time, we can selectively refresh the backdrop. This type of method works really well in 2D games, as it allows the program to lower the cost per-pixel, ironing out the performance for older systems. By older, I mean systems beyond 5 years, closer to 10 years old really
     

  Related Articles

    * A Crash Course In Optimization



 Download

  Attached.

ATLUS