home *** CD-ROM | disk | FTP | other *** search
Wrap
Text File | 1993-03-31 | 9.8 KB | 267 lines | [ TEXT/PJMM]
{The following program demonstrates most of the routines created in the unit CellusoftAnimations. } {} {What does the program do?} {The application created by this program will display a window of size 300 X 300, with a picture taken from} {PICT resource ID 130 and animate a bunch of rotating eyeballs, each of which will rotate from one side of the} {window to the other continuously. The graphics for the eyeballs are from PICT ID 128 and the masks are from} {PICT ID 129. In order to gain fuller understanding of the program, you may wish to look through the resource} {file used by the program.} {} {Animation Project© 1993 Tony Small All Rights Reserved WorldWide} {} {Many parts of the program do not directly pertain to routines in CelluSoftAnimations, but all is thoroughly } {documented to give further insight into graphics, mac design and animation programming.} program Animation_Project; uses CelluSoftAnimations; const MaxEyes = 6; {This number specifies the number of eyeballs that will roll across the window.} {This number should not be bigger than the MaxAnimations constant in the CellusoftAnimations unit.} type eyeballs = record currentRect, lastRect, pictRect: rect; {These rects are required by the animation routines...} {currentRect: Where the eyeball is currently on the screen.} {lastRect: Where the eyeball was on the screen in the last animation loop.} {pictRect: Where the picture of the current eyeball is located in the Eyeball port.} step: integer; {Step specifies at what stage the eyeball is in its movement.} goingLeft: boolean; {If this is true, then the eyeball is moving left. If not, then right.} end; var MainWindow: WindowPtr; {The variable for the main window.} PureBack, Eyeball, Eyeballmask, offLoad: GrafPtr; {The variables for the ports.} eyeBallArray: array[1..MaxEyes] of eyeballs; {The array of eyeball records.} function Randomize (range: Integer): Integer; {This function gives a random number from 1 to range.} var rawResult: Longint; begin rawResult := Random; rawResult := abs(rawResult); Randomize := (rawResult * range) div 32768 + 1; end; procedure InitializeEyes; {This procedure initializes all variables of all eyeball records in the eyeballArray.} {It sets the currentRect for each eyeball at a random rect within the bounds of the window.} {It sets the step to 1 so each eyeball will start out flat on the screen.} {It randomly sets the goingleft boolean variable.} {It sets the pictRect to the area in the eyeball port that holds the flat eyeball picture.} var i: integer; begin for i := 1 to MaxEyes do {Do the following for each eyeball.} with eyeBallArray[i] do begin currentRect.left := Randomize(300 - 19); currentRect.top := Randomize(300 - 10); currentRect.right := currentRect.left + 19; currentRect.bottom := currentRect.top + 10; lastRect := currentRect; step := 1; if Randomize(2) = 1 then goingLeft := true else goingLeft := false; setRect(pictRect, 0, 0, 19, 10); end; end; procedure CalcEyeBall (num: integer); {This lengthly procedure does not pertain to specific animation routines. It merely sees at what step the specified} {eyeball is at, then changes the step, currentrect, and pictRect accordingly.} {One also very important thing this procedure does is set lastRect to the currentRect BEFORE the new currentRect is set.} procedure ChangeEye (num, newIn, left, top, right, bottom, mleft, mtop, mright, mbottom: integer); begin with eyeBallArray[num] do begin setRect(pictRect, left, top, right, bottom); moverect(currentRect, mleft, mtop, mright, mbottom); step := newIn; end; end; begin with eyeBallArray[num] do begin lastRect := currentRect; {Very important!!!!} case step of 1: if goingLeft then {Determine which direction the eyeball is going.} ChangeEye(num, 20, 0, 11, 19, 23, 0, -2, 0, 0) else ChangeEye(num, 40, 0, 167, 19, 179, 0, -2, 0, 0); 20: ChangeEye(num, 21, 0, 24, 18, 38, 1, -2, 0, 0); {The above for example does the following:} {If the step equals 20, then change the step to 21, change the PictRect to coordinates 0,24,18,38, and } {move the currentRect, one pixel over from the left and two pixels up from the top.} 21: ChangeEye(num, 22, 0, 39, 16, 56, 2, -3, 0, 0); 22: ChangeEye(num, 23, 0, 57, 14, 75, 2, -1, 0, 0); 23: ChangeEye(num, 24, 0, 76, 12, 95, 2, -1, 0, 0); 24: ChangeEye(num, 25, 0, 96, 11, 115, 1, 0, 0, 0); 25: ChangeEye(num, 26, 0, 116, 14, 134, 0, 1, 3, 0); 26: ChangeEye(num, 27, 0, 135, 17, 151, 0, 2, 3, 0); 27: ChangeEye(num, 28, 0, 152, 18, 166, 0, 2, 1, 0); 28: ChangeEye(num, 29, 0, 167, 19, 179, 0, 2, 1, 0); 29: begin if currentRect.right > 300 then {If the eyeball is at the edge of the screen, then change direction.} begin ChangeEye(num, 1, 0, 0, 19, 10, 0, 2, 0, 0); goingLeft := false; end else begin ChangeEye(num, 1, 0, 0, 19, 10, 0, 2, 0, 0); end; end; 40: ChangeEye(num, 41, 0, 152, 18, 166, 0, -2, -1, 0); 41: ChangeEye(num, 42, 0, 135, 17, 151, 0, -2, -1, 0); 42: ChangeEye(num, 43, 0, 116, 14, 134, 0, -2, -3, 0); 43: ChangeEye(num, 44, 0, 96, 11, 115, 0, -1, -3, 0); 44: ChangeEye(num, 45, 0, 76, 12, 95, -1, 0, 0, 0); 45: ChangeEye(num, 46, 0, 57, 14, 75, -2, 1, 0, 0); 46: ChangeEye(num, 47, 0, 39, 16, 56, -2, 1, 0, 0); 47: ChangeEye(num, 48, 0, 24, 18, 38, -2, 3, 0, 0); 48: ChangeEye(num, 49, 0, 11, 19, 23, -1, 2, 0, 0); 49: begin if currentRect.left < 0 then {If the eyeball is at the edge of the screen, then change direction.} begin ChangeEye(num, 1, 0, 0, 19, 10, 0, 2, 0, 0); goingleft := true; end else begin ChangeEye(num, 1, 0, 0, 19, 10, 0, 2, 0, 0); end; end; end; end; end; procedure DoTheEyes; {For each record in the eyeballarray, this procedure calls the above procedure, then} {installs the eyeball into the Animation array with setAnimation.} var i: integer; begin for i := 1 to MaxEyes do with eyeballarray[i] do begin CalcEyeBall(i); setAnimation(lastRect, currentRect, pictRect, eyeball, eyeballmask); {eyeball and eyeball mask are the two} {ports that have the eyeball and mask pictures.} end; end; function CenterWindow (width, height: integer): rect; {This function centers a rect on the screen with dimensions of width and height.} {This function is called when the Mainwindow is created.} var screenHeight, screenWidth, i: integer; begin with screenbits.bounds do begin screenWidth := right - left; {Get the exact pixel height and width of the screen.} screenHeight := bottom - top; end; tr[1].top := (screenHeight - height + 20) div 2; tr[1].left := (screenWidth - width) div 2; setRect(CenterWindow, tr[1].left, tr[1].top, tr[1].left + width, tr[1].top + height); end; procedure SetUpWindow; begin mainWindow := NewCWindow(nil, CenterWindow(300, 300), ' ', True, plainDBox, WindowPtr(-1), False, 0); {Create the plain window at the center of the screen.} setPort(mainWindow); {Set the port to this new window.} showWindow(mainWindow); {Reveal the window.} end; procedure InitializePorts; begin DoColorPort(PureBack, 130, 300, 300); {Create a color port with PICT ID 130 of dimensions 300 X 300} DoColorPort(offLoad, -1, 300, 300); {Create a blank color port of dimensions 300 X 300} DoColorPort(EyeBall, 128, 19, 179); {Create a color port with PICT ID 128 of dimensions 19 X 179} DoPort(EyeBallmask, 129, 19, 179); {Create a black and white port with PICT ID 129 of dimensions 19 X 179} end; procedure DrawBackGround; {This procedure copies the picture from port PureBack to the MainWindow.} begin copy(PureBack, MainWindow, PureBack^.portrect, MainWindow^.portrect); end; procedure DrawMessage; {This procedure draws the message 'Click the Mouse to Quit the Demonstration' at coordinates 5,270} {This message is written on both the pureback port and the MainWindow so that the eyeballs will not erase the message.} {if the eyballs and the message happens to collide.} var fontnumber: integer; thestring: str255; procedure WriteMessage (var TheGraf: Grafptr); begin setPort(TheGraf); GetFNum('Chicago', fontnumber); textfont(fontnumber); textsize(12); moveto(5, 270); drawstring('Click The Mouse To Quit The Demonstration.'); end; begin WriteMessage(pureback); WriteMessage(MainWindow); end; begin getDateTime(randseed); {Initialize the random number generator in the Macintosh system.} hidecursor; {Hide the cursor.} SetUpWindow; {Create the window.} InitializePorts; {Create the ports.} DrawBackGround; {Draw the picture onto the window} InitializeEyes; {Initialize the variables for the eyeballs.} setPort(MainWindow); {Set the current port to the main window.} DrawMessage; {Draw the message onto the screen.} while not button do {While the mouse button is not down, perform the following loop.} begin InitAnimations; {Reset the animations.} DoTheEyes; {Move the eyes and load them into the animation array.} AnimateArray(pureback, offLoad, MainWindow); {Animate the animation array.} {The preserved background graphic is in the port pureback.} {The port to be used for workspace is offLoad.} {The port to draw the animations to is the window MainWindow.} end; showcursor; {Show the cursor before exiting.} end.