Source/WRay_System.c

/* 
 *  WRay_System.c
 *
 *  QuickDraw 3D 1.6 Sample
 *  Robert Dierkes
 *
 *   07/28/98   RDD     Created.
 */
 
/*------------------*/
/*  Include Files   */
/*------------------*/
#include "QD3D.h"
 
#if defined(OS_MACINTOSH) && OS_MACINTOSH
#include <CodeFragments.h>  /* kUnresolvedCFragSymbolAddress */
#include <Events.h>         /* KeyMap */
#include <Gestalt.h>
#include <Sound.h>          /* SysBeep, SndPlay */
#include <Resources.h>
#endif
 
#include "WRay_Main.h"
#include "WRay_System.h"
#include "WRay_Error.h"
 
 
/*----------------------*/
/*  Global Declarations */
/*----------------------*/
#if defined(OS_MACINTOSH) && OS_MACINTOSH
static  SndChannelPtr   gChannel = NULL;
#endif
 
static  TQ3Boolean      gPlaySound = kQ3False;
 
 
/*
 *  QuickDraw3D_Initialize
 */
TQ3Boolean QuickDraw3D_Initialize(
            void)
{
    TQ3Status   status = kQ3Failure;
    OSErr       error;
    long        response;
 
    error = Gestalt(gestaltQD3D, &response);
 
    if ((error != noErr)  ||
        (! (response & (1 << gestaltQD3DPresent)))) {
 
        Error_ShowMessage(kQuickDraw3DNotInstalledStr);
        return kQ3False;
    }
 
    /*
     * Test for specific symbol as recommended in develop, March 1995
     * NOTE: QuickDraw3DLib must be weak linked for this comparison to work.
     */
    if (((void *) Q3Initialize) == (void *) kUnresolvedCFragSymbolAddress) {
        Error_ShowMessage(kQuickDraw3DNotInstalledStr);
        return kQ3False;
    }
 
    status = Q3Initialize();
    if (status == kQ3Failure) {
        ERROR_DEBUG_STR ("QuickDraw3D_Initialize: Q3Initialize returned failure.");
        return kQ3False;
    }
 
    error = Gestalt(gestaltQD3DVersion, &response);
 
    if ((error != noErr)  ||
        (response < kQD3DGestaltVersion_1_6_0)) {
 
        Error_ShowMessage(kQuickDraw3DNotCurrentStr);
        return kQ3False;
    }
 
    return kQ3True;
}
 
 
/*
 *  QuickDraw3D_Exit
 */
TQ3Boolean QuickDraw3D_Exit(
            void)
{
    TQ3Status   status;
    TQ3Boolean  isGood = kQ3True;
 
    status = Q3Exit();
    if (status == kQ3Failure) {
        ERROR_DEBUG_STR ("QuickDraw3D_Exit: Q3Exit returned failure.");
        isGood = kQ3False;
    }
 
#if defined(OS_MACINTOSH) && OS_MACINTOSH
    if (gChannel != NULL) {
        SndDisposeChannel(gChannel, kQ3True);
        gChannel = NULL;
    }
#endif
 
    return isGood;
}
 
 
#pragma mark -
 
/*
 *  System_IsKeyPressed
 *      Returns true if key is pressed, false otherwise.
 *
 *  Passed:
 *      keyCode - key code to test
 *
 *  Global:
 *      Nothing
 *
 *  Returned:
 *      Returns true if key is down, false otherwise.
 */
Boolean System_IsKeyPressed (
            unsigned short keyCode)
{
#if defined(OS_MACINTOSH) && OS_MACINTOSH
    KeyMap          keyMap;
    unsigned char   *pKeyMap;
 
    GetKeys (keyMap);
    pKeyMap = (unsigned char *) keyMap;
 
    return ((pKeyMap [keyCode >> 3] >> (keyCode & 7)) & 1);
#endif
}
 
 
/*
 *  System_Beep
 */
void System_Beep (
    void)
{
#if defined(OS_MACINTOSH) && OS_MACINTOSH
    SysBeep(1);
#endif
}
 
 
/*
 *  System_GetSound
 */
TQ3Boolean System_GetSound (
    void)
{
    return gPlaySound;
}
 
 
/*
 *  System_SetSound
 */
void System_SetSound (
    TQ3Boolean  newState)
{
    gPlaySound = newState;
}
 
 
/*
 *  System_Sound
 */
void System_Sound (
    void)
{
#if defined(OS_MACINTOSH) && OS_MACINTOSH
 
    static  Handle sndHandle = NULL;
 
    #define kSoundRsrcID    -24680
 
    if (gPlaySound == kQ3True) {
 
        if (sndHandle == NULL) {
            /* First time */
            OSErr   osErr = noErr;
 
            if (gChannel == NULL) {
                osErr = SndNewChannel(&gChannel, 0, 0, 0);
            }
 
            /* Keep sound resource locked and loaded */
            sndHandle = GetResource('snd ', kSoundRsrcID);
            if (sndHandle != NULL) {
                HLock(sndHandle);
//              HUnlock(sndHandle);
            }
        }
 
        if (sndHandle != NULL) {
            SndPlay(gChannel, (SndListHandle)sndHandle, 1);
        }
    }
#endif
}
 
 
/*
 *  System_RandomFloat
 *
 *  Returns a float value between 0.1 and 1.0, inclusive.
 */
float System_RandomFloat(
            void)
{
    float   value;
 
    value = (((Random() * TickCount()) % 10) + 1) * 0.1f;
 
    return value;
}