rnd-20040917-1-src
[rocksndiamonds.git] / src / game_em / init.c
1 /* 2000-08-10T18:03:54Z
2  *
3  * open X11 display and sound
4  */
5
6 #include <X11/Xlib.h>
7 #include <X11/Xutil.h>
8 #include <X11/Xatom.h>
9 #include <X11/Xos.h>
10 #include <X11/Intrinsic.h>
11 #include <X11/keysymdef.h>
12
13 #include <X11/keysym.h>
14
15 #include <sys/types.h>
16 #include <sys/wait.h>
17 #include <unistd.h>
18 #include <stdlib.h>
19 #include <stdio.h>
20 #include <signal.h>
21 #include <string.h>
22 #include <errno.h>
23
24 #include "game_em.h"
25
26 #include "global.h"
27 #include "display.h"
28 #include "sample.h"
29
30
31 #if defined(TARGET_X11)
32
33 #if 1
34 Bitmap *objBitmap;
35 Bitmap *botBitmap;
36 Bitmap *sprBitmap;
37 Bitmap *ttlBitmap;
38 #endif
39
40 #if 1
41 Bitmap *screenBitmap;
42 Bitmap *scoreBitmap;
43 #endif
44
45 Pixmap screenPixmap;
46 Pixmap scorePixmap;
47 Pixmap spriteBitmap;
48
49 Pixmap objPixmap;
50 Pixmap objmaskBitmap;
51 Pixmap botPixmap;
52 Pixmap botmaskBitmap;
53 Pixmap sprPixmap;
54 Pixmap sprmaskBitmap;
55 Pixmap ttlPixmap;
56 Pixmap ttlmaskBitmap;
57
58 GC screenGC;
59 GC scoreGC;
60 GC spriteGC;
61
62 char play[SAMPLE_MAX];
63
64 static int sound_pid = -1;
65 int sound_pipe[2] = { -1, -1 }; /* for communication */
66 short *sound_data[SAMPLE_MAX]; /* pointer to sound data */
67 long sound_length[SAMPLE_MAX]; /* length of sound data */
68
69 static Screen *defaultScreen;
70 static Visual *defaultVisual;
71 static Colormap defaultColourmap;
72 static Window defaultRootWindow;
73 static unsigned int screenDepth;
74 static unsigned int screenWidth;
75 static unsigned int screenHeight;
76 static unsigned long screenBlackPixel;
77 static unsigned long screenWhitePixel;
78
79 static XGCValues gcValues;
80
81 #if 1
82 static Bitmap *pcxBitmapsX2[4];
83 #endif
84
85 static const char *sound_names[SAMPLE_MAX] =
86 {
87   "00.blank.au","01.roll.au","02.stone.au","03.nut.au","04.crack.au",
88   "05.bug.au","06.tank.au","07.android.au","08.spring.au","09.slurp.au",
89   "10.eater.au","11.alien.au","12.collect.au","13.diamond.au","14.squash.au",
90   "15.drip.au","16.push.au","17.dirt.au","18.acid.au","19.ball.au",
91   "20.grow.au","21.wonder.au","22.door.au","23.exit.au","24.dynamite.au",
92   "25.tick.au","26.press.au","27.wheel.au","28.boom.au","29.time.au",
93   "30.die.au"
94 };
95 static const int sound_volume[SAMPLE_MAX] =
96 {
97   20,100,100,100,100,20,20,100,100,100,
98   50,100,100,100,100,100,100,100,100,100,
99   100,20,100,100,100,100,100,20,100,100,
100   100
101 };
102
103 int open_all(void)
104 {
105   char name[MAXNAME+2];
106   int i;
107
108   defaultScreen = DefaultScreenOfDisplay(display);
109   defaultVisual = DefaultVisualOfScreen(defaultScreen);
110   defaultColourmap = DefaultColormapOfScreen(defaultScreen);
111   defaultRootWindow = RootWindowOfScreen(defaultScreen);
112   screenDepth = DefaultDepthOfScreen(defaultScreen);
113   screenWidth = WidthOfScreen(defaultScreen);
114   screenHeight = HeightOfScreen(defaultScreen);
115   screenBlackPixel = BlackPixelOfScreen(defaultScreen);
116   screenWhitePixel = WhitePixelOfScreen(defaultScreen);
117
118 #if 1
119   SetBitmaps_EM(pcxBitmapsX2);
120
121   objBitmap = pcxBitmapsX2[0];
122   botBitmap = pcxBitmapsX2[1];
123   sprBitmap = pcxBitmapsX2[2];
124   ttlBitmap = pcxBitmapsX2[3];
125
126   objPixmap = pcxBitmapsX2[0]->drawable;
127   botPixmap = pcxBitmapsX2[1]->drawable;
128   sprPixmap = pcxBitmapsX2[2]->drawable;
129   ttlPixmap = pcxBitmapsX2[3]->drawable;
130
131   objmaskBitmap = pcxBitmapsX2[0]->clip_mask;
132   botmaskBitmap = pcxBitmapsX2[1]->clip_mask;
133   sprmaskBitmap = pcxBitmapsX2[2]->clip_mask;
134   ttlmaskBitmap = pcxBitmapsX2[3]->clip_mask;
135
136   screenBitmap = CreateBitmap(MAX_BUF_XSIZE * TILEX, MAX_BUF_YSIZE * TILEY,
137                               DEFAULT_DEPTH);
138   scoreBitmap = CreateBitmap(20 * TILEX, SCOREY, DEFAULT_DEPTH);
139
140   screenPixmap = screenBitmap->drawable;
141   scorePixmap = scoreBitmap->drawable;
142 #endif
143
144   spriteBitmap = XCreatePixmap(display, xwindow, TILEX, TILEY, 1);
145   if (spriteBitmap == 0)
146   {
147     fprintf(stderr, "%s: \"%s\": %s: %s\n", progname,
148             XDisplayName(arg_display), "failed to create pixmap",
149             strerror(errno));
150     return(1);
151   }
152
153   gcValues.graphics_exposures = False;
154   screenGC = XCreateGC(display, screenPixmap, GCGraphicsExposures, &gcValues);
155   if (screenGC == 0)
156   {
157     fprintf(stderr, "%s: \"%s\": %s: %s\n", progname,
158             XDisplayName(arg_display), "failed to create graphics context",
159             strerror(errno));
160     return(1);
161   }
162
163   gcValues.graphics_exposures = False;
164   scoreGC = XCreateGC(display, scorePixmap, GCGraphicsExposures, &gcValues);
165   if (scoreGC == 0)
166   {
167     fprintf(stderr, "%s: \"%s\": %s: %s\n", progname,
168             XDisplayName(arg_display), "failed to create graphics context",
169             strerror(errno));
170     return(1);
171   }
172
173   gcValues.function =
174     objmaskBitmap ? GXcopyInverted : sprmaskBitmap ? GXcopy : GXset;
175   gcValues.graphics_exposures = False;
176   spriteGC = XCreateGC(display, spriteBitmap, GCFunction | GCGraphicsExposures,
177                        &gcValues);
178   if (spriteGC == 0)
179   {
180     fprintf(stderr, "%s: \"%s\": %s: %s\n", progname,
181             XDisplayName(arg_display), "failed to create graphics context",
182             strerror(errno));
183     return(1);
184   }
185
186   /* ----------------------------------------------------------------- */
187
188 #if defined(PLATFORM_LINUX) || defined(PLATFORM_BSD)
189
190 #if 1
191   /* disable sound */
192   arg_silence = 1;
193 #endif
194
195   if (arg_silence == 0)
196   {
197     for (i = 0; i < SAMPLE_MAX; i++)
198     {
199       name[MAXNAME] = 0;
200
201       if (arg_basedir)
202       {
203         snprintf(name, MAXNAME+2, "%s/%s/%s", arg_basedir, EM_SND_DIR,
204                  sound_names[i]);
205       }
206       else
207       {
208         snprintf(name, MAXNAME+2, "%s/%s", EM_SND_DIR, sound_names[i]);
209       }
210
211       if (name[MAXNAME]) snprintf_overflow("read sounds/ directory");
212
213       if (read_sample(name, &sound_data[i], &sound_length[i]))
214         return(1);
215
216       {
217         short *ptr, *stop;
218         int mult = sound_volume[i] * 65536 / (100 * MIXER_MAX);
219         stop = sound_data[i] + sound_length[i];
220         for (ptr = sound_data[i]; ptr < stop; ptr++)
221           *ptr = (*ptr * mult) / 65536;
222       }
223     }
224
225     if (pipe(sound_pipe) == -1)
226     {
227       fprintf(stderr, "%s: %s: %s\n", progname, "unable to create sound pipe",
228               strerror(errno));
229       return(1);
230     }
231
232     sound_pid = fork();
233     if (sound_pid == -1)
234     {
235       fprintf(stderr, "%s: %s: %s\n", progname, "unable to fork sound thread",
236               strerror(errno));
237       return(1);
238     }
239
240     close(sound_pipe[sound_pid == 0]); sound_pipe[sound_pid == 0] = -1;
241     if (sound_pid == 0)
242       _exit(sound_thread());
243
244     signal(SIGPIPE, SIG_IGN); /* dont crash if sound process dies */
245   }
246
247 #endif /* defined(PLATFORM_LINUX) || defined(PLATFORM_BSD) */
248
249   return(0);
250 }
251
252 void close_all(void)
253 {
254   int i;
255
256   if (sound_pid != -1)
257   {
258     kill(sound_pid, SIGTERM);
259     waitpid(sound_pid, 0, 0);
260   }
261
262   if (sound_pipe[0] != -1)
263     close(sound_pipe[0]);
264   if (sound_pipe[1] != -1)
265     close(sound_pipe[1]);
266
267   for (i = 0; i < SAMPLE_MAX; i++)
268     if (sound_data[i])
269       free(sound_data[i]);
270
271   if (screenGC)
272     XFreeGC(display, screenGC);
273   if (scoreGC)
274     XFreeGC(display, scoreGC);
275   if (spriteGC)
276     XFreeGC(display, spriteGC);
277
278   if (spriteBitmap)
279     XFreePixmap(display, spriteBitmap);
280 }
281
282 /* ---------------------------------------------------------------------- */
283
284 void sound_play(void)
285 {
286   if (sound_pipe[1] != -1)
287   {
288     if (write(sound_pipe[1], &play, sizeof(play)) == -1)
289     {
290       fprintf(stderr, "%s: %s: %s\n", progname, "write sound",
291               strerror(errno));
292
293       if (sound_pipe[0] != -1)
294       {
295         close(sound_pipe[0]);
296         sound_pipe[0] = -1;
297       }
298
299       if (sound_pipe[1] != -1)
300       {
301         close(sound_pipe[1]);
302         sound_pipe[1] = -1;
303       }
304     }
305   }
306
307   memset(play, 0, sizeof(play));
308 }
309
310 #endif