[Super-tux-commit] supertux/lib/audio musicref.cpp,NONE,1.1 musicref.h,NONE,1.1 sound.cpp,NONE,1.1 s
Brought to you by:
wkendrick
From: Tobias G. <to...@us...> - 2004-07-20 17:51:48
|
Update of /cvsroot/super-tux/supertux/lib/audio In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv19557/lib/audio Added Files: musicref.cpp musicref.h sound.cpp sound.h sound_manager.cpp sound_manager.h Log Message: Generated SuperTux libtool library containing more general source, that could prove useful for other applications/games. Caution: It's not yet SuperTux independed, more work on this will follow, that's just the first step. The file structure isn't fixed, better ideas will surely find there way in it! --- NEW FILE: sound.cpp --- // $Id: sound.cpp,v 1.1 2004/07/20 17:51:34 tobgle Exp $ // // SuperTux - A Jump'n Run // Copyright (C) 2000 Bill Kendrick <bi...@ne...> // Copyright (C) 2004 Duong-Khang NGUYEN <neo...@us...> // // 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 <string> #include "audio/sound.h" /*global variable*/ bool use_sound = true; /* handle sound on/off menu and command-line option */ bool use_music = true; /* handle music on/off menu and command-line option */ bool audio_device = true; /* != 0: available and initialized */ #include <SDL_mixer.h> std::vector<Mix_Chunk*> sounds; /* --- OPEN THE AUDIO DEVICE --- */ int open_audio (int frequency, Uint16 format, int channels, int chunksize) { if (Mix_OpenAudio( frequency, format, channels, chunksize ) < 0) return -1; // allocate 16 channels for mixing if (Mix_AllocateChannels(8) != 8) return -2; return 0; } /* --- CLOSE THE AUDIO DEVICE --- */ void close_audio( void ) { if (audio_device) { Mix_CloseAudio(); } } /* --- LOAD A SOUND --- */ Mix_Chunk* load_sound(const std::string& file) { if(!audio_device) return 0; Mix_Chunk* snd = Mix_LoadWAV(file.c_str()); /*if (snd == 0) st_abort("Can't load", file);*/ return(snd); } void free_chunk(Mix_Chunk *chunk) { Mix_FreeChunk( chunk ); } --- NEW FILE: sound_manager.cpp --- // $Id: sound_manager.cpp,v 1.1 2004/07/20 17:51:35 tobgle Exp $ // // SuperTux - A Jump'n Run // Copyright (C) 2004 Matthias Braun <ma...@br... // // 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 <cmath> #include <cassert> #include "audio/sound_manager.h" #include "audio/musicref.h" #include "audio/sound.h" #include "app/globals.h" #include "app/setup.h" #include "special/moving_object.h" SoundManager::SoundManager() : current_music(0), music_enabled(true) { } SoundManager::~SoundManager() { if(audio_device) Mix_HaltMusic(); } void SoundManager::play_sound(Mix_Chunk* sound) { if(!audio_device || !use_sound) return; Mix_PlayChannel(-1, sound, 0); } void SoundManager::play_sound(Mix_Chunk* sound, const MovingObject* object, const Vector& pos) { // TODO keep track of the object later and move the sound along with the // object. play_sound(sound, object->get_pos(), pos); } void SoundManager::play_sound(Mix_Chunk* sound, const Vector& pos, const Vector& pos2) { if(!audio_device || !use_sound) return; // TODO make sure this formula is good float distance = pos2.x- pos.x; int loud = int(255.0/float(screen->w*2) * fabsf(distance)); if(loud > 255) return; int chan = Mix_PlayChannel(-1, sound, 0); if(chan < 0) return; Mix_SetDistance(chan, loud); // very bad way to do this... if(distance > 100) Mix_SetPanning(chan, 230, 24); else if(distance < -100) Mix_SetPanning(chan, 24, 230); } MusicRef SoundManager::load_music(const std::string& file) { if(!audio_device) return MusicRef(0); if(!exists_music(file)) st_abort("Couldn't load musicfile ", file.c_str()); std::map<std::string, MusicResource>::iterator i = musics.find(file); assert(i != musics.end()); return MusicRef(& (i->second)); } bool SoundManager::exists_music(const std::string& file) { if(!audio_device) return true; // song already loaded? std::map<std::string, MusicResource>::iterator i = musics.find(file); if(i != musics.end()) { return true; } Mix_Music* song = Mix_LoadMUS(file.c_str()); if(song == 0) return false; // insert into music list std::pair<std::map<std::string, MusicResource>::iterator, bool> result = musics.insert( std::make_pair<std::string, MusicResource> (file, MusicResource())); MusicResource& resource = result.first->second; resource.manager = this; resource.music = song; return true; } void SoundManager::free_music(MusicResource* ) { // TODO free music, currently we can't do this since SDL_mixer seems to have // some bugs if you load/free alot of mod files. } void SoundManager::play_music(const MusicRef& musicref, int loops) { if(!audio_device) return; if(musicref.music == 0 || current_music == musicref.music) return; if(current_music) current_music->refcount--; current_music = musicref.music; current_music->refcount++; if(music_enabled) Mix_PlayMusic(current_music->music, loops); } void SoundManager::halt_music() { if(!audio_device) return; Mix_HaltMusic(); if(current_music) { current_music->refcount--; if(current_music->refcount == 0) free_music(current_music); current_music = 0; } } void SoundManager::enable_music(bool enable) { if(!audio_device) return; if(enable == music_enabled) return; music_enabled = enable; if(music_enabled == false) { Mix_HaltMusic(); } else { Mix_PlayMusic(current_music->music, -1); } } SoundManager::MusicResource::~MusicResource() { // don't free music buggy SDL_Mixer crashs for some mod files // Mix_FreeMusic(music); } --- NEW FILE: sound_manager.h --- // $Id: sound_manager.h,v 1.1 2004/07/20 17:51:35 tobgle Exp $ // // SuperTux - A Jump'n Run // Copyright (C) 2004 Matthias Braun <ma...@br... // // 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 SUPERTUX_SOUND_MANAGER_H #define SUPERTUX_SOUND_MANAGER_H #include <string> #include <map> #include "SDL_mixer.h" #include "math/vector.h" class MusicRef; class MovingObject; /** This class handles all sounds that are played */ class SoundManager { public: SoundManager(); ~SoundManager(); void play_sound(Mix_Chunk* sound); void play_sound(Mix_Chunk* sound, const Vector& pos, const Vector& pos2); void play_sound(Mix_Chunk* sound, const MovingObject* object, const Vector& pos); MusicRef load_music(const std::string& file); bool exists_music(const std::string& filename); void play_music(const MusicRef& music, int loops = -1); void halt_music(); void enable_music(bool enable); private: // music part friend class MusicRef; class MusicResource { public: ~MusicResource(); SoundManager* manager; Mix_Music* music; int refcount; }; void free_music(MusicResource* music); std::map<std::string, MusicResource> musics; MusicResource* current_music; bool music_enabled; }; #endif /*SUPERTUX_SOUND_MANAGER_H*/ --- NEW FILE: musicref.cpp --- // $Id: musicref.cpp,v 1.1 2004/07/20 17:51:34 tobgle Exp $ // // SuperTux - A Jump'n Run // Copyright (C) 2000 Bill Kendrick <bi...@ne...> // Copyright (C) 2004 Matthias Braun <ma...@br...> // // 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 "audio/musicref.h" MusicRef::MusicRef() : music(0) { } MusicRef::MusicRef(SoundManager::MusicResource* newmusic) : music(newmusic) { if(music) music->refcount++; } MusicRef::~MusicRef() { if(music) { music->refcount--; if(music->refcount == 0) music->manager->free_music(music); } } MusicRef::MusicRef(const MusicRef& other) : music(other.music) { if(music) music->refcount++; } MusicRef& MusicRef::operator =(const MusicRef& other) { SoundManager::MusicResource* oldres = music; music = other.music; if(music) music->refcount++; if(oldres) { oldres->refcount--; if(oldres->refcount == 0) music->manager->free_music(music); } return *this; } --- NEW FILE: musicref.h --- // $Id: musicref.h,v 1.1 2004/07/20 17:51:34 tobgle Exp $ // // SuperTux - A Jump'n Run // Copyright (C) 2004 Matthias Braun <ma...@br...> // // 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 SUPERTUX_MUSICREF_H #define SUPERTUX_MUSICREF_H #include "audio/sound_manager.h" /** This class holds a reference to a music file and maintains a correct * refcount for that file. */ class MusicRef { public: MusicRef(); MusicRef(const MusicRef& other); ~MusicRef(); MusicRef& operator= (const MusicRef& other); private: friend class SoundManager; MusicRef(SoundManager::MusicResource* music); SoundManager::MusicResource* music; }; #endif /*SUPERTUX_MUSICREF_H*/ --- NEW FILE: sound.h --- // $Id: sound.h,v 1.1 2004/07/20 17:51:34 tobgle Exp $ // // SuperTux - A Jump'n Run // Copyright (C) 2000 Bill Kendrick <bi...@ne...> // Copyright (C) 2004 Duong-Khang NGUYEN <neo...@us...> // // 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 SUPERTUX_SOUND_H #define SUPERTUX_SOUND_H //#include "defines.h" /* get YES/NO defines */ #include <vector> /*global variable*/ extern bool use_sound; /* handle sound on/off menu and command-line option */ extern bool use_music; /* handle music on/off menu and command-line */ extern bool audio_device; /* != 0: available and initialized */ /* enum of different internal music types */ enum Music_Type { NO_MUSIC, LEVEL_MUSIC, HURRYUP_MUSIC, HERRING_MUSIC }; #include <string> #include <SDL_mixer.h> /* variables for stocking the sound and music */ extern std::vector<Mix_Chunk*> sounds; /* functions handling the sound and music */ int open_audio(int frequency, Uint16 format, int channels, int chunksize); void close_audio( void ); Mix_Chunk * load_sound(const std::string& file); void free_chunk(Mix_Chunk*chunk); #endif /*SUPERTUX_SOUND_H*/ |