]> git.mxchange.org Git - simgear.git/blobdiff - simgear/sound/soundmgr_openal.cxx
Oops, ALUT 1.0 requires a little more work than expected.
[simgear.git] / simgear / sound / soundmgr_openal.cxx
index c438a6531b3d29a208a5a2027ba9ea53e3c7f8dc..534c4430e4cb348c68866c15fcc6a0c0d1e71ab2 100644 (file)
 
 #if defined(__APPLE__)
 # include <OpenAL/al.h>
-# include <OpenAL/alut.h>
 # include <OpenAL/alc.h>
 #else
 # include <AL/al.h>
-# include <AL/alut.h>
 # include <AL/alc.h>
 #endif
 
 #if defined (__APPLE__)
-// any C++ header file undefines isinf and isnan
-// so this should be included before <iostream>
-inline int (isinf)(double r) { return isinf(r); }
-inline int (isnan)(double r) { return isnan(r); } 
-#endif
-
-#if defined(__MINGW32__)
-#define isnan(x) _isnan(x)
+#  ifdef __GNUC__
+#    if ( __GNUC__ >= 3 ) && ( __GNUC_MINOR__ >= 3 )
+//  #        include <math.h>
+inline int (isnan)(double r) { return !(r <= 0 || r >= 0); }
+#    else
+    // any C++ header file undefines isinf and isnan
+    // so this should be included before <iostream>
+    // the functions are STILL in libm (libSystem on mac os x)
+extern "C" int isnan (double);
+extern "C" int isinf (double);
+#    endif
+#  else
+//    inline int (isinf)(double r) { return isinf(r); }
+//    inline int (isnan)(double r) { return isnan(r); }
+#  endif
 #endif
 
 #if defined (__FreeBSD__)
@@ -61,6 +66,9 @@ inline int (isnan)(double r) { return isnan(r); }
 
 #include "soundmgr_openal.hxx"
 
+#if defined(__MINGW32__)
+#define isnan(x) _isnan(x)
+#endif
 
 //
 // Sound Manager
@@ -72,15 +80,26 @@ SGSoundMgr::SGSoundMgr() {
     SG_LOG( SG_GENERAL, SG_INFO, "Initializing OpenAL sound manager" );
 
     // initialize OpenAL
-    alutInit( 0, NULL );
-    atexit(alutExit);
-
-    if ( alGetError() == AL_NO_ERROR) {
+#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 = alcGetCurrentContext();
+#else
+    if ( (dev = alcOpenDevice( NULL )) != NULL
+            && ( context = alcCreateContext( dev, NULL )) != NULL ) {
         working = true;
+        alcMakeContextCurrent( context );
     } else {
         working = false;
+        context = 0;
        SG_LOG( SG_GENERAL, SG_ALERT, "Audio initialization failed!" );
     }
+#endif
 
     listener_pos[0] = 0.0;
     listener_pos[1] = 0.0;
@@ -116,6 +135,12 @@ SGSoundMgr::SGSoundMgr() {
 
 SGSoundMgr::~SGSoundMgr() {
 
+#if defined(ALUT_API_MAJOR_VERSION) && ALUT_API_MAJOR_VERSION >= 1
+    alutExit ();
+#else
+    if (context)
+        alcDestroyContext( context );
+#endif
     //
     // Remove the samples from the sample manager.
     //
@@ -163,11 +188,12 @@ void SGSoundMgr::update( double dt ) {
 void
 SGSoundMgr::pause ()
 {
-    ALCcontext *pCurContext = alcGetCurrentContext();
-    alcSuspendContext( pCurContext );
-    if ( alGetError() != AL_NO_ERROR) {
-       SG_LOG( SG_GENERAL, SG_ALERT,
-                "Oops AL error after soundmgr pause()!" );
+    if (context) {
+        alcSuspendContext( context );
+        if ( alGetError() != AL_NO_ERROR) {
+           SG_LOG( SG_GENERAL, SG_ALERT,
+                    "Oops AL error after soundmgr pause()!" );
+        }
     }
 }
 
@@ -175,11 +201,12 @@ SGSoundMgr::pause ()
 void
 SGSoundMgr::resume ()
 {
-    ALCcontext *pCurContext = alcGetCurrentContext();
-    alcProcessContext( pCurContext );
-    if ( alGetError() != AL_NO_ERROR) {
-       SG_LOG( SG_GENERAL, SG_ALERT,
-                "Oops AL error after soundmgr resume()!" );
+    if (context) {
+        alcProcessContext( context );
+        if ( alGetError() != AL_NO_ERROR) {
+           SG_LOG( SG_GENERAL, SG_ALERT,
+                    "Oops AL error after soundmgr resume()!" );
+        }
     }
 }