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] =
89 static const int sound_volume[SAMPLE_MAX] =
129 extern void tab_generate();
130 extern void tab_generate_graphics_info_em();
131 extern void ulaw_generate();
135 Bitmap *emc_bitmaps[2];
141 SetBitmaps_EM(emc_bitmaps);
143 objBitmap = emc_bitmaps[0];
144 sprBitmap = emc_bitmaps[1];
147 objPixmap = emc_bitmaps[0]->drawable;
148 sprPixmap = emc_bitmaps[1]->drawable;
150 objmaskBitmap = emc_bitmaps[0]->clip_mask;
151 sprmaskBitmap = emc_bitmaps[1]->clip_mask;
154 screenBitmap = CreateBitmap(MAX_BUF_XSIZE * TILEX, MAX_BUF_YSIZE * TILEY,
157 global_em_info.screenbuffer = screenBitmap;
162 spriteBitmap = XCreatePixmap(display, window->drawable, TILEX, TILEY, 1);
163 if (spriteBitmap == 0)
164 Error(ERR_EXIT, "failed to create sprite pixmap for EM engine");
167 objmaskBitmap ? GXcopyInverted : sprmaskBitmap ? GXcopy : GXset;
168 gcValues.graphics_exposures = False;
169 spriteGC = XCreateGC(display, spriteBitmap, GCFunction | GCGraphicsExposures,
172 Error(ERR_EXIT, "failed to create sprite GC for EM engine");
175 /* ----------------------------------------------------------------- */
177 #if defined(AUDIO_UNIX_NATIVE)
179 #if defined(PLATFORM_LINUX) || defined(PLATFORM_BSD)
181 if (use_native_em_sound)
183 char name[MAXNAME+2];
186 for (i = 0; i < SAMPLE_MAX; i++)
192 snprintf(name, MAXNAME+2, "%s/%s/%s", arg_basedir, EM_SND_DIR,
197 snprintf(name, MAXNAME+2, "%s/%s", EM_SND_DIR, sound_names[i]);
201 Error(ERR_EXIT, "buffer overflow when reading sounds directory");
203 if (read_sample(name, &sound_data[i], &sound_length[i]))
208 int mult = sound_volume[i] * 65536 / (100 * MIXER_MAX);
209 stop = sound_data[i] + sound_length[i];
210 for (ptr = sound_data[i]; ptr < stop; ptr++)
211 *ptr = (*ptr * mult) / 65536;
215 if (pipe(sound_pipe) == -1)
217 Error(ERR_WARN, "unable to create sound pipe for EM engine -- no sound");
225 Error(ERR_WARN, "unable to fork sound thread for EM engine -- no sound");
230 close(sound_pipe[sound_pid == 0]);
231 sound_pipe[sound_pid == 0] = -1;
233 _exit(sound_thread());
235 signal(SIGPIPE, SIG_IGN); /* dont crash if sound process dies */
238 #endif /* defined(PLATFORM_LINUX) || defined(PLATFORM_BSD) */
240 #endif /* AUDIO_UNIX_NATIVE */
247 /* pre-calculate some data */
249 #if defined(PLATFORM_LINUX) || defined(PLATFORM_BSD)
253 progname = "emerald mine";
256 Error(ERR_EXIT, "em_open_all(): open_all() failed");
258 /* after "open_all()", because we need the graphic bitmaps to be defined */
259 tab_generate_graphics_info_em();
264 void em_close_all(void)
266 #if defined(AUDIO_UNIX_NATIVE)
271 kill(sound_pid, SIGTERM);
272 waitpid(sound_pid, 0, 0);
275 if (sound_pipe[0] != -1)
276 close(sound_pipe[0]);
277 if (sound_pipe[1] != -1)
278 close(sound_pipe[1]);
280 for (i = 0; i < SAMPLE_MAX; i++)
287 XFreeGC(display, spriteGC);
290 XFreePixmap(display, spriteBitmap);
294 /* ---------------------------------------------------------------------- */
296 extern unsigned int screen_x;
297 extern unsigned int screen_y;
299 void play_element_sound(int x, int y, int sample, int element)
302 unsigned int left = screen_x / TILEX;
303 unsigned int top = screen_y / TILEY;
305 if ((x == -1 && y == -1) || /* play sound in the middle of the screen */
306 ((unsigned int)(y - top) <= SCR_FIELDY &&
307 (unsigned int)(x - left) <= SCR_FIELDX))
311 PlayLevelSound_EM(x, y, element, sample);
316 play_element[sample] = element;
321 void play_sound(int x, int y, int sample)
323 play_element_sound(x, y, sample, -1);
326 void sound_play(void)
328 if (!use_native_em_sound)
333 UpdateEngineValues(screen_x / TILEX, screen_y / TILEY);
338 for (i = 0; i < SAMPLE_MAX; i++)
340 PlayLevelSound_EM(play_x[i], play_y[i], play_element[i], i);
343 #if defined(AUDIO_UNIX_NATIVE)
344 if (use_native_em_sound && sound_pipe[1] != -1)
346 if (write(sound_pipe[1], &play, sizeof(play)) == -1)
348 Error(ERR_WARN, "cannot write into pipe to child process -- no sounds");
350 if (sound_pipe[0] != -1)
352 close(sound_pipe[0]);
356 if (sound_pipe[1] != -1)
358 close(sound_pipe[1]);
366 memset(play, 0, sizeof(play));
369 unsigned int InitEngineRND_EM(long seed)
371 if (seed == NEW_RANDOMIZE)
373 int simple_rnd = SimpleRND(1000);
376 for (i = 0; i < simple_rnd || Random == NEW_RANDOMIZE; i++)
377 Random = Random * 129 + 1;
384 return (unsigned int) seed;