28 bool audio::audio_initialized =
false;
30 int audio::background_volume;
31 int audio::effects_volume;
35 Mix_Music *audio::music[NUM_MUSIC];
36 string audio::music_file[NUM_MUSIC];
37 Mix_Chunk *audio::sounds[NUM_WAVES];
38 int audio::current_background;
39 int audio::last_background;
40 bool audio::background_paused;
41 int audio::audio_rate;
42 Uint16 audio::buffer_size;
43 Uint16 audio::audio_format;
44 int audio::audio_channels;
48 bool audio::schedule_active = 0;
49 PyObject *audio::schedule_args = NULL;
52 void audio::init (
config *myconfig) {
79 current_background = -1;
81 background_paused =
false;
82 audio_initialized =
false;
83 schedule_active =
false;
86 for (i = 0; i < NUM_WAVES; i++) sounds[i] = NULL;
87 for (i = 0; i < NUM_MUSIC; i++) {
93 i = Mix_OpenAudio(audio_rate, audio_format, audio_channels, buffer_size);
98 fprintf(stderr,
"Unable to open audio: %s\n", SDL_GetError());
99 fprintf(stderr,
"Audio will not be used.\n");
103 audio_initialized =
true;
104 Mix_QuerySpec(&audio_rate, &audio_format, &audio_channels);
105 set_background_volume (background_volume);
109 void audio::cleanup(
void)
113 current_background = -1;
116 for (i = 0; i < NUM_WAVES; i++)
122 for (i = 0; i < NUM_MUSIC; i++)
124 unload_background(i);
133 if (audio_initialized ==
true)
136 audio_initialized =
false;
140 int audio::load_background(
int slot,
char *filename) {
142 if (!audio_initialized)
return (0);
145 if ((slot >= NUM_MUSIC) || (slot < 0)) {
146 fprintf(stderr,
"Error: Tried to put music in invalid slot.\n");
151 FILE *f = fopen (filename,
"r");
153 fprintf(stderr,
"Error: No such file: %s.\n", filename);
159 if (music[slot] != NULL)
160 unload_background (slot);
163 music[slot] = Mix_LoadMUS(filename);
164 music_file[slot] = filename;
176 void audio::unload_background (
int slot)
178 if (music[slot] == NULL)
return;
182 if (current_background == slot)
184 last_background = current_background;
185 current_background = -1;
192 Mix_FreeMusic (music[slot]);
194 music_file[slot] =
"";
201 void audio::pause_music(
void) {
205 void audio::unpause_music(
void) {
211 void audio::set_background_volume(
int volume) {
215 background_volume = 0;
216 }
else if (volume > 100) {
217 background_volume = 100;
219 background_volume = volume;
222 Mix_VolumeMusic(
int(background_volume * 1.28));
227 int audio::load_wave(
int slot,
char *filename) {
229 if (!audio_initialized)
return(1);
232 if ((slot >= NUM_WAVES) || (slot < 0)) {
233 fprintf(stderr,
"Error: Tried to put wave in invalid slot.\n");
237 FILE *f = fopen (filename,
"r");
246 sounds[slot] = Mix_LoadWAV(filename);
251 void audio::unload_wave(
int wave) {
252 if (sounds[wave] != NULL) {
253 Mix_FreeChunk(sounds[wave]);
258 void audio::play_wave(
int channel,
int slot) {
259 if ((slot > -1) && (slot < NUM_CHANNELS))
260 if (sounds[slot] != NULL) Mix_PlayChannel(channel, sounds[slot], 0);
263 void audio::play_background(
int slot) {
264 if (music[slot] != NULL) {
265 current_background = slot;
266 Mix_PlayMusic(music[current_background], 0);
270 void audio::fade_out_background(
int time) {
271 if (Mix_PlayingMusic ())
273 Mix_FadeOutMusic(time);
274 last_background = current_background;
275 current_background = -1;
282 void audio::fade_in_background(
int slot,
int time) {
283 if (music[slot] != NULL) {
284 current_background = slot;
285 Mix_FadeInMusic(music[slot], 0, time);
290 void audio::change_background(
int slot,
int time) {
291 fade_out_background(time);
292 fade_in_background(slot, time);
303 void audio::set_schedule (
string file, PyObject * args)
307 Py_XDECREF (schedule_args);
308 schedule_args = NULL;
313 schedule_args = args;
314 Py_XINCREF (schedule_args);
320 void audio::run_schedule ()
322 PyObject *song = Py_BuildValue (
"(i)", last_background);
323 if (schedule_active) schedule.
call_method (
"music_finished", song);
331 current_background >> file;
334 if (current_background != -1) music_file[current_background] >> file;
344 is_schedule_activated () >> file;
356 last_background << file;
359 if (last_background != -1)
364 if (load_background (last_background, (
char *) song.c_str ()))
365 play_background (last_background);
369 PyObject * args = NULL;
374 set_schedule (script, args);
377 schedule_active << file;
Class to write data from a Gzip compressed file.
void call_method(const string &name, PyObject *args=NULL) const
Call a method of this object.
Class to read data from a Gzip compressed file.
void clear()
Resets the script to it's post-constructor state.
u_int8 audio_sample_rate
The sample rate: 11025 Hz (0), 22050 Hz (1) or 44100 Hz (2)
static void put_tuple(PyObject *tuple, ogzstream &file)
Save a Python tuple into a file.
std::string class_name() const
Returns the class name of this object.
bool create_instance(string file, string classname, PyObject *args=NULL)
Creates an instance of a Python class.
static PyObject * get_tuple(igzstream &file)
Loads a Python tuple previously saved with put_tuple ().
u_int8 audio_volume
The volume: a value betwen 0 and 100.
u_int8 audio_resolution
The resolution: 8 bit (0) or 16 bit (1)
u_int8 audio_channels
The number of channels: mono (0) or stereo (1).
This class contains the engine's configuration read either from the config file or from the command l...
#define s_int8
8 bits long signed integer