Thursday, December 16, 2010

Dirty Rectangles III

The website is still down - Not sure when it will be up again...


More time has been spent on dirty rectangles to try and eek out some performance during rendering to canvas.  There are basically two methods which I am currently fighting with:

  1. Brute force method
  2. Joined rectangles method
The first method would go through the collection of objects, and for each dirty object it would grab an image of the space behind it and store that.  For the next frame, it will restore those stored rectangles and then the process starts again.  Objects which move, but then stop, would need to update the layer to which they draw to keep this process somewhat automatic.  Otherwise, objects that stop being dirty would be lost during the next frame redraw.  The second method would use something similar, but instead of capturing an image for every dirty object, it would combine rectangles which overlap.  The difference between the two is timing.

With the first method, we just go ahead and grab dirty rectangles for each object regardless of whether another object is grabbing that rectangle (or part of it) as well.  Thus, if there are 30 dirty objects, we grab 30 rectangles each frame, draw 30 objects, then for the next frame we restore 30 rectangles and do it again.

If we combine rectangles, it might add up to a savings if the objects overlap.  However, time will be spent determining the overlap and joining rectangles before actually grabbing the images to later restore.  I'm not sure if the trade-off is worth it without actually testing it.  The actual method is pretty intensive and requires visiting objects, which have already been visited, repeatedly.  This might end up causing more overhead than just a simple brute force method.

The other issue, with dirty rectangles, is the particle engine.  Capturing hundreds of tiny little rectangles for the particle engine is messy, and time consuming.  The actual time saved is lost because we're doing twice the work for all of those particles.  For larger objects it makes sense.  If we can eliminate the need to redraw 30 objects, if only 3 are moving, then we save time.  There are some tricks for particles, such as using bitmaps for the explosions which simulate particles but that might look bad.

Maybe using the Asteroids Evolution demo to test all of this isn't the best idea, after all.

No comments:

Post a Comment