--- /dev/null
+// $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;
+}
+
--- /dev/null
+// $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
+
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);
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)
{
mystate = STATE_VANISHING;
sprite->set_action("vanishing", 1);
}
- soundSource->set_position(get_pos());
+ soundSource.set_position(get_pos());
}
if (mystate == STATE_WARPING) {
{
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
#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();
std::string target_sector;
std::string target_spawnpoint;
- SoundSource* soundSource;
+ ManagedSoundSource soundSource;
};
#endif