Thread: [Super-tux-commit] supertux/src sound_manager.cpp,NONE,1.1 sound_manager.h,NONE,1.1 Makefile.am,1.31
Brought to you by:
wkendrick
Update of /cvsroot/super-tux/supertux/src In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv12269 Modified Files: Makefile.am badguy.cpp gameloop.cpp level.cpp leveleditor.cpp moving_object.h musicref.cpp musicref.h physic.cpp physic.h player.cpp resources.cpp resources.h sector.cpp setup.cpp sound.cpp sound.h special.cpp worldmap.cpp Added Files: sound_manager.cpp sound_manager.h Removed Files: music_manager.cpp music_manager.h Log Message: introduce a new SoundManager class and merged MusicManager with it. Using this class we can create real stereo effects based on player position, without adding strange hacks to badguy.cpp Index: resources.h =================================================================== RCS file: /cvsroot/super-tux/supertux/src/resources.h,v retrieving revision 1.10 retrieving revision 1.11 diff -u -d -r1.10 -r1.11 --- resources.h 29 Apr 2004 15:18:27 -0000 1.10 +++ resources.h 31 May 2004 22:13:15 -0000 1.11 @@ -23,7 +23,7 @@ #include "musicref.h" class SpriteManager; -class MusicManager; +class SoundManager; extern Surface* img_waves[3]; extern Surface* img_water; @@ -38,7 +38,7 @@ extern MusicRef level_end_song; extern SpriteManager* sprite_manager; -extern MusicManager* music_manager; +extern SoundManager* sound_manager; void loadshared(); void unloadshared(); --- NEW FILE: sound_manager.h --- // $Id: sound_manager.h,v 1.1 2004/05/31 22:13:15 matzebraun 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 __SOUND_MANAGER_H__ #define __SOUND_MANAGER_H__ #include "vector.h" #include <SDL_mixer.h> #include <string> #include <map> 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); void play_sound(Mix_Chunk* sound, const MovingObject* object); 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 Index: physic.cpp =================================================================== RCS file: /cvsroot/super-tux/supertux/src/physic.cpp,v retrieving revision 1.13 retrieving revision 1.14 diff -u -d -r1.13 -r1.14 --- physic.cpp 31 May 2004 02:40:30 -0000 1.13 +++ physic.cpp 31 May 2004 22:13:15 -0000 1.14 @@ -122,7 +122,7 @@ } void -Physic::apply(float frame_ratio, float &x, float &y) +Physic::apply(float elapsed_time, float &x, float &y) { float gravity = Sector::current()->gravity; float grav; @@ -131,8 +131,15 @@ else grav = 0; - x += vx * frame_ratio + ax * frame_ratio * frame_ratio; - y += vy * frame_ratio + (ay + grav) * frame_ratio * frame_ratio; - vx += ax * frame_ratio; - vy += (ay + grav) * frame_ratio; + x += vx * elapsed_time + ax * elapsed_time * elapsed_time; + y += vy * elapsed_time + (ay + grav) * elapsed_time * elapsed_time; + vx += ax * elapsed_time; + vy += (ay + grav) * elapsed_time; +} + +void +Physic::apply(Vector& vector, float elapsed_time) +{ + apply(elapsed_time, vector.x, vector.y); } + Index: special.cpp =================================================================== RCS file: /cvsroot/super-tux/supertux/src/special.cpp,v retrieving revision 1.53 retrieving revision 1.54 diff -u -d -r1.53 -r1.54 --- special.cpp 31 May 2004 02:40:30 -0000 1.53 +++ special.cpp 31 May 2004 22:13:15 -0000 1.54 @@ -265,7 +265,7 @@ if(kind != UPGRADE_GROWUP) return; - play_sound(sounds[SND_BUMP_UPGRADE], SOUND_CENTER_SPEAKER); + sound_manager->play_sound(sounds[SND_BUMP_UPGRADE], Vector(base.x, base.y)); // determine new direction if (player->base.x + player->base.width/2 > base.x + base.width/2) @@ -308,30 +308,30 @@ if (kind == UPGRADE_GROWUP) { - play_sound(sounds[SND_EXCELLENT], SOUND_CENTER_SPEAKER); + sound_manager->play_sound(sounds[SND_EXCELLENT]); pplayer->grow(true); } else if (kind == UPGRADE_FIREFLOWER) { - play_sound(sounds[SND_COFFEE], SOUND_CENTER_SPEAKER); + sound_manager->play_sound(sounds[SND_COFFEE]); pplayer->grow(true); pplayer->got_power = pplayer->FIRE_POWER; } else if (kind == UPGRADE_ICEFLOWER) { - play_sound(sounds[SND_COFFEE], SOUND_CENTER_SPEAKER); + sound_manager->play_sound(sounds[SND_COFFEE]); pplayer->grow(true); pplayer->got_power = pplayer->ICE_POWER; } else if (kind == UPGRADE_FIREFLOWER) { - play_sound(sounds[SND_COFFEE], SOUND_CENTER_SPEAKER); + sound_manager->play_sound(sounds[SND_COFFEE]); pplayer->grow(true); pplayer->got_power = pplayer->FIRE_POWER; } else if (kind == UPGRADE_HERRING) { - play_sound(sounds[SND_HERRING], SOUND_CENTER_SPEAKER); + sound_manager->play_sound(sounds[SND_HERRING]); pplayer->invincible_timer.start(TUX_INVINCIBLE_TIME); Sector::current()->play_music(HERRING_MUSIC); } @@ -339,7 +339,7 @@ { if(player_status.lives < MAX_LIVES) { player_status.lives++; - play_sound(sounds[SND_LIFEUP], SOUND_CENTER_SPEAKER); + sound_manager->play_sound(sounds[SND_LIFEUP]); } } Index: sound.cpp =================================================================== RCS file: /cvsroot/super-tux/supertux/src/sound.cpp,v retrieving revision 1.16 retrieving revision 1.17 diff -u -d -r1.16 -r1.17 --- sound.cpp 16 May 2004 16:33:21 -0000 1.16 +++ sound.cpp 31 May 2004 22:13:15 -0000 1.17 @@ -17,7 +17,6 @@ // 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 "defines.h" #include "globals.h" #include "sound.h" @@ -66,13 +65,6 @@ if (Mix_AllocateChannels(8) != 8) return -2; - /* reserve some channels and register panning effects */ - if (Mix_ReserveChannels(SOUND_RESERVED_CHANNELS) != SOUND_RESERVED_CHANNELS) - return -3; - - /* prepare the spanning effects */ - Mix_SetPanning( SOUND_LEFT_SPEAKER, 230, 24 ); - Mix_SetPanning( SOUND_RIGHT_SPEAKER, 24, 230 ); return 0; } @@ -82,8 +74,6 @@ void close_audio( void ) { if (audio_device) { - Mix_UnregisterAllEffects( SOUND_LEFT_SPEAKER ); - Mix_UnregisterAllEffects( SOUND_RIGHT_SPEAKER ); Mix_CloseAudio(); } } @@ -104,31 +94,6 @@ return(snd); } -/* --- PLAY A SOUND ON LEFT OR RIGHT OR CENTER SPEAKER --- */ - -void play_sound(Mix_Chunk * snd, enum Sound_Speaker whichSpeaker) -{ - /* this won't call the function if the user has disabled sound - * either via menu or via command-line option - */ - if(!audio_device || !use_sound) - return; - - Mix_PlayChannel( whichSpeaker, snd, 0); - - /* prepare for panning effects for next call */ - switch (whichSpeaker) { - case SOUND_LEFT_SPEAKER: - Mix_SetPanning( SOUND_LEFT_SPEAKER, 230, 24 ); - break; - case SOUND_RIGHT_SPEAKER: - Mix_SetPanning( SOUND_RIGHT_SPEAKER, 24, 230 ); - break; - default: // keep the compiler happy - break; - } -} - void free_chunk(Mix_Chunk *chunk) { Mix_FreeChunk( chunk ); Index: gameloop.cpp =================================================================== RCS file: /cvsroot/super-tux/supertux/src/gameloop.cpp,v retrieving revision 1.142 retrieving revision 1.143 diff -u -d -r1.142 -r1.143 --- gameloop.cpp 31 May 2004 13:43:30 -0000 1.142 +++ gameloop.cpp 31 May 2004 22:13:15 -0000 1.143 @@ -55,7 +55,6 @@ #include "resources.h" #include "background.h" #include "tilemap.h" -#include "music_manager.h" GameSession* GameSession::current_ = 0; @@ -145,7 +144,7 @@ void GameSession::levelintro(void) { - music_manager->halt_music(); + sound_manager->halt_music(); char str[60]; @@ -459,7 +458,7 @@ { end_sequence = ENDSEQUENCE_RUNNING; last_x_pos = -1; - music_manager->play_music(level_end_song, 0); + sound_manager->play_music(level_end_song, 0); endsequence_timer.start(7000); // 5 seconds until we finish the map tux->invincible_timer.start(7000); //FIXME: Implement a winning timer for the end sequence (with special winning animation etc.) } @@ -689,7 +688,7 @@ Sector::current()->add_bouncy_brick(Vector(((int)(x + 1) / 32) * 32, (int)(y / 32) * 32)); - play_sound(sounds[SND_BRICK], SOUND_CENTER_SPEAKER); + sound_manager->play_sound(sounds[SND_BRICK], Vector(x, y)); } /* (Status): */ --- music_manager.cpp DELETED --- Index: player.cpp =================================================================== RCS file: /cvsroot/super-tux/supertux/src/player.cpp,v retrieving revision 1.133 retrieving revision 1.134 diff -u -d -r1.133 -r1.134 --- player.cpp 31 May 2004 13:43:30 -0000 1.133 +++ player.cpp 31 May 2004 22:13:15 -0000 1.134 @@ -31,6 +31,7 @@ #include "tilemap.h" #include "camera.h" #include "gameobjs.h" +#include "resources.h" #include "interactive_object.h" #include "screen/screen.h" @@ -413,7 +414,7 @@ if(on_ground() && ((vx < 0 && dirsign >0) || (vx>0 && dirsign<0))) { if(fabs(vx)>SKID_XM && !skidding_timer.check()) { skidding_timer.start(SKID_TIME); - play_sound(sounds[SND_SKID], SOUND_CENTER_SPEAKER); + sound_manager->play_sound(sounds[SND_SKID]); ax *= 2.5; } else { ax *= 2; @@ -480,9 +481,9 @@ jumping = true; can_jump = false; if (size == SMALL) - play_sound(sounds[SND_JUMP], SOUND_CENTER_SPEAKER); + sound_manager->play_sound(sounds[SND_JUMP]); else - play_sound(sounds[SND_BIGJUMP], SOUND_CENTER_SPEAKER); + sound_manager->play_sound(sounds[SND_BIGJUMP]); } // Let go of jump key else if(input.up == UP && jumping && physic.get_velocity_y() > 0) @@ -668,7 +669,7 @@ if(player_status.lives < MAX_LIVES) ++player_status.lives; /*We want to hear the sound even, if MAX_LIVES is reached*/ - play_sound(sounds[SND_LIFEUP], SOUND_CENTER_SPEAKER); + sound_manager->play_sound(sounds[SND_LIFEUP]); } } @@ -906,7 +907,7 @@ if(dying) return; - play_sound(sounds[SND_HURT], SOUND_CENTER_SPEAKER); + sound_manager->play_sound(sounds[SND_HURT]); physic.set_velocity_x(0); Index: physic.h =================================================================== RCS file: /cvsroot/super-tux/supertux/src/physic.h,v retrieving revision 1.13 retrieving revision 1.14 diff -u -d -r1.13 -r1.14 --- physic.h 20 Apr 2004 17:58:00 -0000 1.13 +++ physic.h 31 May 2004 22:13:15 -0000 1.14 @@ -63,6 +63,9 @@ /** applies the physical simulation to given x and y coordinates */ void apply(float frame_ratio, float &x, float &y); + /** applies the physical simulation to given x and y coordinates */ + void apply(Vector& vector, float frame_ratio); + private: /// horizontal and vertical acceleration float ax, ay; Index: badguy.cpp =================================================================== RCS file: /cvsroot/super-tux/supertux/src/badguy.cpp,v retrieving revision 1.101 retrieving revision 1.102 diff -u -d -r1.101 -r1.102 --- badguy.cpp 31 May 2004 13:43:28 -0000 1.101 +++ badguy.cpp 31 May 2004 22:13:15 -0000 1.102 @@ -356,7 +356,7 @@ tux.kick_timer.start(KICKING_TIME); set_sprite(img_mriceblock_flat_left, img_mriceblock_flat_right); physic.set_velocity_x((dir == LEFT) ? -3.5 : 3.5); - play_sound(sounds[SND_KICK],SOUND_CENTER_SPEAKER); + sound_manager->play_sound(sounds[SND_KICK], this); } } @@ -366,15 +366,7 @@ check_horizontal_bump(); if(mode == KICK && changed != dir) { - float scroll_x = Sector::current()->camera->get_translation().x; - - /* handle stereo sound (number 10 should be tweaked...)*/ - if (base.x < scroll_x + screen->w/2 - 10) - play_sound(sounds[SND_RICOCHET], SOUND_LEFT_SPEAKER); - else if (base.x > scroll_x + screen->w/2 + 10) - play_sound(sounds[SND_RICOCHET], SOUND_RIGHT_SPEAKER); - else - play_sound(sounds[SND_RICOCHET], SOUND_CENTER_SPEAKER); + sound_manager->play_sound(sounds[SND_RICOCHET], get_pos()); } } @@ -568,16 +560,7 @@ dying = DYING_NOT; // now the bomb hurts timer.start(EXPLODETIME); - float scroll_x = Sector::current()->camera->get_translation().x; - - /* play explosion sound */ // FIXME: is the stereo all right? maybe we should use player cordinates... - if (base.x < scroll_x + screen->w/2 - 10) - play_sound(sounds[SND_EXPLODE], SOUND_LEFT_SPEAKER); - else if (base.x > scroll_x + screen->w/2 + 10) - play_sound(sounds[SND_EXPLODE], SOUND_RIGHT_SPEAKER); - else - play_sound(sounds[SND_EXPLODE], SOUND_CENTER_SPEAKER); - + sound_manager->play_sound(sounds[SND_EXPLODE], this); } else if(mode == BOMB_EXPLODE) { remove_me(); return; @@ -1054,7 +1037,8 @@ Sector::current()->add_score(Vector(base.x, base.y), 50 * player_status.score_multiplier); - play_sound(sounds[SND_SQUISH], SOUND_CENTER_SPEAKER); + + sound_manager->play_sound(sounds[SND_SQUISH], get_pos()); player_status.score_multiplier++; dying = DYING_SQUISHED; @@ -1074,7 +1058,7 @@ make_player_jump(player); Sector::current()->add_score(Vector(base.x, base.y), 50 * player_status.score_multiplier); - play_sound(sounds[SND_SQUISH], SOUND_CENTER_SPEAKER); + sound_manager->play_sound(sounds[SND_SQUISH], get_pos()); player_status.score_multiplier++; return; @@ -1082,7 +1066,7 @@ if (mode == NORMAL || mode == KICK) { /* Flatten! */ - play_sound(sounds[SND_STOMP], SOUND_CENTER_SPEAKER); + sound_manager->play_sound(sounds[SND_STOMP], get_pos()); mode = FLAT; set_sprite(img_mriceblock_flat_left, img_mriceblock_flat_right); physic.set_velocity_x(0); @@ -1090,7 +1074,7 @@ timer.start(4000); } else if (mode == FLAT) { /* Kick! */ - play_sound(sounds[SND_KICK], SOUND_CENTER_SPEAKER); + sound_manager->play_sound(sounds[SND_KICK], this); if (player->base.x < base.x + (base.width/2)) { physic.set_velocity_x(5); @@ -1191,7 +1175,7 @@ score * player_status.score_multiplier); /* Play death sound: */ - play_sound(sounds[SND_FALL], SOUND_CENTER_SPEAKER); + sound_manager->play_sound(sounds[SND_FALL], this); } void @@ -1354,7 +1338,7 @@ /* Get kicked if were flat */ if (mode == FLAT && !dying) { - play_sound(sounds[SND_KICK], SOUND_CENTER_SPEAKER); + sound_manager->play_sound(sounds[SND_KICK], this); // Hit from left side if (player->base.x < base.x) { Index: Makefile.am =================================================================== RCS file: /cvsroot/super-tux/supertux/src/Makefile.am,v retrieving revision 1.31 retrieving revision 1.32 diff -u -d -r1.31 -r1.32 --- Makefile.am 31 May 2004 13:43:28 -0000 1.31 +++ Makefile.am 31 May 2004 22:13:14 -0000 1.32 @@ -46,8 +46,6 @@ menu.h \ mousecursor.cpp \ mousecursor.h \ -music_manager.cpp \ -music_manager.h \ musicref.cpp \ musicref.h \ particlesystem.cpp \ @@ -62,6 +60,8 @@ setup.h \ sound.cpp \ sound.h \ +sound_manager.h \ +sound_manager.cpp \ special.cpp \ special.h \ sprite.h \ Index: resources.cpp =================================================================== RCS file: /cvsroot/super-tux/supertux/src/resources.cpp,v retrieving revision 1.35 retrieving revision 1.36 diff -u -d -r1.35 -r1.36 --- resources.cpp 31 May 2004 02:40:30 -0000 1.35 +++ resources.cpp 31 May 2004 22:13:15 -0000 1.36 @@ -25,6 +25,7 @@ #include "special.h" #include "resources.h" #include "sprite_manager.h" +#include "sound_manager.h" #include "setup.h" Surface* img_waves[3]; @@ -39,7 +40,7 @@ MusicRef level_end_song; SpriteManager* sprite_manager = 0; -MusicManager* music_manager = 0; +SoundManager* sound_manager = 0; /* Load graphics/sounds shared between all levels: */ void loadshared() @@ -47,8 +48,8 @@ int i; sprite_manager = new SpriteManager(datadir + "/supertux.strf"); - music_manager = new MusicManager(); - music_manager->enable_music(use_music); + sound_manager = new SoundManager(); + sound_manager->enable_music(use_music); /* Tuxes: */ smalltux_star = sprite_manager->load("smalltux-star"); @@ -214,8 +215,8 @@ sounds[i] = load_sound(datadir + soundfilenames[i]); /* Herring song */ - herring_song = music_manager->load_music(datadir + "/music/SALCON.MOD"); - level_end_song = music_manager->load_music(datadir + "/music/tux-leveldone.mod"); + herring_song = sound_manager->load_music(datadir + "/music/SALCON.MOD"); + level_end_song = sound_manager->load_music(datadir + "/music/tux-leveldone.mod"); } @@ -251,8 +252,8 @@ delete sprite_manager; sprite_manager = 0; - delete music_manager; - music_manager = 0; + delete sound_manager; + sound_manager = 0; } /* EOF */ Index: sector.cpp =================================================================== RCS file: /cvsroot/super-tux/supertux/src/sector.cpp,v retrieving revision 1.2 retrieving revision 1.3 diff -u -d -r1.2 -r1.3 --- sector.cpp 31 May 2004 13:43:30 -0000 1.2 +++ sector.cpp 31 May 2004 22:13:15 -0000 1.3 @@ -33,7 +33,7 @@ #include "particlesystem.h" #include "tile.h" #include "tilemap.h" -#include "music_manager.h" +#include "sound_manager.h" #include "gameloop.h" #include "resources.h" #include "interactive_object.h" @@ -608,7 +608,7 @@ throw std::runtime_error("wrong bullet type."); add_object(new_bullet); - play_sound(sounds[SND_SHOOT], SOUND_CENTER_SPEAKER); + sound_manager->play_sound(sounds[SND_SHOOT]); return true; } @@ -643,7 +643,7 @@ solids->change_at(pos, tile->next_tile); } - play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER); + sound_manager->play_sound(sounds[SND_DISTRO]); player_status.score = player_status.score + SCORE_DISTRO; player_status.distros++; return true; @@ -659,7 +659,7 @@ (int)(pos.y / 32) * 32), tile); /* Get some score: */ - play_sound(sounds[SND_BRICK], SOUND_CENTER_SPEAKER); + sound_manager->play_sound(sounds[SND_BRICK]); player_status.score = player_status.score + SCORE_BRICK; return true; @@ -689,7 +689,7 @@ { case 1: // Box with a distro! add_bouncy_distro(Vector(posx, posy)); - play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER); + sound_manager->play_sound(sounds[SND_DISTRO]); player_status.score = player_status.score + SCORE_DISTRO; player_status.distros++; break; @@ -699,7 +699,7 @@ add_upgrade(Vector(posx, posy), col_side, UPGRADE_GROWUP); else /* Tux is big, add a fireflower: */ add_upgrade(Vector(posx, posy), col_side, UPGRADE_FIREFLOWER); - play_sound(sounds[SND_UPGRADE], SOUND_CENTER_SPEAKER); + sound_manager->play_sound(sounds[SND_UPGRADE]); break; case 5: // Add an ice flower upgrade! @@ -707,7 +707,7 @@ add_upgrade(Vector(posx, posy), col_side, UPGRADE_GROWUP); else /* Tux is big, add an iceflower: */ add_upgrade(Vector(posx, posy), col_side, UPGRADE_ICEFLOWER); - play_sound(sounds[SND_UPGRADE], SOUND_CENTER_SPEAKER); + sound_manager->play_sound(sounds[SND_UPGRADE]); break; case 3: // Add a golden herring @@ -734,7 +734,7 @@ return; solids->change_at(pos, tile->next_tile); - play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER); + sound_manager->play_sound(sounds[SND_DISTRO]); if (bounciness == BOUNCE) { @@ -778,7 +778,7 @@ char* song_path; char* song_subtitle; - level_song = music_manager->load_music(datadir + "/music/" + song_title); + level_song = sound_manager->load_music(datadir + "/music/" + song_title); song_path = (char *) malloc(sizeof(char) * datadir.length() + strlen(song_title.c_str()) + 8 + 5); @@ -786,10 +786,10 @@ strcpy(strstr(song_subtitle, "."), "\0"); sprintf(song_path, "%s/music/%s-fast%s", datadir.c_str(), song_subtitle, strstr(song_title.c_str(), ".")); - if(!music_manager->exists_music(song_path)) { + if(!sound_manager->exists_music(song_path)) { level_song_fast = level_song; } else { - level_song_fast = music_manager->load_music(song_path); + level_song_fast = sound_manager->load_music(song_path); } free(song_subtitle); free(song_path); @@ -801,16 +801,16 @@ currentmusic = type; switch(currentmusic) { case HURRYUP_MUSIC: - music_manager->play_music(level_song_fast); + sound_manager->play_music(level_song_fast); break; case LEVEL_MUSIC: - music_manager->play_music(level_song); + sound_manager->play_music(level_song); break; case HERRING_MUSIC: - music_manager->play_music(herring_song); + sound_manager->play_music(herring_song); break; default: - music_manager->halt_music(); + sound_manager->halt_music(); break; } } --- NEW FILE: sound_manager.cpp --- // $Id: sound_manager.cpp,v 1.1 2004/05/31 22:13:15 matzebraun 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 "sound_manager.h" #include <math.h> #include <assert.h> #include "musicref.h" #include "setup.h" #include "sector.h" #include "camera.h" #include "sound.h" #include "globals.h" #include "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) { // TODO keep track of the object later and move the sound along with the // object. play_sound(sound, object->get_pos()); } void SoundManager::play_sound(Mix_Chunk* sound, const Vector& pos) { if(!audio_device || !use_sound) return; // TODO make sure this formula is good float distance = Sector::current()->player->get_pos().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); } Index: worldmap.cpp =================================================================== RCS file: /cvsroot/super-tux/supertux/src/worldmap.cpp,v retrieving revision 1.79 retrieving revision 1.80 diff -u -d -r1.79 -r1.80 --- worldmap.cpp 31 May 2004 02:40:30 -0000 1.79 +++ worldmap.cpp 31 May 2004 22:13:15 -0000 1.80 @@ -31,6 +31,7 @@ #include "setup.h" #include "sector.h" #include "worldmap.h" +#include "sound_manager.h" #include "resources.h" namespace WorldMapNS { @@ -690,8 +691,8 @@ if (!level->extro_filename.empty()) { MusicRef theme = - music_manager->load_music(datadir + "/music/theme.mod"); - music_manager->play_music(theme); + sound_manager->load_music(datadir + "/music/theme.mod"); + sound_manager->play_music(theme); // Display final credits and go back to the main menu display_text_file(level->extro_filename, "/images/background/extro.jpg", SCROLL_SPEED_MESSAGE); @@ -745,7 +746,7 @@ break; } - music_manager->play_music(song); + sound_manager->play_music(song); Menu::set_current(0); if (!savegame_file.empty()) savegame(savegame_file); @@ -901,8 +902,8 @@ quit = false; - song = music_manager->load_music(datadir + "/music/" + music); - music_manager->play_music(song); + song = sound_manager->load_music(datadir + "/music/" + music); + sound_manager->play_music(song); unsigned int last_update_time; unsigned int update_time; Index: sound.h =================================================================== RCS file: /cvsroot/super-tux/supertux/src/sound.h,v retrieving revision 1.28 retrieving revision 1.29 diff -u -d -r1.28 -r1.29 --- sound.h 26 Apr 2004 12:21:22 -0000 1.28 +++ sound.h 31 May 2004 22:13:15 -0000 1.29 @@ -36,14 +36,6 @@ HERRING_MUSIC }; -/* panning effects: terrible :-) ! */ -enum Sound_Speaker { - SOUND_LEFT_SPEAKER = 0, - SOUND_RIGHT_SPEAKER = 1, - SOUND_RESERVED_CHANNELS = 2, // 2 channels reserved for left/right speaker - SOUND_CENTER_SPEAKER = -1 -}; - /* Sound files: */ enum { SND_JUMP, @@ -82,6 +74,5 @@ Mix_Chunk * load_sound(const std::string& file); void free_chunk(Mix_Chunk*chunk); -void play_sound(Mix_Chunk * snd, enum Sound_Speaker whichSpeaker); #endif /*SUPERTUX_SOUND_H*/ Index: moving_object.h =================================================================== RCS file: /cvsroot/super-tux/supertux/src/moving_object.h,v retrieving revision 1.3 retrieving revision 1.4 diff -u -d -r1.3 -r1.4 --- moving_object.h 31 May 2004 13:43:30 -0000 1.3 +++ moving_object.h 31 May 2004 22:13:15 -0000 1.4 @@ -39,6 +39,9 @@ virtual void collision(const MovingObject& other_object, int collision_type) = 0; + Vector get_pos() const + { return Vector(base.x, base.y); } + base_type base; base_type old_base; Index: musicref.cpp =================================================================== RCS file: /cvsroot/super-tux/supertux/src/musicref.cpp,v retrieving revision 1.1 retrieving revision 1.2 diff -u -d -r1.1 -r1.2 --- musicref.cpp 24 Apr 2004 14:49:03 -0000 1.1 +++ musicref.cpp 31 May 2004 22:13:15 -0000 1.2 @@ -24,7 +24,7 @@ { } -MusicRef::MusicRef(MusicManager::MusicResource* newmusic) +MusicRef::MusicRef(SoundManager::MusicResource* newmusic) : music(newmusic) { if(music) @@ -50,7 +50,7 @@ MusicRef& MusicRef::operator =(const MusicRef& other) { - MusicManager::MusicResource* oldres = music; + SoundManager::MusicResource* oldres = music; music = other.music; if(music) music->refcount++; Index: setup.cpp =================================================================== RCS file: /cvsroot/super-tux/supertux/src/setup.cpp,v retrieving revision 1.89 retrieving revision 1.90 diff -u -d -r1.89 -r1.90 --- setup.cpp 31 May 2004 14:08:11 -0000 1.89 +++ setup.cpp 31 May 2004 22:13:15 -0000 1.90 @@ -51,7 +51,7 @@ #include "worldmap.h" #include "resources.h" #include "intro.h" -#include "music_manager.h" +#include "sound_manager.h" #include "player.h" @@ -558,7 +558,7 @@ if(use_music != options_menu->isToggled(MNID_MUSIC)) { use_music = !use_music; - music_manager->enable_music(use_music); + sound_manager->enable_music(use_music); } break; case MNID_SHOWFPS: Index: musicref.h =================================================================== RCS file: /cvsroot/super-tux/supertux/src/musicref.h,v retrieving revision 1.1 retrieving revision 1.2 diff -u -d -r1.1 -r1.2 --- musicref.h 24 Apr 2004 14:49:04 -0000 1.1 +++ musicref.h 31 May 2004 22:13:15 -0000 1.2 @@ -19,7 +19,7 @@ #ifndef HEADER_MUSIC_RESOURCE_H #define HEADER_MUSIC_RESOURCE_H -#include "music_manager.h" +#include "sound_manager.h" /** This class holds a reference to a music file and maintains a correct * refcount for that file. @@ -34,10 +34,10 @@ MusicRef& operator= (const MusicRef& other); private: - friend class MusicManager; - MusicRef(MusicManager::MusicResource* music); + friend class SoundManager; + MusicRef(SoundManager::MusicResource* music); - MusicManager::MusicResource* music; + SoundManager::MusicResource* music; }; #endif Index: leveleditor.cpp =================================================================== RCS file: /cvsroot/super-tux/supertux/src/leveleditor.cpp,v retrieving revision 1.128 retrieving revision 1.129 diff -u -d -r1.128 -r1.129 --- leveleditor.cpp 30 May 2004 01:08:48 -0000 1.128 +++ leveleditor.cpp 31 May 2004 22:13:15 -0000 1.129 @@ -43,7 +43,6 @@ #include "button.h" #include "tile.h" #include "resources.h" -#include "music_manager.h" #include "background.h" // TODO --- music_manager.h DELETED --- Index: level.cpp =================================================================== RCS file: /cvsroot/super-tux/supertux/src/level.cpp,v retrieving revision 1.88 retrieving revision 1.89 diff -u -d -r1.88 -r1.89 --- level.cpp 31 May 2004 02:40:30 -0000 1.88 +++ level.cpp 31 May 2004 22:13:15 -0000 1.89 @@ -36,7 +36,6 @@ #include "tile.h" #include "lispreader.h" #include "resources.h" -#include "music_manager.h" #include "gameobjs.h" #include "lispwriter.h" |