Trying out new ManagedSoundSource
authorChristoph Sommer <mail@christoph-sommer.de>
Fri, 26 May 2006 11:37:45 +0000 (11:37 +0000)
committerChristoph Sommer <mail@christoph-sommer.de>
Fri, 26 May 2006 11:37:45 +0000 (11:37 +0000)
SVN-Revision: 3590

src/audio/managed_sound_source.cpp [new file with mode: 0644]
src/audio/managed_sound_source.hpp [new file with mode: 0644]
src/badguy/willowisp.cpp
src/badguy/willowisp.hpp

diff --git a/src/audio/managed_sound_source.cpp b/src/audio/managed_sound_source.cpp
new file mode 100644 (file)
index 0000000..3644298
--- /dev/null
@@ -0,0 +1,45 @@
+//  $Id$
+// 
+//  SuperTux - Managed Sound Source
+//  Copyright (C) 2006 Christoph Sommer <christoph.sommer@2006.expires.deltadevelopment.de>
+//
+//  This program is free software; you can redistribute it and/or
+//  modify it under the terms of the GNU General Public License
+//  as published by the Free Software Foundation; either version 2
+//  of the License, or (at your option) any later version.
+//
+//  This program is distributed in the hope that it will be useful,
+//  but WITHOUT ANY WARRANTY; without even the implied warranty of
+//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+//  GNU General Public License for more details.
+// 
+//  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 "audio/managed_sound_source.hpp"
+#include "log.hpp"
+
+ManagedSoundSource::ManagedSoundSource(SoundManager* sound_manager, const std::string& name)
+  : name(name), soundManager(sound_manager)
+{
+  soundSource = soundManager->create_sound_source(name);
+  if(!soundSource) {
+    log_warning << "Couldn't create managed sound source for \"" << name << "\"" << std::endl;
+    return;
+  }
+}
+
+ManagedSoundSource::ManagedSoundSource(const ManagedSoundSource& other) 
+       : name(other.name), soundManager(other.soundManager)
+{
+  soundSource = soundManager->create_sound_source(name);
+}
+
+ManagedSoundSource::~ManagedSoundSource()
+{
+  delete soundSource;
+}
+
diff --git a/src/audio/managed_sound_source.hpp b/src/audio/managed_sound_source.hpp
new file mode 100644 (file)
index 0000000..d22b72a
--- /dev/null
@@ -0,0 +1,70 @@
+//  $Id$
+// 
+//  SuperTux - Managed Sound Source
+//  Copyright (C) 2006 Christoph Sommer <christoph.sommer@2006.expires.deltadevelopment.de>
+//
+//  This program is free software; you can redistribute it and/or
+//  modify it under the terms of the GNU General Public License
+//  as published by the Free Software Foundation; either version 2
+//  of the License, or (at your option) any later version.
+//
+//  This program is distributed in the hope that it will be useful,
+//  but WITHOUT ANY WARRANTY; without even the implied warranty of
+//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+//  GNU General Public License for more details.
+// 
+//  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.
+
+#ifndef __MANAGED_SOUND_SOURCE_H__
+#define __MANAGED_SOUND_SOURCE_H__
+
+#include <string>
+#include "audio/sound_manager.hpp"
+#include "audio/sound_source.hpp"
+
+/**
+ * Encapsulates a SoundSource that is managed by a SoundManager
+ */
+class ManagedSoundSource
+{
+public:
+  ManagedSoundSource(SoundManager* sound_manager, const std::string& name);
+  ManagedSoundSource(const ManagedSoundSource& managed_sound_source);
+  virtual ManagedSoundSource* clone() const { return new ManagedSoundSource(*this); }
+  virtual ~ManagedSoundSource();
+
+  /**
+   * pre-loads SoundSource and indicates success.
+   * If pre-loading failed, the SoundSource will be loaded at first use
+   */
+  bool preload() { return (soundSource != 0); /* TODO: implement */ }
+
+  /**
+   * stops playing and temporarily releases memory for the SoundSource.
+   * Memory will be re-allocated on next use
+   */
+  void release() { if (playing()) stop(); /* TODO: implement*/ }
+
+  void play() { if (preload()) soundSource->play(); }
+  void stop() { if (preload()) soundSource->stop(); }
+  bool playing() { return ((preload())?(soundSource->playing()):(false)); }
+  void set_looping(bool looping) { if (preload()) soundSource->set_looping(looping); }
+  void set_gain(float gain) { if (preload()) soundSource->set_gain(gain); }
+  void set_pitch(float pitch) { if (preload()) soundSource->set_pitch(pitch); }
+  void set_position(Vector position) { if (preload()) soundSource->set_position(position); }
+  void set_velocity(Vector position) { if (preload()) soundSource->set_velocity(position); }
+  void set_reference_distance(float distance) { if (preload()) soundSource->set_reference_distance(distance); }
+
+protected:
+  std::string name;
+  SoundSource* soundSource;
+  SoundManager* soundManager;
+
+private:
+};
+
+#endif
+
index a375543..69de916 100644 (file)
@@ -28,7 +28,7 @@ static const float TRACK_RANGE = 384; /**< at what distance to start tracking th
 static const float VANISH_RANGE = 512; /**< at what distance to stop tracking and vanish */
 
 WillOWisp::WillOWisp(const lisp::Lisp& reader)
-  : BadGuy(reader, "images/creatures/willowisp/willowisp.sprite", LAYER_FLOATINGOBJECTS), mystate(STATE_IDLE), target_sector("main"), target_spawnpoint("main"), soundSource(0)
+  : BadGuy(reader, "images/creatures/willowisp/willowisp.sprite", LAYER_FLOATINGOBJECTS), mystate(STATE_IDLE), target_sector("main"), target_spawnpoint("main"), soundSource(sound_manager, "sounds/willowisp.wav")
 {
   reader.get("sector", target_sector);
   reader.get("spawnpoint", target_spawnpoint);
@@ -36,17 +36,6 @@ WillOWisp::WillOWisp(const lisp::Lisp& reader)
   countMe = false;
 }
 
-WillOWisp::WillOWisp(const WillOWisp& other) 
-       : BadGuy(other), mystate(other.mystate), target_sector(other.target_sector), target_spawnpoint(other.target_spawnpoint)
-{
-  soundSource = sound_manager->create_sound_source("sounds/willowisp.wav");
-}
-
-WillOWisp::~WillOWisp()
-{
-  delete soundSource;
-}
-
 void
 WillOWisp::write(lisp::Writer& writer)
 {
@@ -96,7 +85,7 @@ WillOWisp::active_update(float elapsed_time)
       mystate = STATE_VANISHING;
       sprite->set_action("vanishing", 1);
     }
-    soundSource->set_position(get_pos());
+    soundSource.set_position(get_pos());
   }
 
   if (mystate == STATE_WARPING) {
@@ -118,24 +107,30 @@ WillOWisp::activate()
 {
   sprite->set_action("idle");
 
-  delete soundSource;
-  soundSource = sound_manager->create_sound_source("sounds/willowisp.wav");
-  if(!soundSource) {
-    log_warning << "Couldn't start WillOWisp sound" << std::endl;
-    return;
-  }
-  soundSource->set_position(get_pos());
-  soundSource->set_looping(true);
-  soundSource->set_gain(2.0);
-  soundSource->set_reference_distance(32);
-  soundSource->play();
+  soundSource.set_position(get_pos());
+  soundSource.set_looping(true);
+  soundSource.set_gain(2.0);
+  soundSource.set_reference_distance(32);
+  soundSource.play();
 }
 
 void
 WillOWisp::deactivate()
 {
-  delete soundSource;
-  soundSource = 0;
+  soundSource.stop();
+  soundSource.release();
+
+  switch (mystate) {
+    case STATE_IDLE:
+      break;
+    case STATE_TRACKING:
+      mystate = STATE_IDLE;
+      break;
+    case STATE_WARPING:
+    case STATE_VANISHING:
+      remove_me();
+      break;
+  }
 }
 
 void
index cfbcb19..ce45ced 100644 (file)
 #define __WILLOWISP_H__
 
 #include "badguy.hpp"
+#include "audio/managed_sound_source.hpp"
 
 class WillOWisp : public BadGuy
 {
 public:
   WillOWisp(const lisp::Lisp& reader);
-  WillOWisp(const WillOWisp& willowisp);
-  ~WillOWisp();
 
   void activate();
   void deactivate();
@@ -53,7 +52,7 @@ private:
   std::string target_sector;
   std::string target_spawnpoint;
 
-  SoundSource* soundSource;
+  ManagedSoundSource soundSource;
 };
 
 #endif