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

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