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