1 /* 2000-08-10T18:03:54Z
3 * open X11 display and sound
35 char play[SAMPLE_MAX];
36 int play_x[SAMPLE_MAX];
37 int play_y[SAMPLE_MAX];
38 int play_element[SAMPLE_MAX];
40 #if defined(AUDIO_UNIX_NATIVE)
41 static int sound_pid = -1;
42 int sound_pipe[2] = { -1, -1 }; /* for communication */
43 short *sound_data[SAMPLE_MAX]; /* pointer to sound data */
44 long sound_length[SAMPLE_MAX]; /* length of sound data */
46 static boolean use_native_em_sound = 0;
48 static const char *sound_names[SAMPLE_MAX] =
83 static const int sound_volume[SAMPLE_MAX] =
123 extern void tab_generate();
124 extern void ulaw_generate();
128 Bitmap *emc_bitmaps[2];
134 SetBitmaps_EM(emc_bitmaps);
136 objBitmap = emc_bitmaps[0];
137 sprBitmap = emc_bitmaps[1];
140 objPixmap = emc_bitmaps[0]->drawable;
141 sprPixmap = emc_bitmaps[1]->drawable;
143 objmaskBitmap = emc_bitmaps[0]->clip_mask;
144 sprmaskBitmap = emc_bitmaps[1]->clip_mask;
147 screenBitmap = CreateBitmap(MAX_BUF_XSIZE * TILEX, MAX_BUF_YSIZE * TILEY,
152 spriteBitmap = XCreatePixmap(display, window->drawable, TILEX, TILEY, 1);
153 if (spriteBitmap == 0)
154 Error(ERR_EXIT, "failed to create sprite pixmap for EM engine");
157 objmaskBitmap ? GXcopyInverted : sprmaskBitmap ? GXcopy : GXset;
158 gcValues.graphics_exposures = False;
159 spriteGC = XCreateGC(display, spriteBitmap, GCFunction | GCGraphicsExposures,
162 Error(ERR_EXIT, "failed to create sprite GC for EM engine");
165 /* ----------------------------------------------------------------- */
167 #if defined(AUDIO_UNIX_NATIVE)
169 #if defined(PLATFORM_LINUX) || defined(PLATFORM_BSD)
171 if (use_native_em_sound)
173 char name[MAXNAME+2];
176 for (i = 0; i < SAMPLE_MAX; i++)
182 snprintf(name, MAXNAME+2, "%s/%s/%s", arg_basedir, EM_SND_DIR,
187 snprintf(name, MAXNAME+2, "%s/%s", EM_SND_DIR, sound_names[i]);
191 Error(ERR_EXIT, "buffer overflow when reading sounds directory");
193 if (read_sample(name, &sound_data[i], &sound_length[i]))
198 int mult = sound_volume[i] * 65536 / (100 * MIXER_MAX);
199 stop = sound_data[i] + sound_length[i];
200 for (ptr = sound_data[i]; ptr < stop; ptr++)
201 *ptr = (*ptr * mult) / 65536;
205 if (pipe(sound_pipe) == -1)
207 Error(ERR_WARN, "unable to create sound pipe for EM engine -- no sound");
215 Error(ERR_WARN, "unable to fork sound thread for EM engine -- no sound");
220 close(sound_pipe[sound_pid == 0]);
221 sound_pipe[sound_pid == 0] = -1;
223 _exit(sound_thread());
225 signal(SIGPIPE, SIG_IGN); /* dont crash if sound process dies */
228 #endif /* defined(PLATFORM_LINUX) || defined(PLATFORM_BSD) */
230 #endif /* AUDIO_UNIX_NATIVE */
237 /* pre-calculate some data */
241 progname = "emerald mine";
244 Error(ERR_EXIT, "em_open_all(): open_all() failed");
249 void em_close_all(void)
251 #if defined(AUDIO_UNIX_NATIVE)
256 kill(sound_pid, SIGTERM);
257 waitpid(sound_pid, 0, 0);
260 if (sound_pipe[0] != -1)
261 close(sound_pipe[0]);
262 if (sound_pipe[1] != -1)
263 close(sound_pipe[1]);
265 for (i = 0; i < SAMPLE_MAX; i++)
272 XFreeGC(display, spriteGC);
275 XFreePixmap(display, spriteBitmap);
279 /* ---------------------------------------------------------------------- */
281 extern unsigned int screen_x;
282 extern unsigned int screen_y;
284 void play_element_sound(int x, int y, int sample, int element)
287 unsigned int left = screen_x / TILEX;
288 unsigned int top = screen_y / TILEY;
290 if ((x == -1 && y == -1) || /* play sound in the middle of the screen */
291 ((unsigned int)(y - top) <= SCR_FIELDY &&
292 (unsigned int)(x - left) <= SCR_FIELDX))
296 PlayLevelSound_EM(x, y, element, sample);
301 play_element[sample] = element;
306 void play_sound(int x, int y, int sample)
308 play_element_sound(x, y, sample, -1);
311 void sound_play(void)
313 if (!use_native_em_sound)
318 UpdateEngineValues(screen_x / TILEX, screen_y / TILEY);
323 for (i = 0; i < SAMPLE_MAX; i++)
325 PlayLevelSound_EM(play_x[i], play_y[i], play_element[i], i);
328 #if defined(AUDIO_UNIX_NATIVE)
329 if (use_native_em_sound && sound_pipe[1] != -1)
331 if (write(sound_pipe[1], &play, sizeof(play)) == -1)
333 Error(ERR_WARN, "cannot write into pipe to child process -- no sounds");
335 if (sound_pipe[0] != -1)
337 close(sound_pipe[0]);
341 if (sound_pipe[1] != -1)
343 close(sound_pipe[1]);
351 memset(play, 0, sizeof(play));