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

  1. /********************************************
  2.  **** Animation Class Library V1.0 © 1994 Yves Schmid & Alia Development
  3.  ****
  4.  **** SimpleDemo.cp
  5.  ****
  6.  **** Created:      27 May 1994
  7.  **** Modified:     09 June 1994
  8.  **** Version:      0
  9.  **** Compatible:   C++, Mac System 7
  10.  ****
  11.  **** Description:  A simple demo of ACL.
  12.  ****
  13.  *******************/
  14.  
  15.  
  16. #include "AnimBase.h"
  17.  
  18. //*************************************
  19.  
  20. const long PICT_BACKGROUND = 128;
  21.  
  22. const long PICT_ANIM1 = 129;
  23. const long PICT_ANIM2 = 135;
  24.  
  25.  
  26. //*************************************
  27. // Definition of the two animation types:
  28.  
  29. AnimFrameDef anim1[] = {{PICT_ANIM1  ,NULL,1,0,0,afcmd_frame},
  30.                         {PICT_ANIM1+1,NULL,1,0,0,afcmd_frame},
  31.                         {PICT_ANIM1+2,NULL,1,0,0,afcmd_frame},
  32.                         {PICT_ANIM1+3,NULL,1,0,0,afcmd_frame},
  33.                         {PICT_ANIM1+4,NULL,1,0,0,afcmd_frame},
  34.                         {PICT_ANIM1+5,NULL,1,0,0,afcmd_endanim}};
  35.  
  36. AnimFrameDef anim2[] = {{PICT_ANIM2  ,NULL,1,0,0,afcmd_frame},
  37.                         {PICT_ANIM2+1,NULL,1,0,0,afcmd_frame},
  38.                         {PICT_ANIM2+2,NULL,1,0,0,afcmd_frame},
  39.                         {PICT_ANIM2+3,NULL,1,0,0,afcmd_frame},
  40.                         {PICT_ANIM2+4,NULL,1,0,0,afcmd_frame},
  41.                         {PICT_ANIM2+5,NULL,1,0,0,afcmd_endanim}};
  42.  
  43.  
  44. //*************************************
  45.  
  46. AnimBase *animbase;            // The central animation control
  47. AnimGfx *background;        // Pointer on background
  48.  
  49. AnimControl    controls[4];        // Four animation controls
  50. AnimControl gobackcontrols[4];    // Controls to place the animation at the left of the display
  51.                                 // when the animation is out of screen.
  52.  
  53. Anim *anims[4];    // Pointer on the four animations
  54.  
  55. //*************************************
  56.  
  57. Boolean    testanimout(AnimCObject *a, AnimControl *c)    // Used to test if the anim is out of screen.
  58. {
  59.     if (a->getx()>=background->getwidth()) return TRUE;
  60.  
  61.     return FALSE;
  62. }
  63.  
  64. //*************************************
  65.  
  66. void InitToolbox()
  67. {
  68.     InitGraf((Ptr) &qd.thePort);
  69.     InitFonts();
  70.     InitWindows();
  71.     InitMenus();
  72.     FlushEvents(everyEvent,0);
  73.     TEInit();
  74.     InitDialogs(0L);
  75.     InitCursor();
  76. }
  77.  
  78. //*************************************
  79.  
  80. void InitAnim()
  81. {    
  82.     int i;
  83.     short w;
  84.  
  85.     // Builds background
  86.     background = new AnimGfx;
  87.     background->createbypict(PICT_BACKGROUND);    // Gets background in the pict resource
  88.  
  89.     // Builds AnimBase
  90.     animbase = new AnimBase;    
  91.  
  92.     // Installs background
  93.     animbase->installbackground(background);
  94.     
  95.     // Creates an offscreen buffer to stop flickering, same size of the background
  96.     animbase->buildbufferback();
  97.  
  98.     // Creates animations
  99.     anims[0] = animbase->createanim(anim1);
  100.     anims[1] = animbase->createanim(anim1);
  101.     anims[2] = animbase->createanim(anim2);
  102.     anims[3] = animbase->createanim(anim2);
  103.  
  104.     // Ping-pong animation
  105.     anims[0]->setanimflags(ANF_PINGPONG);
  106.     anims[1]->setanimflags(ANF_PINGPONG);
  107.     anims[2]->setanimflags(ANF_PINGPONG);
  108.     anims[3]->setanimflags(ANF_PINGPONG);
  109.     
  110.     // We don't want to see synchronous animations
  111.     anims[0]->setcurrentframe(0);
  112.     anims[1]->setcurrentframe(2);
  113.     anims[2]->setcurrentframe(4);
  114.     anims[3]->setcurrentframe(3);
  115.     
  116.     // Installs controls
  117.     
  118.     for(i=0;i<4;i++)
  119.     {
  120.         anims[i]->findmaxsize(&w,NULL);
  121.         anims[i]->place(-w,i*20);                // Places animation
  122.     
  123.         controls[i].cmd = acmd_move;
  124.         controls[i].x = 8-(i*2);                // A different speed for each animation
  125.         controls[i].y = 0;
  126.         controls[i].next = &gobackcontrols[i];   // Control to check if the animation is out of
  127.                                                 // the screen
  128.         
  129.         gobackcontrols[i].cmd = acmd_place;
  130.         gobackcontrols[i].x = -w;
  131.         gobackcontrols[i].y = i*20;
  132.         gobackcontrols[i].testproc = &testanimout;
  133.         gobackcontrols[i].next = &controls[i];    // Do a loop
  134.         
  135.         anims[i]->runcontrol(&controls[i]);
  136.     }
  137. }
  138.  
  139. //*************************************
  140.  
  141. void Cleanup()
  142. {
  143.     delete animbase;
  144.     delete background;
  145. }
  146.  
  147. //*************************************
  148.  
  149. void waitwindow(Boolean openw)
  150. {
  151.     static WindowPtr    ww;
  152.     Rect                windowRect;    
  153.  
  154.     if (openw)
  155.     {
  156.  
  157.         SetRect(&windowRect,100,100,375,118);
  158.  
  159.         ww = NewCWindow(NULL,&windowRect,NULL,TRUE,
  160.                         dBoxProc,(WindowPtr)-1,FALSE,0);
  161.  
  162.         SetPort(ww);
  163.         MoveTo(40,12);
  164.         TextFont(systemFont);
  165.         DrawString("\pPlease wait, initializing...");
  166.     }
  167.     else
  168.     {
  169.         DisposeWindow(ww);
  170.     }
  171. }
  172.  
  173. //*************************************
  174.  
  175.  
  176. main()
  177. {
  178.     WindowPtr     aWindow;
  179.     Rect        windowRect;
  180.     Boolean        done = FALSE;
  181.     EventRecord    theEvent;
  182.     WindowPtr    whichWindow;
  183.     short        part;
  184.  
  185.     InitToolbox();
  186.  
  187.     waitwindow(TRUE);
  188.     InitAnim();
  189.     
  190.     SetRect(&windowRect,100,100,200,200);
  191.     aWindow = NewCWindow(NULL,&windowRect,"\pSimple Demo of ACL",FALSE,
  192.                         noGrowDocProc,(WindowPtr)-1,TRUE,0);
  193.                                 
  194.     SetPort(aWindow);
  195.     SizeWindow(aWindow,background->getwidth(),background->getheight(),TRUE);
  196.  
  197.  
  198.     waitwindow(FALSE);
  199.     ShowWindow(aWindow);
  200.     
  201.     while(!done)
  202.     {
  203.         animbase->update();
  204.  
  205.         if (WaitNextEvent(everyEvent,&theEvent,0,NULL))
  206.         {
  207.             switch(theEvent.what)
  208.             {
  209.                 case updateEvt:
  210.                 whichWindow = (WindowPtr)theEvent.message;
  211.                 if (whichWindow==aWindow)
  212.                 {
  213.                     BeginUpdate(whichWindow);
  214.                     animbase->updatewindow();
  215.                     EndUpdate(whichWindow);
  216.                 }
  217.                 break;
  218.             
  219.                 case mouseDown:
  220.                 part = FindWindow(theEvent.where,&whichWindow);
  221.                 if (whichWindow==aWindow)
  222.                 {
  223.                     switch(part)
  224.                     {
  225.                         case inGoAway:
  226.                         done = TrackGoAway(whichWindow,theEvent.where);
  227.                         break;
  228.                         
  229.                         case inDrag:
  230.                         DragWindow(whichWindow,theEvent.where,&qd.screenBits.bounds);
  231.                         break;
  232.                     }
  233.                 }
  234.                 break;
  235.             }
  236.         }
  237.     
  238.     }
  239.  
  240.     DisposeWindow(aWindow);
  241.     Cleanup();
  242.     return 0;
  243. }
  244.