fix file lookup
[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 using namespace SuperTux;
30
31 class MusicRef;
32 class MovingObject;
33
34 /** Sound manager
35  * This class handles all sounds that are played
36  */
37 class SoundManager
38 {
39 public:
40   SoundManager();
41   ~SoundManager();
42
43   /// Play sound.
44   void play_sound(const std::string& sound);
45   /// Play sound relative to two Vectors.
46   void play_sound(const std::string& sound, const Vector& pos,
47       const Vector& pos2);
48   /// Play sound relative to a MovingObject and a Vector.
49   void play_sound(const std::string& sound, const MovingObject* object,
50       const Vector& pos);
51   
52   /** Load music.
53    * Is used to load the music for a MusicRef.
54    */
55   MusicRef load_music(const std::string& file);
56
57   /**
58    * If the sound isn't loaded yet try to load it.
59    * Returns an existing instance of the sound, loads a new one and returns that
60    * or returns 0 if loading failed.
61    */
62   Mix_Chunk* preload_sound(const std::string& name);
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    */
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   /// Is audio available?
94   bool audio_device_available()
95   {
96     return audio_device;
97   }
98
99   void set_audio_device_available(bool available)
100   {
101     audio_device = available;
102   }
103
104 private:
105   friend class MusicRef;
106   friend class Setup;
107   
108   /// Resource for music.
109   /** Contains the raw music data and
110       information for music reference
111       counting. */
112   class MusicResource
113     {
114     public:
115       ~MusicResource();
116
117       SoundManager* manager;
118       Mix_Music* music;
119       int refcount;
120     };
121
122   void free_music(MusicResource* music);
123
124   typedef std::map<std::string, Mix_Chunk*> Sounds;
125   Sounds sounds;
126
127   typedef std::map<std::string, MusicResource> Musics;
128   Musics musics;
129
130   MusicResource* current_music;
131   bool m_music_enabled;
132   bool m_sound_enabled;
133   bool audio_device;        /* true: available and initialized */
134 };
135
136 #endif /*SUPERTUX_SOUND_MANAGER_H*/
137