- SoundManager doesn't need Effects for now - ambient sound volume
[supertux.git] / src / audio / sound_manager.h
1 //  $Id: sound_manager.h 2353 2005-04-06 23:00:16Z matzebraun $
2 //
3 //  SuperTux -  A Jump'n Run
4 //  Copyright (C) 2004 Matthias Braun <matze@braunis.de
5 //
6 //  This program is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU General Public License
8 //  as published by the Free Software Foundation; either version 2
9 //  of the License, or (at your option) any later version.
10 //
11 //  This program is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 //  GNU General Public License for more details.
15 //
16 //  You should have received a copy of the GNU General Public License
17 //  along with this program; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19 #ifndef SUPERTUX_SOUND_MANAGER_H
20 #define SUPERTUX_SOUND_MANAGER_H
21
22 #include <string>
23 #include <vector>
24 #include <map>
25
26 #include "SDL_mixer.h"
27 #include "math/vector.h"
28
29 class MusicRef;
30 class MovingObject;
31
32 /** Sound manager
33  * This class handles all sounds that are played
34  */
35 class SoundManager
36 {
37 public:
38   SoundManager();
39   ~SoundManager();
40
41   /// Play sound (maybe looping), return channel number (or -1 on error)
42   int play_sound(const std::string& sound,int loops=0);
43
44   /// Play sound relative to two Vectors.
45   void play_sound(const std::string& sound, const Vector& pos,
46       const Vector& pos2);
47   /// Play sound relative to a MovingObject and a Vector.
48   void play_sound(const std::string& sound, const MovingObject* object,
49       const Vector& pos);
50   
51   /// register an effect to a channel - basti_
52   void register_effect(int channel,Mix_EffectFunc_t f,Mix_EffectDone_t d, 
53                        void * arg);
54
55   // effects - basti_ 
56   static void volume_adjust(int,void *,int,void *);
57
58   /** Load music.
59    * Is used to load the music for a MusicRef.
60    */
61   MusicRef load_music(const std::string& file);
62
63   /**
64    * If the sound isn't loaded yet try to load it.
65    * Returns an existing instance of the sound, loads a new one and returns that
66    * or returns 0 if loading failed.
67    */
68   Mix_Chunk* preload_sound(const std::string& name);
69
70   /// Test if a certain music file exists.
71   bool exists_music(const std::string& filename);
72
73   /** Play music.
74    * @param loops: Defaults to -1, which means endless loops.
75    */
76   void play_music(const MusicRef& music, int loops = -1);
77
78   /// Halt music.
79   void halt_music();
80
81   /// Enable/Disable music.
82   void enable_music(bool enable);
83
84   /// Is music enabled?
85   bool music_enabled()
86   {
87     return m_music_enabled;
88   }
89
90   /// Enable/Disable sound.
91   void enable_sound(bool enable);
92
93   /// Is sound enabled?
94   bool sound_enabled()
95   {
96     return m_sound_enabled;
97   }
98
99   /// Is audio available?
100   bool audio_device_available()
101   {
102     return audio_device;
103   }
104
105   void set_audio_device_available(bool available)
106   {
107     audio_device = available;
108   }
109
110 private:
111   friend class MusicRef;
112   friend class Setup;
113   
114   /// Resource for music.
115   /** Contains the raw music data and
116       information for music reference
117       counting. */
118   class MusicResource
119     {
120     public:
121       ~MusicResource();
122
123       SoundManager* manager;
124       Mix_Music* music;
125       int refcount;
126     };
127
128   void free_music(MusicResource* music);
129
130   typedef std::map<std::string, Mix_Chunk*> Sounds;
131   Sounds sounds;
132
133   typedef std::map<std::string, MusicResource> Musics;
134   Musics musics;
135
136   MusicResource* current_music;
137   bool m_music_enabled;
138   bool m_sound_enabled;
139   bool audio_device;        /* true: available and initialized */
140 };
141
142 #endif /*SUPERTUX_SOUND_MANAGER_H*/
143