1 /* 2000-08-10T18:03:54Z
3 * open X11 display and sound
36 char play[SAMPLE_MAX];
37 int play_x[SAMPLE_MAX];
38 int play_y[SAMPLE_MAX];
39 int play_element[SAMPLE_MAX];
41 #if defined(AUDIO_UNIX_NATIVE)
42 static int sound_pid = -1;
43 int sound_pipe[2] = { -1, -1 }; /* for communication */
44 short *sound_data[SAMPLE_MAX]; /* pointer to sound data */
45 long sound_length[SAMPLE_MAX]; /* length of sound data */
47 static boolean use_native_em_sound = 0;
49 static const char *sound_names[SAMPLE_MAX] =
84 static const int sound_volume[SAMPLE_MAX] =
124 extern void tab_generate();
125 extern void tab_generate_graphics_info_em();
126 extern void ulaw_generate();
130 Bitmap *emc_bitmaps[2];
136 SetBitmaps_EM(emc_bitmaps);
138 objBitmap = emc_bitmaps[0];
139 sprBitmap = emc_bitmaps[1];
142 objPixmap = emc_bitmaps[0]->drawable;
143 sprPixmap = emc_bitmaps[1]->drawable;
145 objmaskBitmap = emc_bitmaps[0]->clip_mask;
146 sprmaskBitmap = emc_bitmaps[1]->clip_mask;
149 screenBitmap = CreateBitmap(MAX_BUF_XSIZE * TILEX, MAX_BUF_YSIZE * TILEY,
154 spriteBitmap = XCreatePixmap(display, window->drawable, TILEX, TILEY, 1);
155 if (spriteBitmap == 0)
156 Error(ERR_EXIT, "failed to create sprite pixmap for EM engine");
159 objmaskBitmap ? GXcopyInverted : sprmaskBitmap ? GXcopy : GXset;
160 gcValues.graphics_exposures = False;
161 spriteGC = XCreateGC(display, spriteBitmap, GCFunction | GCGraphicsExposures,
164 Error(ERR_EXIT, "failed to create sprite GC for EM engine");
167 /* ----------------------------------------------------------------- */
169 #if defined(AUDIO_UNIX_NATIVE)
171 #if defined(PLATFORM_LINUX) || defined(PLATFORM_BSD)
173 if (use_native_em_sound)
175 char name[MAXNAME+2];
178 for (i = 0; i < SAMPLE_MAX; i++)
184 snprintf(name, MAXNAME+2, "%s/%s/%s", arg_basedir, EM_SND_DIR,
189 snprintf(name, MAXNAME+2, "%s/%s", EM_SND_DIR, sound_names[i]);
193 Error(ERR_EXIT, "buffer overflow when reading sounds directory");
195 if (read_sample(name, &sound_data[i], &sound_length[i]))
200 int mult = sound_volume[i] * 65536 / (100 * MIXER_MAX);
201 stop = sound_data[i] + sound_length[i];
202 for (ptr = sound_data[i]; ptr < stop; ptr++)
203 *ptr = (*ptr * mult) / 65536;
207 if (pipe(sound_pipe) == -1)
209 Error(ERR_WARN, "unable to create sound pipe for EM engine -- no sound");
217 Error(ERR_WARN, "unable to fork sound thread for EM engine -- no sound");
222 close(sound_pipe[sound_pid == 0]);
223 sound_pipe[sound_pid == 0] = -1;
225 _exit(sound_thread());
227 signal(SIGPIPE, SIG_IGN); /* dont crash if sound process dies */
230 #endif /* defined(PLATFORM_LINUX) || defined(PLATFORM_BSD) */
232 #endif /* AUDIO_UNIX_NATIVE */
239 /* pre-calculate some data */
243 progname = "emerald mine";
246 Error(ERR_EXIT, "em_open_all(): open_all() failed");
248 /* after "open_all()", because we need the graphic bitmaps to be defined */
249 tab_generate_graphics_info_em();
254 void em_close_all(void)
256 #if defined(AUDIO_UNIX_NATIVE)
261 kill(sound_pid, SIGTERM);
262 waitpid(sound_pid, 0, 0);
265 if (sound_pipe[0] != -1)
266 close(sound_pipe[0]);
267 if (sound_pipe[1] != -1)
268 close(sound_pipe[1]);
270 for (i = 0; i < SAMPLE_MAX; i++)
277 XFreeGC(display, spriteGC);
280 XFreePixmap(display, spriteBitmap);
284 /* ---------------------------------------------------------------------- */
286 extern unsigned int screen_x;
287 extern unsigned int screen_y;
289 void play_element_sound(int x, int y, int sample, int element)
292 unsigned int left = screen_x / TILEX;
293 unsigned int top = screen_y / TILEY;
295 if ((x == -1 && y == -1) || /* play sound in the middle of the screen */
296 ((unsigned int)(y - top) <= SCR_FIELDY &&
297 (unsigned int)(x - left) <= SCR_FIELDX))
301 PlayLevelSound_EM(x, y, element, sample);
306 play_element[sample] = element;
311 void play_sound(int x, int y, int sample)
313 play_element_sound(x, y, sample, -1);
316 void sound_play(void)
318 if (!use_native_em_sound)
323 UpdateEngineValues(screen_x / TILEX, screen_y / TILEY);
328 for (i = 0; i < SAMPLE_MAX; i++)
330 PlayLevelSound_EM(play_x[i], play_y[i], play_element[i], i);
333 #if defined(AUDIO_UNIX_NATIVE)
334 if (use_native_em_sound && sound_pipe[1] != -1)
336 if (write(sound_pipe[1], &play, sizeof(play)) == -1)
338 Error(ERR_WARN, "cannot write into pipe to child process -- no sounds");
340 if (sound_pipe[0] != -1)
342 close(sound_pipe[0]);
346 if (sound_pipe[1] != -1)
348 close(sound_pipe[1]);
356 memset(play, 0, sizeof(play));
359 unsigned int InitEngineRND_EM(long seed)
361 if (seed == NEW_RANDOMIZE)
363 int simple_rnd = SimpleRND(1000);
366 for (i = 0; i < simple_rnd || Random == NEW_RANDOMIZE; i++)
367 Random = Random * 129 + 1;
374 return (unsigned int) seed;