Index: orxonox/branches/sound/src/sound_control.cc
===================================================================
--- orxonox/branches/sound/src/sound_control.cc	(revision 2854)
+++ orxonox/branches/sound/src/sound_control.cc	(revision 3179)
@@ -18,48 +18,44 @@
 using namespace std;
 
-// global variables
-SoundControl* SoundControl::instance = NULL; // singleton reference
+int sfx_channel1 = -1;
+int sfx_channel2 = -1;
+int finished = 0;
+SoundControl* SoundControl::sound = SoundControl::getInstance();
+SoundControl* SoundControl::instance = 0;
 int volume = SDL_MIX_MAXVOLUME;
-int done = 0;
 int track_number = 1;
-static Mix_Music* music = NULL;
-int audio_rate = MIX_DEFAULT_FREQUENCY, audio_channels = MIX_DEFAULT_CHANNELS, audio_buffers = 16384, bits = 0; 
+Mix_Music* music = NULL;
+int audio_rate = 44100, audio_channels = MIX_DEFAULT_CHANNELS, 
+audio_buffers = 16384, bits = 0; 
 Uint16 audio_format = MIX_DEFAULT_FORMAT;
+SDL_Event event;
 
 
 /** 
     \brief standard constructor 
-    
-    This constructor builds a SoundControl Object, which waits for callers.
+    This constructor builds a SoundControl Object and initialises it .
     All sound output is handled by this singleton object.
 */ 
-SoundControl::SoundControl () { 
-
-  /*
-  initializing sound and calling Mix_OpenAudio 
-  if(SDL_Init(SDL_INIT_AUDIO)<0){ 
+SoundControl::SoundControl() { 
+  if(SDL_Init(SDL_INIT_AUDIO)<0) {
     printf("SDL_Init: INIT_AUDIO error.\n"); 
   } 
-  */
-  
-  if(Mix_OpenAudio(audio_rate, audio_format, audio_channels, audio_buffers)){ 
+  if(Mix_OpenAudio(audio_rate, audio_format, audio_channels, audio_buffers)) { 
     printf("Mix_OpenAudio: Failed to open audio!\n"); 
   } 
-  
   initialise();
 }
 
-
 /** 
     \brief Default destructor 
 */ 
-SoundControl::~SoundControl () { 
+SoundControl::~SoundControl() {
 }
 
 /**
-   \brief Returns a reference to the singleton
+   \brief Returns a reference to the SoundControl singleton
 */
 SoundControl* SoundControl::getInstance() {
-  if (instance == NULL) {
+  if (instance == 0) {
     instance = new SoundControl;
   }
@@ -68,14 +64,10 @@
 
 void SoundControl::deleteInstance() {
-  delete instance;
-  instance = NULL;
-}
-
-/** 
-    \brief Is called by SoundControl object to initiate all values 
+}
+
+/** 
+    \brief Is called by SoundControl object to initiate all values and to output some text
 */ 
 void SoundControl::initialise() { 
-  
-  // Print some info 
   Mix_QuerySpec(&audio_rate, &audio_format, &audio_channels); 
   bits=audio_format&0xFF; 
@@ -84,18 +76,16 @@
 } 
 
-
-/** 
-    \brief Sets the number of output Channels (should not be used)
-*/ 
-void SoundControl::setNumberOfChannels (int number_of_channels) { 
+/** 
+    \brief Sets the number of output Channels
+*/ 
+void SoundControl::setNumberOfChannels(int number_of_channels) { 
   Mix_AllocateChannels(number_of_channels);
 } 
 
-
-/** 
-    \brief May be called from any WorldEntity to play a .xm file 
+/** 
+    \brief Static function to play a .xm file 
     \param filename: self-explanatory 
 */ 
-int SoundControl::playMod (char* fileName) {
+void SoundControl::playMod(char* fileName) {
   Mix_Chunk* chunk = NULL; 
   chunk = Mix_LoadWAV(fileName); 
@@ -105,10 +95,9 @@
 }
 
-
-/** 
-    \brief May be called from any WorldEntity to play a .wav file 
+/** 
+    \brief Static function to play a .wav file 
     \param filename: self-explanatory 
 */ 
-int SoundControl::playWav (char* fileName) {
+void SoundControl::playWav(char* fileName) {
   Mix_Chunk* chunk = NULL; 
   chunk = Mix_LoadWAV(fileName); 
@@ -118,13 +107,12 @@
 } 
 
-
-/** 
-    \brief May be called from any WorldEntity to play a .ogg file 
+/** 
+    \brief Static function to play an .ogg file 
     \param filename: self-explanatory 
 */ 
-int SoundControl::playOgg (char* fileName) {
+void SoundControl::playOgg(char* fileName) {
   Mix_Music* music = NULL;
   music = Mix_LoadMUS(fileName);
-  if(Mix_PlayMusic(music, 1) == -1){
+  if(Mix_PlayMusic(music, 1) == -1) {
     printf("Mix_PlayMusic: %s\n",Mix_GetError());
   }
@@ -132,9 +120,8 @@
 } 
 
-
 /** 
     \brief Heightens the overall volume of output 
 */ 
-void SoundControl::volumeUp () { 
+void SoundControl::volumeUp() { 
   volume = (volume + 1) << 1; 
   if(volume > SDL_MIX_MAXVOLUME) 
@@ -147,25 +134,22 @@
     \brief Lowers the overall volume of output 
 */  
-void SoundControl::volumeDown () { 
+void SoundControl::volumeDown() { 
   volume >>= 1; 
   Mix_VolumeMusic(volume); 
 } 
 
-
 /** 
     \brief Rewinds music to the beginning 
 */  
-void SoundControl::trackRewind () { 
+void SoundControl::trackRewind() { 
   Mix_RewindMusic(); 
 } 
 
-
 /** 
     \brief Rewinds the music 5 seconds 
 */  
-void SoundControl::forwardMusic () { 
+void SoundControl::forwardMusic() { 
   Mix_SetMusicPosition(+5); 
 } 
-
 
 /** 
@@ -176,70 +160,31 @@
 } 
 
-
 /** 
     \brief Pauses music output 
 */ 
-void SoundControl::pauseMusic () {
+void SoundControl::pauseMusic() {
   Mix_PauseMusic();
 } 
 
-
-/** 
-    \brief this function pauses music output 
-*/ 
-void SoundControl::resumeMusic () {
+/** 
+    \brief Pauses music output 
+*/ 
+void SoundControl::resumeMusic() {
   Mix_ResumeMusic();
 } 
 
 /** 
-    \brief Selects the track of all orxonox tracks 
-*/
-void SoundControl::trackSelect() { 
-  switch (track_number) { 
-  case 1:
-    music = Mix_LoadMUS("luke_grey_orxonox1.ogg"); 
-    if(Mix_PlayMusic(music, 1) == -1){
-      printf("Mix_PlayMusic: %s\n",Mix_GetError());
-    }
-    Mix_HookMusicFinished(musicDone);
-    break; 
-  case 2: 
-    music = Mix_LoadMUS("luke_grey_orxonox2.ogg");
-    if(Mix_PlayMusic(music, 1) == -1){
-      printf("Mix_PlayMusic: %s\n",Mix_GetError());
-    }
-    Mix_HookMusicFinished(musicDone);
-    break; 
-  case 3: 
-    music = Mix_LoadMUS("luke_grey_orxonox3.ogg"); 
-    if(Mix_PlayMusic(music, 1) == -1){
-      printf("Mix_PlayMusic: %s\n",Mix_GetError());
-    }
-    Mix_HookMusicFinished(musicDone);    
-    break; 
-  case 4: 
-    music = Mix_LoadMUS("luke_grey_and_aquarius_orxonox.ogg"); 
-    if(Mix_PlayMusic(music, 1) == -1){
-      printf("Mix_PlayMusic: %s\n",Mix_GetError());
-    }
-    Mix_HookMusicFinished(musicDone);    
-    break; 
-  case 5: 
-    music = Mix_LoadMUS("nomenes_orxonox.ogg"); 
-    if(Mix_PlayMusic(music, 1) == -1){
-      printf("Mix_PlayMusic: %s\n",Mix_GetError());
-    }
-    Mix_HookMusicFinished(musicDone);    
-    break; 
-  case 6:
-    music = Mix_LoadMUS("nomenes_funkadudu.ogg");
-    if(Mix_PlayMusic(music, 1) == -1){
-      printf("Mix_PlayMusic: %s\n",Mix_GetError());
-    }
-    Mix_HookMusicFinished(musicDone);
-    break;
-  } 
-}
-
+    \brief Fades in music
+*/ 
+void fadeInMusic(int time) {
+
+}
+
+/** 
+    \brief Fades out music
+*/ 
+void SoundControl::fadeOutMusic(int time) {
+
+}
 
 /** 
@@ -251,2 +196,67 @@
   music = NULL;
 } 
+
+/** 
+    \brief Handles input events
+*/ 
+void SoundControl::handleKey(SDL_KeyboardEvent key) {
+  switch(key.keysym.sym) {
+  case SDLK_a:
+    if(key.type == SDL_KEYDOWN) {
+      if(sfx_channel1 < 0) {
+	sfx_channel1 = 1;
+	sound->playWav("sound1.wav");
+      }
+    } else {
+      Mix_HaltChannel(sfx_channel1);
+      sfx_channel1 = -1;
+    }
+    break;
+  case SDLK_s:
+    if(key.type == SDL_KEYDOWN) {
+      if(sfx_channel2 < 0) {
+	sfx_channel2 = 1;
+	sound->playWav("sound2.wav");
+      }
+    } else {
+      Mix_HaltChannel(sfx_channel2);
+      sfx_channel2 = -1;
+    }
+    break;
+  case SDLK_m:
+    if(key.state == SDL_PRESSED) {
+      sound->playOgg("music.ogg");
+    }
+    break;
+  case SDLK_q:
+    finished = 1;
+    break;
+  default:
+    break;
+  }
+}
+
+int SoundControl::main(int argc, char* argv[]) {
+  SDL_Surface* screen;
+  SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO); 
+  screen = SDL_SetVideoMode(320, 240, 0, 0);
+  while(!finished) {
+    while(SDL_PollEvent(&event)) {
+      switch(event.type) {
+      case SDL_QUIT:
+	finished = 1;
+	break;
+      case SDL_KEYDOWN:
+      case SDL_KEYUP:
+	SoundControl::handleKey(event.key);
+	break;
+      default:
+	break;
+      }
+    }
+    SDL_Delay(50);
+  }
+  deleteInstance();
+  SDL_Quit();
+  return 0;
+}
