Implementing 'Dirty Rectangles' in an arcade emulator

For those who are not familiar with the term 'Dirty Rectangles', it is a means of only updating parts of the display which have changed since the last repaint.  This can save considerable amounts of time if there are only a few small objects moving around.   I have created a very simple scheme for this which divides the display into 32 sections from top to bottom.  This approach allows for the fastest BitBlt'ing to the display since we are using the entire width of the image, but still allows for considerable time savings and easy implementation.  The first step is to mark the dirty rectangles as you are changing the contents of the video buffer.  I use a long var to represent 32 sections and mark it as follows:

lDirtyRect |= 1<< (y / (ScreenHeight/32)); /* when this line changes */

The screen update routine then checks if cDirtyRect != 0 and proceeds to merge adjacent rectangles together and Blit them to the screen.  There will be a maximum of 16 distinct rectangles and the code to merge them is as follows:

int iNumRegions, yTop[16], ySize[16];

iStripRows = ScreenHeight / 32;

iNumRegions = i = 0;
while (i<32)
   {
   if (cDirty & 1<<i)
/* start a region */
      {
      yTop[iNumRegions] = iStripRows * i;
/* Starting y */
      ySize[iNumRegions] = iStripRows;
/* Initial height */
      i++;
      while (i<32 && cDirty & 1<<i)
         {
/* continue region until it ends */
         ySize[iNumRegions] += iStripRows;
         i++;
         }
      iNumRegions++;
/* One more valid region */
      }
   else
      i++;
/* Skip this region */
   }

Then just blit the defined regions to the display and set cDirtyRect = 0 for the next time.

Back