1 /***********************************************************
2 * Rocks'n'Diamonds -- McDuffin Strikes Back! *
3 *----------------------------------------------------------*
4 * ©1995 Artsoft Development *
6 * 33659 Bielefeld-Senne *
7 * Telefon: (0521) 493245 *
8 * eMail: aeglos@valinor.owl.de *
9 * aeglos@uni-paderborn.de *
10 * q99492@pbhrzx.uni-paderborn.de *
11 *----------------------------------------------------------*
14 * Letzte Aenderung: 15.06.1995 *
15 ***********************************************************/
27 int sound_process_id=0;
29 void OpenAll(int argc, char *argv[])
32 LoadPlayerInfo(PLAYER_SETUP);
33 LoadPlayerInfo(PLAYER_LEVEL);
39 InitRND(NEW_RANDOMIZE);
41 signal(SIGINT, CloseAll);
42 signal(SIGTERM, CloseAll);
44 InitDisplay(argc, argv);
45 InitWindow(argc, argv);
50 XMapWindow(display, window);
58 if (sound_status==SOUND_OFF)
61 if (access(sound_device_name,W_OK)<0)
63 fprintf(stderr,"%s: cannot access sound device - no sounds\n",progname);
64 sound_status=SOUND_OFF;
68 if ((sound_device=open(sound_device_name,O_WRONLY))<0)
70 fprintf(stderr,"%s: cannot open sound device - no sounds\n",progname);
71 sound_status=SOUND_OFF;
76 sound_status=SOUND_AVAILABLE;
79 sound_loops_allowed = TRUE;
80 sound_loops_on = TRUE;
83 for(i=0;i<NUM_SOUNDS;i++)
85 Sound[i].name = sound_name[i];
86 if (!LoadSound(&Sound[i]))
88 sound_status=SOUND_OFF;
94 void InitSoundProcess()
96 if (sound_status==SOUND_OFF)
99 if (pipe(sound_pipe)<0)
101 fprintf(stderr,"%s: cannot create pipe - no sounds\n",progname);
102 sound_status=SOUND_OFF;
106 if ((sound_process_id=fork())<0)
108 fprintf(stderr,"%s: cannot create child process - no sounds\n",progname);
109 sound_status=SOUND_OFF;
113 if (!sound_process_id) /* we are child */
115 else /* we are parent */
116 close(sound_pipe[0]); /* no reading from pipe needed */
121 if (global_joystick_status==JOYSTICK_OFF)
124 if (access(joystick_device_name[joystick_nr],R_OK)<0)
126 fprintf(stderr,"%s: cannot access joystick device '%s'\n",
127 joystick_device_name[joystick_nr],progname);
128 joystick_status = JOYSTICK_OFF;
132 if ((joystick_device=open(joystick_device_name[joystick_nr],O_RDONLY))<0)
134 fprintf(stderr,"%s: cannot open joystick device '%s'\n",
135 joystick_device_name[joystick_nr],progname);
136 joystick_status = JOYSTICK_OFF;
140 joystick_status = JOYSTICK_AVAILABLE;
144 void InitDisplay(int argc, char *argv[])
146 char *display_name = NULL;
149 /* get X server to connect to, if given as an argument */
150 for (i=1;i<argc-1;i++)
152 char *dispstr="-display";
153 int len=MAX(strlen(dispstr),strlen(argv[i]));
157 else if (!strncmp(argv[i],dispstr,len))
159 display_name=argv[i+1];
164 /* connect to X server */
165 if (!(display=XOpenDisplay(display_name)))
167 fprintf(stderr,"%s: cannot connect to X server %s\n",
168 progname, XDisplayName(display_name));
172 screen = DefaultScreen(display);
173 cmap = DefaultColormap(display, screen);
174 pen_fg = WhitePixel(display,screen);
175 pen_bg = BlackPixel(display,screen);
178 void InitWindow(int argc, char *argv[])
180 unsigned int border_width = 4;
181 Pixmap icon_pixmap, iconmask_pixmap;
182 unsigned int icon_width,icon_height;
183 int icon_hot_x,icon_hot_y;
184 char icon_filename[256];
185 XSizeHints size_hints;
187 XClassHint class_hints;
188 XTextProperty windowName, iconName;
190 unsigned int gc_valuemask;
191 char *window_name = "Rocks'n'Diamonds";
192 char *icon_name = "Rocks'n'Diamonds";
193 int window_event_mask;
198 window = XCreateSimpleWindow(display, RootWindow(display, screen),
199 WIN_XPOS, WIN_YPOS, width, height, border_width,
202 sprintf(icon_filename,"%s/%s",GFX_PATH,icon_pic.picture_filename);
203 XReadBitmapFile(display,window,icon_filename,
204 &icon_width,&icon_height,
205 &icon_pixmap,&icon_hot_x,&icon_hot_y);
208 fprintf(stderr, "%s: cannot read icon bitmap file '%s'.\n",
209 progname,icon_filename);
213 sprintf(icon_filename,"%s/%s",GFX_PATH,icon_pic.picturemask_filename);
214 XReadBitmapFile(display,window,icon_filename,
215 &icon_width,&icon_height,
216 &iconmask_pixmap,&icon_hot_x,&icon_hot_y);
217 if (!iconmask_pixmap)
219 fprintf(stderr, "%s: cannot read icon bitmap file '%s'.\n",
220 progname,icon_filename);
224 size_hints.flags = PSize | PMinSize | PMaxSize;
225 size_hints.width = size_hints.min_width = size_hints.max_width = width;
226 size_hints.height = size_hints.min_height = size_hints.max_height = height;
228 if (!XStringListToTextProperty(&window_name, 1, &windowName))
230 fprintf(stderr, "%s: structure allocation for windowName failed.\n",
235 if (!XStringListToTextProperty(&icon_name, 1, &iconName))
237 fprintf(stderr, "%s: structure allocation for iconName failed.\n",
242 wm_hints.initial_state = NormalState;
243 wm_hints.input = True;
244 wm_hints.icon_pixmap = icon_pixmap;
245 wm_hints.icon_mask = iconmask_pixmap;
246 wm_hints.flags = StateHint | IconPixmapHint | IconMaskHint | InputHint;
248 class_hints.res_name = progname;
249 class_hints.res_class = "Rocks'n'Diamonds";
251 XSetWMProperties(display, window, &windowName, &iconName,
252 argv, argc, &size_hints, &wm_hints,
255 XFree(windowName.value);
256 XFree(iconName.value);
258 /* Select event types wanted */
259 window_event_mask = ExposureMask | StructureNotifyMask | FocusChangeMask |
260 ButtonPressMask | ButtonReleaseMask | ButtonMotionMask |
261 KeyPressMask | KeyReleaseMask;
262 XSelectInput(display, window, window_event_mask);
264 /* create GC for drawing with window depth */
265 gc_values.graphics_exposures = False;
266 gc_values.foreground = pen_bg;
267 gc_values.background = pen_bg;
268 gc_valuemask = GCGraphicsExposures | GCForeground | GCBackground;
269 gc = XCreateGC(display, window, gc_valuemask, &gc_values);
275 int xpm_err, xbm_err;
276 unsigned int width,height;
279 unsigned int gc_valuemask;
280 XGCValues clip_gc_values;
281 unsigned int clip_gc_valuemask;
285 for(i=0;i<NUM_PICTURES;i++)
287 if (pic[i].picture_filename)
289 sprintf(filename,"%s/%s",GFX_PATH,pic[i].picture_filename);
291 xpm_att[i].valuemask = XpmCloseness;
292 xpm_att[i].closeness = 20000;
293 xpm_err = XpmReadFileToPixmap(display,window,filename,
294 &pix[i],&shapemask,&xpm_att[i]);
298 fprintf(stderr,"Xpm file open failed on '%s' !\n",filename);
302 fprintf(stderr,"Invalid Xpm file '%s'!\n",filename);
306 fprintf(stderr,"Not enough memory !\n");
310 fprintf(stderr,"Can`t get any colors...\n");
318 fprintf(stderr, "%s: cannot read Xpm file '%s'.\n",
325 if (pic[i].picturemask_filename)
327 sprintf(filename,"%s/%s",GFX_PATH,pic[i].picturemask_filename);
329 xbm_err = XReadBitmapFile(display,window,filename,
330 &width,&height,&clipmask[i],&hot_x,&hot_y);
335 case BitmapOpenFailed:
336 fprintf(stderr,"Bitmap file open failed on '%s' !\n",filename);
340 case BitmapFileInvalid:
341 fprintf(stderr,"Bitmap file invalid: '%s' !\n",filename);
346 fprintf(stderr,"No memory for file '%s' !\n",filename);
355 fprintf(stderr, "%s: cannot read X11 bitmap file '%s'.\n",
363 pix[PIX_DB_BACK] = XCreatePixmap(display, window,
365 XDefaultDepth(display,screen));
366 pix[PIX_DB_DOOR] = XCreatePixmap(display, window,
367 3*DXSIZE,DYSIZE+VYSIZE,
368 XDefaultDepth(display,screen));
370 clipmask[PIX_FADEMASK] = XCreatePixmap(display, window,
371 SXSIZE+TILEX,SYSIZE+TILEY,1);
373 if (!pix[PIX_DB_BACK] || !pix[PIX_DB_DOOR] || !clipmask[PIX_FADEMASK])
375 fprintf(stderr, "%s: cannot create additional Pixmaps!\n",progname);
380 /* create GC for drawing with bitplane depth */
381 gc_values.graphics_exposures = False;
382 gc_values.foreground = pen_bg;
383 gc_values.background = pen_bg;
384 gc_valuemask = GCGraphicsExposures | GCForeground | GCBackground;
385 plane_gc = XCreateGC(display, clipmask[PIX_BACK], gc_valuemask, &gc_values);
387 for(y=0;y<=SCR_FIELDY;y++) for(x=0;x<=SCR_FIELDX;x++)
388 XCopyArea(display,clipmask[PIX_BACK],clipmask[PIX_FADEMASK],plane_gc,
389 SX+2*TILEX,SY+10*TILEY,TILEX,TILEY,x*TILEX,y*TILEY);
391 for(i=0;i<NUM_PIXMAPS;i++)
395 clip_gc_values.graphics_exposures = False;
396 clip_gc_values.foreground = pen_fg;
397 clip_gc_values.background = pen_bg;
398 clip_gc_values.clip_mask = clipmask[i];
400 GCGraphicsExposures | GCForeground | GCBackground | GCClipMask;
401 clip_gc[i] = XCreateGC(display,window,clip_gc_valuemask,&clip_gc_values);
405 drawto = drawto_field = backbuffer = pix[PIX_DB_BACK];
407 XCopyArea(display,pix[PIX_BACK],backbuffer,gc,
408 0,0, WIN_XSIZE,WIN_YSIZE, 0,0);
409 XFillRectangle(display,backbuffer,gc,
410 REAL_SX,REAL_SY, FULL_SXSIZE,FULL_SYSIZE);
412 for(i=0;i<SCR_FIELDX;i++)
413 for(j=0;j<SCR_FIELDY;j++)
416 redraw_mask=REDRAW_ALL;
423 if (sound_process_id)
426 kill(sound_process_id, SIGTERM);
427 FreeSounds(NUM_SOUNDS);
430 for(i=0;i<NUM_PIXMAPS;i++)
434 if (i<NUM_PICTURES) /* XPM pictures */
436 XFreeColors(display,DefaultColormap(display,screen),
437 xpm_att[i].pixels,xpm_att[i].npixels,0);
438 XpmFreeAttributes(&xpm_att[i]);
440 XFreePixmap(display,pix[i]);
443 XFreePixmap(display,clipmask[i]);
445 XFreeGC(display, clip_gc[i]);
449 XFreeGC(display, gc);
451 XFreeGC(display, plane_gc);
453 XCloseDisplay(display);