[comp.sys.sgi] Animation!

PEPKE%FSU.MFENET@NMFECC.ARPA (05/04/88)

Brodie Lockard <I.ISIMO@LEAR.STANFORD.EDU> writes:

> es1o+@andrew.cmu.edu (Eric Mitchell Snider) writes:

>>     Ok, here's another problem I'd like some help with.  I'm working on
>>a program where several small objects move around in front of a
>>background.  The problem is I don't know a good way to replace the
>>background after my objects have been erased and moved.  At the moment
>>everything (including the background) is a separate picture (PICT)
>>resource.  What do I need to do?!?

> A typical algorithm used for animation like you mentioned is to
> 1. Figure out where you're first going to draw the foreground (the
> thing being moved), and save what's there (call this "Under").
> 2. Draw your foreground where it belongs.
> 3. Whenever your object moves,
>  A. Redraw Under where it came from.
>  B. Figure out where you're first going to draw the foreground,
>  and save what's there in Under again.
>  C. Draw your foreground in the new place.

I have an application which involves a variable number of objects (typically
a half dozen or so) moving around in 3-space.  My first attempt was similar
to what Brodie describes, but I found it to be too slow, especially as all
the special cases that test for objects moving in front of each other adds
code.  What I instead adopted was a simpler approach which lends itself to
some good optimizations.  For each object on the screen (including the 
background) I keep an offscreen bitmap and mask (if it's not rectangular).  
Every time I want to emit a frame, I copy the bitmaps from back to front.  When
objects move, all you have to do is change the position at which you copy
the bitmaps.  When the pictures of objects change, you have to redraw the
PICTs into the bitmaps and possibly change the size of the bitmaps.  All
the redrawing is done into an off-screen bitmap, which is then CopyBitsed
onto the screen.

Now for the optimizations:
The off-screen bitmap has no regions to deal with, so CopyBits is much
faster.  In some cases you don't even need to do a CopyBits.  For example,
if you ensure that the background has the same bounds and rowBytes as
the off-screen bitmap, you can simply use BlockMove or a tight assembly
loop to put the background down.  For the other objects that don't move very 
often, ensure that the CopyBits will start on a word boundary.  You might
even consider keeping 16 preshifted bitmaps around for small objects.  I can 
think of many more optimizations that I am too lazy to implement, and you 
probably can too.

My application has to do massive amounts of other work per frame including
sorts, editing heierarchies, environment searches, et cetera, but even
so, I saw almost an order of magnitude speed improvement.  With tighter
and simpler code you will probably see a greater improvement.

Eric Pepke                           pepke%fsu.mfenet@nmfecc.arpa
Supercomputer Computations           pepke%scri.hepnet@lbl-csa2.arpa
   Research Institute                pepke%fsu.bitnet@wiscvm.wisc.edu
Florida State University             "You're living in your own private Idaho
Tallahassee, FL 32306-4052            On the ground like a wild potato."

Disclaimer: My employers seldom even LISTEN to my opinions.
Meta-disclaimer: Any society that needs disclaimers has too many lawyers.