home *** CD-ROM | disk | FTP | other *** search
Text File | 1994-10-01 | 7.9 KB | 357 lines | [TEXT/MPCC] |
- /********************************************
- **** Animation Class Library V1.0 © 1994 Yves Schmid & Alia Development
- ****
- **** FunCurves.cp
- ****
- **** Created: 28 May 1994
- **** Modified: 16 August 1994
- **** Version: 0
- **** Compatible: C++, Mac System 7
- ****
- **** Description: Animation demo developped using ACL.
- ****
- *******************/
-
-
- #include "AnimScrollBase.h"
-
- //*************************************
-
- unsigned short RangedRdm( unsigned short min, unsigned short max );
-
-
- //*************************************
-
- const long PICT_BACKGROUND = 128;
-
- const long PICT_ANIM = 129;
- const long PICT_CLICK = 154;
-
- const short NANIMFRAMES = 25; // Number of frames in the animation
-
- const short NANIMOBJECTS = 8; // Number of animation objects (try to change it for fun!!!)
-
- const short NCLICKFRAMES = 7; // Number of frames in the click animation
-
-
- //*************************************
- // For the definition of animation:
-
- AnimFrameDef animdef[NANIMFRAMES];
- AnimFrameDef animclickdef[NCLICKFRAMES*2] ={{PICT_CLICK ,NULL,1,0,0,afcmd_frame},
- {PICT_CLICK+1,NULL,1,0,0,afcmd_frame},
- {PICT_CLICK+2,NULL,1,0,0,afcmd_frame},
- {PICT_CLICK+3,NULL,1,0,0,afcmd_frame},
- {PICT_CLICK+4,NULL,1,0,0,afcmd_frame},
- {PICT_CLICK+5,NULL,1,0,0,afcmd_frame},
- {PICT_CLICK+6,NULL,1,0,0,afcmd_frame},
- {PICT_CLICK+6,NULL,1,0,0,afcmd_frame},
- {PICT_CLICK+5,NULL,1,0,0,afcmd_frame},
- {PICT_CLICK+4,NULL,1,0,0,afcmd_frame},
- {PICT_CLICK+3,NULL,1,0,0,afcmd_frame},
- {PICT_CLICK+2,NULL,1,0,0,afcmd_frame},
- {PICT_CLICK+1,NULL,1,0,0,afcmd_frame},
- {PICT_CLICK, NULL,1,0,0,afcmd_endanim}};
-
-
-
- //*************************************
-
- AnimScrollBase *animbase; // The central animation class
- AnimGfx *background; // Pointer on background
-
- AnimControl controls[NANIMOBJECTS][2]; // Animation controls, two controls for each
- // animation. First control to go to the mouse
- // click, second control to go at a random position
- // out of the visual part of the window. This controls
- // are used only when the user clicks in the window.
-
-
- Anim *anims[NANIMOBJECTS]; // Pointer on animation objects
-
-
- //*************************************
-
- void InitToolbox()
- {
- InitGraf((Ptr) &qd.thePort);
- InitFonts();
- InitWindows();
- InitMenus();
- FlushEvents(everyEvent,0);
- TEInit();
- InitDialogs(0L);
- InitCursor();
-
- GetDateTime((unsigned long*)&qd.randSeed); // Initializes the random number generator
- }
-
- //*************************************
-
- void randomoutplace(Anim *anim, short *x, short *y) // Find a random position out of the screen
- {
- short border;
- short aw,ah,bw,bh;
-
- bw = background->getwidth();
- bh = background->getheight();
- anim->findmaxsize(&aw,&ah);
-
- border = RangedRdm(0,3);
-
- switch(border)
- {
- case 0: // Left
- *x = -aw;
- *y = RangedRdm(0,bh);
- break;
-
- case 1: // Right
- *x = bw;
- *y = RangedRdm(0,bh);
- break;
-
- case 2: // Top
- *x = RangedRdm(0,bw);
- *y = -ah;
- break;
-
- default: // Bottom
- *x = RangedRdm(0,bw);
- *y = bh;
- break;
- }
- }
-
- //*************************************
-
- void processmouseclick(short x, short y)
- {
- int i;
- short rx,ry,w,h;
- static AnimControl clickcontrols[2];
- Anim *clickanim;
-
- for(i=0;i<NANIMOBJECTS;i++)
- {
- anims[i]->stopcontrol();
-
- controls[i][0].next = &controls[i][1]; // Links the two controls
- controls[i][1].next = NULL;
-
- controls[i][0].cmd = acmd_curve;
- controls[i][0].x = x;
- controls[i][0].y = y;
- controls[i][0].cx = RangedRdm(0,background->getwidth()-20);
- controls[i][0].cy = RangedRdm(0,background->getheight()-20);
- controls[i][0].speed = 8;
- controls[i][0].acceleration = RangedRdm(0,1);
-
-
- randomoutplace(anims[i], &rx, &ry);
-
- controls[i][1].cmd = acmd_curve;
- controls[i][1].x = rx;
- controls[i][1].y = ry;
- controls[i][1].cx = RangedRdm(0,background->getwidth()-20);
- controls[i][1].cy = RangedRdm(0,background->getheight()-20);
- controls[i][1].speed = RangedRdm(1,3);
- controls[i][1].acceleration = RangedRdm(1,40)/10;
-
- anims[i]->runcontrol(controls[i]);
- }
-
- clickanim = animbase->createanim(animclickdef); // Creates the "click" animation
- clickanim->findmaxsize(&w,&h);
- clickanim->place(x-(w/2),y-(h/2)); // Centers animation
-
- clickcontrols[0].next = &clickcontrols[1]; // Links the two controls
- clickcontrols[1].next = NULL;
-
- clickcontrols[0].cmd = acmd_waitnframes; // Waits the last frame
- clickcontrols[0].wait = (NCLICKFRAMES*2);
-
- clickcontrols[1].cmd = acmd_delete; // Deletes the anim when the last frame is reached
-
- clickanim->runcontrol(clickcontrols);
-
- }
-
- //*************************************
-
- void InitAnim()
- {
- int i;
- short w,x,y;
-
- // Fills definitions
- for(i=0;i<NANIMFRAMES;i++)
- {
- animdef[i].pictRESID = PICT_ANIM+i;
- animdef[i].changecounter = 1;
- animdef[i].cmd = afcmd_frame;
- }
-
- animdef[i-1].cmd = afcmd_endanim; // Don't forget to mark the end of the definition!
-
-
- // Build background
- background = new AnimGfx;
- background->createbypict(PICT_BACKGROUND); // Get background in the pict resource
-
- // Build AnimBase
- animbase = new AnimScrollBase;
-
- // Install background
- animbase->installbackground(background);
-
- // Create an offscreen buffer to stop flickering, same size as the background size
- animbase->buildbufferback();
-
- for(i=0;i<NANIMOBJECTS;i++)
- {
-
- // Create animations
- anims[i] = animbase->createanim(animdef);
-
- // We don't want to see synchronous animations
- anims[i]->setcurrentframe(RangedRdm(0,NANIMFRAMES-1));
-
- // At the start of the demo, we place animations out of the visual part of the window
- randomoutplace(anims[i], &x, &y);
- anims[i]->place(x,y);
- }
-
- animbase->sethscrollvect(2);
- animbase->setvscrollvect(1);
-
-
- // Preload the pictures of the "click" animation in memory
- animbase->preload_framedef(animclickdef);
- }
-
- //*************************************
-
- void Cleanup()
- {
- delete animbase;
- delete background;
- }
-
-
- //*************************************
-
- void waitwindow(Boolean openw)
- {
- static WindowPtr ww;
- Rect windowRect;
-
- if (openw)
- {
-
- SetRect(&windowRect,100,100,375,118);
-
- ww = NewCWindow(NULL,&windowRect,NULL,TRUE,
- dBoxProc,(WindowPtr)-1,FALSE,0);
-
- SetPort(ww);
- MoveTo(40,12);
- TextFont(systemFont);
- DrawString("\pPlease wait, initializing...");
- }
- else
- {
- DisposeWindow(ww);
- }
- }
-
- //*************************************
-
-
- main()
- {
- WindowPtr aWindow;
- Rect windowRect;
- Boolean done = FALSE;
- EventRecord theEvent;
- WindowPtr whichWindow;
- short part;
-
- InitToolbox();
-
- waitwindow(TRUE);
- InitAnim();
-
- SetRect(&windowRect,100,100,200,200);
- aWindow = NewCWindow(NULL,&windowRect,"\pACL Demo by Yves Schmid",FALSE,
- noGrowDocProc,(WindowPtr)-1,TRUE,0);
-
- SetPort(aWindow);
- SizeWindow(aWindow,background->getwidth(),background->getheight(),TRUE);
-
- waitwindow(FALSE);
- ShowWindow(aWindow);
-
- while(!done)
- {
- animbase->update();
- if (WaitNextEvent(everyEvent,&theEvent,0,NULL))
- {
- switch(theEvent.what)
- {
- case updateEvt:
- whichWindow = (WindowPtr)theEvent.message;
- if (whichWindow==aWindow)
- {
- BeginUpdate(whichWindow);
- animbase->updatewindow();
- EndUpdate(whichWindow);
- }
- break;
-
- case mouseDown:
- part = FindWindow(theEvent.where,&whichWindow);
- if (whichWindow==aWindow)
- {
- switch(part)
- {
- case inGoAway:
- done = TrackGoAway(whichWindow,theEvent.where);
- break;
-
- case inDrag:
- DragWindow(whichWindow,theEvent.where,&qd.screenBits.bounds);
- break;
-
- case inContent:
- GlobalToLocal(&theEvent.where);
- processmouseclick(theEvent.where.h, theEvent.where.v);
- break;
- }
- }
- break;
- }
- }
-
- }
-
- DisposeWindow(aWindow);
- Cleanup();
- return 0;
- }
-
-
-
- //*************************************
-
- unsigned short RangedRdm( unsigned short min, unsigned short max )
- {
- unsigned short qdRdm;
- unsigned long range, t;
-
- qdRdm = Random();
- range = max - min;
- t = (qdRdm * range) / 65536;
- return( t+min );
- }
-