home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World 2006 July & August
/
PCWorld_2006-07-08_cd.bin
/
temacd
/
planearcade
/
planearcade.exe
/
Tank3.bmp
/
sound.cpp
< prev
next >
Wrap
C/C++ Source or Header
|
2004-10-30
|
8KB
|
421 lines
#include "main.h"
//-----------
//Global
//-----------
ALCcontext *Context = NULL;
ALCdevice *Device = NULL;
//------------------------------------------------------------------
// Name: InitializeSound()
// Desc: Inicializacia zvuku
//------------------------------------------------------------------
void InitializeSound()
{
//DirectMusic
//--------------
if (MusicEnabled == 1)
{
CoInitialize(NULL);
}
//OpenAL3D
//--------------
if (SoundEnabled == 1)
{
LogPrint("Inicializujem OpenAL3D");
//Open device
if(SoundEnabled3D == 1)
{
if(HardwareSound == 1)
Device = alcOpenDevice((ALubyte*)"DirectSound3D");
else
Device = alcOpenDevice((ALubyte*)"DirectSound");
}
else
Device = alcOpenDevice((ALubyte*)"DirectSound");
if (Device == NULL)
{
//we failed to initialize the device
LogPrint(" chyba pri inicializacii");
MessageBox(hWnd,"Chyba pri iniciaσizßciφ zvuku",
"ERROR",MB_OK|MB_ICONERROR);
}
//Create context(s)
LogPrint("alcCreateContext");
Context=alcCreateContext(Device,NULL);
//Set active context
LogPrint("alcMakeContextCurrentK");
alcMakeContextCurrent(Context);
//alutInit(0,NULL);
}
}
//------------------------------------------------------------------
// Name: CleanUpSound()
// Desc: Odstrani z pamati objekty zvuku
//------------------------------------------------------------------
void CleanUpSound()
{
//DirectMusic
//-------------
if (MusicEnabled == 1)
{
//UnititializeCom
CoUninitialize();
}
//OpenAL3D
//--------------
if (SoundEnabled == 1)
{
//Get active context
Context=alcGetCurrentContext();
//Get device for active context
Device=alcGetContextsDevice(Context);
//Disable context
alcMakeContextCurrent(NULL);
//Release context(s)
alcDestroyContext(Context);
//Close device
alcCloseDevice(Device);
}
}
//------------------------------------------------------------------
// Name: SOUND()
// Desc: Konstruktor
//------------------------------------------------------------------
SOUND::SOUND()
{
//MP3
m_pGraph = NULL;
m_pMediaControl = NULL;
m_pMediaPosition = NULL;
}
//------------------------------------------------------------------
// Name: ~SOUND()
// Desc: Destruktor
//------------------------------------------------------------------
SOUND::~SOUND()
{
Stop();
//Direct Music
//-------------
if (m_enumFormat == MP3)
{
if(m_pGraph)
m_pGraph->Release();
if(m_pMediaControl)
m_pMediaControl->Release();
if(m_pMediaPosition)
m_pMediaPosition->Release();
}
//OpenAL3D
//-----------
if (m_enumFormat == WavMidi3D)
{
alDeleteSources(1,&alSource);
alDeleteBuffers(1,&alSampleSet);
}
}
//------------------------------------------------------------------
// Name: InitialiseForWavMidi()
// Desc: pripravy zvuk pre prehrßvanie MIDI alebo WAW
//------------------------------------------------------------------
void SOUND::InitialiseForWavMidi()
{
//ak je vypnuty zvuk skonci
if (SoundEnabled == 0)
return;
LogPrint("Vytvaram zvuk WavMidi");
m_enumFormat = WavMidi3D;
}
//------------------------------------------------------------------
// Name: InitialiseForWavMidi()
// Desc: pripravy zvuk pre prehrßvanie MIDI alebo WAW
//------------------------------------------------------------------
void SOUND::InitialiseForWavMidi3D()
{
//ak je vypnuty zvuk skonci
if (SoundEnabled == 0)
return;
LogPrint("Vytvaram zvuk WavMidi3D");
m_enumFormat = WavMidi3D;
alSourcei(alSource,AL_SOURCE_RELATIVE,AL_TRUE);
}
//------------------------------------------------------------------
// Name: InitialiseForMP3()
// Desc: pripravy zvuk na prehravanie MP3
//------------------------------------------------------------------
void SOUND::InitialiseForMP3()
{
//ak je vypnuty zvuk skonci
if (MusicEnabled == 0)
return;
LogPrint("Inicializujem Direct Music");
LogPrint("Vytvaram zvuk MP3");
CoCreateInstance(CLSID_FilterGraph, NULL,
CLSCTX_INPROC, IID_IGraphBuilder, (void**)&m_pGraph);
m_pGraph->QueryInterface(IID_IMediaControl, (void**)&m_pMediaControl);
m_pGraph->QueryInterface(IID_IMediaPosition, (void**)&m_pMediaPosition);
m_enumFormat = MP3;
}
//------------------------------------------------------------------
// Name: LoadSound()
// Desc: Loadne zvuk
//------------------------------------------------------------------
bool SOUND::LoadSound(char* szSoundFileName)
{
WCHAR wstrSoundPath[MAX_PATH];
//Log Info
char cBuf[80];
sprintf(cBuf," Zvuk: %s",szSoundFileName);
LogPrint(cBuf);
switch(m_enumFormat)
{
//MP3
//-------------
case MP3:
//ak je vypnuta hudba - skonci
if (MusicEnabled == 0)
return false;
//Convert the path to unicode.
MultiByteToWideChar(CP_ACP, 0, szSoundFileName, -1, wstrSoundPath, MAX_PATH);
m_pGraph->RenderFile(wstrSoundPath, NULL);
break;
//Wav3D
//-------------
case WavMidi3D:
//ak je vypnuty zvuk skonci
if (SoundEnabled == 0)
return false;
//load our sound
ALboolean loop;
alutLoadWAVFile(szSoundFileName,&alFormatBuffer, (void **) &alBuffer,(unsigned int *)&alBufferLen, &alFreqBuffer, &loop);
alGenSources(1, &alSource);
alGenBuffers(1, &alSampleSet);
alBufferData(alSampleSet, alFormatBuffer, alBuffer, alBufferLen, alFreqBuffer);
alSourcei(alSource, AL_BUFFER, alSampleSet);
alutUnloadWAV(alFormatBuffer, alBuffer, alBufferLen, alFreqBuffer);
//set the pitch
alSourcef(alSource,AL_PITCH,1.0f);
//set the gain
alSourcef(alSource,AL_GAIN,1.0f);
if (alSource == NULL)
LogPrint(" chyba pri vytvarani zvuku");
break;
//Wav2D
//------
case WavMidi:
break;
default:
return false;
}
return true;
}
//------------------------------------------------------------------
// Name: Play()
// Desc: Prehra zvuk - dwNumOfRepeats - pocet prehrani
//------------------------------------------------------------------
bool SOUND::Play(bool Loop)
{
switch(m_enumFormat)
{
//MP3
//-------------
case MP3:
//ak je hudba vypnuta
if (MusicEnabled == 0)
return false;
//rewind
m_pMediaPosition->put_CurrentPosition(0);
//Play mp3
m_pMediaControl->Run();
break;
//Wav3D
//-------------
case WavMidi3D:
//ak je vypnuty zvuk skonci
if (SoundEnabled == 0)
return false;
//set looping to true
alSourcei(alSource,AL_LOOPING,Loop);
alSourcePlay(alSource);
break;
//Wav2D
//-------------
case WavMidi:
break;
default:
return false;
}
return true;
}
//------------------------------------------------------------------
// Name: Stop()
// Desc: Zastavy zvuk
//------------------------------------------------------------------
bool SOUND::Stop()
{
switch(m_enumFormat)
{
//MP3
//-------------
case MP3:
//ak je vypnuty zvuk skonci
if (MusicEnabled == 0)
return false;
m_pMediaControl->Stop();
break;
//Wav3D
//-------------
case WavMidi3D:
//ak je vypnuty zvuk skonci
if (SoundEnabled == 0)
return false;
alSourceStop(alSource);
break;
//Wav2D
//-------------
case WavMidi:
break;
default:
return false;
}
return true;
}
//------------------------------------------------------------------
// Name: SetPosition()
// Desc: Nastavi poziciu zvuku, zvuk sa prepoΦitava pod╛a pozicie kamery
//------------------------------------------------------------------
void SOUND::SetPosition(VECTOR3D Point,float Scale)
{
//ak je vypnuty zvuk skonci
if (SoundEnabled == 0)
return;
if (SoundEnabled3D == 0)
return;
if (m_enumFormat != WavMidi3D)
return;
//Camera
//---------
alListener3f(AL_POSITION,0.0f,0.0f,0.0f);
float vec[6];
vec[0] = 0.0f;
vec[1] = 0.0f;
vec[2] = 1.0f;
vec[3] = 0.0f;
vec[4] = 0.0f;
vec[5] = 1.0f;
alListenerfv(AL_ORIENTATION, vec);
//Sound
//----------
Point = TransformPoint(Point,matView);
alSource3f(alSource,AL_POSITION,Point.X/Scale,-Point.Y/Scale,-Point.Z/Scale);
alSource3f(alSource,AL_VELOCITY,-20.0f,0.0f,20.0f);
}