Completely re-written from scratch of buttons code.
[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
51       /// Play sound.
52       void play_sound(Mix_Chunk* sound);
53       /// Play sound relative to two Vectors.
54       void play_sound(Mix_Chunk* sound, const Vector& pos, const Vector& pos2);
55       /// Play sound relative to a MovingObject and a Vector.
56       void play_sound(Mix_Chunk* sound, const MovingObject* object, const Vector& pos);
57       
58       /// Load music.
59       /** Is used to load the music for a MusicRef. */
60       MusicRef load_music(const std::string& file);
61
62       /// Load sound.
63       Mix_Chunk * load_sound(const std::string& file);
64
65       /// Test if a certain music file exists.
66       bool exists_music(const std::string& filename);
67
68       /// Play music.
69       /** @param loops: Defaults to -1, which means endless loops. */
70       void play_music(const MusicRef& music, int loops = -1);
71
72       /// Halt music.
73       void halt_music();
74
75       /// Enable/Disable music.
76       void enable_music(bool enable);
77
78       /// Is music enabled?
79       bool music_enabled()
80       {
81         return m_music_enabled;
82       }
83
84       /// Enable/Disable sound.
85       void enable_sound(bool enable);
86
87       /// Is sound enabled?
88       bool sound_enabled()
89       {
90         return m_sound_enabled;
91       }
92
93       /* functions handling the sound and music */
94       int open_audio(int frequency, Uint16 format, int channels, int chunksize);
95       void close_audio( void );
96
97       /// Is audio available?
98       bool audio_device_available()
99       {
100         return audio_device;
101       }
102
103       void set_audio_device_available(bool available)
104       {
105         audio_device = available;
106       }
107
108       static SoundManager* get()
109         {
110           return instance_ ? instance_ : instance_ = new SoundManager();
111         }
112       static void destroy_instance()
113       {
114         delete instance_;
115         instance_ = 0;
116       }
117       
118       void add_sound(Mix_Chunk* sound, int id)
119       {
120         sounds[id] = sound;
121       }
122       
123       Mix_Chunk* get_sound(int id)
124       {
125         return sounds[id];
126       }
127
128     private:
129       SoundManager();
130       ~SoundManager();
131
132       // music part
133       friend class MusicRef;
134       friend class Setup;
135       friend Mix_Chunk* IDToSound(int id);
136       
137       static SoundManager* instance_ ;
138
139       void free_chunk(Mix_Chunk* chunk);
140
141       /// Resource for music.
142       /** Contains the raw music data and
143           information for music reference
144           counting. */
145       class MusicResource
146         {
147         public:
148           ~MusicResource();
149
150           SoundManager* manager;
151           Mix_Music* music;
152           int refcount;
153         };
154
155       void free_music(MusicResource* music);
156
157       /*variables for stocking the sound and music*/
158       std::map<int, Mix_Chunk*> sounds;
159       std::map<std::string, MusicResource> musics;
160       MusicResource* current_music;
161       bool m_music_enabled;
162       bool m_sound_enabled;
163       bool audio_device;        /* != 0: available and initialized */
164
165     };
166
167    Mix_Chunk* IDToSound(int id);
168     
169 } // namespace SuperTux
170 #endif /*SUPERTUX_SOUND_MANAGER_H*/
171