home *** CD-ROM | disk | FTP | other *** search
/ QuickTime 2.0 Developer Kit / QuickTime 2.0 Developer Kit.iso / mac / MAC / Programming Stuff / Sample Code / Movie Controller / MCComponent ƒ / FadeScreen.c next >
Encoding:
C/C++ Source or Header  |  1994-03-14  |  5.1 KB  |  183 lines  |  [TEXT/KAHL]

  1. /*
  2.     File:        FadeScreen.c
  3.  
  4.     Contains:    code snippet for fading a screen of any depth in or out.
  5.  
  6.     Written by:    John Wang
  7.  
  8.     Copyright:    © 1994 by Apple Computer, Inc., all rights reserved.
  9.  
  10.     Change History (most recent first):
  11.  
  12.         <1>        03/14/94    JW        Re-Created for Universal Headers.
  13.  
  14.     To Do:        For some reason, I noticed that 256 and millions of colors
  15.                 fades slower.  I think this is due to hardware but I'm not sure.
  16. */
  17.  
  18. #ifdef THINK_C
  19. #define        applec
  20. #endif
  21.  
  22. #include    <Memory.h>
  23. #include    <QuickDraw.h>
  24. #include    <Devices.h>
  25. #include    <Video.h>
  26. #include    <LowMem.h>
  27.  
  28. #include    "FadeScreen.h"
  29.  
  30. /* ------------------------------------------------------------------------- */
  31.  
  32. /*
  33.     Description:    Call this routine to fade any screen in or out.
  34.  
  35.     Format Params:    
  36.         Name            Usage    Description/Assumptions
  37.         ----            ----    -----------------------
  38.         myDevice        PI        Pass in the device handle to the screen.
  39.         inOut            PI        Pass in whether to fade in or out.  TRUE to fade out (black).
  40.                                 FALSE to fade back in.
  41.         smoothness        PI        Number of steps to fade in or out.  A good number is 60.
  42.         
  43.     Usage: P=Parameter,R=ReturnValue,E=External,G=FileGlobal,L=Local,I=Input,O=Output
  44.     
  45.     Error Handling:    If it can't allocate the color table necessary, then it returns without
  46.                     doing anything.
  47.  
  48.     Special Notes:    xxx put other comments here xxx
  49.  
  50. */
  51.  
  52. void FadeInOut(GDHandle myDevice, Boolean inOut, short smoothness)
  53. {
  54.     GDHandle        saveDevice;
  55.     
  56.     saveDevice = GetGDevice();
  57.     SetGDevice(myDevice);
  58.  
  59.     if ( (**myDevice).gdType == directType ) {
  60.         DirectFadeInOut(myDevice, inOut, smoothness);
  61.     } else {
  62.         IndexedFadeInOut(myDevice, inOut, smoothness);
  63.     }
  64.  
  65.     SetGDevice(saveDevice);
  66. }
  67.  
  68. /* ------------------------------------------------------------------------- */
  69.  
  70. /*    Internal routines used by fadeInOut.    */
  71.  
  72. void DirectFadeInOut(GDHandle myDevice, Boolean inOut, short smoothness)
  73. {
  74.     VDSetEntryRecord    setEntriesRec;    /* DirectSetEntries information */
  75.     Ptr                    csPtr;            /* Passes setEntriesRect to the video card */
  76.     short                index;            /* Index into the color table */
  77.     short                iter;
  78.     CTabHandle            animClut;        /* Color table we’re using for animation. */
  79.     short                pixelSize;
  80.     long                colorComp;
  81.     RGBColor            *thisRGB;
  82.     long                now;
  83.     
  84.     animClut = (CTabHandle) NewHandle(sizeof(ColorTable) + 255 * sizeof(ColorSpec));
  85.     if (animClut == nil)
  86.         goto bail;
  87.         
  88.     (**animClut).ctSeed = GetCTSeed ();
  89.     (**animClut).ctFlags = 0;
  90.     pixelSize = (**((**myDevice).gdPMap)).pixelSize;
  91.     if ( pixelSize == 32 )
  92.         (**animClut).ctSize = 255;
  93.     else if ( pixelSize == 16 )
  94.         (**animClut).ctSize = 31;
  95.     else
  96.         goto bail;
  97.  
  98.     for ( iter = 0; iter < smoothness; iter++ ) {
  99.         now = TickCount();
  100.         for ( index = 0; index <= (**animClut).ctSize; index++ ) {
  101.             if (pixelSize == 32)
  102.                 colorComp = (index << 8) + index;
  103.             else if (pixelSize == 16)
  104.                 colorComp = (index << 11) + (index << 6) + (index << 1) + (index >> 4);
  105.             if (inOut)
  106.                 colorComp = (colorComp * (smoothness - iter)) / smoothness;
  107.             else
  108.                 colorComp = (colorComp * iter) / smoothness;
  109.             thisRGB = &((**animClut).ctTable[index].rgb);
  110.             thisRGB->red = (short) colorComp;
  111.             thisRGB->green = (short) colorComp;
  112.             thisRGB->blue = (short) colorComp;
  113.         }
  114.         
  115.         //    Set the video card’s DACs
  116.         setEntriesRec.csTable = (**animClut).ctTable;
  117.         setEntriesRec.csStart = 0;
  118.         setEntriesRec.csCount = (**animClut).ctSize;
  119.         csPtr = (Ptr) &setEntriesRec;
  120.         Control ((**myDevice).gdRefNum, cscDirectSetEntries, (Ptr) &csPtr);
  121.         
  122.         //    Wait
  123.         while ( now == TickCount() );
  124.     }
  125.     
  126. bail:
  127.     if (animClut != nil)
  128.         DisposHandle((Handle) animClut);
  129. }
  130.  
  131. void IndexedFadeInOut(GDHandle myDevice, Boolean inOut, short smoothness)
  132. {
  133.     VDSetEntryRecord    setEntriesRec; /* DirectSetEntries information */
  134.     Ptr                    csPtr;         /* Passes setEntriesRect to the video card */
  135.     short                index;         /* Index into the color table */
  136.     short                iter;
  137.     CTabHandle            animClut, sourceColorTB;
  138.     RGBColor            *thisRGB, *sourceRGB;
  139.     long                now;
  140.     
  141.     animClut = (CTabHandle) NewHandle (sizeof (ColorTable) + 255 * sizeof (ColorSpec));
  142.     if ( animClut == nil )
  143.         goto bail;
  144.         
  145.     sourceColorTB = (**((**myDevice).gdPMap)).pmTable;
  146.  
  147.     (**animClut).ctSeed = GetCTSeed();
  148.     (**animClut).ctFlags = 0;
  149.     (**animClut).ctSize = (**sourceColorTB).ctSize;
  150.  
  151.     //    Set the video card’s DACs        
  152.     for ( iter = 0; iter < smoothness; iter++ ) {
  153.         now = TickCount();
  154.         for ( index = 0; index <= (**animClut).ctSize; index++ ) {
  155.             thisRGB = &((**animClut).ctTable[index].rgb);
  156.             sourceRGB = &((**sourceColorTB).ctTable[index].rgb);
  157.             if (inOut) {
  158.                 thisRGB->red = (sourceRGB->red * (smoothness - iter)) / smoothness;
  159.                 thisRGB->green = (sourceRGB->green * (smoothness - iter)) / smoothness;
  160.                 thisRGB->blue = (sourceRGB->blue * (smoothness - iter)) / smoothness;
  161.             } else {
  162.                 thisRGB->red = (sourceRGB->red * iter) / smoothness;
  163.                 thisRGB->green = (sourceRGB->green * iter) / smoothness;
  164.                 thisRGB->blue = (sourceRGB->blue * iter) / smoothness;
  165.             }
  166.         }
  167.         
  168.         //    Set the video card’s DACs
  169.         setEntriesRec.csTable = (**animClut).ctTable;
  170.         setEntriesRec.csStart = 0;
  171.         setEntriesRec.csCount = (**animClut).ctSize;
  172.         csPtr = (Ptr) &setEntriesRec;
  173.         Control ((**myDevice).gdRefNum, cscSetEntries, (Ptr) &csPtr);
  174.         
  175.         //    Wait
  176.         while ( now == TickCount() );
  177.     }
  178.     
  179. bail:
  180.     if (animClut != nil)
  181.         DisposHandle((Handle) animClut);
  182. }
  183.