]> git.mxchange.org Git - simgear.git/blobdiff - simgear/sound/soundmgr_openal.cxx
Dont execute code in case the soundmanager isn't properly initialized
[simgear.git] / simgear / sound / soundmgr_openal.cxx
index 7d97d95cc8e7a9ac2a07912e99b45542439fcf32..fb49d247f984d074b5d3406d0e4d34e5c9a3ec68 100644 (file)
 //
 // You should have received a copy of the GNU General Public License
 // along with this program; if not, write to the Free Software
-// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 //
 // $Id$
 
+#ifdef HAVE_CONFIG_H
+#  include <simgear_config.h>
+#endif
+
 #include <simgear/compiler.h>
 
 #if defined(__APPLE__)
@@ -59,7 +63,12 @@ extern "C" int isinf (double);
 #  endif
 #endif
 
-#include STL_IOSTREAM
+#if defined (__CYGWIN__)
+#include <ieeefp.h>
+#endif
+
+
+#include <iostream>
 
 #include <simgear/debug/logstream.hxx>
 #include <simgear/misc/sg_path.hxx>
@@ -80,6 +89,22 @@ SGSoundMgr::SGSoundMgr() {
     SG_LOG( SG_GENERAL, SG_INFO, "Initializing OpenAL sound manager" );
 
     // initialize OpenAL
+#if defined(ALUT_API_MAJOR_VERSION) && ALUT_API_MAJOR_VERSION >= 1
+    if (!alutInit(NULL, NULL))
+    {
+        ALenum error = alutGetError ();
+        SG_LOG( SG_GENERAL, SG_ALERT, "Audio initialization failed!" );
+        SG_LOG( SG_GENERAL, SG_ALERT, "   "+string(alutGetErrorString(error)));
+        working = false;
+        context = 0;
+        return;
+    }
+    else
+    {
+        working = true;
+        context = alcGetCurrentContext();
+    }
+#else
     if ( (dev = alcOpenDevice( NULL )) != NULL
             && ( context = alcCreateContext( dev, NULL )) != NULL ) {
         working = true;
@@ -87,8 +112,10 @@ SGSoundMgr::SGSoundMgr() {
     } else {
         working = false;
         context = 0;
-       SG_LOG( SG_GENERAL, SG_ALERT, "Audio initialization failed!" );
+        SG_LOG( SG_GENERAL, SG_ALERT, "Audio initialization failed!" );
+        return;
     }
+#endif
 
     listener_pos[0] = 0.0;
     listener_pos[1] = 0.0;
@@ -111,7 +138,7 @@ SGSoundMgr::SGSoundMgr() {
     alListenerfv( AL_ORIENTATION, listener_ori );
     alGetError();
     if ( alGetError() != AL_NO_ERROR) {
-       SG_LOG( SG_GENERAL, SG_ALERT,
+        SG_LOG( SG_GENERAL, SG_ALERT,
                 "Oops AL error after audio initialization!" );
     }
 
@@ -124,17 +151,12 @@ SGSoundMgr::SGSoundMgr() {
 
 SGSoundMgr::~SGSoundMgr() {
 
+#if defined(ALUT_API_MAJOR_VERSION) && ALUT_API_MAJOR_VERSION >= 1
+    alutExit ();
+#else
     if (context)
         alcDestroyContext( context );
-    //
-    // Remove the samples from the sample manager.
-    //
-    sample_map_iterator sample_current = samples.begin();
-    sample_map_iterator sample_end = samples.end();
-    for ( ; sample_current != sample_end; ++sample_current ) {
-       SGSoundSample *sample = sample_current->second;
-       delete sample;
-    }
+#endif
 }
 
 
@@ -143,12 +165,6 @@ void SGSoundMgr::init() {
     //
     // Remove the samples from the sample manager.
     //
-    sample_map_iterator sample_current = samples.begin();
-    sample_map_iterator sample_end = samples.end();
-    for ( ; sample_current != sample_end; ++sample_current ) {
-        SGSoundSample *sample = sample_current->second;
-        delete sample;
-    }
     samples.clear();
 }
 
@@ -176,7 +192,7 @@ SGSoundMgr::pause ()
     if (context) {
         alcSuspendContext( context );
         if ( alGetError() != AL_NO_ERROR) {
-           SG_LOG( SG_GENERAL, SG_ALERT,
+            SG_LOG( SG_GENERAL, SG_ALERT,
                     "Oops AL error after soundmgr pause()!" );
         }
     }
@@ -189,7 +205,7 @@ SGSoundMgr::resume ()
     if (context) {
         alcProcessContext( context );
         if ( alGetError() != AL_NO_ERROR) {
-           SG_LOG( SG_GENERAL, SG_ALERT,
+            SG_LOG( SG_GENERAL, SG_ALERT,
                     "Oops AL error after soundmgr resume()!" );
         }
     }
@@ -216,14 +232,12 @@ bool SGSoundMgr::remove( const string &refname ) {
 
     sample_map_iterator sample_it = samples.find( refname );
     if ( sample_it != samples.end() ) {
-       // first stop the sound from playing (so we don't bomb the
-       // audio scheduler)
-       SGSoundSample *sample = sample_it->second;
-        delete sample;
+        // first stop the sound from playing (so we don't bomb the
+        // audio scheduler)
         samples.erase( sample_it );
 
         // cout << "sndmgr: removed -> " << refname << endl;
-       return true;
+        return true;
     } else {
         // cout << "sndmgr: failed remove -> " << refname << endl;
         return false;
@@ -235,9 +249,9 @@ bool SGSoundMgr::remove( const string &refname ) {
 bool SGSoundMgr::exists( const string &refname ) {
     sample_map_iterator sample_it = samples.find( refname );
     if ( sample_it != samples.end() ) {
-       return true;
+        return true;
     } else {
-       return false;
+        return false;
     }
 }
 
@@ -247,9 +261,9 @@ bool SGSoundMgr::exists( const string &refname ) {
 SGSoundSample *SGSoundMgr::find( const string &refname ) {
     sample_map_iterator sample_it = samples.find( refname );
     if ( sample_it != samples.end() ) {
-       return sample_it->second;
+        return sample_it->second;
     } else {
-       return NULL;
+        return NULL;
     }
 }
 
@@ -316,7 +330,7 @@ void SGSoundMgr::set_source_pos_all( ALfloat *pos ) {
     sample_map_iterator sample_current = samples.begin();
     sample_map_iterator sample_end = samples.end();
     for ( ; sample_current != sample_end; ++sample_current ) {
-       SGSoundSample *sample = sample_current->second;
+        SGSoundSample *sample = sample_current->second;
         sample->set_source_pos( pos );
     }
 }
@@ -332,7 +346,7 @@ void SGSoundMgr::set_source_vel_all( ALfloat *vel ) {
     sample_map_iterator sample_current = samples.begin();
     sample_map_iterator sample_end = samples.end();
     for ( ; sample_current != sample_end; ++sample_current ) {
-       SGSoundSample *sample = sample_current->second;
-        sample->set_source_vel( vel );
+        SGSoundSample *sample = sample_current->second;
+        sample->set_source_vel( vel, listener_vel );
     }
 }