home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Snippets / UNIMAC / source / UniMacMain.c < prev   
Encoding:
Text File  |  1993-03-15  |  4.5 KB  |  212 lines  |  [TEXT/KAHL]

  1.  
  2. //============================================================================
  3. //----------------------------------------------------------------------------
  4. //
  5. //                                    UNIMAC
  6. //
  7. //----------------------------------------------------------------------------
  8. //============================================================================
  9.  
  10.  
  11. //========================================================  Includes
  12.  
  13. #include <Palettes.h>
  14.  
  15. //========================================================  Defines
  16.  
  17. #define kNilPointer            0L
  18. #define ScreenDepth(gdh)    ((*((*gdh)->gdPMap))->pixelSize)
  19. #define MaxColors(gdh)    (1<<ScreenDepth(gdh))
  20.  
  21. //========================================================  Prototypes
  22.  
  23. void ToolBoxInit (void);
  24. void OpenMainWindow (void);
  25. void RedrawWindow (void);
  26. void main (void);
  27.  
  28. //========================================================  Global Variables
  29.  
  30. Rect            windowRect;
  31. WindowPtr        mainWindow;
  32. PaletteHandle    thePalette;
  33. CTabHandle        colorTable;
  34. Boolean            quitting;
  35.  
  36. //========================================================  Functions
  37. //--------------------------------------------------------  ToolBoxInit
  38.  
  39. void ToolBoxInit (void)
  40.  
  41. {
  42.     short        count;
  43.     
  44.     InitGraf(&thePort);
  45.     InitFonts();
  46.     FlushEvents(everyEvent, 0);
  47.     InitWindows();
  48.     InitMenus();
  49.     TEInit();
  50.     InitDialogs(kNilPointer);
  51.     InitCursor();
  52.     
  53.     MoreMasters();
  54.     MoreMasters();
  55. }
  56.  
  57. //--------------------------------------------------------  RandomInt
  58.  
  59. short RandomInt (short range)
  60.  
  61. {
  62.     register long    rawResult;
  63.     
  64.     rawResult = Random();
  65.     if (rawResult < 0)
  66.         rawResult *= -1;
  67.     return (rawResult * (long)range / 32768);
  68. }
  69.  
  70. //--------------------------------------------------------  OpenMainWindow
  71.  
  72. void OpenMainWindow (void)
  73.  
  74. {
  75.     #define            kCLUTResID        128
  76.     
  77.     colorTable = GetCTable(kCLUTResID);
  78.     
  79.     windowRect = screenBits.bounds;
  80.     windowRect.bottom -= 20;
  81.     mainWindow = kNilPointer;
  82.     mainWindow = NewCWindow(kNilPointer, &windowRect, "\pSample", TRUE, plainDBox, 
  83.             (WindowPtr)-1L, TRUE, 0L);
  84.     SetPort(mainWindow);
  85.     MoveWindow(mainWindow, 0, 20, TRUE);
  86.     
  87.     thePalette = NewPalette(32, colorTable, pmAnimated, 0x1500);
  88.     SetPalette((WindowPtr)mainWindow, thePalette, TRUE);
  89. }
  90.  
  91. //--------------------------------------------------------  RedrawWindow
  92.  
  93. void RedrawWindow (void)
  94.  
  95. {
  96.     Rect        theLight;
  97.     short        i, j, horiLights, vertLights;
  98.     
  99.     SetPort(mainWindow);
  100.     FillRect(&windowRect, black);
  101.     
  102.     horiLights = (screenBits.bounds.right - screenBits.bounds.left) / 32;
  103.     vertLights = (screenBits.bounds.bottom - screenBits.bounds.top) / 32 - 1;
  104.     
  105.     for (i = 0; i < horiLights; i++)
  106.     {
  107.         for (j = 0; j < vertLights; j++)
  108.         {
  109.             SetRect(&theLight, 0, 0, 32, 32);
  110.             OffsetRect(&theLight, i * 32, j * 32);
  111.             InsetRect(&theLight, 1, 1);
  112.             PmForeColor(RandomInt(30) + 1);
  113.             PaintRect(&theLight);
  114.         }
  115.     }
  116. }
  117.  
  118. //--------------------------------------------------------  BlinkLights
  119.  
  120. void BlinkLights (void)
  121.  
  122. {
  123.     RGBColor    takeColor, putColor;
  124.     short        taking, putting;
  125.     
  126.     taking = RandomInt(30) + 1;
  127.     putting = RandomInt(30) + 1;
  128.     
  129.     GetEntryColor (thePalette, taking, &takeColor);
  130.     GetEntryColor (thePalette, putting, &putColor);
  131.     
  132.     AnimateEntry (mainWindow, taking, &putColor);
  133.     AnimateEntry (mainWindow, putting, &takeColor);
  134.     
  135. //    Palette2CTab (thePalette, colorTable);
  136. }
  137.  
  138. //--------------------------------------------------------  main
  139.  
  140. void main (void)
  141.  
  142. {
  143.     Rect            newRectangle;
  144.     EventRecord        theEvent;
  145.     GDHandle        thisGDevice;
  146.     MenuHandle        appleMenu;
  147.     WindowPtr        whichWindow;
  148.     long            dummyLong, sleep = 10;
  149.     char            theKey;
  150.     
  151.     ToolBoxInit();
  152.     
  153.     appleMenu = GetMenu(128);
  154.     AddResMenu(appleMenu,'DRVR');
  155.     InsertMenu(appleMenu,0);
  156.     DrawMenuBar();
  157.     
  158.     thisGDevice = GetGDevice();
  159.     OpenMainWindow();
  160.     
  161.     quitting = FALSE;
  162.     
  163.     while (!quitting)
  164.     {
  165.         if (WaitNextEvent(everyEvent, &theEvent, sleep, kNilPointer))
  166.         {
  167.             switch (theEvent.what)
  168.             {
  169.                 case mouseDown:
  170.                 {
  171.                     if (FindWindow(theEvent.where, &whichWindow) == inMenuBar)
  172.                     {
  173.                         dummyLong = MenuSelect(theEvent.where);
  174.                         HiliteMenu(0);
  175.                     }
  176.                 }
  177.                 break;
  178.                 
  179.                 case keyDown:
  180.                 case autoKey:
  181.                 {
  182.                     theKey = BitAnd(theEvent.message, charCodeMask);
  183.                     if ((BitAnd(theEvent.modifiers, cmdKey) != 0) && (theKey == 'q'))
  184.                         quitting = TRUE;
  185.                 }
  186.                 break;
  187.                 
  188.                 case updateEvt:
  189.                 {
  190.                     if ((WindowPtr)theEvent.message == mainWindow)
  191.                     {
  192.                         SetPort(mainWindow);
  193.                         BeginUpdate((GrafPtr)theEvent.message);
  194.                         RedrawWindow();
  195.                         EndUpdate((GrafPtr)theEvent.message);
  196.                     }
  197.                 }
  198.                 break;
  199.                 default:
  200.                 {
  201.                 }
  202.             }
  203.         }
  204.         
  205.         BlinkLights();
  206.     }
  207.     
  208.     if (mainWindow != kNilPointer)
  209.         DisposeWindow(mainWindow);
  210.     RestoreDeviceClut(thisGDevice);
  211.     FlushEvents(everyEvent, 0);
  212. }