rnd-20040823-2-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 (arg_silence == 0)
191   {
192     for (i = 0; i < SAMPLE_MAX; i++)
193     {
194       name[MAXNAME] = 0;
195
196       if (arg_basedir)
197       {
198         snprintf(name, MAXNAME+2, "%s/%s/%s", arg_basedir, EM_SND_DIR,
199                  sound_names[i]);
200       }
201       else
202       {
203         snprintf(name, MAXNAME+2, "%s/%s", EM_SND_DIR, sound_names[i]);
204       }
205
206       if (name[MAXNAME]) snprintf_overflow("read sounds/ directory");
207
208       if (read_sample(name, &sound_data[i], &sound_length[i]))
209         return(1);
210
211       {
212         short *ptr, *stop;
213         int mult = sound_volume[i] * 65536 / (100 * MIXER_MAX);
214         stop = sound_data[i] + sound_length[i];
215         for (ptr = sound_data[i]; ptr < stop; ptr++)
216           *ptr = (*ptr * mult) / 65536;
217       }
218     }
219
220     if (pipe(sound_pipe) == -1)
221     {
222       fprintf(stderr, "%s: %s: %s\n", progname, "unable to create sound pipe",
223               strerror(errno));
224       return(1);
225     }
226
227     sound_pid = fork();
228     if (sound_pid == -1)
229     {
230       fprintf(stderr, "%s: %s: %s\n", progname, "unable to fork sound thread",
231               strerror(errno));
232       return(1);
233     }
234
235     close(sound_pipe[sound_pid == 0]); sound_pipe[sound_pid == 0] = -1;
236     if (sound_pid == 0)
237       _exit(sound_thread());
238
239     signal(SIGPIPE, SIG_IGN); /* dont crash if sound process dies */
240   }
241
242 #endif /* defined(PLATFORM_LINUX) || defined(PLATFORM_BSD) */
243
244   return(0);
245 }
246
247 void close_all(void)
248 {
249   int i;
250
251   if (sound_pid != -1)
252   {
253     kill(sound_pid, SIGTERM);
254     waitpid(sound_pid, 0, 0);
255   }
256
257   if (sound_pipe[0] != -1)
258     close(sound_pipe[0]);
259   if (sound_pipe[1] != -1)
260     close(sound_pipe[1]);
261
262   for (i = 0; i < SAMPLE_MAX; i++)
263     if (sound_data[i])
264       free(sound_data[i]);
265
266   if (screenGC)
267     XFreeGC(display, screenGC);
268   if (scoreGC)
269     XFreeGC(display, scoreGC);
270   if (spriteGC)
271     XFreeGC(display, spriteGC);
272
273   if (spriteBitmap)
274     XFreePixmap(display, spriteBitmap);
275 }
276
277 /* ---------------------------------------------------------------------- */
278
279 void sound_play(void)
280 {
281   if (sound_pipe[1] != -1)
282   {
283     if (write(sound_pipe[1], &play, sizeof(play)) == -1)
284     {
285       fprintf(stderr, "%s: %s: %s\n", progname, "write sound",
286               strerror(errno));
287
288       if (sound_pipe[0] != -1)
289       {
290         close(sound_pipe[0]);
291         sound_pipe[0] = -1;
292       }
293
294       if (sound_pipe[1] != -1)
295       {
296         close(sound_pipe[1]);
297         sound_pipe[1] = -1;
298       }
299     }
300   }
301
302   memset(play, 0, sizeof(play));
303 }
304
305 #endif