Life
Volume Number: 3
Issue Number: 9
Column Tag: Macintosh II
Life, Quickdraw & The Picker
By Steven Sheets, Contributing Editor, Hoffman Estates,. IL
Steve wins our Program of the Month award for his excellent treatment of the
whole subject of color quickdraw as presented in this example program. Enjoy an extra
$50 on MacTutor, and thanks for sharing the technology!
Color Life
Life is one of the oldest computer “Games”. Besides being an extremely
interesting mathematical puzzle, even a black and white Life program can provide
hypnotising graphic animation. It is almost appropriate that this column’s first Color
Quickdraw example is this classic program. Using the full color of the Macintosh //,
Life is more spell binding than ever! [Yeah, now if Apple would ever ship us some
color monitors maybe we’ll see what this program can really do some day! -Ed]
This article will detail the development of a Color Quickdraw program, Color
Life. It will explain how to check for Color Quickdraw, how create Color
Grafports/Windows, how the Color RGB Model works, how Color Drawing works, how
to use the Color Picker Package and how to create Pop Up Menus. First however it will
explain what the game of Life is about....
Game of Life
Life simulates the life and death of a group of cells from one generation to the
next. Usually the goal is to find some stable life form (ie. one that will not die). Life
was originally designed by Prof. John Conway at the University of Cambridge. It first
appeared in Martin Gardner’s “Mathematical Games” column of Scientific American in
October 1970. Scientific American and Byte Magazine are two of the main sources of
good articles on Life.
The rules of Life are simple. The game is played on a two dimensional grid of a
certain size. Each spot (or cell) on the grid can be alive or dead (empty). Also each
cell is surrounded by it’s 8 neighboring cells, which can also be alive or dead. The
number of living cells around the center cell becomes very important in calculating
deaths and births each turn.
Every turn (usually called a generation), a living cell can die or live on to the
next generation. Also a empty cell can have a birth (ie. cell becomes alive). If a live
cell is surrounded by 2 or 3 cells this generation, it will survive till the next
generation. If a live cell is surrounded by 1 or less cells this generation, it dies (from
starvation). If the cell is surrounded by 4 or more cells this generation, it dies (from
over crowding). Finally if a empty cell is surrounded by exactly 3 living neighbors,
there is a birth there. Next generation a new cell exists at that spot.
From these simple rules, Life emerges.
Fig. 1 Color Quickdraw (cmd-shift-3 only works in black and white!)
Color QuickDraw
The very first thing a Macintosh // Color program has to check for is what
environment the program is running in. The program is not actually looking for a
Macintosh //; it requires Color Quickdraw to run. While the Macintosh // is now the
only computer with Color Quickdraw, it may not be in the future (upgraded Mac //,
Mac SE with Color Quickdraw expansion card or even a Mac ///).
No matter what the computer, if Color Quickdraw exists on the machine, the two
high bits of the low-memory global ROM85 (word at $028E) will be cleared (set to
zero). By examining this memory location, a program can discover if Color Quickdraw
exists. The check for Color Quickdraw should be done after the normal Mac
Initialization (InitGraf, InitFont, InitWindow, etc.), but before any Color Quickdraw
commands are called (creating Color Grafports or color Pixel Patterns). If Color
Quickdraw does not exist, the program should politely inform the user that he needs a
Macintosh //. A system bomb is not a polite way of informing the user!
Color Grafports & Color Windows
Once the program knows Color Quickdraw exists, it can create and work with
Color Grafports. All the new Color Quickdraw commands must be done on a Color
Grafport, not an old-style Grafport. All the older Quickdraw commands have been
expanded so they can work with either a Grafport or a Color Grafport (also called
cGrafport). The cGrafport data structure is the same size as the older Grafport
structure. While many data fields are the same, some have been changed. The exact
format is not important for this program since none of the fields are accessed directly.
The new cGrafports are allocated in manners similar to the old Grafports (either a
NewPtr call or a pointer to a holding Variable). However, instead of using the
Quickdraw commands OpenPort, InitPort and ClosePort, the new Color Quickdraw
commands OpenCPort, InitCPort and CloseCPort are used to open, init and close a Color
Grafport.
In practise, the new Color Quickdraw Port commands are used as often as the
older Quickdraw Port commands; that is almost never. The vast majority of the
Macintosh programs draw on a Window (which keeps track of the Grafport itself).
Generally the only time a Macintosh program directly manipulates a Grafport is when
using an off screen bitmap.
The Window Manager has been expanded to include Color Windows. The changes in
the Window Manager are similar to the changes in Color Quickdraw. There is a new
Color Window Record of the same size as the old Window Record. All of the field are the
same except the Port field now contains a cGrafPort instead of a Grafport. All existing
Window Manager calls have been expanded to so that either a Window Pointer or a
Color Window Pointer can be used with them. Where the NewWindow or
GetNewWindow command was used to create a window, the NewCWindow and
GetNewCWindow commands create a color window. The two new commands even have
the same parameters and purpose (create a window from scratch or from a window
resource) as the old commands. They just return a Color Window instead (complete
with a correctly created Color Grafport).
RGB Color
To understand exactly how Color Drawing is handled on the newly created Color
Grafport/Windows, the RGB color model must be reviewed. Color Quickdraw uses a
RGB color model as an conceptional model for all drawing. A color is defined as 3
non-signed integer values (0-65535). This defines the strength of the Red, Green and
Blue portions of the color. A Black color would have the values 0,0,0, while white
would have 65535, 65535, 65535. Blue would be 0, 65535, 0, while Purple would
be 65535, 0, 65535. There are 1,777,216 distinct colors in this model. When
something is conceptionally drawn, it must be in a color defined by this model.
In reality, the number of colors that can be drawn at one time, is dependent on
the pixel depth of the color device. Most graphics devices are video card, but they can
be almost anything (printer, off screen bit map, etc.). A color device that can allocate
4 bits of memory for each pixel (pixel depth of 4), can use 16 colors at one time (2 to
the 4th power). A color device that has pixel depth of 8, can have 256 colors at one
time (2 to the 8th power). Remember that usually the user can set what pixel depth
the video card is using by using the Monitor portion of the Control Panel. A 8 pixel
depth card may be only using 4, 2 or even 1 pixels at a specific moment. When color
Quickdraw tries to draw in a specific RGB color, it uses the Color Manager to find the
closest match on the graphics device. That is the color that is actually displayed.
A good example of this would be trying to display a light, medium and dark shade
of red. A video card which has been set (and as enough memory) for a pixel depth of 8
would probably have a few shades of red. Even if the shades were not the exact RGB
colors requested, the user would at least know that one shade was darker and one shade
lighter than regular red when the colors were displayed on the screen. Suppose then
the user set the video card pixel level to 4, thus only having 16 colors at one time.
There most likely would only be one red color being used by the video card at one time.
Chances are that the programs 3 red RGB shades would match to the cards single red
color (it would be the closest match for any of them). The user would then not be able