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 *----------------------------------------------------------*
13 ***********************************************************/
23 static int sound_process_id = 0;
25 void OpenAll(int argc, char *argv[])
27 InitLevelAndPlayerInfo();
33 InitRND(NEW_RANDOMIZE);
35 signal(SIGINT, CloseAll);
36 signal(SIGTERM, CloseAll);
38 InitDisplay(argc, argv);
39 InitWindow(argc, argv);
41 XMapWindow(display, window);
45 InitElementProperties();
50 void InitLevelAndPlayerInfo()
52 if (!LoadLevelInfo()) /* global level info */
55 LoadPlayerInfo(PLAYER_SETUP); /* global setup info */
56 LoadPlayerInfo(PLAYER_LEVEL); /* level specific info */
63 if (sound_status==SOUND_OFF)
66 if (access(sound_device_name,W_OK)<0)
68 fprintf(stderr,"%s: cannot access sound device - no sounds\n",progname);
69 sound_status=SOUND_OFF;
73 if ((sound_device=open(sound_device_name,O_WRONLY))<0)
75 fprintf(stderr,"%s: cannot open sound device - no sounds\n",progname);
76 sound_status=SOUND_OFF;
81 sound_status=SOUND_AVAILABLE;
84 sound_loops_allowed = TRUE;
85 sound_loops_on = TRUE;
88 for(i=0;i<NUM_SOUNDS;i++)
90 Sound[i].name = sound_name[i];
91 if (!LoadSound(&Sound[i]))
93 sound_status=SOUND_OFF;
99 void InitSoundProcess()
101 if (sound_status==SOUND_OFF)
104 if (pipe(sound_pipe)<0)
106 fprintf(stderr,"%s: cannot create pipe - no sounds\n",progname);
107 sound_status=SOUND_OFF;
111 if ((sound_process_id=fork())<0)
113 fprintf(stderr,"%s: cannot create child process - no sounds\n",progname);
114 sound_status=SOUND_OFF;
118 if (!sound_process_id) /* we are child */
120 else /* we are parent */
121 close(sound_pipe[0]); /* no reading from pipe needed */
126 if (global_joystick_status==JOYSTICK_OFF)
129 if (access(joystick_device_name[joystick_nr],R_OK)<0)
131 fprintf(stderr,"%s: cannot access joystick device '%s'\n",
132 progname,joystick_device_name[joystick_nr]);
133 joystick_status = JOYSTICK_OFF;
137 if ((joystick_device=open(joystick_device_name[joystick_nr],O_RDONLY))<0)
139 fprintf(stderr,"%s: cannot open joystick device '%s'\n",
140 progname,joystick_device_name[joystick_nr]);
141 joystick_status = JOYSTICK_OFF;
145 joystick_status = JOYSTICK_AVAILABLE;
149 void InitDisplay(int argc, char *argv[])
151 char *display_name = NULL;
154 /* get X server to connect to, if given as an argument */
155 for (i=1;i<argc-1;i++)
157 char *dispstr="-display";
158 int len=MAX(strlen(dispstr),strlen(argv[i]));
162 else if (!strncmp(argv[i],dispstr,len))
164 display_name=argv[i+1];
169 /* connect to X server */
170 if (!(display=XOpenDisplay(display_name)))
172 fprintf(stderr,"%s: cannot connect to X server %s\n",
173 progname, XDisplayName(display_name));
177 screen = DefaultScreen(display);
178 cmap = DefaultColormap(display, screen);
179 pen_fg = WhitePixel(display,screen);
180 pen_bg = BlackPixel(display,screen);
183 void InitWindow(int argc, char *argv[])
185 unsigned int border_width = 4;
186 Pixmap icon_pixmap, iconmask_pixmap;
187 unsigned int icon_width,icon_height;
188 int icon_hot_x,icon_hot_y;
189 char icon_filename[256];
190 XSizeHints size_hints;
192 XClassHint class_hints;
193 XTextProperty windowName, iconName;
195 unsigned long gc_valuemask;
196 char *window_name = WINDOWTITLE_STRING;
197 char *icon_name = WINDOWTITLE_STRING;
198 long window_event_mask;
199 static struct PictureFile icon_pic =
208 window = XCreateSimpleWindow(display, RootWindow(display, screen),
209 WIN_XPOS, WIN_YPOS, width, height, border_width,
212 sprintf(icon_filename,"%s/%s",GFX_PATH,icon_pic.picture_filename);
213 XReadBitmapFile(display,window,icon_filename,
214 &icon_width,&icon_height,
215 &icon_pixmap,&icon_hot_x,&icon_hot_y);
218 fprintf(stderr, "%s: cannot read icon bitmap file '%s'.\n",
219 progname,icon_filename);
223 sprintf(icon_filename,"%s/%s",GFX_PATH,icon_pic.picturemask_filename);
224 XReadBitmapFile(display,window,icon_filename,
225 &icon_width,&icon_height,
226 &iconmask_pixmap,&icon_hot_x,&icon_hot_y);
227 if (!iconmask_pixmap)
229 fprintf(stderr, "%s: cannot read icon bitmap file '%s'.\n",
230 progname,icon_filename);
234 size_hints.flags = PSize | PMinSize | PMaxSize;
235 size_hints.width = size_hints.min_width = size_hints.max_width = width;
236 size_hints.height = size_hints.min_height = size_hints.max_height = height;
238 if (!XStringListToTextProperty(&window_name, 1, &windowName))
240 fprintf(stderr, "%s: structure allocation for windowName failed.\n",
245 if (!XStringListToTextProperty(&icon_name, 1, &iconName))
247 fprintf(stderr, "%s: structure allocation for iconName failed.\n",
252 wm_hints.initial_state = NormalState;
253 wm_hints.input = True;
254 wm_hints.icon_pixmap = icon_pixmap;
255 wm_hints.icon_mask = iconmask_pixmap;
256 wm_hints.flags = StateHint | IconPixmapHint | IconMaskHint | InputHint;
258 class_hints.res_name = progname;
259 class_hints.res_class = "Rocks'n'Diamonds";
261 XSetWMProperties(display, window, &windowName, &iconName,
262 argv, argc, &size_hints, &wm_hints,
265 XFree(windowName.value);
266 XFree(iconName.value);
268 /* Select event types wanted */
269 window_event_mask = ExposureMask | StructureNotifyMask | FocusChangeMask |
270 ButtonPressMask | ButtonReleaseMask | ButtonMotionMask |
271 KeyPressMask | KeyReleaseMask;
272 XSelectInput(display, window, window_event_mask);
274 /* create GC for drawing with window depth */
275 gc_values.graphics_exposures = False;
276 gc_values.foreground = pen_bg;
277 gc_values.background = pen_bg;
278 gc_valuemask = GCGraphicsExposures | GCForeground | GCBackground;
279 gc = XCreateGC(display, window, gc_valuemask, &gc_values);
282 void DrawInitText(char *text, int ypos, int color)
284 if (display && window && pix[PIX_SMALLFONT])
286 XFillRectangle(display,window,gc,0,ypos, WIN_XSIZE,FONT2_YSIZE);
287 DrawTextExt(window,gc,(WIN_XSIZE-strlen(text)*FONT2_XSIZE)/2,
288 ypos,text,FS_SMALL,color);
296 XGCValues clip_gc_values;
297 unsigned long clip_gc_valuemask;
298 static struct PictureFile pic[NUM_PICTURES] =
300 { "RocksScreen.xpm", "RocksScreenMaske.xbm" },
301 { "RocksDoor.xpm", "RocksDoorMaske.xbm" },
302 { "RocksToons.xpm", "RocksToonsMaske.xbm" },
303 { "RocksFont.xpm", NULL },
304 { "RocksFont2.xpm", NULL }
307 LoadGfx(PIX_SMALLFONT,&pic[PIX_SMALLFONT]);
308 DrawInitText(WINDOWTITLE_STRING,20,FC_YELLOW);
309 DrawInitText(COPYRIGHT_STRING,50,FC_RED);
310 DrawInitText("Loading graphics:",120,FC_GREEN);
312 for(i=0;i<NUM_PICTURES;i++)
313 if (i!=PIX_SMALLFONT)
316 pix[PIX_DB_BACK] = XCreatePixmap(display, window,
318 XDefaultDepth(display,screen));
319 pix[PIX_DB_DOOR] = XCreatePixmap(display, window,
320 3*DXSIZE,DYSIZE+VYSIZE,
321 XDefaultDepth(display,screen));
323 if (!pix[PIX_DB_BACK] || !pix[PIX_DB_DOOR])
325 fprintf(stderr, "%s: cannot create additional Pixmaps!\n",progname);
330 for(i=0;i<NUM_PIXMAPS;i++)
334 clip_gc_values.graphics_exposures = False;
335 clip_gc_values.foreground = pen_fg;
336 clip_gc_values.background = pen_bg;
337 clip_gc_values.clip_mask = clipmask[i];
339 GCGraphicsExposures | GCForeground | GCBackground | GCClipMask;
340 clip_gc[i] = XCreateGC(display,window,clip_gc_valuemask,&clip_gc_values);
344 drawto = drawto_field = backbuffer = pix[PIX_DB_BACK];
346 XCopyArea(display,pix[PIX_BACK],backbuffer,gc,
347 0,0, WIN_XSIZE,WIN_YSIZE, 0,0);
348 XFillRectangle(display,pix[PIX_DB_BACK],gc,
349 REAL_SX,REAL_SY, FULL_SXSIZE,FULL_SYSIZE);
350 XFillRectangle(display,pix[PIX_DB_DOOR],gc,
351 0,0, 3*DXSIZE,DYSIZE+VYSIZE);
353 for(i=0;i<SCR_FIELDX;i++)
354 for(j=0;j<SCR_FIELDY;j++)
357 redraw_mask=REDRAW_ALL;
360 void LoadGfx(int pos, struct PictureFile *pic)
362 int xpm_err, xbm_err;
363 unsigned int width,height;
369 if (pic->picture_filename)
371 DrawInitText(pic->picture_filename,150,FC_YELLOW);
372 sprintf(filename,"%s/%s",GFX_PATH,pic->picture_filename);
374 xpm_att[pos].valuemask = XpmCloseness;
375 xpm_att[pos].closeness = 20000;
376 xpm_err = XpmReadFileToPixmap(display,window,filename,
377 &pix[pos],&shapemask,&xpm_att[pos]);
381 fprintf(stderr,"Xpm file open failed on '%s' !\n",filename);
385 fprintf(stderr,"Invalid Xpm file '%s'!\n",filename);
389 fprintf(stderr,"Not enough memory !\n");
393 fprintf(stderr,"Can`t get any colors...\n");
402 fprintf(stderr, "%s: cannot read Xpm file '%s'.\n",
409 /* zugehörige Maske laden (wenn vorhanden) */
410 if (pic->picturemask_filename)
412 DrawInitText(pic->picturemask_filename,150,FC_YELLOW);
413 sprintf(filename,"%s/%s",GFX_PATH,pic->picturemask_filename);
415 xbm_err = XReadBitmapFile(display,window,filename,
416 &width,&height,&clipmask[pos],&hot_x,&hot_y);
421 case BitmapOpenFailed:
422 fprintf(stderr,"Bitmap file open failed on '%s' !\n",filename);
426 case BitmapFileInvalid:
427 fprintf(stderr,"Bitmap file invalid: '%s' !\n",filename);
432 fprintf(stderr,"No memory for file '%s' !\n",filename);
442 fprintf(stderr, "%s: cannot read X11 bitmap file '%s'.\n",
450 void InitElementProperties()
454 static int ep_amoebalive[] =
461 static int ep_amoebalive_num = sizeof(ep_amoebalive)/sizeof(int);
463 static int ep_amoeboid[] =
471 static int ep_amoeboid_num = sizeof(ep_amoeboid)/sizeof(int);
473 static int ep_badewannoid[] =
481 static int ep_badewannoid_num = sizeof(ep_badewannoid)/sizeof(int);
483 static int ep_schluessel[] =
490 static int ep_schluessel_num = sizeof(ep_schluessel)/sizeof(int);
492 static int ep_pforte[] =
503 static int ep_pforte_num = sizeof(ep_pforte)/sizeof(int);
505 static int ep_solid[] =
535 static int ep_solid_num = sizeof(ep_solid)/sizeof(int);
537 static int ep_massiv[] =
555 static int ep_massiv_num = sizeof(ep_massiv)/sizeof(int);
557 static int ep_slippery[] =
578 static int ep_slippery_num = sizeof(ep_slippery)/sizeof(int);
580 static int ep_enemy[] =
591 static int ep_enemy_num = sizeof(ep_enemy)/sizeof(int);
593 static int ep_mauer[] =
612 static int ep_mauer_num = sizeof(ep_mauer)/sizeof(int);
614 static int ep_can_fall[] =
632 static int ep_can_fall_num = sizeof(ep_can_fall)/sizeof(int);
634 static int ep_can_smash[] =
653 static int ep_can_smash_num = sizeof(ep_can_smash)/sizeof(int);
655 static int ep_can_change[] =
665 static int ep_can_change_num = sizeof(ep_can_change)/sizeof(int);
667 static int ep_can_move[] =
678 static int ep_can_move_num = sizeof(ep_can_move)/sizeof(int);
680 static int ep_could_move[] =
703 static int ep_could_move_num = sizeof(ep_could_move)/sizeof(int);
705 static int ep_dont_touch[] =
712 static int ep_dont_touch_num = sizeof(ep_dont_touch)/sizeof(int);
714 static int ep_dont_go_to[] =
727 static int ep_dont_go_to_num = sizeof(ep_dont_go_to)/sizeof(int);
729 static int ep_mampf2[] =
752 static int ep_mampf2_num = sizeof(ep_mampf2)/sizeof(int);
754 static int ep_bd_element[] =
779 static int ep_bd_element_num = sizeof(ep_bd_element)/sizeof(int);
781 static int ep_sb_element[] =
786 EL_SOKOBAN_FELD_LEER,
787 EL_SOKOBAN_FELD_VOLL,
790 static int ep_sb_element_num = sizeof(ep_sb_element)/sizeof(int);
792 static int ep_gem[] =
801 static int ep_gem_num = sizeof(ep_gem)/sizeof(int);
803 static int ep_inactive[] =
839 EL_SOKOBAN_FELD_LEER,
840 EL_SOKOBAN_FELD_VOLL,
853 static int ep_inactive_num = sizeof(ep_inactive)/sizeof(int);
855 static long ep_bit[] =
880 static int *ep_array[] =
905 static int *ep_num[] =
930 static int num_properties = sizeof(ep_num)/sizeof(int *);
932 for(i=0;i<MAX_ELEMENTS;i++)
933 Elementeigenschaften[i] = 0;
935 for(i=0;i<num_properties;i++)
936 for(j=0;j<*(ep_num[i]);j++)
937 Elementeigenschaften[(ep_array[i])[j]] |= ep_bit[i];
938 for(i=EL_CHAR_START;i<EL_CHAR_END;i++)
939 Elementeigenschaften[i] |= (EP_BIT_CHAR | EP_BIT_INACTIVE);
946 if (sound_process_id)
949 kill(sound_process_id, SIGTERM);
950 FreeSounds(NUM_SOUNDS);
953 for(i=0;i<NUM_PIXMAPS;i++)
957 if (i<NUM_PICTURES) /* XPM pictures */
959 XFreeColors(display,DefaultColormap(display,screen),
960 xpm_att[i].pixels,xpm_att[i].npixels,0);
961 XpmFreeAttributes(&xpm_att[i]);
963 XFreePixmap(display,pix[i]);
966 XFreePixmap(display,clipmask[i]);
968 XFreeGC(display, clip_gc[i]);
972 XFreeGC(display, gc);
974 XCloseDisplay(display);