Color Animation II
Volume Number: 10
Issue Number: 4
Column Tag: Getting Started
Finally - Color Animation!!! 
Flying color bits around flicker-free
By Dave Mark, MacTech Magazine Regular Contributing Author
Note: Source code files accompanying article are located on MacTech CD-ROM orsource code disks.
A while ago, we got into bitmap animation using BitMapper. BitMapper was based
on QuickDraw’s BitMap data structure. Since BitMaps are always 1 pixel deep,
BitMapper was limited to black and white animation. This month, we’ll colorize
BitMapper, taking advantage of Color QuickDraw’s PixMap data structure.
PixMaps and GWorlds
The PixMap is much more complex than the BitMap. With BitMap animation, all
you had to do was fill out the relatively simple BitMap structure, create a GrafPort,
then connect the two via a call to SetPortBits(). Once that’s done, you are ready to copy
the BitMap from port to port via a call to CopyBits(). If you need a little refresher,
check back to the BitMapper column (September, ‘93).
On the other hand, PixMaps are complicated beasties. Pop open your copy of
Inside Macintosh, Volume V (or THINK Reference, if you prefer) and look up the
PixMap data structure. To create a PixMap by hand, you’d have to initialize all of those
fields. Notice the pmTable field, listed as a handle to a color table (also known as a
color lookup table, or CLUT). In addition to the PixMap fields, you’ve also got to create
and initialize a ColorTable structure. Look it up.
This stuff is non-trivial. Fortunately, the Toolbox offers a high-level set of
functions that make the creation of off-screen PixMaps relatively simple. These
off-screen PixMaps are known as GWorlds. Off-screen simply means that you are
creating something that is not drawn in a window on the screen. GWorlds are created in
memory and typically drawn to a window via a call to CopyBits(), just as we did with
BitMaps in BitMapper.
PixMapper
This month’s program is called PixMapper. PixMapper creates a window the size
of the main screen and fills it in with a randomly generated sequence of colored
squares. Next, PixMapper loads a PICT resource and uses a series of GWorlds to
smoothly animate the PICT over the colored background.
As usual, we’ll create the program this month, then go over the program details
in next month’s column.
Creating the PixMapper Resources
Start by creating a folder named PixMapper inside your Development folder. Fire
up ResEdit and create a file named PixMapper.π.rsrc inside your PixMapper folder.
Now create an ALRT resource (along with a corresponding DITL resource) for our
error alert. The ALRT resource should have a Top: of 40, a Left: of 30, a Height: of
116, and a Width: of 292. Be sure to set the DITL ID: to 128.
Next, create a DITL with an id of 128. Use the specifications in Figure 2 to create
the OK button and the specifications in Figure 3 to create the error alert’s static text
item.
Figure 2. Specifications for the error alert’s OK button.
Figure 3. Specifications for the error alert’s static text item.
Next, create an MBAR resource with an id of 128. The MBAR should list four
MENU ids: 128, 129, 130 and 131. Create four MENU resources according to the
specifications in Figure 4. Be sure to include a separator line as the second item in the
File menu. Also, be sure to mark the appropriate items in the Colors menu with a
check mark (). Use the popup menu in the Mark: field to do this.
Figure 4. The four MENU resources.
Finally, go into the scrapbook (or your favorite graphics application) and use
Copy and Paste to create a PICT resource in the resource file. Be sure that the PICT has
a resource id of 128. When choosing your PICT resource, here’s a couple of things to
keep in mind. First of all, smaller is probably better. A smaller picture takes up less
memory and contains less pixels to copy around in memory. Something around the size
of a color icon is probably a good size to start with.
Next, you might want to create a picture that is non-rectangular or that has a
hole in it, then use the lasso tool to select only the pixels in the picture (and not the
background). Though a rectangular picture will work just fine, a non-rectangular
picture (like an X or an O shape) produces much more impressive results.
Creating the PixMapper Project
Save your changes and quit ResEdit. Launch THINK C and create a new project
named PixMapper.π in the PixMapper folder. Add MacTraps to the project. Next,