31ada8d8e10511ab71629ce5347e284e612bc65c
[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]); sound_pipe[sound_pid == 0] = -1;
163     if (sound_pid == 0)
164       _exit(sound_thread());
165
166     signal(SIGPIPE, SIG_IGN); /* dont crash if sound process dies */
167   }
168
169 #endif  /* defined(PLATFORM_LINUX) || defined(PLATFORM_BSD) */
170
171 #endif  /* AUDIO_UNIX_NATIVE */
172
173   return(0);
174 }
175
176 void em_open_all()
177 {
178   /* pre-calculate some data */
179   tab_generate();
180   ulaw_generate();
181
182   progname = "emerald mine";
183
184   if (open_all() != 0)
185     Error(ERR_EXIT, "em_open_all(): open_all() failed");
186
187   game_init_vars();
188 }
189
190 void em_close_all(void)
191 {
192 #if defined(AUDIO_UNIX_NATIVE)
193   int i;
194
195   if (sound_pid != -1)
196   {
197     kill(sound_pid, SIGTERM);
198     waitpid(sound_pid, 0, 0);
199   }
200
201   if (sound_pipe[0] != -1)
202     close(sound_pipe[0]);
203   if (sound_pipe[1] != -1)
204     close(sound_pipe[1]);
205
206   for (i = 0; i < SAMPLE_MAX; i++)
207     if (sound_data[i])
208       free(sound_data[i]);
209 #endif
210
211 #if 0
212   if (spriteGC)
213     XFreeGC(display, spriteGC);
214
215   if (spriteBitmap)
216     XFreePixmap(display, spriteBitmap);
217 #endif
218 }
219
220 /* ---------------------------------------------------------------------- */
221
222 void sound_play(void)
223 {
224 #if defined(AUDIO_UNIX_NATIVE)
225   if (sound_pipe[1] != -1)
226   {
227     if (write(sound_pipe[1], &play, sizeof(play)) == -1)
228     {
229       Error(ERR_WARN, "cannot write into pipe to child process -- no sounds");
230
231       if (sound_pipe[0] != -1)
232       {
233         close(sound_pipe[0]);
234         sound_pipe[0] = -1;
235       }
236
237       if (sound_pipe[1] != -1)
238       {
239         close(sound_pipe[1]);
240         sound_pipe[1] = -1;
241       }
242     }
243   }
244
245   memset(play, 0, sizeof(play));
246 #endif
247 }