1 /* 2000-08-10T18:03:54Z
3 * open X11 display and sound
30 char play[SAMPLE_MAX];
31 int play_x[SAMPLE_MAX];
32 int play_y[SAMPLE_MAX];
33 int play_element[SAMPLE_MAX];
35 static boolean use_native_em_sound = 0;
37 struct GlobalInfo_EM global_em_info;
38 struct GameInfo_EM game_em;
40 #if defined(AUDIO_UNIX_NATIVE)
41 static int sound_pid = -1;
42 int sound_pipe[2] = { -1, -1 }; /* for communication */
43 short *sound_data[SAMPLE_MAX]; /* pointer to sound data */
44 int sound_length[SAMPLE_MAX]; /* length of sound data */
46 static const char *sound_names[SAMPLE_MAX] =
56 "06.tank.au", /* android moving */
60 "10.eater.au", /* eater eating */
84 static const int sound_volume[SAMPLE_MAX] =
127 extern void tab_generate();
128 extern void tab_generate_graphics_info_em();
129 extern void ulaw_generate();
133 Bitmap *emc_bitmaps[2];
139 SetBitmaps_EM(emc_bitmaps);
141 objBitmap = emc_bitmaps[0];
142 sprBitmap = emc_bitmaps[1];
145 objPixmap = emc_bitmaps[0]->drawable;
146 sprPixmap = emc_bitmaps[1]->drawable;
148 objmaskBitmap = emc_bitmaps[0]->clip_mask;
149 sprmaskBitmap = emc_bitmaps[1]->clip_mask;
153 printf("::: CreateBitmap: %d, %d => %d\n",
154 MAX_BUF_XSIZE, TILEX, MAX_BUF_XSIZE * TILEX);
156 screenBitmap = CreateBitmap(MAX_BUF_XSIZE * TILEX, MAX_BUF_YSIZE * TILEY,
159 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 */
248 void InitGfxBuffers_EM()
254 printf("::: InitGfxBuffers_EM: %d, %d => %d\n",
255 MAX_BUF_XSIZE, TILEX, MAX_BUF_XSIZE * TILEX);
258 ReCreateBitmap(&screenBitmap, MAX_BUF_XSIZE * TILEX, MAX_BUF_YSIZE * TILEY,
261 global_em_info.screenbuffer = screenBitmap;
265 printf("::: CreateBitmap: %d, %d => %d\n",
266 MAX_BUF_XSIZE, TILEX, MAX_BUF_XSIZE * TILEX);
268 screenBitmap = CreateBitmap(MAX_BUF_XSIZE * TILEX, MAX_BUF_YSIZE * TILEY,
271 global_em_info.screenbuffer = screenBitmap;
277 /* pre-calculate some data */
279 #if defined(PLATFORM_LINUX) || defined(PLATFORM_BSD)
283 progname = "emerald mine";
286 Error(ERR_EXIT, "em_open_all(): open_all() failed");
288 /* after "open_all()", because we need the graphic bitmaps to be defined */
289 tab_generate_graphics_info_em();
294 void em_close_all(void)
296 #if defined(AUDIO_UNIX_NATIVE)
301 kill(sound_pid, SIGTERM);
302 waitpid(sound_pid, 0, 0);
305 if (sound_pipe[0] != -1)
306 close(sound_pipe[0]);
307 if (sound_pipe[1] != -1)
308 close(sound_pipe[1]);
310 for (i = 0; i < SAMPLE_MAX; i++)
317 XFreeGC(display, spriteGC);
320 XFreePixmap(display, spriteBitmap);
324 /* ---------------------------------------------------------------------- */
329 void play_element_sound(int x, int y, int sample, int element)
332 int left = screen_x / TILEX;
333 int top = screen_y / TILEY;
335 if ((x == -1 && y == -1) || /* play sound in the middle of the screen */
336 ((int)(y - top) <= SCR_FIELDY &&
337 (int)(x - left) <= SCR_FIELDX))
341 PlayLevelSound_EM(x, y, element, sample);
346 play_element[sample] = element;
351 void play_sound(int x, int y, int sample)
353 play_element_sound(x, y, sample, -1);
356 void sound_play(void)
358 if (!use_native_em_sound)
363 UpdateEngineValues(screen_x / TILEX, screen_y / TILEY);
368 for (i = 0; i < SAMPLE_MAX; i++)
370 PlayLevelSound_EM(play_x[i], play_y[i], play_element[i], i);
373 #if defined(AUDIO_UNIX_NATIVE)
374 if (use_native_em_sound && sound_pipe[1] != -1)
376 if (write(sound_pipe[1], &play, sizeof(play)) == -1)
378 Error(ERR_WARN, "cannot write into pipe to child process -- no sounds");
380 if (sound_pipe[0] != -1)
382 close(sound_pipe[0]);
386 if (sound_pipe[1] != -1)
388 close(sound_pipe[1]);
396 clear_mem(play, sizeof(play));
399 unsigned int InitEngineRandom_EM(int seed)
401 if (seed == NEW_RANDOMIZE)
403 int simple_rnd = GetSimpleRandom(1000);
406 for (i = 0; i < simple_rnd || RandomEM == NEW_RANDOMIZE; i++)
407 RandomEM = RandomEM * 129 + 1;
414 return (unsigned int) seed;