1 /* 2000-08-10T18:03:54Z
3 * open X11 display and sound
11 #if !defined(TARGET_SDL)
35 char play[SAMPLE_MAX];
36 int play_x[SAMPLE_MAX];
37 int play_y[SAMPLE_MAX];
38 int play_element[SAMPLE_MAX];
40 static boolean use_native_em_sound = 0;
42 struct GlobalInfo_EM global_em_info;
43 struct GameInfo_EM game_em;
45 #if defined(AUDIO_UNIX_NATIVE)
46 static int sound_pid = -1;
47 int sound_pipe[2] = { -1, -1 }; /* for communication */
48 short *sound_data[SAMPLE_MAX]; /* pointer to sound data */
49 long sound_length[SAMPLE_MAX]; /* length of sound data */
51 static const char *sound_names[SAMPLE_MAX] =
61 "06.tank.au", /* android moving */
65 "10.eater.au", /* eater eating */
89 static const int sound_volume[SAMPLE_MAX] =
132 extern void tab_generate();
133 extern void tab_generate_graphics_info_em();
134 extern void ulaw_generate();
138 Bitmap *emc_bitmaps[2];
144 SetBitmaps_EM(emc_bitmaps);
146 objBitmap = emc_bitmaps[0];
147 sprBitmap = emc_bitmaps[1];
150 objPixmap = emc_bitmaps[0]->drawable;
151 sprPixmap = emc_bitmaps[1]->drawable;
153 objmaskBitmap = emc_bitmaps[0]->clip_mask;
154 sprmaskBitmap = emc_bitmaps[1]->clip_mask;
157 screenBitmap = CreateBitmap(MAX_BUF_XSIZE * TILEX, MAX_BUF_YSIZE * TILEY,
160 global_em_info.screenbuffer = screenBitmap;
165 spriteBitmap = XCreatePixmap(display, window->drawable, TILEX, TILEY, 1);
166 if (spriteBitmap == 0)
167 Error(ERR_EXIT, "failed to create sprite pixmap for EM engine");
170 objmaskBitmap ? GXcopyInverted : sprmaskBitmap ? GXcopy : GXset;
171 gcValues.graphics_exposures = False;
172 spriteGC = XCreateGC(display, spriteBitmap, GCFunction | GCGraphicsExposures,
175 Error(ERR_EXIT, "failed to create sprite GC for EM engine");
178 /* ----------------------------------------------------------------- */
180 #if defined(AUDIO_UNIX_NATIVE)
182 #if defined(PLATFORM_LINUX) || defined(PLATFORM_BSD)
184 if (use_native_em_sound)
186 char name[MAXNAME+2];
189 for (i = 0; i < SAMPLE_MAX; i++)
195 snprintf(name, MAXNAME+2, "%s/%s/%s", arg_basedir, EM_SND_DIR,
200 snprintf(name, MAXNAME+2, "%s/%s", EM_SND_DIR, sound_names[i]);
204 Error(ERR_EXIT, "buffer overflow when reading sounds directory");
206 if (read_sample(name, &sound_data[i], &sound_length[i]))
211 int mult = sound_volume[i] * 65536 / (100 * MIXER_MAX);
212 stop = sound_data[i] + sound_length[i];
213 for (ptr = sound_data[i]; ptr < stop; ptr++)
214 *ptr = (*ptr * mult) / 65536;
218 if (pipe(sound_pipe) == -1)
220 Error(ERR_WARN, "unable to create sound pipe for EM engine -- no sound");
228 Error(ERR_WARN, "unable to fork sound thread for EM engine -- no sound");
233 close(sound_pipe[sound_pid == 0]);
234 sound_pipe[sound_pid == 0] = -1;
236 _exit(sound_thread());
238 signal(SIGPIPE, SIG_IGN); /* dont crash if sound process dies */
241 #endif /* defined(PLATFORM_LINUX) || defined(PLATFORM_BSD) */
243 #endif /* AUDIO_UNIX_NATIVE */
250 /* pre-calculate some data */
252 #if defined(PLATFORM_LINUX) || defined(PLATFORM_BSD)
256 progname = "emerald mine";
259 Error(ERR_EXIT, "em_open_all(): open_all() failed");
261 /* after "open_all()", because we need the graphic bitmaps to be defined */
262 tab_generate_graphics_info_em();
267 void em_close_all(void)
269 #if defined(AUDIO_UNIX_NATIVE)
274 kill(sound_pid, SIGTERM);
275 waitpid(sound_pid, 0, 0);
278 if (sound_pipe[0] != -1)
279 close(sound_pipe[0]);
280 if (sound_pipe[1] != -1)
281 close(sound_pipe[1]);
283 for (i = 0; i < SAMPLE_MAX; i++)
290 XFreeGC(display, spriteGC);
293 XFreePixmap(display, spriteBitmap);
297 /* ---------------------------------------------------------------------- */
302 void play_element_sound(int x, int y, int sample, int element)
305 int left = screen_x / TILEX;
306 int top = screen_y / TILEY;
308 if ((x == -1 && y == -1) || /* play sound in the middle of the screen */
309 ((int)(y - top) <= SCR_FIELDY &&
310 (int)(x - left) <= SCR_FIELDX))
314 PlayLevelSound_EM(x, y, element, sample);
319 play_element[sample] = element;
324 void play_sound(int x, int y, int sample)
326 play_element_sound(x, y, sample, -1);
329 void sound_play(void)
331 if (!use_native_em_sound)
336 UpdateEngineValues(screen_x / TILEX, screen_y / TILEY);
341 for (i = 0; i < SAMPLE_MAX; i++)
343 PlayLevelSound_EM(play_x[i], play_y[i], play_element[i], i);
346 #if defined(AUDIO_UNIX_NATIVE)
347 if (use_native_em_sound && sound_pipe[1] != -1)
349 if (write(sound_pipe[1], &play, sizeof(play)) == -1)
351 Error(ERR_WARN, "cannot write into pipe to child process -- no sounds");
353 if (sound_pipe[0] != -1)
355 close(sound_pipe[0]);
359 if (sound_pipe[1] != -1)
361 close(sound_pipe[1]);
369 clear_mem(play, sizeof(play));
372 unsigned int InitEngineRandom_EM(long seed)
374 if (seed == NEW_RANDOMIZE)
376 int simple_rnd = GetSimpleRandom(1000);
379 for (i = 0; i < simple_rnd || RandomEM == NEW_RANDOMIZE; i++)
380 RandomEM = RandomEM * 129 + 1;
387 return (unsigned int) seed;