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