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 ulaw_generate();
129 Bitmap *emc_bitmaps[2];
135 SetBitmaps_EM(emc_bitmaps);
137 objBitmap = emc_bitmaps[0];
138 sprBitmap = emc_bitmaps[1];
141 objPixmap = emc_bitmaps[0]->drawable;
142 sprPixmap = emc_bitmaps[1]->drawable;
144 objmaskBitmap = emc_bitmaps[0]->clip_mask;
145 sprmaskBitmap = emc_bitmaps[1]->clip_mask;
148 screenBitmap = CreateBitmap(MAX_BUF_XSIZE * TILEX, MAX_BUF_YSIZE * TILEY,
153 spriteBitmap = XCreatePixmap(display, window->drawable, TILEX, TILEY, 1);
154 if (spriteBitmap == 0)
155 Error(ERR_EXIT, "failed to create sprite pixmap for EM engine");
158 objmaskBitmap ? GXcopyInverted : sprmaskBitmap ? GXcopy : GXset;
159 gcValues.graphics_exposures = False;
160 spriteGC = XCreateGC(display, spriteBitmap, GCFunction | GCGraphicsExposures,
163 Error(ERR_EXIT, "failed to create sprite GC for EM engine");
166 /* ----------------------------------------------------------------- */
168 #if defined(AUDIO_UNIX_NATIVE)
170 #if defined(PLATFORM_LINUX) || defined(PLATFORM_BSD)
172 if (use_native_em_sound)
174 char name[MAXNAME+2];
177 for (i = 0; i < SAMPLE_MAX; i++)
183 snprintf(name, MAXNAME+2, "%s/%s/%s", arg_basedir, EM_SND_DIR,
188 snprintf(name, MAXNAME+2, "%s/%s", EM_SND_DIR, sound_names[i]);
192 Error(ERR_EXIT, "buffer overflow when reading sounds directory");
194 if (read_sample(name, &sound_data[i], &sound_length[i]))
199 int mult = sound_volume[i] * 65536 / (100 * MIXER_MAX);
200 stop = sound_data[i] + sound_length[i];
201 for (ptr = sound_data[i]; ptr < stop; ptr++)
202 *ptr = (*ptr * mult) / 65536;
206 if (pipe(sound_pipe) == -1)
208 Error(ERR_WARN, "unable to create sound pipe for EM engine -- no sound");
216 Error(ERR_WARN, "unable to fork sound thread for EM engine -- no sound");
221 close(sound_pipe[sound_pid == 0]);
222 sound_pipe[sound_pid == 0] = -1;
224 _exit(sound_thread());
226 signal(SIGPIPE, SIG_IGN); /* dont crash if sound process dies */
229 #endif /* defined(PLATFORM_LINUX) || defined(PLATFORM_BSD) */
231 #endif /* AUDIO_UNIX_NATIVE */
238 /* pre-calculate some data */
242 progname = "emerald mine";
245 Error(ERR_EXIT, "em_open_all(): open_all() failed");
250 void em_close_all(void)
252 #if defined(AUDIO_UNIX_NATIVE)
257 kill(sound_pid, SIGTERM);
258 waitpid(sound_pid, 0, 0);
261 if (sound_pipe[0] != -1)
262 close(sound_pipe[0]);
263 if (sound_pipe[1] != -1)
264 close(sound_pipe[1]);
266 for (i = 0; i < SAMPLE_MAX; i++)
273 XFreeGC(display, spriteGC);
276 XFreePixmap(display, spriteBitmap);
280 /* ---------------------------------------------------------------------- */
282 extern unsigned int screen_x;
283 extern unsigned int screen_y;
285 void play_element_sound(int x, int y, int sample, int element)
288 unsigned int left = screen_x / TILEX;
289 unsigned int top = screen_y / TILEY;
291 if ((x == -1 && y == -1) || /* play sound in the middle of the screen */
292 ((unsigned int)(y - top) <= SCR_FIELDY &&
293 (unsigned int)(x - left) <= SCR_FIELDX))
297 PlayLevelSound_EM(x, y, element, sample);
302 play_element[sample] = element;
307 void play_sound(int x, int y, int sample)
309 play_element_sound(x, y, sample, -1);
312 void sound_play(void)
314 if (!use_native_em_sound)
319 UpdateEngineValues(screen_x / TILEX, screen_y / TILEY);
324 for (i = 0; i < SAMPLE_MAX; i++)
326 PlayLevelSound_EM(play_x[i], play_y[i], play_element[i], i);
329 #if defined(AUDIO_UNIX_NATIVE)
330 if (use_native_em_sound && sound_pipe[1] != -1)
332 if (write(sound_pipe[1], &play, sizeof(play)) == -1)
334 Error(ERR_WARN, "cannot write into pipe to child process -- no sounds");
336 if (sound_pipe[0] != -1)
338 close(sound_pipe[0]);
342 if (sound_pipe[1] != -1)
344 close(sound_pipe[1]);
352 memset(play, 0, sizeof(play));
355 unsigned int InitEngineRND_EM(long seed)
357 if (seed == NEW_RANDOMIZE)
359 int simple_rnd = SimpleRND(1000);
362 for (i = 0; i < simple_rnd || Random == NEW_RANDOMIZE; i++)
363 Random = Random * 129 + 1;
370 return (unsigned int) seed;