+// $Id$
+//
+// SuperTux
+// Copyright (C) 2006 Matthias Braun <matze@braunis.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 "sound_manager.hpp"
#include <stdexcept>
#include "sound_file.hpp"
#include "sound_source.hpp"
#include "stream_sound_source.hpp"
+#include "log.hpp"
+#include "timer.hpp"
SoundManager* sound_manager = 0;
{
try {
device = alcOpenDevice(0);
- if(device == 0) {
- print_openal_version();
+ if (device == NULL) {
throw std::runtime_error("Couldn't open audio device.");
}
check_al_error("Audio error after init: ");
sound_enabled = true;
} catch(std::exception& e) {
- device = 0;
- context = 0;
- std::cerr << "Couldn't initialize audio device:" << e.what() << "\n";
+ if(context != NULL)
+ alcDestroyContext(context);
+ context = NULL;
+ if(device != NULL)
+ alcCloseDevice(device);
+ device = NULL;
+ log_warning << "Couldn't initialize audio device: " << e.what() << std::endl;
print_openal_version();
}
}
alDeleteBuffers(1, &buffer);
}
- if(context != 0) {
+ if(context != NULL) {
alcDestroyContext(context);
}
- if(device != 0) {
+ if(device != NULL) {
alcCloseDevice(device);
}
}
{
try {
SoundSource* source = create_sound_source(filename);
- if(source == 0)
+ if(source == NULL)
return;
if(pos == Vector(-1, -1)) {
alSourcef(source->source, AL_ROLLOFF_FACTOR, 0);
source->play();
sources.push_back(source);
} catch(std::exception& e) {
- std::cout << "Couldn't play sound " << filename << ": " << e.what() << "\n";
+ log_warning << "Couldn't play sound " << filename << ": " << e.what() << std::endl;
+ }
+}
+
+void
+SoundManager::play_and_delete(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;
}
}
void
SoundManager::enable_sound(bool enable)
{
- if(device == 0)
+ if(device == NULL)
return;
+
sound_enabled = enable;
}
void
SoundManager::enable_music(bool enable)
{
- if(device == 0)
+ if(device == NULL)
return;
+
music_enabled = enable;
if(music_enabled) {
play_music(current_music);
music_source->set_fading(StreamSoundSource::FadingOff, fadetime);
} else {
delete music_source;
- music_source = 0;
+ music_source = NULL;
}
current_music = "";
}
if(filename == "") {
delete music_source;
- music_source = 0;
+ music_source = NULL;
return;
}
std::auto_ptr<StreamSoundSource> newmusic (new StreamSoundSource());
alSourcef(newmusic->source, AL_ROLLOFF_FACTOR, 0);
newmusic->set_sound_file(load_sound_file(filename));
+ newmusic->set_looping(true);
if(fade)
newmusic->set_fading(StreamSoundSource::FadingOn, .5f);
newmusic->play();
delete music_source;
music_source = newmusic.release();
} catch(std::exception& e) {
- std::cerr << "Couldn't play music file '" << filename << "': "
- << e.what() << "\n";
+ log_warning << "Couldn't play music file '" << filename << "': " << e.what() << std::endl;
}
}
void
SoundManager::update()
{
- static Uint32 lastticks = 0;
+ static float lasttime = real_time;
- Uint32 current_ticks = SDL_GetTicks();
- if(current_ticks - lastticks < 300)
+ if(real_time - lasttime < 0.3)
return;
- lastticks = current_ticks;
+ lasttime = real_time;
// update and check for finished sound sources
for(SoundSources::iterator i = sources.begin(); i != sources.end(); ) {
if(music_source) {
music_source->update();
}
-
- alcProcessContext(context);
- check_alc_error("Error while processing audio context: ");
+
+ if (context)
+ {
+ alcProcessContext(context);
+ check_alc_error("Error while processing audio context: ");
+ }
}
ALenum
void
SoundManager::print_openal_version()
{
- std::cout << "OpenAL Vendor: " << alGetString(AL_VENDOR) << "\n"
- << "OpenAL Version: " << alGetString(AL_VERSION) << "\n"
- << "OpenAL Renderer: " << alGetString(AL_RENDERER) << "\n"
- << "OpenAl Extensions: " << alGetString(AL_EXTENSIONS) << "\n";
+ log_info << "OpenAL Vendor: " << alGetString(AL_VENDOR) << std::endl;
+ log_info << "OpenAL Version: " << alGetString(AL_VERSION) << std::endl;
+ log_info << "OpenAL Renderer: " << alGetString(AL_RENDERER) << std::endl;
+ log_info << "OpenAl Extensions: " << alGetString(AL_EXTENSIONS) << std::endl;
}
void