Back to index

supertuxkart  0.5+dfsg1
sfx_openal.cpp
Go to the documentation of this file.
00001 //  $Id$
00002 //
00003 //  SuperTuxKart - a fun racing game with go-kart
00004 //  Copyright (C) 2006 Patrick Ammann <pammann@aro.ch>
00005 //
00006 //  This program is free software; you can redistribute it and/or
00007 //  modify it under the terms of the GNU General Public License
00008 //  as published by the Free Software Foundation; either version 2
00009 //  of the License, or (at your option) any later version.
00010 //
00011 //  This program is distributed in the hope that it will be useful,
00012 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 //  GNU General Public License for more details.
00015 //
00016 //  You should have received a copy of the GNU General Public License
00017 //  along with this program; if not, write to the Free Software
00018 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00019 
00020 #if (HAVE_OPENAL && HAVE_OGGVORBIS)
00021 
00022 #include <assert.h>
00023 #include <stdio.h>
00024 #include <string>
00025 
00026 #ifdef __APPLE__
00027 #  include <OpenAL/al.h>
00028 #else
00029 #  include <AL/al.h>
00030 #endif
00031 #include <AL/alut.h>
00032 
00033 #include "sfx_openal.hpp"
00034 #include "file_manager.hpp"
00035 #include "user_config.hpp"
00036 
00037 SFXImpl::SFXImpl(const char* filename)
00038 {
00039     m_soundBuffer= 0;
00040     m_soundSource= 0;
00041     const bool LOADED = load(filename);
00042     assert( LOADED );
00043 }
00044 
00045 //-----------------------------------------------------------------------------
00046 SFXImpl::~SFXImpl()
00047 {
00048     alDeleteBuffers(1, &m_soundBuffer);
00049     alDeleteSources(1, &m_soundSource);
00050 }
00051 
00052 //-----------------------------------------------------------------------------
00053 void SFXImpl::play()
00054 {
00055     alSourcef(m_soundSource,AL_GAIN,user_config->m_sfx_volume);
00056     alSourcePlay(m_soundSource);
00057 
00058     // Check (and clear) the error flag
00059     int error = alGetError();
00060 
00061     if(error != AL_NO_ERROR)
00062     {
00063         fprintf(stderr, "SFX OpenAL error: %d\n", error);
00064     }
00065 
00066 }
00067 
00068 //-----------------------------------------------------------------------------
00069 bool SFXImpl::load(const char* filename)
00070 {
00071     std::string path = file_manager->getSFXFile(filename);
00072 
00073     alGenBuffers(1, &m_soundBuffer);
00074     if (alGetError() != AL_NO_ERROR)
00075     {
00076         fprintf(stderr, "Loading '%s' failed\n",filename);
00077         return false;
00078     }
00079     ALenum format = 0;
00080     ALsizei size = 0, freq = 0;
00081     ALvoid* data = NULL;
00082     ALboolean loop = AL_FALSE;
00083 
00084 #ifdef __APPLE__
00085     alutLoadWAVFile((ALbyte*)path.c_str(), &format, &data, &size, &freq);
00086 #else
00087     alutLoadWAVFile((ALbyte*)path.c_str(), &format, &data, &size, &freq, &loop);
00088 #endif
00089 
00090     if (data == NULL)
00091     {
00092         const int ALUT_ERROR = alutGetError();
00093         fprintf(stderr, "Error 1 loading SFX: %s failed because %s \n", path.c_str(), alutGetErrorString(ALUT_ERROR));
00094         return false;
00095     }
00096 
00097     alBufferData(m_soundBuffer, format, data, size, freq);
00098     if (alGetError() != AL_NO_ERROR)
00099     {
00100         fprintf(stderr, "Error 2 loading SFX: %s failed\n", path.c_str());
00101         return false;
00102     }
00103 
00104     alutUnloadWAV(format, data, size, freq);
00105     if (alGetError() != AL_NO_ERROR)
00106     {
00107         fprintf(stderr, "Error 3 loading SFX: %s failed\n", path.c_str());
00108         return false;
00109     }
00110 
00111     alGenSources(1, &m_soundSource );
00112     if (alGetError() != AL_NO_ERROR)
00113     {
00114         fprintf(stderr, "Error 4 loading SFX: %s failed\n", path.c_str());
00115         return false;
00116     }
00117 
00118     // not 3D yet
00119     alSourcei (m_soundSource, AL_BUFFER,          m_soundBuffer);
00120     alSource3f(m_soundSource, AL_POSITION,        0.0, 0.0, 0.0);
00121     alSource3f(m_soundSource, AL_VELOCITY,        0.0, 0.0, 0.0);
00122     alSource3f(m_soundSource, AL_DIRECTION,       0.0, 0.0, 0.0);
00123     alSourcef (m_soundSource, AL_ROLLOFF_FACTOR,  0.0          );
00124     alSourcei (m_soundSource, AL_SOURCE_RELATIVE, AL_TRUE      );
00125 
00126     return true;
00127 }
00128 
00129 #endif //if (HAVE_OPENAL && HAVE_OGGVORBIS)
00130