⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 soundmanager.cpp

📁 吃豆子游戏源码
💻 CPP
字号:
/**
 *	File	:	SoundManager.cpp
 *  Author	:	Kevin Lynx
 *	Date	:	2007/8/3
 */
#include "stdafx.h"
#include "SoundManager.h"

#pragma comment( lib, "irrKlang.lib" )

const char *SND_NAME[ SoundManager::SND_COUNT ] = { 
									  "data/audio/death1.wav",
									  "data/audio/death2.wav",
									  "data/audio/eat.wav",
									  "data/audio/explode_bomb.wav",
									  "data/audio/take_bonus.wav",
									  "data/audio/logo.wav"
};

SoundManager::SoundManager()
{
	mSndEngine = 0;
	mCurMusic = 0;
}

SoundManager::~SoundManager()
{
	if( mSndEngine )
		mSndEngine->drop();
}

bool	SoundManager::init( float g_volume )
{
	mSndEngine = createIrrKlangDevice();

	if( mSndEngine == 0 )
	{
		return false;
	}
	
	mSndEngine->setSoundVolume( g_volume );
	mSndEngine->setDefault3DSoundMaxDistance( 4500 );
	mSndEngine->setDefault3DSoundMinDistance( 1000 );

	return loadMusicList( "data/audio/list.txt" );
}

bool	SoundManager::loadMusicList( const char *file )
{
	FILE *fp = fopen( file, "r" );
	if( fp == 0 )
	{
		return false;
	}

	mMusicList.clear();

	int count;
	char name[256];

	fscanf( fp, "%d", &count );
	for( int i = 0; i < count; ++ i )
	{
		fscanf( fp, "%s", name );
		mMusicList.push_back( name );
	}

	fclose( fp );

	return true;
}

void	SoundManager::playNextMusic()
{
	int i = rand() % int( mMusicList.size() );

	if( mCurMusic != 0 )
	{
		mCurMusic->stop();
		mCurMusic->drop();
		mCurMusic = 0;
	}
	
	mCurMusic = mSndEngine->play2D( mMusicList.at( i ).c_str(), 
		false, false, true );//must set 3rd or 4th or 6th to true if you want to 
					  //get a valid ISound ,and later call ISound::drop
}

void	SoundManager::update( const vector3df &pos, const vector3df &lookat )
{
	if( mCurMusic != 0 &&
		mCurMusic->isFinished() )
	{
		playNextMusic();
	}

	///
	mSndEngine->setListenerPosition( pos, lookat );
}

void	SoundManager::playSound( int sndType, const vector3df pos )
{
	if( sndType < 0 ||
		sndType >= SND_COUNT )
	{
		return;
	}

	//mSndEngine->play2D( SND_NAME[sndType] );
	mSndEngine->play3D( SND_NAME[sndType], pos ); 
}

void	SoundManager::playSound( int sndType )
{
	if( sndType < 0 ||
		sndType >= SND_COUNT )
	{
		return;
	}

	mSndEngine->play2D( SND_NAME[sndType] );
}



⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -