CodeWarrior (OS 9)/CopyMask.c

/*
    File:       CopyMask.c
 
    Contains:   Shows how CopyMask can used to fade a screen to a lighter color.    
 
    Written by: JW
 
    Copyright:  Copyright © 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):
                08/2000     JM              Carbonized, non-Carbon code is commented out
                                            for demonstration purposes.
                7/9/1999    KG              Updated for Metrowerks Codewarror Pro 2.1
                
 
*/
#include    "CarbonPrefix.h"
#include    <Dialogs.h>
#include    <Fonts.h>
#include    <Processes.h>
#include    <QDOffscreen.h>
#include    <Sound.h>
#include    <Gestalt.h>
 
void main(void)
{
    WindowPtr       mainWinPtr;
    OSErr           error;
    //SysEnvRec     theWorld;   //not available in carbon
    long            result;     //used in carbonization
    GWorldPtr       myOffscreen1, myOffscreen2;
    GDHandle        oldDevice;
    CGrafPtr        oldPort;
    Rect            windRect, offRect, myRect;
    RGBColor        theColor;
    int             i;
    RgnHandle       rgnHandle = NewRgn();
    
    /* Make sure ColorQD exists. */
    /*error = SysEnvirons(1, &theWorld);
    if (theWorld.hasColorQD == false) {
        SysBeep(50);
        ExitToShell();
    }*/
    
    error = Gestalt(gestaltQuickdrawVersion, &result);
    if (error != noErr || result < gestalt8BitQD) {
        SysBeep(50);
        ExitToShell();
    }
    
    /* Initialize all the needed managers. */
    /*InitGraf(&qd.thePort);
    InitFonts();
    InitWindows();
    InitMenus();
    TEInit();
    InitDialogs(nil);*/
    InitCursor();
 
    /* Define output window. */
    SetRect(&windRect, 100, 100, 400, 400);
    SetRect(&offRect, 0, 0, 300, 300);
    mainWinPtr = NewCWindow(nil, &windRect, "\pJohn", true, documentProc, (WindowPtr) -1, false, 0);
    //SetPort(mainWinPtr);
    SetPortWindowPort(mainWinPtr);
    GetGWorld(&oldPort, &oldDevice);
 
    /* Create two offscreen GWorlds. */
    if (NewGWorld(&myOffscreen1, 0, &offRect, nil, nil, 0))
        Debugger();
    if (NewGWorld(&myOffscreen2, 0, &offRect, nil, nil, 0))
        Debugger();
    
    /* Now, draw ramp to first offscreen pixmap. */
    SetGWorld(myOffscreen1, nil);
    for (i=0; i<10; i++) {
        theColor.red = theColor.green = theColor.blue = (i*28) << 8;
        RGBForeColor(&theColor);
        SetRect(&myRect, 0, i*30, 300, i*30+30);
        PaintRect(&myRect);
    }
 
    /* Now, draw mask to second offscreen pixmap. */
    SetGWorld(myOffscreen2, nil);
    SetRect(&myRect, 0, 0, 300, 300);
    theColor.red = theColor.green = theColor.blue = (128) << 8;
    RGBForeColor(&theColor);
    PaintRect(&myRect);
 
    /*  Draw to the ramp to the window. */
    SetGWorld(oldPort, oldDevice);
    /*CopyBits((BitMapPtr) *myOffscreen1->portPixMap, &(*mainWinPtr).portBits, &offRect,
                &offRect, 0, nil);*/
    CopyBits((BitMapPtr) *(GetPortPixMap(myOffscreen1)), GetPortBitMapForCopyBits(GetWindowPort(mainWinPtr)), &offRect,
                &offRect, 0, nil);
    QDFlushPortBuffer(GetWindowPort(mainWinPtr), GetPortVisibleRegion(GetWindowPort(mainWinPtr), rgnHandle));
 
    /* Wait until user clicks button. */
    do {
    } while (!Button());
    
    /*  Now fade the window to white using CopyMask.  The destination must be erased
        for CopyMask to work.   */
    SetGWorld(oldPort, oldDevice);
    EraseRect(&offRect);
    /*CopyMask((BitMapPtr) *myOffscreen1->portPixMap, (BitMapPtr) *myOffscreen2->portPixMap,
                &(*mainWinPtr).portBits, &offRect, &offRect, &offRect);*/
    CopyMask((BitMapPtr) *(GetPortPixMap(myOffscreen1)), (BitMapPtr) *(GetPortPixMap(myOffscreen2)),
                GetPortBitMapForCopyBits(GetWindowPort(mainWinPtr)), &offRect, &offRect, &offRect);
    QDFlushPortBuffer(GetWindowPort(mainWinPtr), GetPortVisibleRegion(GetWindowPort(mainWinPtr), rgnHandle));
 
    /* Wait until user clicks button. */
    do {
    } while (Button());
    do {
    } while (!Button());
    
    DisposeRgn(rgnHandle);
}