Index: branches/network/src/lib/sound/sound_engine.cc
===================================================================
--- branches/network/src/lib/sound/sound_engine.cc	(revision 5832)
+++ branches/network/src/lib/sound/sound_engine.cc	(revision 5968)
@@ -27,5 +27,5 @@
 #include "resource_manager.h"
 #include "debug.h"
-#include "ini_parser.h"
+#include "parser/ini_parser/ini_parser.h"
 #include "globals.h"
 
@@ -47,4 +47,9 @@
   this->bufferList = NULL;
   this->sourceList = NULL;
+
+  this->device = NULL;
+  this->context = NULL;
+
+  this->maxSourceCount = 32;
 }
 
@@ -64,4 +69,10 @@
     while (this->sourceList->size() > 0)
       delete dynamic_cast<SoundSource*>(this->sourceList->front());
+  }
+
+  while(!this->ALSources.empty())
+  {
+    alDeleteSources(1, &this->ALSources.top());
+    this->ALSources.pop();
   }
 
@@ -86,4 +97,6 @@
 void SoundEngine::loadSettings(IniParser* iniParser)
 {
+  const char* channels = iniParser->getVar(CONFIG_NAME_AUDIO_CHANNELS, CONFIG_SECTION_AUDIO, "32");
+  this->maxSourceCount = atoi(channels);
   const char* musicVolume = iniParser->getVar(CONFIG_NAME_MUSIC_VOLUME, CONFIG_SECTION_AUDIO, "80");
   this->musicVolume = atof(musicVolume)/100.0;
@@ -118,40 +131,22 @@
 
 
-/**
- *  adds a SoundBuffer to the bufferList of the SoundEngine
- * @param buffer The buffer to add to the bufferList
-*/
-void SoundEngine::addBuffer(SoundBuffer* buffer)
-{
-  if (unlikely(this->bufferList == NULL))
-    this->bufferList = ClassList::getList(CL_SOUND_BUFFER);
-}
-
-/**
- *  removes a SoundBuffer from the bufferList of the SoundEngine
- * @param buffer The buffer to delete from the SoundEngine
-*/
-void SoundEngine::removeBuffer(SoundBuffer* buffer)
-{
-  // look if there are any sources that have the buffer still loaded
-  if (this->sourceList != NULL)
-  {
-    list<BaseObject*>::iterator source;
-    for (source = this->sourceList->begin(); source != this->sourceList->end(); source++)
-    {
-      if (buffer == static_cast<SoundSource*>(*source)->getBuffer())
-        delete (*source);
+void SoundEngine::popALSource(ALuint& source)
+{
+  if (source != 0)
+    return;
+  else
+  {
+
+    /// @TODO try to create more sources if needed
+    if (!this->ALSources.empty())
+    {
+
+      source = this->ALSources.top();
+      printf("test: : %d\n", source);
+      this->ALSources.pop();
     }
   }
 }
 
-/**
- * adds a SoundSource to the sourceList of the SoundEngine
- * @param source The source to add to the sourceList
-*/
-void SoundEngine::addSource(SoundSource* source)
-{
-  this->sourceList = ClassList::getList(CL_SOUND_SOURCE);
-}
 
 /**
@@ -180,21 +175,34 @@
 
   // updating all the Sources positions
-  if (likely(this->sourceList != NULL))
-  {
-    list<BaseObject*>::iterator sourceIT;
+  if (likely(this->sourceList != NULL || (this->sourceList = ClassList::getList(CL_SOUND_SOURCE)) != NULL))
+  {
+    list<BaseObject*>::const_iterator sourceIT;
     SoundSource* source;
     for (sourceIT = this->sourceList->begin(); sourceIT != this->sourceList->end(); sourceIT++)
     {
       source = static_cast<SoundSource*>(*sourceIT);
-      if (likely(source->getNode() != NULL))
+      if (source->isPlaying())
       {
-        alSource3f(source->getID(), AL_POSITION,
-                   source->getNode()->getAbsCoor().x,
-                   source->getNode()->getAbsCoor().y,
-                   source->getNode()->getAbsCoor().z);
-        alSource3f(source->getID(), AL_VELOCITY,
-                   source->getNode()->getVelocity().x,
-                   source->getNode()->getVelocity().y,
-                   source->getNode()->getVelocity().z);
+        int play;
+        alGetSourcei(source->getID(), AL_SOURCE_STATE, &play);
+        if(play == AL_PLAYING)
+        {
+          if (likely(source->getNode() != NULL))
+          {
+            alSource3f(source->getID(), AL_POSITION,
+                       source->getNode()->getAbsCoor().x,
+                       source->getNode()->getAbsCoor().y,
+                       source->getNode()->getAbsCoor().z);
+            alSource3f(source->getID(), AL_VELOCITY,
+                       source->getNode()->getVelocity().x,
+                       source->getNode()->getVelocity().y,
+                       source->getNode()->getVelocity().z);
+          }
+
+        }
+        else
+        {
+          source->stop();
+        }
       }
     }
@@ -282,9 +290,14 @@
   // INITIALIZING THE DEVICE:
 
-  ALchar deviceName[] =
+#ifdef AL_VERSION_1_1
+  ALubyte deviceName[] =
+#else
+  ALCchar deviceName[] =
+#endif
+
 #ifdef __WIN32__
-      "native";
+      "Direct3D";
 #else
-      "'( ( devices '( native arts null ) ) )";
+      "'( ( devices '( native null ) ) )";
 #endif
   //
@@ -297,7 +310,33 @@
 
   if ((result = alGetError()) != AL_NO_ERROR)
-    SoundEngine::PrintALErrorString(result);
+    PRINTF(2)("%s\n", SoundEngine::getALErrorString(result));
 
   this->setDopplerValues(SOUND_DOPPLER_FACTOR, SOUND_DOPPLER_VELOCITY);
+}
+
+
+/**
+ * Allocates openAL sources
+ * @param count how many sources to allocate
+ * @returns true on success, false if at least one source could not be allocated
+ */
+bool SoundEngine::allocateSources(unsigned int count)
+{
+  ALenum result;
+  // Setting default values.
+  for (unsigned int i = 0; i < count; i++)
+  {
+    ALuint source;
+
+    alGenSources(1, &source);
+    if ((result = alGetError()) != AL_NO_ERROR)
+      PRINTF(1)("Error Generating Sources: '%s'\n", SoundEngine::getALErrorString(result));
+
+    alSourcef (source, AL_PITCH,    1.0      );
+    alSourcef (source, AL_GAIN,     this->getEffectsVolume() );
+    alSourcei (source, AL_LOOPING,  AL_FALSE );
+    this->ALSources.push(source);
+  }
+  return true;
 }
 
@@ -306,31 +345,20 @@
  * @param err The error found
 */
-void SoundEngine::PrintALErrorString(ALenum err)
+const char* SoundEngine::getALErrorString(ALenum err)
 {
   switch(err)
     {
     case AL_NO_ERROR:
-      PRINTF(4)("AL_NO_ERROR\n");
-      break;
-
+      return ("AL_NO_ERROR");
     case AL_INVALID_NAME:
-      PRINTF(2)("AL_INVALID_NAME\n");
-      break;
-
+      return ("AL_INVALID_NAME");
     case AL_INVALID_ENUM:
-      PRINTF(2)("AL_INVALID_ENUM\n");
-      break;
-
+      return ("AL_INVALID_ENUM");
     case AL_INVALID_VALUE:
-      PRINTF(2)("AL_INVALID_VALUE\n");
-      break;
-
+      return ("AL_INVALID_VALUE");
     case AL_INVALID_OPERATION:
-      PRINTF(2)("AL_INVALID_OPERATION\n");
-      break;
-
+      return ("AL_INVALID_OPERATION");
     case AL_OUT_OF_MEMORY:
-      PRINTF(2)("AL_OUT_OF_MEMORY\n");
-      break;
+      return ("AL_OUT_OF_MEMORY");
     };
 }
@@ -338,5 +366,4 @@
 void SoundEngine::listDevices()
 {
-
   printf("%s\n",(const char*)alcGetString(NULL, ALC_DEVICE_SPECIFIER));
 }
