Use an absolute directory in /usr/share/games/ for game data
[kball.git] / src / sound.cpp
blob974362ac80c50f4f24869d39ebf6b7c4a45ee1c0
1 // -----------------------------------------------
2 // Sound System
3 // By Kronoman
4 // Copyright (c) 2004, Kronoman
5 // In loving memory of my father
6 // -----------------------------------------------
7 // sound.cpp
8 // -----------------------------------------------
9 // This system wraps around Allegro and DUMB,
10 // so I can use the high level sound routines,
11 // global adjust volume, disable volume, etc
12 // -----------------------------------------------
13 // The formulae for the sound volume is:
15 // 255 -- D (digital volume)
16 // V ---- X1 ==> X1 = (V * D) / 255
18 // 255 -- G (global volume, final result)
19 // X1 --- X2 ==> X2 = (X1 * G) / 255
21 // ==> X2 = (( (V * D) / 255) * G ) / 255
23 // For the music:
24 // 255 -- 1.0f
25 // X2 --- X3 ==> X3 = (X2 * 1.0f) / 255.0f ==> X2 / 255.0f
26 // -----------------------------------------------
28 #include <allegro.h> // Allegro : http://alleg.sf.net/
29 #include <aldumb.h> // DUMB : http://dumb.sf.net/
30 #include "sound.h"
32 // -----------------------------------------------
33 // static members and methods
34 // -----------------------------------------------
36 bool CSoundWrapper::global_enabled = true; // enables the sound system ?
37 int CSoundWrapper::global_volume = 255;
39 void CSoundWrapper::global_set_volume(int v)
41 if ( v < 0 || v > 255)
42 return;
44 global_volume = v;
47 int CSoundWrapper::global_get_volume()
49 return global_volume;
52 void CSoundWrapper::global_set_enabled(bool s)
54 global_enabled = s;
57 bool CSoundWrapper::global_is_enabled()
59 return global_enabled;
62 // -----------------------------------------------
63 // class methods
64 // -----------------------------------------------
67 CSoundWrapper::CSoundWrapper()
69 duh = NULL;
70 dp = NULL;
71 volume_d = 255;
72 volume_m = 255;
73 enabled = true;
76 CSoundWrapper::~CSoundWrapper()
78 // nothing to do for now
81 // -----------------------------------------------
82 // digital sample playing
83 // -----------------------------------------------
84 int CSoundWrapper::play_sample(const SAMPLE *spl, int vol, int pan, int freq, int loop)
86 if (spl == NULL || volume_d < 1 || global_volume < 1 || !enabled || !global_enabled)
87 return -1; // error man :P
89 int v = (((vol * volume_d) / 255) * global_volume ) / 255;
91 return ::play_sample(spl, v, pan, freq, loop); // call real allegro function
94 // -----------------------------------------------
95 // volume digital
96 // -----------------------------------------------
97 void CSoundWrapper::set_volume_d(int v)
99 if (v < 0 || v > 255)
100 return ;
102 this->volume_d = v;
105 int CSoundWrapper::get_volume_d()
107 return this->volume_d;
110 // -----------------------------------------------
111 // music
112 // -----------------------------------------------
114 // -----------------------------------------------
115 // call this before playing, dat should be a pointer to a music object of a datafile
116 // dat should be something like:
117 // DATAFILE *dat = load_datafile("smurf.dat");
118 // DUH *myduh = (DUH *)dat[GAME_MUSIC].dat;
119 // -----------------------------------------------
120 void CSoundWrapper::music_load(DUH *dat)
122 // DEBUG -- revisar esto, o algo
123 duh = dat;
126 // -----------------------------------------------
127 // start playing
128 // -----------------------------------------------
129 void CSoundWrapper::music_start()
131 if (duh == NULL)
132 return ;
134 if (!enabled || !global_enabled)
135 return;
137 dp = al_start_duh(duh, 2, 0, ((float)(volume_m / 255) * global_volume ) / 255.0 , 4096, 11025);
140 // -----------------------------------------------
141 // pause playback
142 // -----------------------------------------------
144 void CSoundWrapper::music_pause()
146 if (duh == NULL || dp == NULL)
147 return ;
149 al_pause_duh(dp);
152 // -----------------------------------------------
153 // resume playback
154 // -----------------------------------------------
156 void CSoundWrapper::music_resume()
158 if (duh == NULL) // no hay musica
159 return ;
161 if (dp == NULL) // no estaba tocando
163 music_start();
164 return;
167 al_resume_duh(dp);
170 // -----------------------------------------------
171 // poll music
172 // _must_ be called at regular intervals to hear the music
173 // -----------------------------------------------
174 void CSoundWrapper::music_poll()
176 if (duh == NULL)
177 return ;
179 if (dp == NULL)
180 music_start();
182 if (enabled && global_enabled && global_volume > 0)
183 al_poll_duh(dp);
184 else
185 music_stop();
188 // -----------------------------------------------
189 // stop playing
190 // -----------------------------------------------
191 void CSoundWrapper::music_stop()
193 if (duh == NULL || dp == NULL)
194 return ;
196 al_stop_duh(dp);
198 dp = NULL;
201 // -----------------------------------------------
202 // volume music
203 // -----------------------------------------------
204 void CSoundWrapper::set_volume_m(int v)
206 if ( v < 0 || v > 255)
207 return ;
209 this->volume_m = v;
211 if (dp != NULL)
212 al_duh_set_volume(dp, ((float)(volume_m / 255) * global_volume ) / 255.0 );
215 int CSoundWrapper::get_volume_m()
217 return volume_m;
220 // -----------------------------------------------
221 // for all the object
222 // -----------------------------------------------
223 void CSoundWrapper::set_enabled(bool s)
225 this->enabled = s;
228 bool CSoundWrapper::is_enabled()
230 return this->enabled;