File: scenesound.c

package info (click to toggle)
searchandrescue 0.8.2-10
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 4,656 kB
  • ctags: 6,111
  • sloc: ansic: 89,072; cpp: 7,691; sh: 90; makefile: 80
file content (244 lines) | stat: -rw-r--r-- 5,838 bytes parent folder | download | duplicates (3)
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>

#ifdef __MSW__
# include <windows.h>
#endif

#include "obj.h"
#include "objsound.h"
#include "objutils.h"
#include "sound.h"
#include "sar.h"
#include "scenesound.h"
#include "config.h"


void SARSceneSoundUpdate(
	sar_core_struct *core_ptr,
	Boolean engine_sounds,
	Boolean event_sounds,
	Boolean voice_sounds,
	Boolean music
);


#define ATOI(s)         (((s) != NULL) ? atoi(s) : 0)
#define ATOL(s)         (((s) != NULL) ? atol(s) : 0)
#define ATOF(s)         (((s) != NULL) ? atof(s) : 0.0f)
#define STRDUP(s)       (((s) != NULL) ? strdup(s) : NULL)

#define MAX(a,b)        (((a) > (b)) ? (a) : (b))
#define MIN(a,b)        (((a) < (b)) ? (a) : (b))
#define CLIP(a,l,h)     (MIN(MAX((a),(l)),(h)))
#define STRLEN(s)       (((s) != NULL) ? ((int)strlen(s)) : 0)


/*
 *	Updates all sound resources on the core's scene structure and
 *	its objects with respect to the given sound conditions. Purpose
 *	for this function is to start or stop sounds at startup or as the
 *	sound options change in the middle of a game.
 *
 *	The core structure's scene must be valid (not NULL).
 *	If the core structure's recorder is NULL and any given inputs
 *	suggest sound is to be enabled then the recorder will be
 *	initialized.
 *
 *	The core structure's recorder will never be shut down by this
 *	function.
 */
void SARSceneSoundUpdate(
	sar_core_struct *core_ptr,
	Boolean engine_sounds,
	Boolean event_sounds,
	Boolean voice_sounds,
	Boolean music 
)
{
	int obj_num;
	const char *name;
	int sndsrc_num;
	sar_object_struct *obj_ptr;
	sar_object_aircraft_struct *aircraft;
	void **snd_play_rtn;
	const char *sound_server_connect_arg = "127.0.0.1:9433";
	gw_display_struct *display = core_ptr->display;
	sar_scene_struct *scene = core_ptr->scene;
	snd_recorder_struct *recorder = core_ptr->recorder;

	/* Is recorder NULL? */
	if(recorder == NULL)
	{
	    /* Turn sound on? */
	    if(engine_sounds || event_sounds || voice_sounds ||
	       music
	    )
	    {
		/* Need to initialize recorder. */
		void *window;

		GWContextGet(
		    display, GWContextCurrent(display),
		    &window, NULL,
		    NULL, NULL, NULL, NULL
		);

		GWSetInputBusy(core_ptr->display);
		recorder = SoundInit(
		    core_ptr, SNDSERV_TYPE_Y,
		    sound_server_connect_arg,
		    NULL,          /* Do not start sound server. */
		    window
		);
		if(recorder == NULL)
		{
		    const char *con_arg = sound_server_connect_arg;
		    char *buf = (char *)malloc(
			(256 + STRLEN(con_arg)) * sizeof(char)
		    );
		    sprintf(
			buf,
"Unable to connect to sound server at:\n\n    %s",
			con_arg
		    );
		    GWOutputMessage(
			core_ptr->display,
			GWOutputMessageTypeError,
			"Sound initialization failed!",
			buf,
"Please check to make sure that your sound server\n\
is running and available at the specified address.\n\
Also make sure that you have sufficient permission\n\
to connect to it."
		    );
		    free(buf);
		}
		else
		{
		    core_ptr->recorder = recorder;

		    SoundChangeMode(
			core_ptr->recorder, core_ptr->audio_mode_name
		    );
		}
		GWSetInputReady(core_ptr->display);
	    }
	    else
	    {
		/* Recorder not initialized and sound is turned off,
		 * there is nothing further to do.
		 */
		return;
	    }
	}

	/* Scene structure must be valid. */
	if(scene == NULL)
	    return;


	/* Update sounds on the scene structure. */




	/* Update sounds on each object. */
	for(obj_num = 0; obj_num < core_ptr->total_objects; obj_num++)
	{
	    obj_ptr = core_ptr->object[obj_num];
	    if(obj_ptr == NULL)
		continue;

	    switch(obj_ptr->type)
	    {
	      case SAR_OBJ_TYPE_GARBAGE:
	      case SAR_OBJ_TYPE_STATIC:
	      case SAR_OBJ_TYPE_AUTOMOBILE:
	      case SAR_OBJ_TYPE_WATERCRAFT:
		break;
	      case SAR_OBJ_TYPE_AIRCRAFT:
		aircraft = SAR_OBJ_GET_AIRCRAFT(obj_ptr);
		if(aircraft != NULL)
		{
#define DO_LOAD_ENGINE_SOUND				\
{ if(*snd_play_rtn == NULL)				\
  *snd_play_rtn = SARSoundSourcePlayFromListRepeating(	\
   recorder, obj_ptr->sndsrc, obj_ptr->total_sndsrcs,	\
   name, &sndsrc_num					\
  );							\
}
#define DO_UNLOAD_ENGINE_SOUND				\
{ if(*snd_play_rtn != NULL)				\
 {							\
  SoundStopPlay(recorder, *snd_play_rtn);		\
  *snd_play_rtn = NULL;					\
 }							\
}
		    /* Load/unload engine sounds. */
		    if(engine_sounds)
		    {
			/* Turn engine sounds on as needed. */

			name = "engine_inside";
			snd_play_rtn = &aircraft->engine_inside_sndplay;
			DO_LOAD_ENGINE_SOUND
			aircraft->engine_inside_sndsrc = sndsrc_num;

			name = "engine_outside";
			snd_play_rtn = &aircraft->engine_outside_sndplay;
			DO_LOAD_ENGINE_SOUND
			aircraft->engine_outside_sndsrc = sndsrc_num;
		    }
		    else
		    {
			/* Turn engine sounds off as needed. */

			snd_play_rtn = &aircraft->engine_inside_sndplay;
			DO_UNLOAD_ENGINE_SOUND

			snd_play_rtn = &aircraft->engine_outside_sndplay;
			DO_UNLOAD_ENGINE_SOUND
		    }
#undef DO_UNLOAD_ENGINE_SOUND
#undef DO_LOAD_ENGINE_SOUND

#define DO_GET_SNDSRC_INDEX	{			\
 sndsrc_num = SARSoundSourceMatchFromList(		\
  obj_ptr->sndsrc, obj_ptr->total_sndsrcs,		\
  name							\
 );							\
}
		    /* Get sound source index of warning sounds. */
		    if(event_sounds)
		    {
			name = "stall";
			DO_GET_SNDSRC_INDEX
			aircraft->stall_sndsrc = sndsrc_num;

			name = "overspeed";
			DO_GET_SNDSRC_INDEX
			aircraft->overspeed_sndsrc = sndsrc_num;

		    }
#undef DO_GET_SNDSRC_INDEX
		}
		break;
	      case SAR_OBJ_TYPE_GROUND:
	      case SAR_OBJ_TYPE_RUNWAY:
	      case SAR_OBJ_TYPE_HELIPAD:
	      case SAR_OBJ_TYPE_HUMAN:
	      case SAR_OBJ_TYPE_SMOKE:
	      case SAR_OBJ_TYPE_FIRE:
	      case SAR_OBJ_TYPE_EXPLOSION:
	      case SAR_OBJ_TYPE_CHEMICAL_SPRAY:
	      case SAR_OBJ_TYPE_FUELTANK:
	      case SAR_OBJ_TYPE_PREMODELED:
		break;
	    }
	}


}