1 // Copyright (C) 2006 Ulf Lorenz
2 // Copyright (C) 2006 Andrea Paternesi
3 // Copyright (C) 2007, 2009 Ben Asselstine
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 3 of the License, or
8 // (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU Library General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23 #include "xmlhelper.h"
28 #include <sigc++/trackable.h>
30 #include <SDL_mixer.h>
35 * The purpose of putting the sound code into one class is (besides hiding the
36 * internals and supplying a friendly interface) to put all these ugly ifdefs
37 * in one place (sound can be disabled for fewer dependencies).
39 * @note: As this becomes too complicated, I throw away the caching stuff.
41 * Sound and music are treated a bit differently since the mixer calls are others.
42 * However, both are referenced by strings (I think in this case, id's aren't
43 * very readable). There are basically two types of music. Background music
44 * is enabled by enableBackground(). It will always play until disabled again
45 * by looking through the databse of available background music pieces. On top
46 * of that, it is possible to play other music pieces. In this case, the
47 * background music fades out (maybe), the other music fades or pops in
48 * and goes away again with the backgroun dmusic taking its place again
54 // The file where the sound piece can be loaded from
56 // Can it be played in the background?
58 // If loading this file fails, we can define an alias to load instead.
63 //! This class manages sound within the game.
64 class Sound : public sigc::trackable
69 //! Returns whether music is enabled
70 bool isMusicEnabled();
72 //! Returns the music volume in the range 0..128
78 /** Enables/disables music and sets volume. If the sound is disabled,
79 * subsequent calls to play sounds will be silently ignored.
81 * @param enable enable/disable sound
82 * @param volume set the sound volume in the range from 0 to 128
84 * @return false for wrong volume data, otherwise true
86 bool setMusic(bool enable, int volume);
89 // Methods that operate on class data and modify the class.
91 /** Plays a given music piece.
93 * The current (background) track will be stopped (faded out) if
94 * neccessary and the new piece will be faded in. Each call to
95 * playMusic should be accompanied by a call to haltMusic(), otherwise the
96 * background music wil not continue.
98 * @param piece the identifier(name) of the music track to play.
99 * @param nloops the amount of time the piece should be played
100 * (-1: infinitely often)
101 * @param fade if set to true, fade out a playing music piece
102 * @return false if any error occurred.
104 bool playMusic(std::string piece, int nloops = -1, bool fade = true);
106 /** Stops the current (event) music. Note that the background music might
107 * continue with playing.
109 * @param fade if set to true, fade out a playing piece.
111 * @return false on error.
113 bool haltMusic(bool fade = true);
115 /** Enables background music.
117 * Starts playing background music. Picks a random piece that has
118 * the background tag enabled and starts playing it, then picks the
121 void enableBackground();
123 /** Stops playing of background music
125 * @param fade if set to true, fade out.
127 void disableBackground(bool fade=true);
129 //! Activates the next background piece
136 static Sound* getInstance();
138 //! Explicitely delete the singleton
139 static void deleteInstance();
142 //! Constructor. Initializes the sound and loads the music data
145 //! Destructor. Deinitializes sound
148 //! Callback for the music data, see XML_Helper
149 bool loadMusic(std::string tag, XML_Helper* helper);
153 // music is stored here, access by d_musicMap[name]
154 std::map<std::string, MusicItem*> d_musicMap;
155 std::vector<std::string> d_bgMap; // shallow copy of background pieces
157 // currently playing background and foreground piece
162 // if initialization failed, set this to true => no music/sound played
165 // if set to true, play background music
168 // if set to true, don't continue with next piece
171 // static instanton pointer
172 static Sound* s_instance;