home *** CD-ROM | disk | FTP | other *** search
/ Otherware / Otherware_1_SB_Development.iso / mac / developm / scnote / tubetest.004 / TubeTest.p < prev    next >
Text File  |  1988-08-17  |  14KB  |  401 lines

  1. {------------------------------------------------------------------------------
  2. #
  3. #    Macintosh Developer Technical Support
  4. #
  5. #    Simple Color QuickDraw Animation Sample Application
  6. #
  7. #    TubeTest
  8. #
  9. #    TubeTest.p    -    Pascal Source
  10. #
  11. #    Copyright ⌐ 1988 Apple Computer, Inc.
  12. #    All rights reserved.
  13. #
  14. #    Versions:    1.0                    8/88
  15. #
  16. #    Components:    TubeTest.p            August 1, 1988
  17. #                TubeTest.c            August 1, 1988
  18. #                TubeTest.r            August 1, 1988
  19. #                PTubeTest.make        August 1, 1988
  20. #                CTubeTest.make        August 1, 1988
  21. #
  22. #    The TubeTest program is a simple demonstration of how to use the Palette 
  23. #    Manager in a color program.  It has a special color palette that is associated
  24. #    with the main window.  The colors are animated using the Palette Manager 
  25. #    to give a flowing tube effect.  The program is very simple, and the Palette
  26. #    Manager and drawing parts are put in separate subroutines to make it easier
  27. #    to figure out what is happening.
  28. #    
  29. #    The program is still a complete Macintosh application with a Main Event Loop,
  30. #    so there is the extra code to run the MEL.  
  31. #    
  32. #    There is a resource file that is necessary as well, to define the Menus, Window,
  33. #    Dialog, and Palette resources used in the program.  
  34. #
  35. #    See Sample and TESample for the general structure and MultiFinder techniques that
  36. #    we recommend that you use when building a new application.
  37. #
  38. ------------------------------------------------------------------------------}
  39.  
  40. PROGRAM TubeTest;
  41.  (*
  42.     File TubeTest.p
  43.  
  44.     Version 1.0: 6/2/88
  45.     
  46.     4/19/88:
  47.     TubeTest -- A small sample application written by Bo3b Johnson.
  48.     The idea is to draw two circles in varying colors in the window, then
  49.     animate the colors when the menu is chosen.    This is a complete program
  50.     with event loop and everything.    It is intended to be a simple example of
  51.     how to use the Palette Manager to do some minor color animation, and
  52.     how to use the PM to set up the colors desired in a window.
  53.     
  54.     Also see the resource file that goes with this to see how the Palette 
  55.     itself is layed out.
  56.     
  57.     Could be built with something like this:
  58.         rez TubeTest.r -o TubeTest
  59.         pascal TubeTest.p
  60.         Link TubeTest.p.o ╢
  61.             "{MPW}Libraries:"Interface.o ╢
  62.             "{MPW}Libraries:"Runtime.o ╢
  63.             "{MPW}PLibraries:"Paslib.o ╢
  64.             -o TubeTest
  65.         TubeTest
  66.  *)
  67.  
  68.  {Where does it fit:
  69.     This is a series of sample programs for those doing development
  70.     using Color QuickDraw.  Since the whole color problem depends
  71.     upon the exact effect desired, there are a number of answers
  72.     to how to use colors, from the simple to the radically complex.
  73.     These programs try to cover the gamut, so you should use 
  74.     which ever seems appropriate.  In most cases, use the simplest
  75.     one that will give the desired results.  The compatibility
  76.     rating is from 0..9 where low is better.  The more known risks 
  77.     there are the higher the rating.
  78.     
  79.     
  80.     The programs (in order of compatibility):
  81.     
  82.         SillyBalls:
  83.             This is the simplest use of Color QuickDraw, and does
  84.             not use the Palette Manager.  It draws randomly colored
  85.             balls in a color window.  This is intended to give you
  86.             the absolute minimum required to get color on the screen.
  87.             Written in straight Pascal code.
  88.             Compatibility rating = 0, no known risks.
  89.         
  90.         FracAppPalette:
  91.             This is a version of FracApp that uses only the Palette
  92.             Manager.  It does not support color table animation
  93.             since that part of the Palette Manager is not sufficient.
  94.             The program demonstrates a full color palette that is
  95.             used to display the Mandelbrot set.  It uses an offscreen
  96.             gDevice w/ Port to handle the data, using CopyBits to
  97.             draw to the window.  The Palette is automatically 
  98.             associated with each window.  The PICT files are read
  99.             and written using the bottlenecks, to save on memory
  100.             useage.
  101.             Written in MacApp Object Pascal code.
  102.             Compatibility rating = 0, no known risks.
  103.         
  104.         TubeTest:    (***)
  105.             This is a small demo program that demonstrates using the
  106.             Palette Manager for color table animation.  It uses a 
  107.             color palette with animating entries, and draws using the
  108.             Palette Manager.  There are two circles of animating colors
  109.             which gives a flowing tube effect.  This is a valid case
  110.             for using the animating colors aspect of the Palette Manager,
  111.             since the image is being drawn directly.
  112.             Written in straight Pascal code.
  113.             Compatibility rating = 0, no known risks.
  114.         
  115.         FracApp:
  116.             This is the ╘commercial quality╒ version of FracApp.  This
  117.             version supports color table animation, using an offscreen
  118.             gDevice w/ Port, and handles multiple documents.  The
  119.             CopyBits updates to the screen are as fast as possible.  The
  120.             program does not use the Palette Manager, except to
  121.             provide for the system palette, or color modes with less than
  122.             255 colors.  For color table animation using an offscreen
  123.             gDevice w/ Port, it uses the Color Manager and handles the
  124.             colors itself.  Strict compatibility was relaxed to allow for
  125.             a higher performance program.  This is the most ╘real╒ of the
  126.             sample programs.
  127.             Written in MacApp Object Pascal code.
  128.             Compatibility rating = 2.  (nothing will break, but it may not
  129.             always look correct.)
  130.         
  131.         FracApp300:
  132.             This doesn't support colors, but demonstrates how to create and
  133.             use a 300 dpi bitmap w/ Port.  The bitmap is printed at full
  134.             resolution on LaserWriters, and clipped on other printers (but
  135.             they still print).  It demonstrates how to use a high resolution
  136.             image as a PICT file, and how to print them out.
  137.             Written in MacApp Object Pascal code.
  138.             Compatibility rating = 1.  (The use of PrGeneral is slightly 
  139.             out of the ordinary, although supported.)
  140. }
  141.  
  142.  {$R-}    { No range checking.}
  143.  {$D+}    { Debugging labels on. }
  144.  
  145.  
  146.     { Interface files with all the happy Macintosh stuff in them. }
  147. USES 
  148.     MemTypes, QuickDraw, OsIntf, ToolIntf, PaletteMgr;
  149.     
  150.     
  151. CONST
  152.     appleID                    = 1000;        { resource IDs/menu IDs for Apple, File and Edit menus }
  153.     fileID                    = 1001;
  154.     editID                    = 1002;
  155.     
  156.     appleM                    = 1;        { Index for each menu in myMenus (array of menu handles) }
  157.     fileM                    = 2;
  158.     editM                    = 3;
  159.  
  160.     menuCount                = 3;        { Total number of menus }
  161.  
  162.     windowID                = 1000;        { Resource ID for main window }
  163.     aboutMeDLOG             = 1000;        { And Resource ID for About box dialog. }
  164.  
  165.     tubularItem                = 1;        { When checked, animation of colors. }
  166.     quitItem                = 3;        { Quit in the menu of course. }
  167.  
  168.     aboutMeCommand            = 1;        { Menu item in apple menu for About TubeTest item }
  169.     
  170.     totalColors                = 152;        { use 150 colors in our palette for drawing eyes. }
  171.     numColors                = 150;        { to skip black and white. }
  172.     
  173.     
  174.  
  175. VAR
  176.     myMenus:        ARRAY [1..menuCount] OF MenuHandle;
  177.     dragRect:        Rect;                { Rectangle used to mark bounds for dragging window }
  178.     doneFlag:        BOOLEAN;            { TRUE if user has chosen Quit command }
  179.     myEvent:        EventRecord;
  180.     myWindow:        WindowPtr;
  181.     whichWindow:    WindowPtr;
  182.     tubeCheck:        Boolean;            { if true, the menu is checked, and we animate. }
  183.     theChar:        Char;
  184.     error:             OSErr;
  185.     theWorld:         SysEnvRec;
  186.     
  187.     { This routine will update the window when required by update events.    It
  188.         will draw two circular dudes that are indexed in colors through the colors
  189.         we are using. 0 and 1 are skipped, since those are white and black in the
  190.         palette. }
  191. PROCEDURE DrawEyes;
  192.     
  193.     VAR        TempRect:    Rect;
  194.             I:            Integer;
  195.     
  196. BEGIN
  197.     SetRect(TempRect, numColors, numColors, numColors, numColors);
  198.     For I := 2 To totalColors Do Begin
  199.         PmForeColor(I);
  200.         FrameOval (TempRect);
  201.         InsetRect (TempRect, -1, -1);
  202.     END;
  203.     
  204.     SetRect(TempRect, numColors*3, numColors, numColors*3, numColors);
  205.     For I := totalColors DownTo 2 Do Begin
  206.         PmForeColor(I);
  207.         FrameOval (TempRect);
  208.         InsetRect (TempRect, -1, -1);
  209.     END;
  210. END;    { DrawEyes }
  211.  
  212.  
  213. PROCEDURE SetUpMenus;
  214.  
  215.     VAR        I:    INTEGER;
  216.  
  217. BEGIN
  218.     { Read menu descriptions from resource file into memory and store handles
  219.     in myMenus array }
  220.     myMenus[appleM] := GetMenu(appleID);    {read Apple menu from resource file}
  221.     AddResMenu(myMenus[appleM], 'DRVR');    {add desk accessory names to Apple menu}
  222.     myMenus[fileM] := GetMenu(fileID);        {read file menu from resource file}
  223.     myMenus[editM] := GetMenu(editID);        {read edit menu from resource file}
  224.  
  225.     FOR I := 1 TO menuCount DO InsertMenu(myMenus[I], 0); {install menus in menu bar}
  226.     
  227.     DrawMenuBar;                            {and draw menu bar}
  228. END; { SetUpMenus }
  229.  
  230.  
  231.     { Use the Palette currently attached to the main window to animate the colors 
  232.     in the circular eye shapes.  This will rotate them around to give the flowing 
  233.     tube effect. We make the palette into a color table so we can move entries 
  234.     around.    We have to skip the first two entries since those are black and white. 
  235.     (entries 0 and 1) }
  236. PROCEDURE ShiftyColors;
  237.     
  238.     VAR        currPalette:    PaletteHandle;
  239.             destCTab:        CTabHandle;
  240.             lastCSpec:        ColorSpec;
  241.     
  242. BEGIN
  243.     SetPort (myWindow);
  244.     
  245.     currPalette := GetPalette(myWindow);
  246.     destCTab := CTabHandle (NewHandle (SIZEOF (ColorTable)+(totalColors*SIZEOF(ColorSpec))));
  247.     IF destCTab = NIL  THEN Exit (ShiftyColors);
  248.     Palette2CTab(currPalette, destCTab);
  249.     
  250.         { Move the colors around in the color table, skipping 0 and 1, and moving
  251.         all the elements down by one, and copying the element at 2 back to the 
  252.         end of the table. The effect is to rotate the colors in the table.    }
  253.     lastCSpec := destCTab^^.ctTable[2];                            { pull first one off. }
  254.     BlockMove (@destCTab^^.ctTable[3], @destCTab^^.ctTable[2], 
  255.                 (numColors) * SIZEOF (ColorSpec) );                { copy all one entry down. }
  256.     destCTab^^.ctTable[totalColors-1] := lastCSpec;                { put last color back on front. }
  257.         
  258.     AnimatePalette(myWindow, destCTab, 2, 2, numColors);
  259.     
  260.     DisposHandle (Handle (destCTab));
  261. END;    { ShiftyColors }
  262.  
  263.  
  264.     { Display the dialog box in response to the 'About TubeTest' menu item. }
  265. PROCEDURE ShowAboutMeDialog;
  266.  
  267.     VAR        theDialog:    DialogPtr;
  268.             itemHit:    Integer;
  269.  
  270. BEGIN
  271.     theDialog := GetNewDialog(aboutMeDLOG, NIL, WindowPtr( - 1));
  272.     ModalDialog(NIL, itemHit);
  273.     DisposDialog(theDialog);
  274. END; { ShowAboutMeDialog }
  275.  
  276.  
  277.     { Execute menu command specified by mResult, the result of MenuSelect }
  278. PROCEDURE DoCommand(mResult: LONGINT);
  279.  
  280.     VAR        theItem:    INTEGER;                { menu item number from mResult low-order word }
  281.             theMenu:    INTEGER;                { menu number from mResult high-order word }
  282.             name:         Str255;                    { desk accessory name }
  283.             temp:         INTEGER;
  284.             dummy:        Boolean;
  285.  
  286. BEGIN
  287.     theItem := LoWord(mResult);                    { call Toolbox Utility routines to }
  288.     theMenu := HiWord(mResult);                    { set menu item number and menu }
  289.  
  290.     CASE theMenu OF                                { case on menu ID }
  291.  
  292.         appleID:
  293.             IF (theItem = aboutMeCommand) THEN  ShowAboutMeDialog
  294.             ELSE
  295.                 BEGIN                            { call Menu Manager to get desk acc.}
  296.                     GetItem(myMenus[appleM], theItem, name);
  297.                     temp := OpenDeskAcc(name);
  298.                     SetPort(myWindow);
  299.                 END; { appleID }
  300.  
  301.         fileID:
  302.             BEGIN
  303.                 IF theItem = quitItem THEN doneFlag := TRUE;
  304.                 
  305.                 IF theItem = tubularItem THEN BEGIN
  306.                     tubeCheck := NOT tubeCheck;
  307.                     CheckItem(myMenus[fileM], tubularItem, tubeCheck);
  308.                 END;
  309.             END;    { fileID }
  310.  
  311.         editID:
  312.                 dummy := SystemEdit(theItem - 1);{ Pass the command on to the Desk Manager.}
  313.  
  314.     END; {of menu CASE}
  315.  
  316.     HiliteMenu(0);                        { Unhighlight menu title(highlighted by MenuSelect) }
  317. END; {of DoCommand}
  318.  
  319.  
  320. BEGIN  { Main }
  321.     { Test the computer to be sure we can do color.  If not we would crash, which
  322.     would be bad.  If we can╒t run, just beep and exit. }
  323.     error := SysEnvirons(1, theWorld);
  324.     IF NOT theWorld.hasColorQD THEN BEGIN
  325.         SysBeep (50);
  326.         ExitToShell;                { If no color QD, we must leave. }
  327.     END;
  328.  
  329.     InitGraf(@thePort);
  330.     InitFonts;
  331.     InitWindows;
  332.     InitMenus;
  333.     TEInit;
  334.     InitDialogs(NIL);
  335.     InitCursor;
  336.  
  337.     WITH screenBits.bounds DO
  338.         SetRect(dragRect, 4, 24, right - 4, bottom - 4);
  339.     doneFlag := FALSE;                { flag to detect when Quit command is chosen}
  340.     tubeCheck := FALSE;                { flag for animating color is initially off. }
  341.  
  342.         { Open the color window. }
  343.     myWindow := GetNewCWindow(windowID, NIL, POINTER(-1));
  344.     SetPort(myWindow);
  345.  
  346.         { Set up menus last, since the menu drawing can then use the palette we have for ou
  347.         window. Makes the Apple look better, in particular. }
  348.     SetUpMenus;
  349.     
  350.     { Main Event Loop }
  351.     REPEAT
  352.         SystemTask;
  353.  
  354.         IF GetNextEvent(everyEvent, myEvent) THEN
  355.             CASE myEvent.what OF            { case on event type}
  356.  
  357.                 mouseDown:
  358.                     CASE FindWindow(myEvent.where, whichWindow) OF
  359.  
  360.                         inSysWindow:                { desk accessory window: call Desk Manager to handle it}
  361.                                 SystemClick(myEvent, whichWindow);
  362.  
  363.                         inMenuBar:                    { Menu bar: learn which command, then execute it. }
  364.                                 DoCommand(MenuSelect(myEvent.where));
  365.  
  366.                         inDrag:                        { title bar: call Window Manager to drag}
  367.                                 DragWindow(whichWindow, myEvent.where, dragRect);
  368.  
  369.                         inContent:                    { body of application window: }
  370.                                 IF whichWindow <> frontWindow THEN
  371.                                     SelectWindow(whichWindow); {and make it active if not}
  372.                     END; {of mouseDown}
  373.  
  374.                 updateEvt:                            { Update the eyes in window. }
  375.                     IF WindowPtr(myEvent.message) = myWindow THEN
  376.                         BEGIN
  377.                             BeginUpdate(WindowPtr(myEvent.message));
  378.                                 DrawEyes;
  379.                             EndUpdate(WindowPtr(myEvent.message));
  380.                         END; {of updateEvt}
  381.                             
  382.                 keyDown, autoKey:                    { key pressed once or held down to repeat }
  383.                     IF myWindow = frontWindow THEN
  384.                         BEGIN
  385.                             theChar := CHR(BAnd(myEvent.message, charCodeMask)); { get the char }
  386.                                 { If Command key down, do it as a Menu Command. }
  387.                             IF BAnd(myEvent.modifiers, cmdKey) <> 0 THEN DoCommand(MenuKey(theChar));
  388.                         END; {of keyDown and autoKey}
  389.  
  390.             END; {of event CASE}
  391.  
  392.             { If we have menu item checked, go ahead and animate colors. }
  393.         IF tubeCheck THEN ShiftyColors;
  394.         
  395.     UNTIL doneFlag;
  396.  
  397.         { clean up after palette manager, so he can chuck the palette in use. }
  398.     DisposeWindow (myWindow);
  399.     
  400. END.
  401.