somre sound preloading
authorMatthias Braun <matze@braunis.de>
Sun, 14 May 2006 19:35:43 +0000 (19:35 +0000)
committerMatthias Braun <matze@braunis.de>
Sun, 14 May 2006 19:35:43 +0000 (19:35 +0000)
SVN-Revision: 3520

src/audio/sound_manager.cpp
src/audio/sound_manager.hpp
src/badguy/badguy.cpp
src/object/player.cpp

index 0576fe6..fd1be4f 100644 (file)
@@ -16,6 +16,7 @@
 //  You should have received a copy of the GNU General Public License
 //  along with this program; if not, write to the Free Software
 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+#include <config.h>
 
 #include "sound_manager.hpp"
 
@@ -138,6 +139,26 @@ SoundManager::create_sound_source(const std::string& filename)
 }
 
 void
+SoundManager::preload(const std::string& filename)
+{
+  if(!sound_enabled)
+    return;
+  
+  SoundBuffers::iterator i = buffers.find(filename);
+  // already loaded?
+  if(i != buffers.end())
+    return;
+
+  std::auto_ptr<SoundFile> file (load_sound_file(filename));
+  // only keep small files
+  if(file->size >= 100000)
+    return;
+
+  ALuint buffer = load_file_into_buffer(file.get());
+  buffers.insert(std::make_pair(filename, buffer));
+}
+
+void
 SoundManager::play(const std::string& filename, const Vector& pos)
 {
   try {
index c87f890..88f2437 100644 (file)
@@ -51,6 +51,8 @@ public:
    */
   void play(const std::string& name, const Vector& pos = Vector(-1, -1));
   void play_and_delete(SoundSource* source);
+  /// preloads a sound, so that you don't get a lag later when playing it
+  void preload(const std::string& name);
 
   void set_listener_position(const Vector& position);
   void set_listener_velocity(const Vector& velocity);
index 51d5fd9..8718b19 100644 (file)
@@ -16,7 +16,6 @@
 //  You should have received a copy of the GNU General Public License
 //  along with this program; if not, write to the Free Software
 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
-
 #include <config.h>
 
 #include "badguy.hpp"
@@ -37,12 +36,18 @@ BadGuy::BadGuy(const Vector& pos, const std::string& sprite_name, int layer)
   : MovingSprite(pos, sprite_name, layer, COLGROUP_DISABLED), countMe(true), dir(LEFT), state(STATE_INIT) 
 {
   start_position = bbox.p1;
+
+  sound_manager->preload("sounds/squish.wav");
+  sound_manager->preload("sounds/fall.wav");
 }
 
 BadGuy::BadGuy(const lisp::Lisp& reader, const std::string& sprite_name, int layer)
   : MovingSprite(reader, sprite_name, layer, COLGROUP_DISABLED), countMe(true), dir(LEFT), state(STATE_INIT) 
 {
   start_position = bbox.p1;
+
+  sound_manager->preload("sounds/squish.wav");
+  sound_manager->preload("sounds/fall.wav");
 }
 
 void
index eefc7bb..f9efddf 100644 (file)
@@ -109,6 +109,11 @@ Player::Player(PlayerStatus* _player_status)
   smalltux_star = sprite_manager->create("images/creatures/tux_small/smalltux-star.sprite");
   bigtux_star = sprite_manager->create("images/creatures/tux_big/bigtux-star.sprite");
 
+  sound_manager->preload("sounds/bigjump.wav");
+  sound_manager->preload("sounds/jump.wav");
+  sound_manager->preload("sounds/hurt.wav");
+  sound_manager->preload("sounds/skid.wav");
+
   init();
 }