1 /* 2000-08-10T18:03:54Z
3 * open X11 display and sound
8 #if !defined(TARGET_SDL)
39 char play[SAMPLE_MAX];
40 int play_x[SAMPLE_MAX];
41 int play_y[SAMPLE_MAX];
42 int play_element[SAMPLE_MAX];
44 static boolean use_native_em_sound = 0;
46 struct GlobalInfo_EM global_em_info;
48 #if defined(AUDIO_UNIX_NATIVE)
49 static int sound_pid = -1;
50 int sound_pipe[2] = { -1, -1 }; /* for communication */
51 short *sound_data[SAMPLE_MAX]; /* pointer to sound data */
52 long sound_length[SAMPLE_MAX]; /* length of sound data */
54 static const char *sound_names[SAMPLE_MAX] =
64 "06.tank.au", /* android moving */
68 "10.eater.au", /* eater eating */
92 static const int sound_volume[SAMPLE_MAX] =
135 extern void tab_generate();
136 extern void tab_generate_graphics_info_em();
137 extern void ulaw_generate();
141 Bitmap *emc_bitmaps[2];
147 SetBitmaps_EM(emc_bitmaps);
149 objBitmap = emc_bitmaps[0];
150 sprBitmap = emc_bitmaps[1];
153 objPixmap = emc_bitmaps[0]->drawable;
154 sprPixmap = emc_bitmaps[1]->drawable;
156 objmaskBitmap = emc_bitmaps[0]->clip_mask;
157 sprmaskBitmap = emc_bitmaps[1]->clip_mask;
160 screenBitmap = CreateBitmap(MAX_BUF_XSIZE * TILEX, MAX_BUF_YSIZE * TILEY,
163 global_em_info.screenbuffer = screenBitmap;
168 spriteBitmap = XCreatePixmap(display, window->drawable, TILEX, TILEY, 1);
169 if (spriteBitmap == 0)
170 Error(ERR_EXIT, "failed to create sprite pixmap for EM engine");
173 objmaskBitmap ? GXcopyInverted : sprmaskBitmap ? GXcopy : GXset;
174 gcValues.graphics_exposures = False;
175 spriteGC = XCreateGC(display, spriteBitmap, GCFunction | GCGraphicsExposures,
178 Error(ERR_EXIT, "failed to create sprite GC for EM engine");
181 /* ----------------------------------------------------------------- */
183 #if defined(AUDIO_UNIX_NATIVE)
185 #if defined(PLATFORM_LINUX) || defined(PLATFORM_BSD)
187 if (use_native_em_sound)
189 char name[MAXNAME+2];
192 for (i = 0; i < SAMPLE_MAX; i++)
198 snprintf(name, MAXNAME+2, "%s/%s/%s", arg_basedir, EM_SND_DIR,
203 snprintf(name, MAXNAME+2, "%s/%s", EM_SND_DIR, sound_names[i]);
207 Error(ERR_EXIT, "buffer overflow when reading sounds directory");
209 if (read_sample(name, &sound_data[i], &sound_length[i]))
214 int mult = sound_volume[i] * 65536 / (100 * MIXER_MAX);
215 stop = sound_data[i] + sound_length[i];
216 for (ptr = sound_data[i]; ptr < stop; ptr++)
217 *ptr = (*ptr * mult) / 65536;
221 if (pipe(sound_pipe) == -1)
223 Error(ERR_WARN, "unable to create sound pipe for EM engine -- no sound");
231 Error(ERR_WARN, "unable to fork sound thread for EM engine -- no sound");
236 close(sound_pipe[sound_pid == 0]);
237 sound_pipe[sound_pid == 0] = -1;
239 _exit(sound_thread());
241 signal(SIGPIPE, SIG_IGN); /* dont crash if sound process dies */
244 #endif /* defined(PLATFORM_LINUX) || defined(PLATFORM_BSD) */
246 #endif /* AUDIO_UNIX_NATIVE */
253 /* pre-calculate some data */
255 #if defined(PLATFORM_LINUX) || defined(PLATFORM_BSD)
259 progname = "emerald mine";
262 Error(ERR_EXIT, "em_open_all(): open_all() failed");
264 /* after "open_all()", because we need the graphic bitmaps to be defined */
265 tab_generate_graphics_info_em();
270 void em_close_all(void)
272 #if defined(AUDIO_UNIX_NATIVE)
277 kill(sound_pid, SIGTERM);
278 waitpid(sound_pid, 0, 0);
281 if (sound_pipe[0] != -1)
282 close(sound_pipe[0]);
283 if (sound_pipe[1] != -1)
284 close(sound_pipe[1]);
286 for (i = 0; i < SAMPLE_MAX; i++)
293 XFreeGC(display, spriteGC);
296 XFreePixmap(display, spriteBitmap);
300 /* ---------------------------------------------------------------------- */
305 void play_element_sound(int x, int y, int sample, int element)
308 int left = screen_x / TILEX;
309 int top = screen_y / TILEY;
311 if ((x == -1 && y == -1) || /* play sound in the middle of the screen */
312 ((int)(y - top) <= SCR_FIELDY &&
313 (int)(x - left) <= SCR_FIELDX))
317 PlayLevelSound_EM(x, y, element, sample);
322 play_element[sample] = element;
327 void play_sound(int x, int y, int sample)
329 play_element_sound(x, y, sample, -1);
332 void sound_play(void)
334 if (!use_native_em_sound)
339 UpdateEngineValues(screen_x / TILEX, screen_y / TILEY);
344 for (i = 0; i < SAMPLE_MAX; i++)
346 PlayLevelSound_EM(play_x[i], play_y[i], play_element[i], i);
349 #if defined(AUDIO_UNIX_NATIVE)
350 if (use_native_em_sound && sound_pipe[1] != -1)
352 if (write(sound_pipe[1], &play, sizeof(play)) == -1)
354 Error(ERR_WARN, "cannot write into pipe to child process -- no sounds");
356 if (sound_pipe[0] != -1)
358 close(sound_pipe[0]);
362 if (sound_pipe[1] != -1)
364 close(sound_pipe[1]);
372 memset(play, 0, sizeof(play));
375 unsigned int InitEngineRND_EM(long seed)
377 if (seed == NEW_RANDOMIZE)
379 int simple_rnd = SimpleRND(1000);
382 for (i = 0; i < simple_rnd || RandomEM == NEW_RANDOMIZE; i++)
383 RandomEM = RandomEM * 129 + 1;
390 return (unsigned int) seed;