delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/08/03/21:48:15

From: Shawn Hargreaves <Shawn AT talula DOT demon DOT co DOT uk>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Dirty Rectangles/Allegro
Date: Sun, 3 Aug 1997 19:07:57 +0100
Organization: None
Distribution: world
Message-ID: <gJbbWNB9jM5zEwY9@talula.demon.co.uk>
References: <33E48E9A DOT 168D2B4F AT cs DOT curtin DOT edu DOT au>
NNTP-Posting-Host: talula.demon.co.uk
MIME-Version: 1.0
Lines: 47
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Dave writes:
>Okay I have tried to look at the source for Demo that comes with allegro
>but i just cant work out the specifics of the dirty rectangle
>allgorithm.
>
>Could someone post an algorithm in code or in Pseudo-code :)

Ok, here goes!

Create a memory bitmap to use as a drawing buffer, the same size as the
physical screen. Also create two lists of rectangles (I'll call them
current and previous) with room to hold a number of entries at least
equal to the maximum number of objects you are ever going to display at
a time. Initialise both lists to contain a single rectangle covering the
entire screen.

Your redraw routine then looks something like:

    For each rectangle in the previous list, reset that area of your 
    memory bitmap to the background image. That might mean just clearing 
    it to black, or copying graphics across from another image, or 
    drawing tiles from a map, or whatever.

    Clear the current list to empty.

    For each movable object in your game, draw it onto the memory 
    bitmap, and add the area it covers into the current list.

    For each rectangle in both the previous and current lists, blit that 
    area of your memory bitmap across onto the screen.

    Copy the current list across so that it becomes the new previous 
    list.

    Repeat.

Obviously this can be implemented without an actual copy of the current
to the previous list, by using pointers and flipping which area of
memory is used by each list. Useful optimisations are sorting the lists
by Y coordinate before the blit to the screen (to cit down on bank
switching), and merging overlapping regions into a single rectangle
while adding new items into each list...


--
Shawn Hargreaves - shawn AT talula DOT demon DOT co DOT uk - http://www.talula.demon.co.uk/
Beauty is a French phonetic corruption of a short cloth neck ornament.

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019