09c0d00aa4c7c4acbddf90f6453dbadcd758cad0
[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.
42   void play_sound(const std::string& sound);
43   /// Play sound relative to two Vectors.
44   void play_sound(const std::string& sound, const Vector& pos,
45       const Vector& pos2);
46   /// Play sound relative to a MovingObject and a Vector.
47   void play_sound(const std::string& sound, const MovingObject* object,
48       const Vector& pos);
49   
50   /** Load music.
51    * Is used to load the music for a MusicRef.
52    */
53   MusicRef load_music(const std::string& file);
54
55   /**
56    * If the sound isn't loaded yet try to load it.
57    * Returns an existing instance of the sound, loads a new one and returns that
58    * or returns 0 if loading failed.
59    */
60   Mix_Chunk* preload_sound(const std::string& name);
61
62   /// Test if a certain music file exists.
63   bool exists_music(const std::string& filename);
64
65   /** Play music.
66    * @param loops: Defaults to -1, which means endless loops.
67    */
68   void play_music(const MusicRef& music, int loops = -1);
69
70   /// Halt music.
71   void halt_music();
72
73   /// Enable/Disable music.
74   void enable_music(bool enable);
75
76   /// Is music enabled?
77   bool music_enabled()
78   {
79     return m_music_enabled;
80   }
81
82   /// Enable/Disable sound.
83   void enable_sound(bool enable);
84
85   /// Is sound enabled?
86   bool sound_enabled()
87   {
88     return m_sound_enabled;
89   }
90
91   /// Is audio available?
92   bool audio_device_available()
93   {
94     return audio_device;
95   }
96
97   void set_audio_device_available(bool available)
98   {
99     audio_device = available;
100   }
101
102 private:
103   friend class MusicRef;
104   friend class Setup;
105   
106   /// Resource for music.
107   /** Contains the raw music data and
108       information for music reference
109       counting. */
110   class MusicResource
111     {
112     public:
113       ~MusicResource();
114
115       SoundManager* manager;
116       Mix_Music* music;
117       int refcount;
118     };
119
120   void free_music(MusicResource* music);
121
122   typedef std::map<std::string, Mix_Chunk*> Sounds;
123   Sounds sounds;
124
125   typedef std::map<std::string, MusicResource> Musics;
126   Musics musics;
127
128   MusicResource* current_music;
129   bool m_music_enabled;
130   bool m_sound_enabled;
131   bool audio_device;        /* true: available and initialized */
132 };
133
134 #endif /*SUPERTUX_SOUND_MANAGER_H*/
135