PaletteAnimation.c

/*
    File:       PaletteAnimation.c
 
    Contains:    RICHARD P. COLLYER
 
    Written by:     
 
    Copyright:  Copyright © 1990-1999 by Apple Computer, Inc., All Rights Reserved.
 
                You may incorporate this Apple sample source code into your program(s) without
                restriction. This Apple sample source code has been provided "AS IS" and the
                responsibility for its operation is yours. You are not permitted to redistribute
                this Apple sample source code as "Apple sample source code" after having made
                changes. If you're going to re-distribute the source, we require that you make
                it clear in the source that the code was descended from Apple sample source
                code, but that you've made changes.
 
    Change History (most recent first):
                7/13/1999   Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
                
 
*/
#include    <Quickdraw.h>
#include    <Windows.h>
#include    <Events.h>
#include    <GestaltEqu.h>
#include    <OSEvents.h>
#include    <Palettes.h>
#include    <SegLoad.h>
#include    <TextUtils.h>
 
extern _DataInit();
 
#define TRUE            0xFF
#define FALSE           0
 
#define clutID          40
#define numcolor        256
 
#ifdef powerc
   QDGlobals    qd;
#endif
 
Rect                WinMinusScroll;
WindowPtr           myWindow;
CTabHandle          mycolors;
PaletteHandle       srcPalette;
Boolean             DoneFlag;
void Draw();
void init();
/*______________________________________________________*/
/*             Set Up Usage of Palette                  */
/*______________________________________________________*/
void Draw()
/* The drawing routine is a collection of 253 random Rects which are colored with 
the corresponding color in the color table. */
{
    float                   left, top, right, bottom;
    Rect                    CRect;
    int                     i;
 
    EraseRect (&WinMinusScroll);
    for (i = 1; i < numcolor-2; ++i) {
        do {
            left = Random() / 32767.0 * WinMinusScroll.right;
            if (left < 0)
                left = -left;
            } while (left > WinMinusScroll.right - 40);
            
        do {
            right = Random() / 32767.0 * WinMinusScroll.right;
            if (right < 0)
                right = -right;
            } while (right < left);
            
        do {
            top = Random() / 32767.0 * WinMinusScroll.bottom;
            if (top < 0)
                top = -top;
            } while (top > WinMinusScroll.bottom - 40);
            
        do {
            bottom = Random() / 32767.0 * WinMinusScroll.bottom;
            if (bottom < 0)
                bottom = -bottom;
            } while (bottom < top);
        SetRect(&CRect, left, top, right, bottom);
        PmForeColor(i);
        FrameRect (&CRect);
        PaintRect (&CRect);
        }
}
 
 
/*______________________________________________________*/
/*               Initialization traps                   */
/*______________________________________________________*/
void init()
{
    Rect                BaseRect;
    OSErr               err;
    long                feature;
    
    UnloadSeg(_DataInit);
    InitGraf(&qd.thePort);
    FlushEvents(everyEvent, 0);
    InitWindows();
    InitCursor();
    
    DoneFlag = FALSE;
/*______________________________________________________*/
/*            Use Gestalt to find 32BQD                 */
/*______________________________________________________*/
    err = Gestalt(gestaltQuickdrawVersion, &feature);
    if (!err) {
        if ((feature & 0x0f00) != 0x0200)
            DoneFlag = TRUE;
        }
    else 
        DoneFlag = TRUE;
/*______________________________________________________*/
/*                     Set Rects                        */
/*______________________________________________________*/
    SetRect(&BaseRect, 40, 60, 472, 282);
    SetRect(&WinMinusScroll, BaseRect.left-40, BaseRect.top-60, BaseRect.right-60, 
                BaseRect.bottom - 80);
/*______________________________________________________*/
/*        Open Window & set Palette & Picture           */
/*______________________________________________________*/
    mycolors = GetCTable (clutID);
 
    myWindow = NewCWindow(nil, &BaseRect, "\pUsing Palette Manager", TRUE, documentProc, 
                            (WindowPtr) -1, TRUE, 150);
    SetPort((WindowPtr) myWindow);
 
    srcPalette = NewPalette (numcolor, mycolors, pmAnimated+pmExplicit, 0);
    SetPalette ((WindowPtr) myWindow, srcPalette, TRUE);
    
    Draw();
}
 
main()
{
    EventRecord     myEvent;
    int             yieldTime = 0;
    RGBColor        changecolor;
/*______________________________________________________*/
/*                   Main Event loop                    */
/*______________________________________________________*/
    init();
    for ( ;; ) {
        if (DoneFlag)
            ExitToShell();
        if (WaitNextEvent(everyEvent, &myEvent, yieldTime, nil)) {
            switch (myEvent.what) {
                case mouseDown:
                case keyDown:
                case autoKey:
                    DoneFlag = TRUE;
                    break;
                default:
                    break;
                }
            }
            
        /* Animate the colors one step for eash event loop */
        GetEntryColor (srcPalette, 1, &changecolor);
        AnimatePalette (myWindow, mycolors, 2, 1, numcolor - 2);
        AnimateEntry (myWindow, numcolor - 1, &changecolor);
        Palette2CTab (srcPalette, mycolors);
        }
}