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;
158 printf("::: CreateBitmap: %d, %d => %d\n",
159 MAX_BUF_XSIZE, TILEX, MAX_BUF_XSIZE * TILEX);
161 screenBitmap = CreateBitmap(MAX_BUF_XSIZE * TILEX, MAX_BUF_YSIZE * TILEY,
164 global_em_info.screenbuffer = screenBitmap;
170 spriteBitmap = XCreatePixmap(display, window->drawable, TILEX, TILEY, 1);
171 if (spriteBitmap == 0)
172 Error(ERR_EXIT, "failed to create sprite pixmap for EM engine");
175 objmaskBitmap ? GXcopyInverted : sprmaskBitmap ? GXcopy : GXset;
176 gcValues.graphics_exposures = False;
177 spriteGC = XCreateGC(display, spriteBitmap, GCFunction | GCGraphicsExposures,
180 Error(ERR_EXIT, "failed to create sprite GC for EM engine");
183 /* ----------------------------------------------------------------- */
185 #if defined(AUDIO_UNIX_NATIVE)
187 #if defined(PLATFORM_LINUX) || defined(PLATFORM_BSD)
189 if (use_native_em_sound)
191 char name[MAXNAME+2];
194 for (i = 0; i < SAMPLE_MAX; i++)
200 snprintf(name, MAXNAME+2, "%s/%s/%s", arg_basedir, EM_SND_DIR,
205 snprintf(name, MAXNAME+2, "%s/%s", EM_SND_DIR, sound_names[i]);
209 Error(ERR_EXIT, "buffer overflow when reading sounds directory");
211 if (read_sample(name, &sound_data[i], &sound_length[i]))
216 int mult = sound_volume[i] * 65536 / (100 * MIXER_MAX);
217 stop = sound_data[i] + sound_length[i];
218 for (ptr = sound_data[i]; ptr < stop; ptr++)
219 *ptr = (*ptr * mult) / 65536;
223 if (pipe(sound_pipe) == -1)
225 Error(ERR_WARN, "unable to create sound pipe for EM engine -- no sound");
233 Error(ERR_WARN, "unable to fork sound thread for EM engine -- no sound");
238 close(sound_pipe[sound_pid == 0]);
239 sound_pipe[sound_pid == 0] = -1;
241 _exit(sound_thread());
243 signal(SIGPIPE, SIG_IGN); /* dont crash if sound process dies */
246 #endif /* defined(PLATFORM_LINUX) || defined(PLATFORM_BSD) */
248 #endif /* AUDIO_UNIX_NATIVE */
253 void InitGfxBuffers_EM()
259 printf("::: InitGfxBuffers_EM: %d, %d => %d\n",
260 MAX_BUF_XSIZE, TILEX, MAX_BUF_XSIZE * TILEX);
263 ReCreateBitmap(&screenBitmap, MAX_BUF_XSIZE * TILEX, MAX_BUF_YSIZE * TILEY,
266 global_em_info.screenbuffer = screenBitmap;
270 printf("::: CreateBitmap: %d, %d => %d\n",
271 MAX_BUF_XSIZE, TILEX, MAX_BUF_XSIZE * TILEX);
273 screenBitmap = CreateBitmap(MAX_BUF_XSIZE * TILEX, MAX_BUF_YSIZE * TILEY,
276 global_em_info.screenbuffer = screenBitmap;
282 /* pre-calculate some data */
284 #if defined(PLATFORM_LINUX) || defined(PLATFORM_BSD)
288 progname = "emerald mine";
291 Error(ERR_EXIT, "em_open_all(): open_all() failed");
293 /* after "open_all()", because we need the graphic bitmaps to be defined */
294 tab_generate_graphics_info_em();
299 void em_close_all(void)
301 #if defined(AUDIO_UNIX_NATIVE)
306 kill(sound_pid, SIGTERM);
307 waitpid(sound_pid, 0, 0);
310 if (sound_pipe[0] != -1)
311 close(sound_pipe[0]);
312 if (sound_pipe[1] != -1)
313 close(sound_pipe[1]);
315 for (i = 0; i < SAMPLE_MAX; i++)
322 XFreeGC(display, spriteGC);
325 XFreePixmap(display, spriteBitmap);
329 /* ---------------------------------------------------------------------- */
334 void play_element_sound(int x, int y, int sample, int element)
337 int left = screen_x / TILEX;
338 int top = screen_y / TILEY;
340 if ((x == -1 && y == -1) || /* play sound in the middle of the screen */
341 ((int)(y - top) <= SCR_FIELDY &&
342 (int)(x - left) <= SCR_FIELDX))
346 PlayLevelSound_EM(x, y, element, sample);
351 play_element[sample] = element;
356 void play_sound(int x, int y, int sample)
358 play_element_sound(x, y, sample, -1);
361 void sound_play(void)
363 if (!use_native_em_sound)
368 UpdateEngineValues(screen_x / TILEX, screen_y / TILEY);
373 for (i = 0; i < SAMPLE_MAX; i++)
375 PlayLevelSound_EM(play_x[i], play_y[i], play_element[i], i);
378 #if defined(AUDIO_UNIX_NATIVE)
379 if (use_native_em_sound && sound_pipe[1] != -1)
381 if (write(sound_pipe[1], &play, sizeof(play)) == -1)
383 Error(ERR_WARN, "cannot write into pipe to child process -- no sounds");
385 if (sound_pipe[0] != -1)
387 close(sound_pipe[0]);
391 if (sound_pipe[1] != -1)
393 close(sound_pipe[1]);
401 clear_mem(play, sizeof(play));
404 unsigned int InitEngineRandom_EM(long seed)
406 if (seed == NEW_RANDOMIZE)
408 int simple_rnd = GetSimpleRandom(1000);
411 for (i = 0; i < simple_rnd || RandomEM == NEW_RANDOMIZE; i++)
412 RandomEM = RandomEM * 129 + 1;
419 return (unsigned int) seed;