home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Learn 3D Graphics Programming on the PC
/
Learn_3D_Graphics_Programming_on_the_PC_Ferraro.iso
/
rwwin
/
winsound.c_
/
winsound.bin
Wrap
Text File
|
1995-11-14
|
11KB
|
420 lines
/**********************************************************************
*
* File : winsound.c
*
* Abstract :
**********************************************************************
*
* This file is a product of Criterion Software Ltd.
*
* This file is provided as is with no warranties of any kind and is
* provided without any obligation on Criterion Software Ltd. or
* Canon Inc. to assist in its use or modification.
*
* Criterion Software Ltd. will not, under any
* circumstances, be liable for any lost revenue or other damages arising
* from the use of this file.
*
* Copyright (c) 1995 Criterion Software Ltd.
* All Rights Reserved.
*
* RenderWare is a trademark of Canon Inc.
*
************************************************************************/
/*--- Include files ---*/
#define INCLUDE_SHELLAPI_H
#include <windows.h>
#include "global.h"
#ifdef __WINDOWS_386__
#include "wmwatcom.h"
#else
#include "wavemix.h"
#endif
#include "rwwin.h"
#include "resource.h"
/*--- Macro and Magic Number Definitions ---*/
/*
* This is the GetWinFlags() flag that indicates we are running a
* Windows 3.1 app under WOW. This is defined under WOW but not in
* the standard Windows include files so we have to define it
* manually.
*/
#if !defined(WIN32)
#if !defined(WF_WINNT)
#define WF_WINNT 0x4000
#endif /* !defined(WF_WINNT) */
#endif /* !defined(WIN32) */
#define MAX_SOUND_NAME 16 /* The maximum lenght of a sound name */
#define WAVEMIX_CHANNELS 4 /* The number of Wavemix channels */
/*--- Structure Definitions ---*/
/* Sound: This structure defines a single instance of a .wav file.
*/
typedef struct
{
char sName[MAX_SOUND_NAME]; /* The name of the sound */
LPMIXWAVE wSound; /* The WaveMix data */
int nDefaultChannel; /* The default channel that the sound will be
played on */
} Sound;
/* AllSounds: A single global variable of this type is declared and used to hold
* all of the global sound data.
*/
typedef struct
{
int nAmoSounds; /* Number of sounds currently defined */
int nSetup; /* Sound status flag, 0 = Cant play Sounds */
Sound *spSounds; /* Array of loaded sounds */
HANDLE hSession; /* Wavemix session information */
} AllSounds;
AllSounds asGSounds;
/* WinVer: Enumerated type which is used in determining the version of windows
* that we are currently running under
*/
typedef enum
{
rwNAWINVER, /* Error code */
rwWINDOWSNT, /* Windows NT */
rwWINDOWS95, /* Windows 95 */
rwWINDOWS31 /* For a WIN32 library this means Win32s */
} WinVer;
/************************************************************************
*
* Function: WindowsVersion()
*
* Description: Determine the version of Windows that we are running on.
*
* Return Value: One of the versions of Windows defined in the WinVer
* enumerated type
*
************************************************************************/
static WinVer
WindowsVersion(void)
{
DWORD version;
int majorVersion;
int minorVersion;
/*
* NOTE: In all this version checking we assume that the major
* Windows version number is three or more as we could not have
* got this far if we were running under Windows 2.0 or less.
*/
version = GetVersion();
majorVersion = LOBYTE(LOWORD(version));
minorVersion = HIBYTE(LOWORD(version));
/*
* Note: Windows 95 is not Windows v4.0 but v3.95 so this is what
* we check for. Also, we assume that anything at all after
* Windows 95 is compatible with Windows 95.
*/
if ((majorVersion == 3) && (minorVersion < 95))
{
/*
* This could be either Windows 3.1 or Windows NT - you
* can't tell from the version number. To find out we
* need to look at GetWinFlags() which returns the
* value WF_WINNT under Windows NT. The value WF_WINNT
* is not defined in the standard Windows 3.1 include
* files so we have defined it ourselved at the top
* of this file.
*/
if ((GetWinFlags() & WF_WINNT) == WF_WINNT)
{
/*
* This is Windows NT of some form. As RenderWare is only
* supported on NT 3.5 we have to check the version number
* to ensure this is not NT 3.1.
*/
if ((majorVersion == 3) && (minorVersion < 5))
{
/*
* This is NT 3.1 which we do not support, return an error.
*/
return rwNAWINVER;
}
else
{
/*
* Windows NT 3.5.
*/
return rwWINDOWSNT;
}
}
else
{
/*
* Windows 3.1.
*/
return rwWINDOWS31;
}
}
else
{
/*
* Windows 95.
*/
return rwWINDOWS95;
}
}
/************************************************************************
*
* Function: AllSoundsSetup()
*
* Description: Initialise the global AllSounds data structure
*
* Return Value: TRUE on success, FALSE on failure
*
************************************************************************/
int AllSoundsSetup(void)
{
int nCount;
MIXCONFIG cConfig;
asGSounds.nSetup = 0; /* Not initialized */
if (WindowsVersion() != rwWINDOWS31)
{
/* If you are not running 3.1 the wave mix stuff doesn't work
correctly */
return FALSE;
}
/* No sounds loaded yet */
asGSounds.spSounds = NULL;
asGSounds.nAmoSounds = 0;
#if defined(__WINDOWS_386__)
/* Open the wavemix library. Only necessary if we are using
Watcom. Visual C++ and Borland C++ use an import library */
if (!WaveMixOpen())
{
return FALSE;
}
#endif
cConfig.wSize = sizeof(MIXCONFIG);
cConfig.dwFlags = WMIX_CONFIG_CHANNELS|WMIX_CONFIG_SAMPLINGRATE;
cConfig.wChannels = 1;
cConfig.wSamplingRate =11;
if ((asGSounds.hSession = WaveMixConfigureInit(&cConfig))==NULL)
{
#if defined(__WINDOWS_386__)
WaveMixClose();
#endif
return FALSE;
}
for (nCount=0; nCount < WAVEMIX_CHANNELS; nCount++)
{
if (WaveMixOpenChannel(asGSounds.hSession, nCount, WMIX_ALL))
{
break;
}
}
if (nCount < WAVEMIX_CHANNELS)
{
/* Cant open 4 channels */
for (; nCount-1 >= 0; nCount--)
{
WaveMixCloseChannel(asGSounds.hSession, nCount, 0UL);
}
WaveMixCloseSession(asGSounds.hSession);
#if defined(__WINDOWS_386__)
WaveMixClose();
#endif
return FALSE;
}
WaveMixActivate(asGSounds.hSession, TRUE);
/* Mark as ready to play sounds */
asGSounds.nSetup = 1; /* Can play sounds */
return TRUE;
}
/************************************************************************
*
* Function: AllSoundsDestroy()
*
* Description: Destroy the global AllSounds data structure
*
* Return Value: None
*
************************************************************************/
void AllSoundsDestroy(void)
{
int nCount;
if (!asGSounds.nSetup)
{
/* Sounds weren't set up so we don't need to do anything */
return;
}
WaveMixActivate(asGSounds.hSession, FALSE);
if (asGSounds.spSounds)
{
/* Sounds loaded - free them up */
for (nCount = 0; nCount < asGSounds.nAmoSounds; nCount++)
{
WaveMixFreeWave(asGSounds.hSession, asGSounds.spSounds[nCount].wSound);
}
free(asGSounds.spSounds);
}
/* Take out the sound handler */
for (nCount = 0; nCount < WAVEMIX_CHANNELS; nCount++)
{
WaveMixCloseChannel(asGSounds.hSession, nCount, 0UL);
}
WaveMixCloseSession(asGSounds.hSession);
#if defined(__WINDOWS_386__)
WaveMixClose();
#endif
}
/************************************************************************
*
* Function: AllSoundsAddSound()
*
* Description: Add another sounds to our sound library
*
* Parameters: cpFilename - the name of the .wav file to load
* nChannel - the default channel (0 -3 ) for this sound
*
* Return Value: None
*
************************************************************************/
int AllSoundsAddSound(char *cpFilename, int nChannel)
{
LPMIXWAVE wWave;
Sound *spSound;
char *cpTmp;
if (!asGSounds.nSetup)
{
/* We're not using sounds so don't do anything */
return FALSE;
}
wWave = WaveMixOpenWave(asGSounds.hSession, cpFilename, NULL, WMIX_FILE);
if (wWave == NULL)
{
return FALSE;
}
if (asGSounds.nAmoSounds)
{
/* This isn't the first so so we need to realloc */
asGSounds.spSounds =
realloc(asGSounds.spSounds, sizeof(Sound) * (asGSounds.nAmoSounds+1));
}
else
{
asGSounds.spSounds = malloc(sizeof(Sound));
}
spSound = &asGSounds.spSounds[asGSounds.nAmoSounds];
asGSounds.nAmoSounds++;
spSound->wSound = wWave;
strcpy(spSound->sName,cpFilename);
/* Remove filename extension */
for (cpTmp = spSound->sName; ((*cpTmp) && ((*cpTmp)!='.')); cpTmp++)
;
if (*cpTmp=='.')
{
*cpTmp = '\0';
}
/* Save the other characteristics */
spSound->nDefaultChannel = nChannel;
return TRUE;
}
/************************************************************************
*
* Function: AllSoundsPlaySound()
*
* Description: Play the sound
*
* Parameters: cpFilename - the name of the sound to play
*
* Return Value: TRUE if the sound played, FALSE otherwise
*
************************************************************************/
int AllSoundsPlaySound(char *cpFilename)
{
int nCount;
Sound *spSound;
MIXPLAYPARAMS pParams;
if (!asGSounds.nSetup)
{
/* Not using sound so don't play a sound */
return FALSE;
};
spSound = asGSounds.spSounds;
for (nCount=0; nCount < asGSounds.nAmoSounds; nCount++)
{
if (!strcmp(spSound->sName, cpFilename))
{
/* Found the little sucker. Play it */
pParams.wSize = sizeof(MIXPLAYPARAMS);
pParams.hMixSession = asGSounds.hSession;
pParams.iChannel = spSound->nDefaultChannel;
pParams.lpMixWave = spSound->wSound;
pParams.hWndNotify = NULL;
pParams.dwFlags = WMIX_CLEARQUEUE|WMIX_HIPRIORITY;
pParams.wLoops = 0;
WaveMixPlay(&pParams);
return TRUE;
}
spSound++;
}
return FALSE;
}