From b4868c421ba670cd6301394558f1034e38a0c7b3 Mon Sep 17 00:00:00 2001 From: Matthias Braun Date: Fri, 26 May 2006 17:37:36 +0000 Subject: [PATCH] - Remove ManagedSoundSource - Make SoundManage::create_sound_source always return a valid object, it returns a dummy sound source now if something fails SVN-Revision: 3595 --- Jamrules | 2 +- src/audio/dummy_sound_source.cpp | 82 ++++++++++++++++++++++ src/audio/dummy_sound_source.hpp | 27 +++++++ src/audio/managed_sound_source.cpp | 80 --------------------- src/audio/managed_sound_source.hpp | 70 ------------------ .../{sound_source.cpp => openal_sound_source.cpp} | 35 +++++---- src/audio/openal_sound_source.hpp | 53 ++++++++++++++ src/audio/sound_manager.cpp | 60 +++++++++------- src/audio/sound_manager.hpp | 11 ++- src/audio/sound_source.hpp | 37 +++++----- src/audio/stream_sound_source.hpp | 4 +- src/badguy/badguy.hpp | 1 - src/badguy/dart.cpp | 31 ++++---- src/badguy/dart.hpp | 2 +- src/badguy/flame.cpp | 37 ++++------ src/badguy/flame.hpp | 4 +- src/badguy/willowisp.cpp | 20 +++--- src/badguy/willowisp.hpp | 4 +- src/gui/menu.cpp | 4 -- src/object/coin.cpp | 3 +- src/object/moving_sprite.hpp | 2 - src/object/scripted_object.cpp | 2 + src/sector.hpp | 2 +- 23 files changed, 287 insertions(+), 286 deletions(-) create mode 100644 src/audio/dummy_sound_source.cpp create mode 100644 src/audio/dummy_sound_source.hpp delete mode 100644 src/audio/managed_sound_source.cpp delete mode 100644 src/audio/managed_sound_source.hpp rename src/audio/{sound_source.cpp => openal_sound_source.cpp} (73%) create mode 100644 src/audio/openal_sound_source.hpp diff --git a/Jamrules b/Jamrules index 920185f5a..5c355902d 100644 --- a/Jamrules +++ b/Jamrules @@ -52,7 +52,7 @@ if $(XGETTEXT) != "" { actions XGetText { - $(XGETTEXT) $(XGETTEXT_FLAGS) --keyword='_:1' -o $(<) "$(>)" + $(XGETTEXT) $(XGETTEXT_FLAGS) -o $(<) "$(>)" } rule MakePot { diff --git a/src/audio/dummy_sound_source.cpp b/src/audio/dummy_sound_source.cpp new file mode 100644 index 000000000..13c1c2e03 --- /dev/null +++ b/src/audio/dummy_sound_source.cpp @@ -0,0 +1,82 @@ +// $Id: sound_source.hpp 3462 2006-04-28 19:38:41Z sommer $ +// +// SuperTux +// Copyright (C) 2006 Matthias Braun +// +// 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 + +#include "dummy_sound_source.hpp" + +class DummySoundSource : public SoundSource +{ +public: + DummySoundSource() + {} + virtual ~DummySoundSource() + {} + + virtual void play() + { + is_playing = true; + } + + virtual void stop() + { + is_playing = false; + } + + virtual bool playing() + { + return is_playing; + } + + virtual void set_looping(bool ) + { + } + + virtual void set_gain(float ) + { + } + + virtual void set_pitch(float ) + { + } + + virtual void set_position(const Vector& ) + { + } + + virtual void set_velocity(const Vector& ) + { + } + + virtual void set_reference_distance(float ) + { + } + + virtual void set_rollof_factor(float ) + { + } + +private: + bool is_playing; +}; + +SoundSource* create_dummy_sound_source() +{ + return new DummySoundSource(); +} + diff --git a/src/audio/dummy_sound_source.hpp b/src/audio/dummy_sound_source.hpp new file mode 100644 index 000000000..53a3e911d --- /dev/null +++ b/src/audio/dummy_sound_source.hpp @@ -0,0 +1,27 @@ +// $Id: sound_source.hpp 3462 2006-04-28 19:38:41Z sommer $ +// +// SuperTux +// Copyright (C) 2006 Matthias Braun +// +// 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 __DUMMY_SOUND_SOURCE_HPP__ +#define __DUMMY_SOUND_SOURCE_HPP__ + +#include "sound_source.hpp" + +SoundSource* create_dummy_sound_source(); + +#endif + diff --git a/src/audio/managed_sound_source.cpp b/src/audio/managed_sound_source.cpp deleted file mode 100644 index 167608f8e..000000000 --- a/src/audio/managed_sound_source.cpp +++ /dev/null @@ -1,80 +0,0 @@ -// $Id$ -// -// SuperTux - Managed Sound Source -// Copyright (C) 2006 Christoph Sommer -// -// 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 - -#include "audio/managed_sound_source.hpp" -#include "log.hpp" - -ManagedSoundSource::ManagedSoundSource(SoundManager* sound_manager, const std::string& name) - : name(name), soundSource(0), soundManager(sound_manager) -{ -} - -ManagedSoundSource::ManagedSoundSource(const ManagedSoundSource& other) - : name(other.name), soundSource(0), soundManager(other.soundManager) -{ -} - -ManagedSoundSource::~ManagedSoundSource() -{ - delete soundSource; -} - -bool -ManagedSoundSource::preload() -{ - if (soundSource) return true; - soundSource = soundManager->create_sound_source(name); - return (soundSource != 0); -} - -void -ManagedSoundSource::release() -{ - if (!soundSource) return; - if (playing()) soundSource->stop(); - delete soundSource; - soundSource = 0; -} - -void -ManagedSoundSource::play() -{ - if (!preload()) { - log_warning << "Couldn't play \"" << name << "\"" << std::endl; - return; - } - soundSource->play(); -} - -void -ManagedSoundSource::stop() -{ - // FIXME: calling release() instead of stop() seems necessary due to an unconfirmed sound bug - release(); -} - -bool -ManagedSoundSource::playing() -{ - if (!soundSource) return false; - return soundSource->playing(); -} - diff --git a/src/audio/managed_sound_source.hpp b/src/audio/managed_sound_source.hpp deleted file mode 100644 index cebf6ad09..000000000 --- a/src/audio/managed_sound_source.hpp +++ /dev/null @@ -1,70 +0,0 @@ -// $Id$ -// -// SuperTux - Managed Sound Source -// Copyright (C) 2006 Christoph Sommer -// -// 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 -#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(); - - /** - * stops playing and temporarily releases memory for the SoundSource. - * Memory will be re-allocated on next use - */ - void release(); - - void play(); - void stop(); - bool playing(); - 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 - diff --git a/src/audio/sound_source.cpp b/src/audio/openal_sound_source.cpp similarity index 73% rename from src/audio/sound_source.cpp rename to src/audio/openal_sound_source.cpp index 986489483..e5c586188 100644 --- a/src/audio/sound_source.cpp +++ b/src/audio/openal_sound_source.cpp @@ -16,42 +16,41 @@ // 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 -#include "sound_source.hpp" +#include "openal_sound_source.hpp" #include "sound_manager.hpp" -SoundSource::SoundSource() +OpenALSoundSource::OpenALSoundSource() { alGenSources(1, &source); SoundManager::check_al_error("Couldn't create audio source: "); set_reference_distance(128); } -SoundSource::~SoundSource() +OpenALSoundSource::~OpenALSoundSource() { stop(); + alSourcei(source, AL_BUFFER, AL_NONE); alDeleteSources(1, &source); } void -SoundSource::stop() +OpenALSoundSource::stop() { alSourceStop(source); - alSourcei(source, AL_BUFFER, AL_NONE); SoundManager::check_al_error("Problem stopping audio source: "); } void -SoundSource::play() +OpenALSoundSource::play() { alSourcePlay(source); SoundManager::check_al_error("Couldn't start audio source: "); } bool -SoundSource::playing() +OpenALSoundSource::playing() { ALint state = AL_PLAYING; alGetSourcei(source, AL_SOURCE_STATE, &state); @@ -59,42 +58,48 @@ SoundSource::playing() } void -SoundSource::update() +OpenALSoundSource::update() { } void -SoundSource::set_looping(bool looping) +OpenALSoundSource::set_looping(bool looping) { alSourcei(source, AL_LOOPING, looping ? AL_TRUE : AL_FALSE); } void -SoundSource::set_position(Vector position) +OpenALSoundSource::set_position(const Vector& position) { alSource3f(source, AL_POSITION, position.x, position.y, 0); } void -SoundSource::set_velocity(Vector velocity) +OpenALSoundSource::set_velocity(const Vector& velocity) { alSource3f(source, AL_VELOCITY, velocity.x, velocity.y, 0); } void -SoundSource::set_gain(float gain) +OpenALSoundSource::set_gain(float gain) { alSourcef(source, AL_GAIN, gain); } void -SoundSource::set_pitch(float pitch) +OpenALSoundSource::set_pitch(float pitch) { alSourcef(source, AL_PITCH, pitch); } void -SoundSource::set_reference_distance(float distance) +OpenALSoundSource::set_reference_distance(float distance) { alSourcef(source, AL_REFERENCE_DISTANCE, distance); } + +void +OpenALSoundSource::set_rollof_factor(float factor) +{ + alSourcef(source, AL_ROLLOFF_FACTOR, factor); +} diff --git a/src/audio/openal_sound_source.hpp b/src/audio/openal_sound_source.hpp new file mode 100644 index 000000000..75b6ef680 --- /dev/null +++ b/src/audio/openal_sound_source.hpp @@ -0,0 +1,53 @@ +// $Id: sound_source.hpp 3462 2006-04-28 19:38:41Z sommer $ +// +// SuperTux +// Copyright (C) 2006 Matthias Braun +// +// 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 __OPENAL_SOUND_SOURCE_H__ +#define __OPENAL_SOUND_SOURCE_H__ + +#include +#include "math/vector.hpp" +#include "sound_source.hpp" + +class OpenALSoundSource : public SoundSource +{ +public: + OpenALSoundSource(); + virtual ~OpenALSoundSource(); + + virtual void play(); + virtual void stop(); + virtual bool playing(); + + virtual void update(); + + virtual void set_looping(bool looping); + virtual void set_gain(float gain); + virtual void set_pitch(float pitch); + virtual void set_position(const Vector& position); + virtual void set_velocity(const Vector& position); + virtual void set_reference_distance(float distance); + virtual void set_rollof_factor(float factor); + +protected: + friend class SoundManager; + + ALuint source; +}; + +#endif + diff --git a/src/audio/sound_manager.cpp b/src/audio/sound_manager.cpp index 6905621fa..0c77b997e 100644 --- a/src/audio/sound_manager.cpp +++ b/src/audio/sound_manager.cpp @@ -27,7 +27,9 @@ #include "sound_file.hpp" #include "sound_source.hpp" +#include "openal_sound_source.hpp" #include "stream_sound_source.hpp" +#include "dummy_sound_source.hpp" #include "log.hpp" #include "timer.hpp" @@ -112,7 +114,7 @@ SoundSource* SoundManager::create_sound_source(const std::string& filename) { if(!sound_enabled) - return 0; + return create_dummy_sound_source(); ALuint buffer; @@ -121,20 +123,25 @@ SoundManager::create_sound_source(const std::string& filename) if(i != buffers.end()) { buffer = i->second; } else { - // Load sound file - std::auto_ptr file (load_sound_file(filename)); - - if(file->size < 100000) { - buffer = load_file_into_buffer(file.get()); - buffers.insert(std::make_pair(filename, buffer)); - } else { - StreamSoundSource* source = new StreamSoundSource(); - source->set_sound_file(file.release()); - return source; + try { + // Load sound file + std::auto_ptr file (load_sound_file(filename)); + + if(file->size < 100000) { + buffer = load_file_into_buffer(file.get()); + buffers.insert(std::make_pair(filename, buffer)); + } else { + StreamSoundSource* source = new StreamSoundSource(); + source->set_sound_file(file.release()); + return source; + } + } catch(std::exception& e) { + log_warning << "Couldn't load soundfile '" << filename << "': " << e.what() << std::endl; + return create_dummy_sound_source(); } } - SoundSource* source = new SoundSource(); + OpenALSoundSource* source = new OpenALSoundSource(); alSourcei(source->source, AL_BUFFER, buffer); return source; } @@ -162,12 +169,15 @@ SoundManager::preload(const std::string& filename) void SoundManager::play(const std::string& filename, const Vector& pos) { + if(!sound_enabled) + return; + try { - SoundSource* source = create_sound_source(filename); - if(source == NULL) - return; + OpenALSoundSource* source + = static_cast (create_sound_source(filename)); + if(pos == Vector(-1, -1)) { - alSourcef(source->source, AL_ROLLOFF_FACTOR, 0); + source->set_rollof_factor(0); } else { source->set_position(pos); } @@ -179,17 +189,13 @@ SoundManager::play(const std::string& filename, const Vector& pos) } void -SoundManager::play_and_delete(SoundSource* source) +SoundManager::manage_source(SoundSource* source) { - if (!source) { - log_debug << "ignoring NULL SoundSource" << std::endl; - return; - } - try { - source->play(); - sources.push_back(source); - } catch(std::exception& e) { - log_warning << "Couldn't play SoundSource: " << e.what() << std::endl; + assert(source != NULL); + + OpenALSoundSource* openal_source = dynamic_cast (source); + if(openal_source != NULL) { + sources.push_back(openal_source); } } @@ -294,7 +300,7 @@ SoundManager::update() // update and check for finished sound sources for(SoundSources::iterator i = sources.begin(); i != sources.end(); ) { - SoundSource* source = *i; + OpenALSoundSource* source = *i; source->update(); diff --git a/src/audio/sound_manager.hpp b/src/audio/sound_manager.hpp index 88f2437f2..a715d9976 100644 --- a/src/audio/sound_manager.hpp +++ b/src/audio/sound_manager.hpp @@ -30,6 +30,7 @@ class SoundFile; class SoundSource; class StreamSoundSource; +class OpenALSoundSource; class SoundManager { @@ -50,7 +51,11 @@ public: * Convenience function to simply play a sound at a given position. */ void play(const std::string& name, const Vector& pos = Vector(-1, -1)); - void play_and_delete(SoundSource* source); + /** + * Adds the source to the list of managed sources (= the source gets deleted + * when it finished playing) + */ + void manage_source(SoundSource* source); /// preloads a sound, so that you don't get a lag later when playing it void preload(const std::string& name); @@ -71,7 +76,7 @@ public: void update(); private: - friend class SoundSource; + friend class OpenALSoundSource; friend class StreamSoundSource; static ALuint load_file_into_buffer(SoundFile* file); @@ -87,7 +92,7 @@ private: typedef std::map SoundBuffers; SoundBuffers buffers; - typedef std::vector SoundSources; + typedef std::vector SoundSources; SoundSources sources; StreamSoundSource* music_source; diff --git a/src/audio/sound_source.hpp b/src/audio/sound_source.hpp index d878ef42a..3824c3c1d 100644 --- a/src/audio/sound_source.hpp +++ b/src/audio/sound_source.hpp @@ -16,37 +16,34 @@ // 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 __SOUND_SOURCE_H__ #define __SOUND_SOURCE_H__ -#include #include "math/vector.hpp" +/** + * A sound source represents the source of audio output. You can place + * sources at certain points in your world or set their velocity to produce + * doppler effects + */ class SoundSource { public: - SoundSource(); - virtual ~SoundSource(); - - void play(); - void stop(); - bool playing(); + virtual ~SoundSource() + { } - virtual void update(); + virtual void play() = 0; + virtual void stop() = 0; + virtual bool playing() = 0; - void set_looping(bool looping); + virtual void set_looping(bool looping) = 0; /// Set volume (0.0 is silent, 1.0 is normal) - void set_gain(float gain); - void set_pitch(float pitch); - void set_position(Vector position); - void set_velocity(Vector position); - void set_reference_distance(float distance); - -protected: - friend class SoundManager; - - ALuint source; + virtual void set_gain(float gain) = 0; + virtual void set_pitch(float pitch) = 0; + virtual void set_position(const Vector& position) = 0; + virtual void set_velocity(const Vector& position) = 0; + virtual void set_reference_distance(float distance) = 0; + virtual void set_rollof_factor(float factor) = 0; }; #endif diff --git a/src/audio/stream_sound_source.hpp b/src/audio/stream_sound_source.hpp index 18b2dc979..de1dbc05c 100644 --- a/src/audio/stream_sound_source.hpp +++ b/src/audio/stream_sound_source.hpp @@ -22,11 +22,11 @@ #include #include -#include "sound_source.hpp" +#include "openal_sound_source.hpp" class SoundFile; -class StreamSoundSource : public SoundSource +class StreamSoundSource : public OpenALSoundSource { public: StreamSoundSource(); diff --git a/src/badguy/badguy.hpp b/src/badguy/badguy.hpp index eee973f52..425213da7 100644 --- a/src/badguy/badguy.hpp +++ b/src/badguy/badguy.hpp @@ -43,7 +43,6 @@ class BadGuy : public MovingSprite, public Serializable public: BadGuy(const Vector& pos, const std::string& sprite_name, int layer = LAYER_OBJECTS); BadGuy(const lisp::Lisp& reader, const std::string& sprite_name, int layer = LAYER_OBJECTS); - virtual BadGuy* clone() const = 0; /** Called when the badguy is drawn. The default implementation simply draws * the badguy sprite on screen diff --git a/src/badguy/dart.cpp b/src/badguy/dart.cpp index ce9a95608..a4cc63e2d 100644 --- a/src/badguy/dart.cpp +++ b/src/badguy/dart.cpp @@ -26,15 +26,17 @@ namespace { const float SPEED = 200; } +static const std::string SOUNDFILE = "sounds/flame.wav"; + Dart::Dart(const lisp::Lisp& reader) - : BadGuy(reader, "images/creatures/dart/dart.sprite"), set_direction(false), parent(0), soundSource(0) + : BadGuy(reader, "images/creatures/dart/dart.sprite"), set_direction(false), parent(0) { physic.enable_gravity(false); countMe = false; } Dart::Dart(const Vector& pos, Direction d, const BadGuy* parent = 0) - : BadGuy(pos, "images/creatures/dart/dart.sprite"), set_direction(true), initial_direction(d), parent(parent), soundSource(0) + : BadGuy(pos, "images/creatures/dart/dart.sprite"), set_direction(true), initial_direction(d), parent(parent) { physic.enable_gravity(false); countMe = false; @@ -43,12 +45,11 @@ Dart::Dart(const Vector& pos, Direction d, const BadGuy* parent = 0) Dart::Dart(const Dart& other) : BadGuy(other), set_direction(other.set_direction), initial_direction(other.initial_direction), parent(other.parent) { - soundSource = sound_manager->create_sound_source("sounds/flame.wav"); + sound_source.reset(sound_manager->create_sound_source(SOUNDFILE)); } Dart::~Dart() { - delete soundSource; } bool @@ -77,24 +78,18 @@ Dart::activate() physic.set_velocity_x(dir == LEFT ? -::SPEED : ::SPEED); sprite->set_action(dir == LEFT ? "flying-left" : "flying-right"); - delete soundSource; - soundSource = sound_manager->create_sound_source("sounds/flame.wav"); - if(soundSource) { - soundSource->set_position(get_pos()); - soundSource->set_looping(true); - soundSource->set_gain(1.0); - soundSource->set_reference_distance(32); - soundSource->play(); - } else { - log_warning << "Couldn't start Dart ambient sound" << std::endl; - } + sound_source.reset(sound_manager->create_sound_source(SOUNDFILE)); + sound_source->set_position(get_pos()); + sound_source->set_looping(true); + sound_source->set_gain(1.0); + sound_source->set_reference_distance(32); + sound_source->play(); } void Dart::deactivate() { - delete soundSource; - soundSource = 0; + sound_source.release(); remove_me(); } @@ -102,7 +97,7 @@ void Dart::active_update(float elapsed_time) { BadGuy::active_update(elapsed_time); - if (soundSource) soundSource->set_position(get_pos()); + sound_source->set_position(get_pos()); } diff --git a/src/badguy/dart.hpp b/src/badguy/dart.hpp index 6a1c20d8a..1c6a3d33d 100644 --- a/src/badguy/dart.hpp +++ b/src/badguy/dart.hpp @@ -52,7 +52,7 @@ protected: bool set_direction; Direction initial_direction; const BadGuy* parent; /**< collisions with this BadGuy will be ignored */ - SoundSource* soundSource; /**< SoundSource for ambient sound */ + std::auto_ptr sound_source; /**< SoundSource for ambient sound */ }; #endif diff --git a/src/badguy/flame.cpp b/src/badguy/flame.cpp index 330d8280b..19211d2a8 100644 --- a/src/badguy/flame.cpp +++ b/src/badguy/flame.cpp @@ -22,22 +22,17 @@ #include "flame.hpp" #include "log.hpp" +static const std::string SOUNDFILE = "sounds/flame.wav"; + Flame::Flame(const lisp::Lisp& reader) - : BadGuy(reader, "images/creatures/flame/flame.sprite", LAYER_FLOATINGOBJECTS), angle(0), radius(100), speed(2), source(0) + : BadGuy(reader, "images/creatures/flame/flame.sprite", LAYER_FLOATINGOBJECTS), angle(0), radius(100), speed(2), sound_source(0) { reader.get("radius", radius); reader.get("speed", speed); bbox.set_pos(Vector(start_position.x + cos(angle) * radius, start_position.y + sin(angle) * radius)); countMe = false; -} - -Flame::Flame(const Flame& other) - : BadGuy(other), angle(other.angle), radius(other.radius), speed(other.speed) -{ - if (sound_manager->is_sound_enabled()) { - source.reset(sound_manager->create_sound_source("sounds/flame.wav")); - } + sound_manager->preload(SOUNDFILE); } void @@ -61,8 +56,7 @@ Flame::active_update(float elapsed_time) start_position.y + sin(angle) * radius); movement = newpos - get_pos(); - if (sound_manager->is_sound_enabled()) - source->set_position(get_pos()); + sound_source->set_position(get_pos()); } void @@ -70,25 +64,18 @@ Flame::activate() { set_group(COLGROUP_TOUCHABLE); - if (!sound_manager->is_sound_enabled()) - return; - - source.reset(sound_manager->create_sound_source("sounds/flame.wav")); - if(source.get() == NULL) { - log_warning << "Couldn't start flame sound" << std::endl; - return; - } - source->set_position(get_pos()); - source->set_looping(true); - source->set_gain(2.0); - source->set_reference_distance(32); - source->play(); + sound_source.reset(sound_manager->create_sound_source(SOUNDFILE)); + sound_source->set_position(get_pos()); + sound_source->set_looping(true); + sound_source->set_gain(2.0); + sound_source->set_reference_distance(32); + sound_source->play(); } void Flame::deactivate() { - source.release(); + sound_source.release(); } void diff --git a/src/badguy/flame.hpp b/src/badguy/flame.hpp index 303c69848..b099eae5d 100644 --- a/src/badguy/flame.hpp +++ b/src/badguy/flame.hpp @@ -34,14 +34,12 @@ public: void active_update(float elapsed_time); void kill_fall(); - virtual Flame* clone() const { return new Flame(*this); } - private: float angle; float radius; float speed; - std::auto_ptr source; + std::auto_ptr sound_source; }; #endif diff --git a/src/badguy/willowisp.cpp b/src/badguy/willowisp.cpp index 69de9166d..6480c1675 100644 --- a/src/badguy/willowisp.cpp +++ b/src/badguy/willowisp.cpp @@ -26,14 +26,16 @@ static const float FLYSPEED = 64; /**< speed in px per second */ static const float TRACK_RANGE = 384; /**< at what distance to start tracking the player */ static const float VANISH_RANGE = 512; /**< at what distance to stop tracking and vanish */ +static const std::string SOUNDFILE = "sounds/willowisp.wav"; 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(sound_manager, "sounds/willowisp.wav") + : BadGuy(reader, "images/creatures/willowisp/willowisp.sprite", LAYER_FLOATINGOBJECTS), mystate(STATE_IDLE), target_sector("main"), target_spawnpoint("main") { reader.get("sector", target_sector); reader.get("spawnpoint", target_spawnpoint); countMe = false; + sound_manager->preload(SOUNDFILE); } void @@ -85,7 +87,7 @@ WillOWisp::active_update(float elapsed_time) mystate = STATE_VANISHING; sprite->set_action("vanishing", 1); } - soundSource.set_position(get_pos()); + sound_source->set_position(get_pos()); } if (mystate == STATE_WARPING) { @@ -107,18 +109,18 @@ WillOWisp::activate() { sprite->set_action("idle"); - soundSource.set_position(get_pos()); - soundSource.set_looping(true); - soundSource.set_gain(2.0); - soundSource.set_reference_distance(32); - soundSource.play(); + sound_source.reset(sound_manager->create_sound_source(SOUNDFILE)); + sound_source->set_position(get_pos()); + sound_source->set_looping(true); + sound_source->set_gain(2.0); + sound_source->set_reference_distance(32); + sound_source->play(); } void WillOWisp::deactivate() { - soundSource.stop(); - soundSource.release(); + sound_source.reset(NULL); switch (mystate) { case STATE_IDLE: diff --git a/src/badguy/willowisp.hpp b/src/badguy/willowisp.hpp index ce45ced76..00cc4c9a9 100644 --- a/src/badguy/willowisp.hpp +++ b/src/badguy/willowisp.hpp @@ -38,8 +38,6 @@ public: virtual void draw(DrawingContext& context); - virtual WillOWisp* clone() const { return new WillOWisp(*this); } - protected: HitResponse collision_player(Player& player, const CollisionHit& hit); @@ -52,7 +50,7 @@ private: std::string target_sector; std::string target_spawnpoint; - ManagedSoundSource soundSource; + std::auto_ptr sound_source; }; #endif diff --git a/src/gui/menu.cpp b/src/gui/menu.cpp index c1408047e..e230b3d4f 100644 --- a/src/gui/menu.cpp +++ b/src/gui/menu.cpp @@ -194,12 +194,8 @@ Menu::~Menu() for(std::vector::iterator i = items.begin(); i != items.end(); ++i) delete *i; -#ifdef DEBUG - assert(current_ != this); -#else if(current_ == this) current_ = NULL; -#endif } Menu::Menu() diff --git a/src/object/coin.cpp b/src/object/coin.cpp index 566d29489..2684bcd2a 100644 --- a/src/object/coin.cpp +++ b/src/object/coin.cpp @@ -115,7 +115,8 @@ Coin::collect() SoundSource* soundSource = sound_manager->create_sound_source("sounds/coin.wav"); soundSource->set_position(get_pos()); soundSource->set_pitch(pitch); - sound_manager->play_and_delete(soundSource); + soundSource->play(); + sound_manager->manage_source(soundSource); */ Sector::current()->player->get_status()->add_coins(1); Sector::current()->add_object(new BouncyCoin(get_pos())); diff --git a/src/object/moving_sprite.hpp b/src/object/moving_sprite.hpp index ae80634a3..df1743dbc 100644 --- a/src/object/moving_sprite.hpp +++ b/src/object/moving_sprite.hpp @@ -42,8 +42,6 @@ public: virtual void draw(DrawingContext& context); virtual void update(float elapsed_time); - virtual MovingSprite* clone() const = 0; - protected: std::string sprite_name; Sprite* sprite; diff --git a/src/object/scripted_object.cpp b/src/object/scripted_object.cpp index 009b4a51e..69f15ca2a 100644 --- a/src/object/scripted_object.cpp +++ b/src/object/scripted_object.cpp @@ -74,7 +74,9 @@ ScriptedObject::move(float x, float y) void ScriptedObject::set_pos(float x, float y) { + printf("SetPos: %f %f\n", x, y); bbox.set_pos(Vector(x, y)); + physic.reset(); } float diff --git a/src/sector.hpp b/src/sector.hpp index 02af0266e..ab96230e6 100644 --- a/src/sector.hpp +++ b/src/sector.hpp @@ -162,7 +162,7 @@ private: void handle_collisions(); /** - * Collision checks 2 objects against each other and does instant + * Does collision detection between 2 objects and does instant * collision response handling in case of a collision */ void collision_object(MovingObject* object1, MovingObject* object2) const; -- 2.11.0