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

📄 oasissound.cpp

📁 使用stl技术,(还没看,是听说的)
💻 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    : oasisSound.cpp

 * DESCRIPTION : Sound instance

 * AUTHOR      : Zephie Greyvenstein

 *****************************************************************************/



#include "oasisSound.h"

#include "oasisSoundResource.h"

#include "oasisException.h"

#include "oasisVector3.h"

#include "oasisAudioCommon.h"



#include "OgreLogManager.h"



namespace Oasis {

  

  sound::sound( ) {

    source = 0;

  

    alGenSources( 1, ( ALuint* )&source );

   

    if ( alGetError( ) != AL_NO_ERROR ) {

      oasisError( exception::ET_INTERNAL_ERROR,

      "Could not allocate source for sound",

      "sound::( )" );

    }



    alSourcef( source, AL_PITCH, 1 );      

    alSource3f( source, AL_VELOCITY, 0.0, 0.0, 0.0 );

    

    if ( alGetError( ) != AL_NO_ERROR ) {

      oasisError( exception::ET_INTERNAL_ERROR,

      "Could not set source for sound",

      "sound::( )" );

    }

  

    setVolume( 1 );

    setLooping( false );

    setPosition( vector3::ZERO );

  }

  

  sound::~sound( ) {

    clear( );

    alDeleteSources( 1, ( ALuint* )&source );

    

    if ( alGetError( ) != AL_NO_ERROR ) {

      oasisError( exception::ET_INTERNAL_ERROR,

      "Could not delete source for sound",

      "sound::~( )" );

    }

  }

 

  void sound::setVolume( const real newVolume ) {

    volume = newVolume;

    alSourcef( source, AL_MAX_GAIN, ( ALfloat )volume );

   

    if ( alGetError( ) != AL_NO_ERROR ) {

      oasisError( exception::ET_INTERNAL_ERROR,

      "Could not set volume for source",

      "sound::setVolume" );

    }

  }



  const real sound::getVolume( void ) const {

    return volume;

  }



  void sound::setLooping( const bool newLooping ) {

    looping = newLooping;



    if ( looping ) {

      alSourcei( source, AL_LOOPING, AL_TRUE );

    } else {

      alSourcei( source, AL_LOOPING, AL_FALSE );

    }



    if ( alGetError( ) != AL_NO_ERROR ) {

      oasisError( exception::ET_INTERNAL_ERROR,

      "Could not set looping for source",

      "sound::setLooping" );

    }

  }

  

  const bool sound::getLooping( void ) const {

    return looping;

  }



  void sound::setPosition( const vector3 &newPosition ) {  

    alSource3f( source, AL_POSITION,

    newPosition.x,

    newPosition.y,

    newPosition.z );

   

    if ( alGetError( ) != AL_NO_ERROR ) {

      oasisError( exception::ET_INTERNAL_ERROR,

      "Could not set position for source",

      "sound::setPosition" );

    }

  }



  void sound::play( void ) {

    alSourcePlay( source );



    if ( alGetError( ) != AL_NO_ERROR ) {

      oasisError( exception::ET_INTERNAL_ERROR,

      "Could not play sound source",

      "sound::play" );

    }

  }



  void sound::stop( void ) {

    alSourceStop( source );



    if ( alGetError( ) != AL_NO_ERROR ) {

      oasisError( exception::ET_INTERNAL_ERROR,

      "Could not stop sound source",

      "sound::stop" );

    }

  }



  const bool sound::isPlaying( void ) const {

    ALenum state;

    alGetSourcei( source, AL_SOURCE_STATE, &state );

  

    if ( alGetError( ) != AL_NO_ERROR ) {

      oasisError( exception::ET_INTERNAL_ERROR,

      "Could not retrieve source state",

      "sound::isPlaying" );

    }



    return ( state == AL_PLAYING );

  }



  void sound::setup( resource *parent ) {

    soundResource *sp = ( soundResource* )parent;



    setPosition( vector3::ZERO );

    setVolume( sp->getVolume( ) );

    setLooping( sp->getLooping( ) );

    

    alSourcef( source, AL_MAX_DISTANCE, ( ALfloat )sp->getAttenuationMax( ) );

    alSourcei( source, AL_BUFFER, sp->getBuffer( ) );



    if ( alGetError( ) != AL_NO_ERROR ) {

      oasisError( exception::ET_INTERNAL_ERROR,

      "Could not setup sound source for use",

      "sound::setup" );

    }

  }



  void sound::clear( void ) {

    stop( );

  }

}

⌨️ 快捷键说明

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