]> git.zerfleddert.de Git - micropolis/blame - src/sim/w_sound.c
Fix SDL includes for OS X.
[micropolis] / src / sim / w_sound.c
CommitLineData
bf4857d3
DP
1/*
2 * Portions Copyright (c) 2008 Deanna Phillips <deanna@sdf.lonestar.org>
3 */
4
6a5fa4e0
MG
5/* w_sound.c
6 *
7 * Micropolis, Unix Version. This game was released for the Unix platform
8 * in or about 1990 and has been modified for inclusion in the One Laptop
9 * Per Child program. Copyright (C) 1989 - 2007 Electronic Arts Inc. If
10 * you need assistance with this program, you may contact:
11 * http://wiki.laptop.org/go/Micropolis or email micropolis@laptop.org.
12 *
13 * This program is free software: you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation, either version 3 of the License, or (at
16 * your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful, but
19 * WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 * General Public License for more details. You should have received a
22 * copy of the GNU General Public License along with this program. If
23 * not, see <http://www.gnu.org/licenses/>.
24 *
25 * ADDITIONAL TERMS per GNU GPL Section 7
26 *
27 * No trademark or publicity rights are granted. This license does NOT
28 * give you any right, title or interest in the trademark SimCity or any
29 * other Electronic Arts trademark. You may not distribute any
30 * modification of this program using the trademark SimCity or claim any
31 * affliation or association with Electronic Arts Inc. or its employees.
32 *
33 * Any propagation or conveyance of this program must include this
34 * copyright notice and these terms.
35 *
36 * If you convey this program (or any modifications of it) and assume
37 * contractual liability for the program to recipients of it, you agree
38 * to indemnify Electronic Arts for any liability that those contractual
39 * assumptions impose on Electronic Arts.
40 *
41 * You may not misrepresent the origins of this program; modified
42 * versions of the program must be marked as such and not identified as
43 * the original program.
44 *
45 * This disclaimer supplements the one included in the General Public
46 * License. TO THE FULLEST EXTENT PERMISSIBLE UNDER APPLICABLE LAW, THIS
47 * PROGRAM IS PROVIDED TO YOU "AS IS," WITH ALL FAULTS, WITHOUT WARRANTY
48 * OF ANY KIND, AND YOUR USE IS AT YOUR SOLE RISK. THE ENTIRE RISK OF
49 * SATISFACTORY QUALITY AND PERFORMANCE RESIDES WITH YOU. ELECTRONIC ARTS
50 * DISCLAIMS ANY AND ALL EXPRESS, IMPLIED OR STATUTORY WARRANTIES,
51 * INCLUDING IMPLIED WARRANTIES OF MERCHANTABILITY, SATISFACTORY QUALITY,
52 * FITNESS FOR A PARTICULAR PURPOSE, NONINFRINGEMENT OF THIRD PARTY
53 * RIGHTS, AND WARRANTIES (IF ANY) ARISING FROM A COURSE OF DEALING,
54 * USAGE, OR TRADE PRACTICE. ELECTRONIC ARTS DOES NOT WARRANT AGAINST
55 * INTERFERENCE WITH YOUR ENJOYMENT OF THE PROGRAM; THAT THE PROGRAM WILL
56 * MEET YOUR REQUIREMENTS; THAT OPERATION OF THE PROGRAM WILL BE
57 * UNINTERRUPTED OR ERROR-FREE, OR THAT THE PROGRAM WILL BE COMPATIBLE
58 * WITH THIRD PARTY SOFTWARE OR THAT ANY ERRORS IN THE PROGRAM WILL BE
59 * CORRECTED. NO ORAL OR WRITTEN ADVICE PROVIDED BY ELECTRONIC ARTS OR
60 * ANY AUTHORIZED REPRESENTATIVE SHALL CREATE A WARRANTY. SOME
61 * JURISDICTIONS DO NOT ALLOW THE EXCLUSION OF OR LIMITATIONS ON IMPLIED
62 * WARRANTIES OR THE LIMITATIONS ON THE APPLICABLE STATUTORY RIGHTS OF A
63 * CONSUMER, SO SOME OR ALL OF THE ABOVE EXCLUSIONS AND LIMITATIONS MAY
64 * NOT APPLY TO YOU.
65 */
0a295df9
DP
66#include "SDL.h"
67#include "SDL_mixer.h"
6a5fa4e0
MG
68#include "sim.h"
69
70
bf4857d3
DP
71#define SIM_NSOUNDS 47
72#define SIM_NCHANNELS 32
73#define DOZER_CHANNEL 0
74#define DOZER_SOUND "rumble.wav"
75
76struct sound {
77 char *id;
78 char *file;
79 Mix_Chunk *wave;
80};
81
82struct sound sounds[SIM_NSOUNDS] = {
83 { "A", "a.wav", NULL },
84 { "Aaah", "aaah.wav", NULL },
85 { "Airport", "airport.wav", NULL },
86 { "Beep", "beep.wav", NULL },
87 { "Boing", "boing.wav", NULL },
88 { "Bop", "bop.wav", NULL },
89 { "Build", "build.wav", NULL },
90 { "Bulldozer", "bulldozer.wav", NULL },
91 { "Chalk", "chalk.wav", NULL },
92 { "Coal", "coal.wav", NULL },
93 { "Com", "com.wav", NULL },
94 { "Computer", "computer.wav", NULL },
95 { "Cuckoo", "cuckoo.wav", NULL },
96 { "E", "e.wav", NULL },
97 { "Eraser", "eraser.wav", NULL },
98 { "Explosion-High", "explosion-high.wav", NULL },
99 { "Explosion-Low", "explosion-low.wav", NULL },
100 { "Fire", "fire.wav", NULL },
101 { "HeavyTraffic", "heavytraffic.wav", NULL },
102 { "HonkHonk-High", "honkhonk-high.wav", NULL },
103 { "HonkHonk-Low", "honkhonk-low.wav", NULL },
104 { "HonkHonk-Med", "honkhonk-med.wav", NULL },
105 { "Ignition", "ignition.wav", NULL },
106 { "Ind", "ind.wav", NULL },
107 { "Monster", "monster.wav", NULL },
108 { "Nuclear", "nuclear.wav", NULL },
109 { "O", "o.wav", NULL },
110 { "Oop", "oop.wav", NULL },
111 { "Park", "park.wav", NULL },
112 { "Player", "player.wav", NULL },
113 { "Police", "police.wav", NULL },
114 { "QuackQuack", "quackquack.wav", NULL },
115 { "Query", "query.wav", NULL },
116 { "Rail", "rail.wav", NULL },
117 { "Res", "res.wav", NULL },
118 { "Road", "road.wav", NULL },
119 { "Rumble", "rumble.wav", NULL },
120 { "Seaport", "seaport.wav", NULL },
121 { "Siren", "siren.wav", NULL },
122 { "Skid", "skid.wav", NULL },
123 { "Sorry", "sorry.wav", NULL },
124 { "Stadium", "stadium.wav", NULL },
125 { "UhUh", "uhuh.wav", NULL },
126 { "Whip", "whip.wav", NULL },
127 { "Wire", "wire.wav", NULL },
128 { "Woosh", "woosh.wav", NULL },
129 { "Zone", "zone.wav", NULL }
130};
131
6a5fa4e0
MG
132/* Sound routines */
133
134
135int SoundInitialized = 0;
bf4857d3 136Mix_Chunk *rumble;
6a5fa4e0
MG
137
138
139InitializeSound()
140{
bf4857d3
DP
141 int reserved_chans;
142 char buf[256];
6a5fa4e0 143
bf4857d3
DP
144 if (SDL_Init(SDL_INIT_AUDIO) == -1) {
145 fprintf(stderr, "SDL_Init: %s\n", SDL_GetError());
146 return;
147 }
6a5fa4e0 148
bf4857d3
DP
149 if (Mix_OpenAudio(22050, MIX_DEFAULT_FORMAT, 1, 1024) == -1) {
150 fprintf(stderr, "Mix_OpenAudio: %s\n", Mix_GetError());
151 return;
152 }
153
154 reserved_chans = Mix_ReserveChannels(1);
6a5fa4e0 155
bf4857d3
DP
156 if (reserved_chans != 1) {
157 fprintf(stderr, "Mix_ReserveChannels: %s\n", Mix_GetError());
158 return;
159 }
160
161 if (Mix_AllocateChannels(SIM_NCHANNELS) == -1) {
162 fprintf(stderr, "Mix_AllocateChannels: %s\n", Mix_GetError());
163 return;
164 }
165
166 snprintf(buf, sizeof(buf), "%s/sounds/%s", ResourceDir, DOZER_SOUND);
167 rumble = Mix_LoadWAV(buf);
168
169 if (rumble == NULL) {
170 printf("Mix_LoadWAV: %s\n", Mix_GetError());
171 return;
172 }
173
174 SoundInitialized = 1;
6a5fa4e0
MG
175}
176
177
178ShutDownSound()
179{
bf4857d3
DP
180 int i;
181 SoundInitialized = 0;
182
183 for (i = 0; i < SIM_NSOUNDS; i++) {
184 if (sounds[i].wave) {
185 Mix_FreeChunk(sounds[i].wave);
186 sounds[i].wave = NULL;
187 }
188 }
189 if (rumble) {
190 Mix_FreeChunk(rumble);
191 rumble = NULL;
6a5fa4e0 192 }
bf4857d3
DP
193 Mix_CloseAudio();
194 SDL_Quit();
6a5fa4e0
MG
195}
196
197
198MakeSound(char *channel, char *id)
199{
200 char buf[256];
bf4857d3 201 int i;
6a5fa4e0
MG
202
203 if (!UserSoundOn) return;
bf4857d3 204 if (!SoundInitialized) return;
6a5fa4e0 205
bf4857d3
DP
206 for (i = 0; i < SIM_NSOUNDS; i++) {
207 if (!strcmp(sounds[i].id, id))
208 break;
209 }
210
211 if (sounds[i].wave) {
212 if (Mix_PlayChannel(-1, sounds[i].wave, 0) == -1)
213 fprintf(stderr, "Mix_PlayChannel: %s\n", Mix_GetError());
214 return;
215 }
216
217 snprintf(buf, sizeof(buf), "%s/sounds/%s", ResourceDir,
218 sounds[i].file);
219
220 sounds[i].wave = Mix_LoadWAV(buf);
6a5fa4e0 221
bf4857d3
DP
222 if (sounds[i].wave == NULL) {
223 fprintf(stderr, "Mix_LoadWAV: %s\n", Mix_GetError());
224 return;
225 }
226
227 if (Mix_PlayChannel(-1, sounds[i].wave, 0) == -1)
228 fprintf(stderr, "Mix_PlayChannel: %s\n", Mix_GetError());
229}
6a5fa4e0
MG
230
231MakeSoundOn(SimView *view, char *channel, char *id)
232{
6a5fa4e0 233 if (!UserSoundOn) return;
bf4857d3 234 if (!SoundInitialized) return;
6a5fa4e0 235
bf4857d3 236 MakeSound(channel, id);
6a5fa4e0
MG
237}
238
239
240StartBulldozer(void)
241{
242 if (!UserSoundOn) return;
bf4857d3
DP
243 if (!SoundInitialized) return;
244
245 if (Mix_PlayChannel(DOZER_CHANNEL, rumble, 4) == -1) {
246 printf("Mix_PlayChannel: %s\n", Mix_GetError());
247 return;
6a5fa4e0
MG
248 }
249}
250
251
252StopBulldozer(void)
253{
bf4857d3
DP
254 if (!UserSoundOn) return;
255 if (!SoundInitialized) return;
256
257 Mix_HaltChannel(DOZER_CHANNEL);
6a5fa4e0
MG
258}
259
260
bf4857d3 261/* XXX comefrom: doKeyEvent */
6a5fa4e0
MG
262SoundOff(void)
263{
bf4857d3 264 ShutDownSound();
6a5fa4e0
MG
265}
266
267
268DoStartSound(char *channel, char *id)
269{
bf4857d3
DP
270 MakeSound(channel, id);
271}
6a5fa4e0 272
bf4857d3
DP
273DoStopSound(char *id)
274{
275 StopBulldozer();
6a5fa4e0
MG
276}
277
bf4857d3
DP
278SoundCmd(CLIENT_ARGS)
279{
280 if (!strcmp(argv[2], "Rumble"))
281 StartBulldozer();
282 else
283 MakeSound(NULL, argv[2]);
284 return 0;
285}
6a5fa4e0 286
bf4857d3 287DozerCmd(CLIENT_ARGS)
6a5fa4e0 288{
bf4857d3
DP
289 StopBulldozer();
290 return 0;
291}
6a5fa4e0 292
bf4857d3
DP
293sound_command_init()
294{
295 Tcl_CreateCommand(tk_mainInterp, "playsound", SoundCmd,
296 (ClientData)NULL, (void (*)()) NULL);
297 Tcl_CreateCommand(tk_mainInterp, "stopdozer", DozerCmd,
298 (ClientData)NULL, (void (*)()) NULL);
6a5fa4e0 299}
Impressum, Datenschutz