updating Nolok contrib templates
[supertux.git] / lib / audio / sound_manager.h
1 //  $Id$
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
20 #ifndef SUPERTUX_SOUND_MANAGER_H
21 #define SUPERTUX_SOUND_MANAGER_H
22
23 #include <string>
24 #include <vector>
25 #include <map>
26
27 #include "SDL_mixer.h"
28 #include "math/vector.h"
29
30 namespace SuperTux
31   {
32
33   class MusicRef;
34   class MovingObject;
35
36   /// enum of different internal music types
37   enum Music_Type {
38     NO_MUSIC,
39     LEVEL_MUSIC,
40     HURRYUP_MUSIC,
41     HERRING_MUSIC
42   };
43
44   /// Sound manager
45   /** This class handles all sounds that are played
46    */
47   class SoundManager
48     {
49     public:
50       /// Play sound.
51       void play_sound(Mix_Chunk* sound);
52       /// Play sound relative to two Vectors.
53       void play_sound(Mix_Chunk* sound, const Vector& pos, const Vector& pos2);
54       /// Play sound relative to a MovingObject and a Vector.
55       void play_sound(Mix_Chunk* sound, const MovingObject* object, const Vector& pos);
56       
57       /// Load music.
58       /** Is used to load the music for a MusicRef. */
59       MusicRef load_music(const std::string& file);
60
61       /// Load sound.
62       Mix_Chunk* load_sound(const std::string& file);
63
64       /// Test if a certain music file exists.
65       bool exists_music(const std::string& filename);
66
67       /// Play music.
68       /** @param loops: Defaults to -1, which means endless loops. */
69       void play_music(const MusicRef& music, int loops = -1);
70
71       /// Halt music.
72       void halt_music();
73
74       /// Enable/Disable music.
75       void enable_music(bool enable);
76
77       /// Is music enabled?
78       bool music_enabled()
79       {
80         return m_music_enabled;
81       }
82
83       /// Enable/Disable sound.
84       void enable_sound(bool enable);
85
86       /// Is sound enabled?
87       bool sound_enabled()
88       {
89         return m_sound_enabled;
90       }
91
92       /* functions handling the sound and music */
93       int open_audio(int frequency, Uint16 format, int channels, int chunksize);
94       void close_audio( void );
95
96       /// Is audio available?
97       bool audio_device_available()
98       {
99         return audio_device;
100       }
101
102       void set_audio_device_available(bool available)
103       {
104         audio_device = available;
105       }
106
107       static SoundManager* get()
108         {
109           return instance_ ? instance_ : instance_ = new SoundManager();
110         }
111       static void destroy_instance()
112       {
113         delete instance_;
114         instance_ = 0;
115       }
116       
117       void add_sound(Mix_Chunk* sound, int id)
118       {
119         sounds[id] = sound;
120       }
121       
122       Mix_Chunk* get_sound(int id)
123       {
124         return sounds[id];
125       }
126
127     private:
128       SoundManager();
129       ~SoundManager();
130
131       // music part
132       friend class MusicRef;
133       friend class Setup;
134       friend Mix_Chunk* IDToSound(int id);
135       
136       static SoundManager* instance_ ;
137
138       void free_chunk(Mix_Chunk* chunk);
139
140       /// Resource for music.
141       /** Contains the raw music data and
142           information for music reference
143           counting. */
144       class MusicResource
145         {
146         public:
147           ~MusicResource();
148
149           SoundManager* manager;
150           Mix_Music* music;
151           int refcount;
152         };
153
154       void free_music(MusicResource* music);
155
156       /*variables for stocking the sound and music*/
157       std::map<int, Mix_Chunk*> sounds;
158       std::map<std::string, MusicResource> musics;
159       MusicResource* current_music;
160       bool m_music_enabled;
161       bool m_sound_enabled;
162       bool audio_device;        /* != 0: available and initialized */
163     };
164
165    Mix_Chunk* IDToSound(int id);
166     
167 } // namespace SuperTux
168 #endif /*SUPERTUX_SOUND_MANAGER_H*/
169