Index: trunk/src/lib/sound/ogg_player.cc
===================================================================
--- trunk/src/lib/sound/ogg_player.cc	(revision 7292)
+++ trunk/src/lib/sound/ogg_player.cc	(revision 7293)
@@ -35,5 +35,6 @@
   this->setClassID(CL_SOUND_OGG_PLAYER, "OggPlayer");
 
-  this->trackLoaded = false;
+  this->state = None;
+
   this->source = 0;
   this->buffers[0] = 0;
@@ -58,34 +59,52 @@
 bool OggPlayer::open(const std::string& fileName)
 {
+  // release old State
+  if (state & FileOpened)
+    release();
+
+  // allocating Buffers
   if (this->buffers[0] == 0)
     alGenBuffers(2, this->buffers);
   SoundEngine::checkError("Allocating Buffers", __LINE__);
-
+  if (this->buffers[0] != 0 && this->buffers[1] != 0)
+    state |= BuffersAllocated;
+  else  {
+    PRINTF(2)("Unable to allocate al-Buffers\n");
+    this->release();
+    return false;
+  }
+  // allocating source
   if (this->source == 0)
     SoundEngine::getInstance()->popALSource(this->source);
-  if (this->source == 0)
-  {
-    this->trackLoaded = false;
-    return false;
-  }
-
+  if (this->source != 0)
+    state |= SourceAllocated;
+  else
+  {
+    PRINTF(2)("No more Sources Availiable (maybe you should consider raising the source-count\n");
+    this->release();
+    return false;
+  }
+
+  // opening the FILE;
   int result;
-
   if(!(oggFile = fopen(fileName.c_str(), "rb")))
   {
     PRINTF(2)("Could not open Ogg file.");
-    return false;
-  }
-
+    this->release();
+    return false;
+  }
+  // reading the Stream.
   if((result = ov_open(oggFile, &oggStream, NULL, 0)) < 0)
   {
     PRINTF(2)("Could not open Ogg stream. %s", errorString(result));
     fclose(oggFile);
-    return false;
-  }
-
+    this->release();
+    return false;
+  }
+
+  // acquiring the vorbis-properties.
   vorbisInfo = ov_info(&oggStream, -1);
   vorbisComment = ov_comment(&oggStream, -1);
-
+  this->state |= FileOpened;
   if(vorbisInfo->channels == 1)
     format = AL_FORMAT_MONO16;
@@ -93,4 +112,5 @@
     format = AL_FORMAT_STEREO16;
 
+  // setting the Source Properties.
   alSource3f(source, AL_POSITION,        0.0, 0.0, 0.0);
   alSource3f(source, AL_VELOCITY,        0.0, 0.0, 0.0);
@@ -101,5 +121,4 @@
   SoundEngine::checkError("OggPlayer::SetSourceProperties", __LINE__);
 
-  this->trackLoaded = true;
   return true;
 }
@@ -110,18 +129,33 @@
 void OggPlayer::release()
 {
-  if (!this->trackLoaded)
-    return;
-  alSourceStop(source);
-  SoundEngine::checkError("OggPlayer::release()::alSourceStop", __LINE__);
-  empty();
-  SoundEngine::getInstance()->pushALSource(source);
-  this->source = 0;
-  alDeleteBuffers(2, buffers);
-  SoundEngine::checkError("OggPlayer::release()::alDeleteBuffers", __LINE__);
-  this->buffers[0] = 0;
-  this->buffers[1] = 0;
-
-  ov_clear(&oggStream);
-  this->trackLoaded = false;
+  if (this->state & SourceAllocated)
+  {
+    assert(alIsSource(this->source));
+    if (this->state & Playing);
+    {
+      alSourceStop(source);
+      SoundEngine::checkError("OggPlayer::release()::alSourceStop", __LINE__);
+      this->state & !Playing;
+    }
+    empty();
+    SoundEngine::getInstance()->pushALSource(source);
+    this->source = 0;
+    this->state &= !SourceAllocated;
+  }
+  if (this->state & BuffersAllocated)
+  {
+    assert (this->buffers[0] != 0 && this->buffers[1] != 0);
+    alDeleteBuffers(2, buffers);
+    SoundEngine::checkError("OggPlayer::release()::alDeleteBuffers", __LINE__);
+    this->buffers[0] = 0;
+    this->buffers[1] = 0;
+    this->state &= !BuffersAllocated;
+  }
+
+  if (this->state & FileOpened)
+  {
+    ov_clear(&oggStream);
+    this->state &= ! FileOpened;
+  }
 }
 
@@ -133,5 +167,5 @@
 bool OggPlayer::playback()
 {
-  if (!this->trackLoaded)
+  if (!(this->state & FileOpened))
     return false;
 
@@ -148,5 +182,5 @@
   if (DEBUG >= 3)
     SoundEngine::checkError("OggPlayer::playback()::alSourcePlay", __LINE__);
-  printf("%d\n", this->source);
+  this->state |= Playing;
   return true;
 }
@@ -158,5 +192,5 @@
 bool OggPlayer::playing()
 {
-  if (!this->trackLoaded)
+  if (!(this->state & FileOpened))
     return false;
   ALenum state;
@@ -173,5 +207,5 @@
 bool OggPlayer::update()
 {
-  if (unlikely(!this->trackLoaded))
+  if (unlikely(!(this->state & Playing)))
     return false;
 
@@ -208,5 +242,5 @@
 bool OggPlayer::stream(ALuint buffer)
 {
-  if (!this->trackLoaded)
+  if (unlikely(!(this->state & Playing)))
     return false;
   char pcm[BUFFER_SIZE];
