home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 January / macformat-020.iso / Shareware City / Developers / ACL / Examples / FunLines / FunLines.cp next >
Encoding:
Text File  |  1994-10-01  |  6.3 KB  |  318 lines  |  [TEXT/MPCC]

  1. /********************************************
  2.  **** Animation Class Library V1.0 © 1994 Yves Schmid & Alia Development
  3.  ****
  4.  **** FunLines.cp
  5.  ****
  6.  **** Created:      27 May 1994
  7.  **** Modified:     20 June 1994
  8.  **** Version:      0
  9.  **** Compatible:   C++, Mac System 7
  10.  ****
  11.  **** Description:  Animation demo developed using ACL.
  12.  ****
  13.  *******************/
  14.  
  15.  
  16. #include "AnimBase.h"
  17.  
  18. //*************************************
  19.  
  20. unsigned short RangedRdm( unsigned short min, unsigned short max );
  21.  
  22.  
  23. //*************************************
  24.  
  25. const long PICT_BACKGROUND = 128;
  26.  
  27. const long PICT_ANIM = 129;
  28. const short NANIMFRAMES = 25;    // Number of frames in the animation
  29.  
  30. const short NANIMOBJECTS = 8;    // Number of animation objects (try to change it for fun!!!)
  31.  
  32.  
  33. //*************************************
  34. // For the definition of animation:
  35.  
  36. AnimFrameDef animdef[NANIMFRAMES];
  37.  
  38.  
  39. //*************************************
  40.  
  41. AnimBase *animbase;            // The central animation class
  42. AnimGfx *background;        // Pointer on background
  43.  
  44. AnimControl    controls[NANIMOBJECTS][2];        // Animation controls, two controls for each
  45.                                             // animation. First control to go to the mouse
  46.                                             // click, second control to go at a random position
  47.                                             // out of the visual part of the window. This controls
  48.                                             // are used only when the user clicks in the window.
  49.                             
  50.  
  51. Anim *anims[NANIMOBJECTS];    // Pointer on animation objects
  52.  
  53.  
  54. //*************************************
  55.  
  56. void InitToolbox()
  57. {
  58.     InitGraf((Ptr) &qd.thePort);
  59.     InitFonts();
  60.     InitWindows();
  61.     InitMenus();
  62.     FlushEvents(everyEvent,0);
  63.     TEInit();
  64.     InitDialogs(0L);
  65.     InitCursor();
  66.  
  67.     GetDateTime((unsigned long*)&qd.randSeed);    // Initializes the random number generator
  68. }
  69.  
  70. //*************************************
  71.  
  72. void randomoutplace(Anim *anim, short *x, short *y)    // Find a random position out of the screen
  73. {
  74.     short border;
  75.     short aw,ah,bw,bh;
  76.  
  77.     bw = background->getwidth();
  78.     bh = background->getheight();
  79.     anim->findmaxsize(&aw,&ah);
  80.  
  81.     border = RangedRdm(0,3);
  82.  
  83.     switch(border)
  84.     {
  85.         case 0:        // Left
  86.         *x = -aw;
  87.         *y = RangedRdm(0,bh);
  88.         break;
  89.  
  90.         case 1:        // Right
  91.         *x = bw;
  92.         *y = RangedRdm(0,bh);
  93.         break;
  94.  
  95.         case 2:        // Top
  96.         *x = RangedRdm(0,bw);
  97.         *y = -ah;
  98.         break;
  99.  
  100.         default:    // Bottom
  101.         *x = RangedRdm(0,bw);
  102.         *y = bh;
  103.         break;    
  104.     }
  105. }
  106.  
  107. //*************************************
  108.  
  109. void processmouseclick(short x, short y)
  110. {
  111.     int i;
  112.     short rx,ry;
  113.  
  114.     for(i=0;i<NANIMOBJECTS;i++)
  115.     {
  116.         anims[i]->stopcontrol();
  117.  
  118.         controls[i][0].next = &controls[i][1];    // Links the two controls
  119.         controls[i][1].next = NULL;
  120.  
  121.         controls[i][0].cmd = acmd_goto;
  122.         controls[i][0].x = x;
  123.         controls[i][0].y = y;
  124.         controls[i][0].speed = RangedRdm(1,3);         
  125.         controls[i][0].acceleration = ((float)RangedRdm(1,20))/10;    
  126.  
  127.         
  128.         randomoutplace(anims[i], &rx, &ry);
  129.  
  130.         controls[i][1].cmd = acmd_goto;
  131.         controls[i][1].x = rx;
  132.         controls[i][1].y = ry;
  133.         controls[i][1].speed = RangedRdm(1,3);            
  134.         controls[i][1].acceleration = ((float)RangedRdm(1,20))/10;    
  135.  
  136.         anims[i]->runcontrol(controls[i]);
  137.     }
  138. }
  139.  
  140. //*************************************
  141.  
  142. void InitAnim()
  143. {    
  144.     int i;
  145.     short w,x,y;
  146.  
  147.     // Fills definitions
  148.     for(i=0;i<NANIMFRAMES;i++)
  149.     {
  150.         animdef[i].pictRESID = PICT_ANIM+i;
  151.         animdef[i].changecounter = 1;
  152.         animdef[i].cmd = afcmd_frame;        
  153.     }
  154.     
  155.     animdef[i-1].cmd = afcmd_endanim;    // Don't forget to mark the end of the definition!
  156.  
  157.  
  158.     // Builds background
  159.     background = new AnimGfx;
  160.     background->createbypict(PICT_BACKGROUND);    // Gets background in the pict resource
  161.  
  162.     // Builds AnimBase
  163.     animbase = new AnimBase;    
  164.  
  165.     // Installs background
  166.     animbase->installbackground(background);
  167.     
  168.     // Creates an offscreen buffer to stop flickering, same size as the background size
  169.     animbase->buildbufferback();
  170.  
  171.     for(i=0;i<NANIMOBJECTS;i++)
  172.     {
  173.         if (i==NANIMOBJECTS/2)
  174.         {
  175.             AnimMask *mask = new AnimMask(animbase,154);
  176.             mask->place(67,42);
  177.         }
  178.  
  179.         // Creates animations
  180.         anims[i] = animbase->createanim(animdef);
  181.         anims[i]->setanimflags(ANF_PINGPONG);
  182.     
  183.         // We don't want to see synchronous animations
  184.         anims[i]->setcurrentframe(RangedRdm(0,NANIMFRAMES-1));
  185.  
  186.         // At the start of the demo, we place animations out of the visual part of the window
  187.         randomoutplace(anims[i], &x, &y);
  188.         anims[i]->place(x,y);
  189.     }
  190. }
  191.  
  192. //*************************************
  193.  
  194. void Cleanup()
  195. {
  196.     delete animbase;
  197.     delete background;
  198. }
  199.  
  200. //*************************************
  201.  
  202. void waitwindow(Boolean openw)
  203. {
  204.     static WindowPtr    ww;
  205.     Rect                windowRect;    
  206.  
  207.     if (openw)
  208.     {
  209.  
  210.         SetRect(&windowRect,100,100,375,118);
  211.  
  212.         ww = NewCWindow(NULL,&windowRect,NULL,TRUE,
  213.                         dBoxProc,(WindowPtr)-1,FALSE,0);
  214.  
  215.         SetPort(ww);
  216.         MoveTo(40,12);
  217.         TextFont(systemFont);
  218.         DrawString("\pPlease wait, initializing...");
  219.     }
  220.     else
  221.     {
  222.         DisposeWindow(ww);
  223.     }
  224. }
  225.  
  226. //*************************************
  227.  
  228.  
  229. main()
  230. {
  231.     WindowPtr     aWindow;
  232.     Rect        windowRect;
  233.     Boolean        done = FALSE;
  234.     EventRecord    theEvent;
  235.     WindowPtr    whichWindow;
  236.     short        part;
  237.  
  238.     InitToolbox();
  239.  
  240.     waitwindow(TRUE);
  241.  
  242.     InitAnim();
  243.     
  244.     SetRect(&windowRect,100,100,200,200);
  245.     aWindow = NewCWindow(NULL,&windowRect,"\pACL Demo by Yves Schmid",FALSE,
  246.                         noGrowDocProc,(WindowPtr)-1,TRUE,0);
  247.                                 
  248.     SetPort(aWindow);
  249.     SizeWindow(aWindow,background->getwidth(),background->getheight(),TRUE);
  250.  
  251.     waitwindow(FALSE);
  252.  
  253.     ShowWindow(aWindow);
  254.     
  255.     while(!done)
  256.     {
  257.         animbase->update();
  258.  
  259.         if (WaitNextEvent(everyEvent,&theEvent,0,NULL))
  260.         {
  261.             switch(theEvent.what)
  262.             {
  263.                 case updateEvt:
  264.                 whichWindow = (WindowPtr)theEvent.message;
  265.                 if (whichWindow==aWindow)
  266.                 {
  267.                     BeginUpdate(whichWindow);
  268.                     animbase->updatewindow();
  269.                     EndUpdate(whichWindow);
  270.                 }
  271.                 break;
  272.             
  273.                 case mouseDown:
  274.                 part = FindWindow(theEvent.where,&whichWindow);
  275.                 if (whichWindow==aWindow)
  276.                 {
  277.                     switch(part)
  278.                     {
  279.                         case inGoAway:
  280.                         done = TrackGoAway(whichWindow,theEvent.where);
  281.                         break;
  282.                         
  283.                         case inDrag:
  284.                         DragWindow(whichWindow,theEvent.where,&qd.screenBits.bounds);
  285.                         break;
  286.                     
  287.                         case inContent:
  288.                         GlobalToLocal(&theEvent.where);
  289.                         processmouseclick(theEvent.where.h, theEvent.where.v);
  290.                         break;
  291.                     }
  292.                 }
  293.                 break;
  294.             }
  295.         }
  296.     
  297.     }
  298.  
  299.     DisposeWindow(aWindow);
  300.     Cleanup();
  301.     return 0;
  302. }
  303.  
  304.  
  305.  
  306. //*************************************
  307.  
  308. unsigned short RangedRdm( unsigned short min, unsigned short max )
  309. {
  310.     unsigned short qdRdm;
  311.     unsigned long    range, t;
  312.  
  313.     qdRdm = Random();
  314.     range = max - min;
  315.     t = (qdRdm * range) / 65536;
  316.     return( t+min );
  317. }
  318.