fix
[supertux.git] / src / audio / sound_manager.cpp
index e463acc..bb26ed2 100644 (file)
 #include <iostream>
 #include <stdexcept>
 #include <sstream>
+#include <physfs.h>
 
 #include "audio/sound_manager.h"
 
 #include "audio/musicref.h"
+#include "physfs/physfs_sdl.h"
 #include "moving_object.h"
 #include "resources.h"
 
@@ -55,30 +57,32 @@ SoundManager::play_sound(const std::string& name,int loops)
     std::cerr << "Sound '" << name << "' not found.\n";
     return -1;
   }
-  return Mix_PlayChannel(-1, chunk, loops);  
+  int chan=Mix_PlayChannel(-1, chunk, loops);  
+  Mix_Volume(chan,MIX_MAX_VOLUME);
+  return chan;
 }
 
 
-void
+int
 SoundManager::play_sound(const std::string& sound, const MovingObject* object,
     const Vector& pos)
 {
   // TODO keep track of the object later and move the sound along with the
   // object.
-  play_sound(sound, object->get_pos(), pos);
+  return play_sound(sound, object->get_pos(), pos);
 }
 
-void
+int
 SoundManager::play_sound(const std::string& sound, const Vector& pos,
     const Vector& pos2)
 {
   if(!audio_device || !m_sound_enabled)
-    return;
+    return -1;
 
   Mix_Chunk* chunk = preload_sound(sound);
   if(chunk == 0) {
     std::cerr << "Sound '" << sound << "' not found.\n";
-    return;                                              
+    return -1;                                               
   }
 
   // TODO make sure this formula is good
@@ -86,11 +90,12 @@ SoundManager::play_sound(const std::string& sound, const Vector& pos,
     = pos2.x- pos.x;
   int loud = int(255.0/float(1600) * fabsf(distance));
   if(loud > 255)
-    return;
+    return -1;
 
   int chan = Mix_PlayChannel(-1, chunk, 0);
   if(chan < 0)
-    return;                                  
+    return -1;         
+  Mix_Volume(chan,MIX_MAX_VOLUME);                         
   Mix_SetDistance(chan, loud);
 
   // very bad way to do this...
@@ -98,6 +103,7 @@ SoundManager::play_sound(const std::string& sound, const Vector& pos,
     Mix_SetPanning(chan, 230, 24);
   else if(distance < -100)
     Mix_SetPanning(chan, 24, 230);
+  return chan;
 }
 
 MusicRef
@@ -118,25 +124,28 @@ SoundManager::load_music(const std::string& file)
 }
 
 bool
-SoundManager::exists_music(const std::string& file)
+SoundManager::exists_music(const std::string& filename)
 {
   if(!audio_device)
     return true;
   
   // song already loaded?
-  std::map<std::string, MusicResource>::iterator i = musics.find(file);
+  std::map<std::string, MusicResource>::iterator i = musics.find(filename);
   if(i != musics.end()) {
     return true;                                      
   }
-  
-  Mix_Music* song = Mix_LoadMUS(file.c_str());
+  const char* dir = PHYSFS_getRealDir(filename.c_str());
+  if(dir == 0)
+    return false;
+  Mix_Music* song = Mix_LoadMUS( (std::string(dir) + "/" + filename).c_str() );
   if(song == 0)
     return false;
 
   // insert into music list
   std::pair<std::map<std::string, MusicResource>::iterator, bool> result = 
     musics.insert(
-        std::make_pair<std::string, MusicResource> (file, MusicResource()));
+        std::make_pair<std::string, MusicResource> (filename, MusicResource()));
   MusicResource& resource = result.first->second;
   resource.manager = this;
   resource.music = song;
@@ -232,9 +241,8 @@ Mix_Chunk* SoundManager::preload_sound(const std::string& name)
   std::string filename = "sounds/";
   filename += name;
   filename += ".wav";
-  filename = get_resource_filename(filename);
   
-  Mix_Chunk* chunk = Mix_LoadWAV(filename.c_str());
+  Mix_Chunk* chunk = Mix_LoadWAV_RW(get_physfs_SDLRWops(filename), true);
   if(chunk != 0) {
     sounds.insert(std::make_pair(name, chunk));
   }