Thursday 5 February 2009

Getting there

Finally got the chance to squash this bug and so far it's looking hopeful. In fact, the redesign has meant that I may be able to speed the draw code up. The reason for this is that I was forced to find a way of preventing objects competing for the front of the queue. It only happens in certain circumstances but is a major problem if I'm to retain the freedom offered by unusual dimensions and locations for any object. If for example only 3 objects are on screen but object1 should appear in front of object 2 and object 2 should appear in front of object 3 and object 3 should appear in front of object 1 it causes one of the objects to pop out from behind an object that is supposedly obscuring it Scroll down to "Big Mistake" post to see a screenshot of the bug in action. The solution which means hopefully faster redraw is as follows:

  • Loop through the list of objects onscreen.
  • At each object quickly loop through all other objects and check for overlaps (2D overlap with actual sprite, not a 3D collision) with surrounding objects.
  • If there is an overlap with an object then check to see if this object is in front of current object about to be drawn.
  • If overlapping object is in front then cut out section of overlapping objects sprite that actually overlaps our object's sprite and draw it over the top.
  • Repeat for all overlapping objects and when finished move onto next object.
This means that anywhere on screen we can draw an object and it will automatically be drawn correctly. Effectively I only have to draw objects that have moved or changed frame and anything not changed does not need redraw. This of course is much faster than redrawing everything onscreen every frame. It also means I haven't got to run sorting algorithm for any objects really since the order that objects get drawn isn't important. All I need to do is check what's onscreen and redraw stuff that's changed. That's the theory anyway. Might be more difficult in practice especially when I come to putting this into OpenGL.

No comments:

Post a Comment