rnd-20040927-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 <signal.h>
7 #include <sys/wait.h>
8
9 #include "game_em.h"
10
11 #include "global.h"
12 #include "display.h"
13 #include "sample.h"
14
15
16 Bitmap *objBitmap;
17 Bitmap *sprBitmap;
18
19 Bitmap *screenBitmap;
20
21 #if 0
22 Pixmap spriteBitmap;
23 #endif
24
25 Pixmap objPixmap;
26 Pixmap sprPixmap;
27
28 #if 0
29 Pixmap objmaskBitmap;
30 Pixmap sprmaskBitmap;
31
32 GC spriteGC;
33 #endif
34
35 char play[SAMPLE_MAX];
36
37 #if defined(AUDIO_UNIX_NATIVE)
38 static int sound_pid = -1;
39 int sound_pipe[2] = { -1, -1 };         /* for communication */
40 short *sound_data[SAMPLE_MAX];          /* pointer to sound data */
41 long sound_length[SAMPLE_MAX];          /* length of sound data */
42
43 static const char *sound_names[SAMPLE_MAX] =
44 {
45   "00.blank.au","01.roll.au","02.stone.au","03.nut.au","04.crack.au",
46   "05.bug.au","06.tank.au","07.android.au","08.spring.au","09.slurp.au",
47   "10.eater.au","11.alien.au","12.collect.au","13.diamond.au","14.squash.au",
48   "15.drip.au","16.push.au","17.dirt.au","18.acid.au","19.ball.au",
49   "20.grow.au","21.wonder.au","22.door.au","23.exit.au","24.dynamite.au",
50   "25.tick.au","26.press.au","27.wheel.au","28.boom.au","29.time.au",
51   "30.die.au"
52 };
53 static const int sound_volume[SAMPLE_MAX] =
54 {
55   20,100,100,100,100,20,20,100,100,100,
56   50,100,100,100,100,100,100,100,100,100,
57   100,20,100,100,100,100,100,20,100,100,
58   100
59 };
60 #endif
61
62 char *progname;
63 char *arg_basedir;
64
65 extern void tab_generate();
66 extern void ulaw_generate();
67
68 int open_all(void)
69 {
70   Bitmap *emc_bitmaps[2];
71 #if 0
72   XGCValues gcValues;
73 #endif
74
75 #if 1
76   SetBitmaps_EM(emc_bitmaps);
77
78   objBitmap = emc_bitmaps[0];
79   sprBitmap = emc_bitmaps[1];
80
81 #if 0
82   objPixmap = emc_bitmaps[0]->drawable;
83   sprPixmap = emc_bitmaps[1]->drawable;
84
85   objmaskBitmap = emc_bitmaps[0]->clip_mask;
86   sprmaskBitmap = emc_bitmaps[1]->clip_mask;
87 #endif
88
89   screenBitmap = CreateBitmap(MAX_BUF_XSIZE * TILEX, MAX_BUF_YSIZE * TILEY,
90                               DEFAULT_DEPTH);
91 #endif
92
93 #if 0
94   spriteBitmap = XCreatePixmap(display, window->drawable, TILEX, TILEY, 1);
95   if (spriteBitmap == 0)
96     Error(ERR_EXIT, "failed to create sprite pixmap for EM engine");
97
98   gcValues.function =
99     objmaskBitmap ? GXcopyInverted : sprmaskBitmap ? GXcopy : GXset;
100   gcValues.graphics_exposures = False;
101   spriteGC = XCreateGC(display, spriteBitmap, GCFunction | GCGraphicsExposures,
102                        &gcValues);
103   if (spriteGC == 0)
104     Error(ERR_EXIT, "failed to create sprite GC for EM engine");
105 #endif
106
107   /* ----------------------------------------------------------------- */
108
109 #if defined(AUDIO_UNIX_NATIVE)
110
111 #if defined(PLATFORM_LINUX) || defined(PLATFORM_BSD)
112
113   if (1)
114   {
115     char name[MAXNAME+2];
116     int i;
117
118     for (i = 0; i < SAMPLE_MAX; i++)
119     {
120       name[MAXNAME] = 0;
121
122       if (arg_basedir)
123       {
124         snprintf(name, MAXNAME+2, "%s/%s/%s", arg_basedir, EM_SND_DIR,
125                  sound_names[i]);
126       }
127       else
128       {
129         snprintf(name, MAXNAME+2, "%s/%s", EM_SND_DIR, sound_names[i]);
130       }
131
132       if (name[MAXNAME])
133         Error(ERR_EXIT, "buffer overflow when reading sounds directory");
134
135       if (read_sample(name, &sound_data[i], &sound_length[i]))
136         return(1);
137
138       {
139         short *ptr, *stop;
140         int mult = sound_volume[i] * 65536 / (100 * MIXER_MAX);
141         stop = sound_data[i] + sound_length[i];
142         for (ptr = sound_data[i]; ptr < stop; ptr++)
143           *ptr = (*ptr * mult) / 65536;
144       }
145     }
146
147     if (pipe(sound_pipe) == -1)
148     {
149       Error(ERR_WARN, "unable to create sound pipe for EM engine -- no sound");
150
151       return(1);
152     }
153
154     sound_pid = fork();
155     if (sound_pid == -1)
156     {
157       Error(ERR_WARN, "unable to fork sound thread for EM engine -- no sound");
158
159       return(1);
160     }
161
162     close(sound_pipe[sound_pid == 0]);
163     sound_pipe[sound_pid == 0] = -1;
164     if (sound_pid == 0)
165       _exit(sound_thread());
166
167     signal(SIGPIPE, SIG_IGN); /* dont crash if sound process dies */
168   }
169
170 #endif  /* defined(PLATFORM_LINUX) || defined(PLATFORM_BSD) */
171
172 #endif  /* AUDIO_UNIX_NATIVE */
173
174   return(0);
175 }
176
177 void em_open_all()
178 {
179   /* pre-calculate some data */
180   tab_generate();
181   ulaw_generate();
182
183   progname = "emerald mine";
184
185   if (open_all() != 0)
186     Error(ERR_EXIT, "em_open_all(): open_all() failed");
187
188   game_init_vars();
189 }
190
191 void em_close_all(void)
192 {
193 #if defined(AUDIO_UNIX_NATIVE)
194   int i;
195
196   if (sound_pid != -1)
197   {
198     kill(sound_pid, SIGTERM);
199     waitpid(sound_pid, 0, 0);
200   }
201
202   if (sound_pipe[0] != -1)
203     close(sound_pipe[0]);
204   if (sound_pipe[1] != -1)
205     close(sound_pipe[1]);
206
207   for (i = 0; i < SAMPLE_MAX; i++)
208     if (sound_data[i])
209       free(sound_data[i]);
210 #endif
211
212 #if 0
213   if (spriteGC)
214     XFreeGC(display, spriteGC);
215
216   if (spriteBitmap)
217     XFreePixmap(display, spriteBitmap);
218 #endif
219 }
220
221 /* ---------------------------------------------------------------------- */
222
223 void sound_play(void)
224 {
225 #if defined(AUDIO_UNIX_NATIVE)
226   if (sound_pipe[1] != -1)
227   {
228     if (write(sound_pipe[1], &play, sizeof(play)) == -1)
229     {
230       Error(ERR_WARN, "cannot write into pipe to child process -- no sounds");
231
232       if (sound_pipe[0] != -1)
233       {
234         close(sound_pipe[0]);
235         sound_pipe[0] = -1;
236       }
237
238       if (sound_pipe[1] != -1)
239       {
240         close(sound_pipe[1]);
241         sound_pipe[1] = -1;
242       }
243     }
244   }
245
246   memset(play, 0, sizeof(play));
247 #endif
248 }