1 /*
2 * IceBreaker
3 * Copyright (c) 2000-2002 Matthew Miller <mattdm@mattdm.org>
4 *
5 * <http://www.mattdm.org/icebreaker/>
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the Free
9 * Software Foundation; either version 2 of the License, or (at your option)
10 * any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 * for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc., 59
19 * Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 */
22 
23 #include <SDL.h>
24 #include <SDL_mixer.h>
25 #include "icebreaker.h"
26 #include "sound.h"
27 #include "options.h"
28 
29 
30 static Mix_Chunk *sndouch = NULL;
31 static Mix_Chunk *sndbreak = NULL;
32 
33 
initsound()34 void initsound()
35 {
36 	gameflags.soundsystemworks=false;
37 
38 	if (commandline.sound==SOUNDOFF)
39 		return;
40 
41 	if (SDL_InitSubSystem(SDL_INIT_AUDIO))
42 	{
43 		fprintf(stderr, "Can't start sound subsystem: %s.\nContinuing without it.\n", SDL_GetError());
44 	}
45 	else if (Mix_OpenAudio(22050, AUDIO_U8, 2, 1024))
46 	{
47 		fprintf(stderr, "Can't open audio: %s.\nContinuing without sound support.\n", SDL_GetError());
48 		fprintf(stderr, "You may see some other errors from system sound libraries. Ignore them.\n");
49 	}
50 	else
51 	{
52 		gameflags.soundsystemworks=true;
53 		loadsounds(NULL,DATAPREFIX "/" SNDFILEOUCH,DATAPREFIX "/" SNDFILEBREAK);
54 	}
55 }
56 
loadsounds(char * themename,char * soundouchfile,char * soundcrashfile)57 int loadsounds(char* themename, char* soundouchfile, char* soundcrashfile)
58 {
59 	int rc=0;
60 
61 	if (gameflags.soundsystemworks==false)
62 		return 0;
63 
64 	if (soundouchfile!=NULL)
65 	{
66 		while (Mix_Playing(-1));
67 		if (sndouch) { Mix_FreeChunk(sndouch); sndouch = NULL; }
68 		if ( (sndouch=Mix_LoadWAV(soundouchfile)) == NULL)
69 		{
70 			fprintf(stderr, "Error loading " DATAPREFIX "/" SNDFILEOUCH "\n%s\n", SDL_GetError());
71 			rc--;
72 		}
73 	}
74 
75 	if (soundcrashfile!=NULL)
76 	{
77 		while (Mix_Playing(-1));
78 		if (sndbreak) { Mix_FreeChunk(sndbreak); sndbreak = NULL; }
79 		if ( (sndbreak=Mix_LoadWAV(soundcrashfile)) == NULL)
80 		{
81 			fprintf(stderr, "Error loading " DATAPREFIX "/" SNDFILEBREAK "\n%s\n", SDL_GetError()); // fix -- is "break" or "crash" a better generic term?
82 			rc--;
83 		}
84 	}
85 
86 	return rc;
87 }
quitsound()88 void quitsound()
89 {
90 	while (Mix_Playing(-1));
91 
92 	if (sndouch) { Mix_FreeChunk(sndouch); sndouch = NULL; }
93 	if (sndbreak) { Mix_FreeChunk(sndbreak); sndbreak = NULL; }
94 
95 	if (gameflags.soundsystemworks) Mix_CloseAudio();
96 }
97 
98 #define MAXBREAKSOUNDOVERLAP 30
playsound(SoundSample s)99 void playsound(SoundSample s)
100 {
101 	static Uint32 lastplayedbreak=0;
102 
103 	if (gameflags.soundsystemworks && options.sound==SOUNDON)
104 	{
105 		switch (s)
106 		{
107 			case SNDOUCH:
108 				Mix_PlayChannel(-1, sndouch, 0);
109 			break;
110 			case SNDBREAK:
111 				if (SDL_GetTicks()-lastplayedbreak>=MAXBREAKSOUNDOVERLAP)
112 				{ // fix -- this is on the right track, but it'd be better
113 				  //        to some how schedule the sound to be played later.
114 					lastplayedbreak=SDL_GetTicks();
115 					Mix_PlayChannel(-1, sndbreak, 0);
116 				}
117 			break;
118 		}
119 	}
120 }
121