JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
oops, was drawing *inactive* bang dots
[vor.git] / sound.c
1 #include <SDL/SDL.h>
2 #include <SDL/SDL_mixer.h>
3 #include <stdlib.h>
4 #include <stdio.h>
5 #include <unistd.h>
6
7 #include "args.h"
8 #include "common.h"
9 #include "config.h"
10 #include "sound.h"
11
12
13 static Mix_Music *music[NUM_TUNES];
14 static int music_volume[NUM_TUNES] = {128,128,128};
15 static Mix_Chunk *wav[NUM_SOUNDS];
16
17 int audio_rate;
18 Uint16 audio_format;
19 int audio_channels;
20
21 char *add_data_path(char *);
22 char *wav_file[] = {
23         "booom.wav",
24         "cboom.wav",
25         "boom.wav",
26         "bzboom.wav"
27 };
28
29 char *tune_file[] = {
30         "magic.mod",
31         "getzznew.mod",
32         "4est_fulla3s.mod"
33 };
34
35 // Return 1 if the sound is ready to roll, and 0 if not.
36 int
37 init_sound() {
38         int i;
39         char *s;
40
41         // Initialise output with SDL_mixer
42         if (Mix_OpenAudio(MIX_DEFAULT_FREQUENCY, AUDIO_S16, MIX_DEFAULT_CHANNELS, 4096) < 0) {
43         fprintf(stderr, "Couldn't open SDL_mixer audio: %s\n", SDL_GetError());
44         return 0;
45         }
46
47         // Preload all the tunes into memory
48         for (i=0; i<NUM_TUNES; i++) {
49                 s = add_data_path(tune_file[i]);
50                 if(s) {
51                         music[i] = Mix_LoadMUS(s);
52                         if(!music[i]) printf("Failed to load %s.\n", s);
53                         free(s);
54                 }
55         }
56
57         // Preload all the wav files into memory
58         for (i=0; i<NUM_SOUNDS; i++) {
59                 s = add_data_path(wav_file[i]);
60                 if(s) {
61                         wav[i] = Mix_LoadWAV(s);
62                         free(s);
63                 }
64         }
65
66         return 1;
67 }
68
69 void
70 play_sound(int i)  {
71         if(!opt_sound) return;
72         Mix_PlayChannel(-1, wav[i], 0);
73 }/*}}}*/
74
75 int playing=-1;
76
77
78 void
79 play_tune(int i) {/*{{{*/
80         if(!opt_music) return;
81         if (playing==i)
82         return;
83         if (playing) {
84                 Mix_FadeOutMusic(1500);
85         }
86         Mix_FadeInMusic(music[i],-1,2000);
87         Mix_VolumeMusic(music_volume[i]);
88
89         playing = i;
90 }
91
92 /*
93  *
94  * The init_sound() routine is called first.
95  * The play_sound() routine is called with the index number of the sound we wish to play.
96  * The play_tune() routine is called with the index number of the tune we wish to play.
97  *
98  */