📄 oasissoundsystem.cpp
字号:
/******************************************************************************
* This source file is part of Bad Camel Gaming
* Copyright (C) 2003 Zephie Greyvenstein
* See Readme.html for acknowledgements
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*****************************************************************************/
/*****************************************************************************
* FILENAME : oasisSoundSystem.cpp
* DESCRIPTION : Sound engine
* AUTHOR : Zephie Greyvenstein
****************************************************************************/
#include "oasisSoundSystem.h"
#include "oasisSound.h"
#include "oasisSoundResource.h"
#include "oasisConfiguration.h"
#include "oasisAudioCommon.h"
#include "OgreResourceManager.h"
#include "OgreLogManager.h"
#include "OgreStringVector.h"
#include "OgreSDDataChunk.h"
#include "OgreProfiler.h"
#include "OgreDataChunk.h"
#include "OgreStringConverter.h"
namespace Oasis {
// Setup singleton
template< > soundSystem *singleton< soundSystem >::singletonPtr = 0;
soundSystem::soundSystem( ) : system( "Sound" ), pool( "sound" ) {
// Parse all sound scripts
_parseAllSources( ".sound" );
}
soundSystem::~soundSystem( ) {
}
soundSystem &soundSystem::get( void ) {
return singleton< soundSystem >::get( );
}
soundSystem *soundSystem::getPtr( void ) {
return singleton< soundSystem >::getPtr( );
}
void soundSystem::reset( void ) {
// Clear all sounds and reset volume
clearPool( );
setMasterVolume( MIXER_DEFAULT_MASTERVOLUME );
}
void soundSystem::update( real time ) {
}
soundResource *soundSystem::getSoundTemplate( const string &name ) {
// Try and get the template
soundResource *soundFile = ( soundResource* )( getByName( name ) );
if ( !soundFile ) {
oasisError( exception::ET_ITEM_NOT_FOUND,
"Sound template " + name + " not found!",
"soundSystem::getSoundTemplate" );
}
return soundFile;
}
soundResource *soundSystem::createSoundTemplate( const string &name ) {
return( ( soundResource* )createResource( name ) );
}
sound *soundSystem::createSound( const string &soundName,
const string &templateName ) {
// Try and get the template
soundResource *temp = getSoundTemplate( templateName );
// Get a sound from the pool
sound *newSound = ( sound* )_getFreePoolable( soundName );
// Set it up
newSound->setup( temp );
return newSound;
}
void soundSystem::removeSound( sound *oldSound ) {
// Return sound to pool
_consumePoolable( oldSound );
}
void soundSystem::removeSound( const string &soundName ) {
// Return sound to pool
_consumePoolable( soundName );
}
void soundSystem::stopAllSounds( void ) {
// Stop all sounds
for( poolableMap::iterator it = usedPool.begin( );
it != usedPool.end( ); ++it ) {
static_cast< sound* >( it->second )->stop( );
}
}
void soundSystem::setListenerPosition( const vector3 &pos,
const vector3 &forward,
const vector3 &up ) {
// Set the new position
real orientation[ ] = { forward.x, forward.y, forward.z,
up.x, up.y, up.z };
alListener3f( AL_POSITION, pos.x, pos.y, pos.z );
alListenerfv( AL_ORIENTATION, orientation );
}
void soundSystem::_parseFile( Ogre::DataChunk &chunk ) {
Ogre::String line;
soundResource *file = NULL;
while( !chunk.isEOF( ) ) {
line = chunk.getLine( );
// Ignore blanks & comments
if( !line.length( ) || line.substr( 0, 2 ) == "//" ) {
continue;
} else {
if ( file == NULL ) {
// No current script
file = createSoundTemplate( line );
// Skip to and over next {
chunk.skipUpTo( "{" );
} else {
// Already in script
if ( line == "}" ) {
// Finished
file = NULL;
} else {
_parseAttribute( line, file );
}
}
}
}
}
void soundSystem::_parseAttribute( const Ogre::String &line,
soundResource *file ) {
Ogre::StringVector params = line.split( );
Ogre::String attrib = params[ 0 ].toLowerCase( );
if ( attrib == "file" ) {
if ( params.size( ) != 2 ) {
_logError( line, file );
return;
}
file->setFileName( params[ 1 ] );
} else if ( attrib == "volume" ) {
if ( params.size( ) != 2 ) {
_logError( line, file );
return;
}
file->setVolume( Ogre::StringConverter::parseReal( params[ 1 ] ) );
} else if ( attrib == "looping" ) {
if ( params.size( ) != 2 ) {
_logError( line, file );
return;
}
file->setLooping( Ogre::StringConverter::parseBool( params[ 1 ] ) );
} else if ( attrib == "attenuation" ) {
if ( params.size( ) != 2 ) {
_logError( line, file );
return;
}
file->setAttenuationMax( Ogre::StringConverter::parseReal( params[ 1 ] ) );
}
}
resource *soundSystem::createResource( const string &name ) {
// First make sure this doesn't exist
if ( getByName( name ) != 0 ) {
oasisError( exception::ET_DUPLICATE_ITEM,
"Sound resource file " + name + "already exists.",
"soundSystem::create" );
}
soundResource *soundFile = new soundResource( name );
// Don't load now
resources[ name ] = soundFile;
return soundFile;
}
poolable *soundSystem::_createPoolable( void ) {
sound *newSound = new sound( );
return newSound;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -