1 /* $Id: sound.c,v 1.8 2005-07-06 13:11:56 stpohle Exp $ */
2 /* sound */
3
4 #include "sound.h"
5
6 _snd snd;
7
8
9 /* display the sound options */
10 void
snd_options()11 snd_options () {
12 /* int menuselect = 0;
13 _menu menu[] = {
14 {1, "Music Play"},
15 {1, "Sound Play"},
16 {2, "Return To Main Menu"},
17 {-1, ""}
18 };
19
20 while (menuselect != -1 && bman.state != GS_quit) {
21 if (snd.playmusic)
22 sprintf (menu[0].text, "Music Play ON");
23 else
24 sprintf (menu[0].text, "Music Play OFF");
25
26 if (snd.playsound)
27 sprintf (menu[1].text, "Sound Play ON");
28 else
29 sprintf (menu[1].text, "Sound Play OFF");
30
31 menuselect = menu_loop ("Audio Options", menu, menuselect);
32 switch (menuselect) {
33 case (0): // Play Music
34 snd.playmusic = 1-snd.playmusic;
35 break;
36 case (1): // Play Sound
37 snd.playsound = 1-snd.playsound;
38 break;
39 case (2):
40 menuselect = -1;
41 break;
42 }
43 } */
44 };
45
46
47 /* play an soundsample SND_* */
48 void
snd_play(int samplenr)49 snd_play (int samplenr)
50 {
51 #if HAVE_SDL_MIXER
52 if (samplenr < SND_max && snd.inited && snd.sample[samplenr] != NULL && snd.playsound)
53 Mix_PlayChannel (-1, snd.sample[samplenr], 0);
54 #endif
55 };
56
57
58 /* start playing music */
59 void
snd_music_start()60 snd_music_start ()
61 {
62 #if HAVE_SDL_MIXER
63 if (snd.inited && snd.music != NULL && snd.playmusic)
64 Mix_PlayMusic (snd.music, -1);
65 #endif
66 };
67
68 /* stop playing music */
69 void
snd_music_stop()70 snd_music_stop ()
71 {
72 #if HAVE_SDL_MIXER
73 if (snd.inited)
74 Mix_HaltMusic ();
75 #endif
76 };
77
78 /* init audio, sdl_mixer */
79 void
snd_init()80 snd_init ()
81 {
82 #if HAVE_SDL_MIXER
83 if (Mix_OpenAudio (snd.audio_rate, snd.audio_format, snd.audio_channels, 1024) < 0) {
84 d_printf ("Couldn't open audio mixer: %s\n", SDL_GetError ());
85 snd.inited = 0;
86 return;
87 }
88
89 Mix_QuerySpec (&snd.audio_rate, &snd.audio_format, &snd.audio_channels);
90 d_printf ("Opened audio at %d Hz %d bit %s\n",
91 snd.audio_rate, (snd.audio_format & 0xFF), (snd.audio_channels > 1 ? "stereo" : "mono"));
92
93 Mix_Volume (-1, 96);
94 Mix_VolumeMusic (48);
95
96 snd.inited = 1;
97 #endif
98 return;
99 };
100
101
102 void
snd_shutdown()103 snd_shutdown () {
104 #if HAVE_SDL_MIXER
105 if (snd.inited) {
106 snd.inited = 0;
107 Mix_CloseAudio ();
108 }
109 #endif
110 return;
111 };
112
113
114 /* load the audio files */
115 void
snd_load(char * tilesetname)116 snd_load (char *tilesetname)
117 {
118 #if HAVE_SDL_MIXER
119 char fullname[LEN_PATHFILENAME];
120 char filename[LEN_FILENAME];
121 _direntry *destart, *de, *desel;
122 int i, max, sel;
123
124 // load samples
125 d_printf ("Loading Audioset\n");
126
127 for (i = 0; i < SND_max; i++) {
128 switch (i) {
129 case (SND_dead):
130 sprintf (filename, "dead.wav");
131 break;
132 case (SND_explode):
133 sprintf (filename, "explode.wav");
134 break;
135 default:
136 sprintf (filename, "drop.wav");
137 break;
138 }
139
140 /* try loading the sample from the tileset or the default */
141 sprintf (fullname, "%s/tileset/%s/%s", bman.datapath, tilesetname, filename);
142 if ((snd.sample[i] = Mix_LoadWAV (fullname)) == NULL) {
143 sprintf (fullname, "%s/tileset/default/%s", bman.datapath, filename);
144 if ((snd.sample[i] = Mix_LoadWAV (fullname)) == NULL)
145 d_printf ("Couldn't load %s: %s\n", fullname, SDL_GetError ());
146 }
147 }
148
149
150 /* random selection of an sound file */
151 sprintf (fullname, "%s/music", bman.datapath);
152 desel = destart = s_getdir (fullname);
153
154 for (max = 0, de = destart; de != NULL; de = de->next)
155 if (de->name[0] != '.' && (de->flags & DF_file) == DF_file)
156 max++;
157
158 sel = s_random (max);
159 for (max = 0, de = destart; max <= sel && de != NULL; de = de->next)
160 if (de->name[0] != '.' && (de->flags & DF_file) == DF_file) {
161 desel = de;
162 max++;
163 }
164
165 /* try loading the music from the tileset or the default */
166 if (desel != NULL) {
167 sprintf (fullname, "%s/music/%s", bman.datapath, desel->name);
168 if ((snd.music = Mix_LoadMUS (fullname)) == NULL)
169 d_printf ("Couldn't load %s: %s\n", fullname, SDL_GetError ());
170 }
171
172 #endif
173 return;
174 };
175
176
177 void
snd_free()178 snd_free ()
179 {
180 #if HAVE_SDL_MIXER
181 int i;
182
183 for (i = 0; i < SND_max; i++)
184 if (snd.sample[i] != NULL)
185 Mix_FreeChunk (snd.sample[i]);
186
187 if (snd.music != NULL)
188 Mix_FreeMusic (snd.music);
189 #endif
190 };
191