www.pudn.com > RCApp-src.zip > SoundManager.cpp


/* 
	RedEye Project (http://members.ozemail.com.au/~ndmcevoy/) 
	Copyright (C) 2003  Nick McEvoy 
 
	This library is free software; you can redistribute it and/or 
	modify it under the terms of the GNU Library General Public 
	License as published by the Free Software Foundation; either 
	version 2 of the License, or (at your option) any later version. 
 
	This library is distributed in the hope that it will be useful, 
	but WITHOUT ANY WARRANTY; without even the implied warranty of 
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
	Library General Public License for more details. 
 
	You should have received a copy of the GNU Library General Public 
	License along with this library; if not, write to the Free 
	Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 
 
	----------------------------------------------------------------- 
 
		Commented for use with Doxygen (http://www.doxygen.org) 
 
	----------------------------------------------------------------- 
*/ 
 
/*! \file SoundManager.cpp 
 *	\brief Sound manager. 
 * 
 *		This file contains the Sound manager. 
 */ 
 
// GLUT includes 
#include  
 
// RedEye includes 
#include "SoundManager.h" 
#include "Engine.h" 
 
//! The one and only instance 
reSoundManager* reSoundManager::mpInstance = NULL; 
 
// todo: to be replaced by reading sound data from file 
struct Sound 
{ 
public: 
	char* msFilename; 
}; 
 
// todo: to be replaced by reading sound data from file 
Sound sounds[] = 
{ 
	{"wavs/boing.wav"}, 
}; 
static int miNumSounds = sizeof(sounds)/sizeof(Sound); 
 
 
 
 
reSound::reSound(int iIndex, 
				 const char* fname, 
				 const class slDSP* dsp) 
: slSample(fname, dsp) 
{ 
	miIndex = iIndex; 
} 
 
 
 
 
reSoundManager::reSoundManager() 
: mSoundList(false) 
{ 
	SetSafetyMargin(); 
	mbMusicOn = true; 
	mbSoundFxOn = true; 
	msCurrentTrack[0] = NULL; 
} 
 
 
 
 
reSoundManager::~reSoundManager() 
{ 
	DestroyDatabase(); 
	mpInstance = NULL; 
} 
 
 
 
 
reSoundManager* 
reSoundManager::GetInstance() 
{ 
	if (mpInstance == NULL) 
	{ 
		mpInstance = new reSoundManager; 
	} 
	return mpInstance; 
} 
 
 
 
 
void 
reSoundManager::CreateDatabase() 
{ 
	printf("reSoundManager::CreateDatabase() - creating sounds...\n"); 
 
	// todo: move sounds to an xml file 
	for (int i = 0; i < miNumSounds; i++) 
	{ 
		reSound* pSound = new reSound(i, sounds[i].msFilename, &mScheduler); 
		pSound->ref(); 
		mSoundList.AddTail(pSound); 
	} 
 
	// todo: read xml 
 
	printf("reSoundManager::CreateDatabase() - done\n"); 
} 
 
 
 
 
void 
reSoundManager::DestroyDatabase() 
{ 
	printf("reSoundManager::DestroyDatabase() - destroying sounds...\n"); 
 
	// Destroy sounds 
	LISTPOSITION pos = mSoundList.GetHeadPosition(); 
	while (pos) 
	{ 
		slSample* pSound = mSoundList.GetNext(pos); 
		pSound->unRef(); 
		if (pSound->getPlayCount() == 0) 
			delete pSound; 
	} 
	mSoundList.RemoveAll(); 
 
	printf("reSoundManager::DestroyDatabase() - done\n"); 
} 
 
 
 
 
void 
reSoundManager::Update() 
{ 
	mScheduler.update(); 
} 
 
 
 
 
void 
reSoundManager::PlaySoundFx(int sound) 
{ 
	// todo: don't find by index ... change to use a name 
	if (mbSoundFxOn) 
	{ 
		LISTPOSITION pos = mSoundList.GetHeadPosition(); 
		while (pos) 
		{ 
			reSound* pSound = mSoundList.GetNext(pos); 
			if (pSound->GetIndex() == sound) 
			{ 
				mScheduler.playSample(pSound, 1, SL_SAMPLE_MUTE, 2, NULL); 
				break; 
			} 
		} 
	} 
} 
 
 
 
 
void 
reSoundManager::ChangeTrack(const char* sFilename) 
{ 
	strcpy(msCurrentTrack, sFilename); 
	SetMusicOn(mbMusicOn); 
} 
 
 
 
 
void 
reSoundManager::SetMusicOn(bool bOn) 
{ 
	mbMusicOn = bOn; 
 
	if (mbMusicOn) 
	{ 
		mScheduler.stopMusic(); 
		if (msCurrentTrack[0] != '\0') 
			mScheduler.loopMusic(msCurrentTrack); 
	} 
	else // turn off 
	{ 
		mScheduler.stopMusic(); 
		mScheduler.update();  // nasty kludge! 
		mScheduler.update();  // nasty kludge! 
	} 
} 
 
 
 
 
void 
reSoundManager::SetSoundFxOn(bool bOn) 
{ 
	mbSoundFxOn = bOn; 
}