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 ***********************************************************/
33 struct PictureFileInfo
35 char *picture_filename;
36 BOOL picture_with_mask;
41 char *picture_filename;
42 char *picturemask_filename;
45 static int sound_process_id = 0;
47 static void InitLevelAndPlayerInfo(void);
48 static void InitDisplay(int, char **);
49 static void InitSound(void);
50 static void InitSoundProcess(void);
51 static void InitWindow(int, char **);
52 static void InitGfx(void);
53 static void LoadGfx(int, struct PictureFileInfo *);
54 static void InitElementProperties(void);
56 void OpenAll(int argc, char *argv[])
58 InitLevelAndPlayerInfo();
64 InitRND(NEW_RANDOMIZE);
66 signal(SIGINT, CloseAll);
67 signal(SIGTERM, CloseAll);
69 InitDisplay(argc, argv);
70 InitWindow(argc, argv);
72 XMapWindow(display, window);
76 InitElementProperties();
81 void InitLevelAndPlayerInfo()
83 if (!LoadLevelInfo()) /* global level info */
86 LoadPlayerInfo(PLAYER_SETUP); /* global setup info */
87 LoadPlayerInfo(PLAYER_LEVEL); /* level specific info */
94 if (sound_status==SOUND_OFF)
98 if (access(sound_device_name,W_OK)<0)
100 fprintf(stderr,"%s: cannot access sound device - no sounds\n",progname);
101 sound_status=SOUND_OFF;
105 if ((sound_device=open(sound_device_name,O_WRONLY))<0)
107 fprintf(stderr,"%s: cannot open sound device - no sounds\n",progname);
108 sound_status=SOUND_OFF;
113 sound_status=SOUND_AVAILABLE;
116 sound_loops_allowed = TRUE;
117 sound_loops_on = TRUE;
120 sound_loops_allowed = TRUE;
121 sound_loops_on = TRUE;
124 for(i=0;i<NUM_SOUNDS;i++)
127 sprintf(sound_name[i], "%d", i+1);
129 Sound[i].name = sound_name[i];
130 if (!LoadSound(&Sound[i]))
132 sound_status=SOUND_OFF;
138 void InitSoundProcess()
140 if (sound_status==SOUND_OFF)
144 if (pipe(sound_pipe)<0)
146 fprintf(stderr,"%s: cannot create pipe - no sounds\n",progname);
147 sound_status=SOUND_OFF;
151 if ((sound_process_id=fork())<0)
153 fprintf(stderr,"%s: cannot create child process - no sounds\n",progname);
154 sound_status=SOUND_OFF;
158 if (!sound_process_id) /* we are child */
160 else /* we are parent */
161 close(sound_pipe[0]); /* no reading from pipe needed */
169 if (global_joystick_status==JOYSTICK_OFF)
173 if (access(joystick_device_name[joystick_nr],R_OK)<0)
175 fprintf(stderr,"%s: cannot access joystick device '%s'\n",
176 progname,joystick_device_name[joystick_nr]);
177 joystick_status = JOYSTICK_OFF;
181 if ((joystick_device=open(joystick_device_name[joystick_nr],O_RDONLY))<0)
183 fprintf(stderr,"%s: cannot open joystick device '%s'\n",
184 progname,joystick_device_name[joystick_nr]);
185 joystick_status = JOYSTICK_OFF;
189 joystick_status = JOYSTICK_AVAILABLE;
192 joystick_status = JOYSTICK_AVAILABLE;
196 void InitDisplay(int argc, char *argv[])
198 char *display_name = NULL;
201 /* get X server to connect to, if given as an argument */
202 for (i=1;i<argc-1;i++)
204 char *dispstr="-display";
205 int len=MAX(strlen(dispstr),strlen(argv[i]));
209 else if (!strncmp(argv[i],dispstr,len))
211 display_name=argv[i+1];
216 /* connect to X server */
217 if (!(display=XOpenDisplay(display_name)))
219 fprintf(stderr,"%s: cannot connect to X server %s\n",
220 progname, XDisplayName(display_name));
224 screen = DefaultScreen(display);
225 cmap = DefaultColormap(display, screen);
226 pen_fg = WhitePixel(display,screen);
227 pen_bg = BlackPixel(display,screen);
230 void InitWindow(int argc, char *argv[])
232 unsigned int border_width = 4;
233 Pixmap icon_pixmap, iconmask_pixmap;
234 unsigned int icon_width,icon_height;
235 int icon_hot_x,icon_hot_y;
236 char icon_filename[256];
237 XSizeHints size_hints;
239 XClassHint class_hints;
240 XTextProperty windowName, iconName;
242 unsigned long gc_valuemask;
243 char *window_name = WINDOWTITLE_STRING;
244 char *icon_name = WINDOWTITLE_STRING;
245 long window_event_mask;
246 Atom proto_atom = None, delete_atom = None;
247 int screen_width, screen_height;
248 int win_xpos = WIN_XPOS, win_ypos = WIN_YPOS;
251 static struct IconFileInfo icon_pic =
258 screen_width = XDisplayWidth(display, screen);
259 screen_height = XDisplayHeight(display, screen);
264 win_xpos = (screen_width - width) / 2;
265 win_ypos = (screen_height - height) / 2;
267 window = XCreateSimpleWindow(display, RootWindow(display, screen),
268 win_xpos, win_ypos, width, height, border_width,
272 proto_atom = XInternAtom(display, "WM_PROTOCOLS", FALSE);
273 delete_atom = XInternAtom(display, "WM_DELETE_WINDOW", FALSE);
274 if ((proto_atom != None) && (delete_atom != None))
275 XChangeProperty(display, window, proto_atom, XA_ATOM, 32,
276 PropModePrepend, (unsigned char *) &delete_atom, 1);
278 sprintf(icon_filename,"%s/%s",GFX_PATH,icon_pic.picture_filename);
279 XReadBitmapFile(display,window,icon_filename,
280 &icon_width,&icon_height,
281 &icon_pixmap,&icon_hot_x,&icon_hot_y);
284 fprintf(stderr, "%s: cannot read icon bitmap file '%s'.\n",
285 progname,icon_filename);
289 sprintf(icon_filename,"%s/%s",GFX_PATH,icon_pic.picturemask_filename);
290 XReadBitmapFile(display,window,icon_filename,
291 &icon_width,&icon_height,
292 &iconmask_pixmap,&icon_hot_x,&icon_hot_y);
293 if (!iconmask_pixmap)
295 fprintf(stderr, "%s: cannot read icon bitmap file '%s'.\n",
296 progname,icon_filename);
300 size_hints.width = size_hints.min_width = size_hints.max_width = width;
301 size_hints.height = size_hints.min_height = size_hints.max_height = height;
302 size_hints.flags = PSize | PMinSize | PMaxSize;
304 if (win_xpos || win_ypos)
306 size_hints.x = win_xpos;
307 size_hints.y = win_ypos;
308 size_hints.flags |= PPosition;
311 if (!XStringListToTextProperty(&window_name, 1, &windowName))
313 fprintf(stderr, "%s: structure allocation for windowName failed.\n",
318 if (!XStringListToTextProperty(&icon_name, 1, &iconName))
320 fprintf(stderr, "%s: structure allocation for iconName failed.\n",
325 wm_hints.initial_state = NormalState;
326 wm_hints.input = True;
327 wm_hints.icon_pixmap = icon_pixmap;
328 wm_hints.icon_mask = iconmask_pixmap;
329 wm_hints.flags = StateHint | IconPixmapHint | IconMaskHint | InputHint;
331 class_hints.res_name = progname;
332 class_hints.res_class = "Rocks'n'Diamonds";
334 XSetWMProperties(display, window, &windowName, &iconName,
335 argv, argc, &size_hints, &wm_hints,
338 XFree(windowName.value);
339 XFree(iconName.value);
341 /* Select event types wanted */
342 window_event_mask = ExposureMask | StructureNotifyMask | FocusChangeMask |
343 ButtonPressMask | ButtonReleaseMask | ButtonMotionMask |
344 KeyPressMask | KeyReleaseMask;
345 XSelectInput(display, window, window_event_mask);
348 /* create GC for drawing with window depth */
349 gc_values.graphics_exposures = False;
350 gc_values.foreground = pen_bg;
351 gc_values.background = pen_bg;
352 gc_valuemask = GCGraphicsExposures | GCForeground | GCBackground;
353 gc = XCreateGC(display, window, gc_valuemask, &gc_values);
356 void DrawInitText(char *text, int ypos, int color)
358 if (display && window && pix[PIX_SMALLFONT])
360 XFillRectangle(display,window,gc,0,ypos, WIN_XSIZE,FONT2_YSIZE);
361 DrawTextExt(window,gc,(WIN_XSIZE-strlen(text)*FONT2_XSIZE)/2,
362 ypos,text,FS_SMALL,color);
370 XGCValues clip_gc_values;
371 unsigned long clip_gc_valuemask;
373 static struct PictureFileInfo pic[NUM_PICTURES] =
383 static struct PictureFileInfo pic[NUM_PICTURES] =
385 { "RocksScreen", TRUE },
386 { "RocksDoor", TRUE },
387 { "RocksHeroes", TRUE },
388 { "RocksToons", TRUE },
389 { "RocksFont", FALSE },
390 { "RocksFont2", FALSE }
399 LoadGfx(PIX_SMALLFONT,&pic[PIX_SMALLFONT]);
400 DrawInitText(WINDOWTITLE_STRING,20,FC_YELLOW);
401 DrawInitText(COPYRIGHT_STRING,50,FC_RED);
403 DrawInitText("MSDOS version done by Guido Schulz",210,FC_BLUE);
406 DrawInitText("Loading graphics:",120,FC_GREEN);
408 for(i=0;i<NUM_PICTURES;i++)
409 if (i!=PIX_SMALLFONT)
414 printf("SUMMARY: %.2f SECONDS LOADING TIME\n",(float)(count2-count1)/100.0);
418 pix[PIX_DB_BACK] = XCreatePixmap(display, window,
420 XDefaultDepth(display,screen));
421 pix[PIX_DB_DOOR] = XCreatePixmap(display, window,
422 3*DXSIZE,DYSIZE+VYSIZE,
423 XDefaultDepth(display,screen));
424 pix[PIX_DB_FIELD] = XCreatePixmap(display, window,
426 XDefaultDepth(display,screen));
428 if (!pix[PIX_DB_BACK] || !pix[PIX_DB_DOOR])
430 fprintf(stderr, "%s: cannot create additional Pixmaps!\n",progname);
435 for(i=0;i<NUM_PIXMAPS;i++)
439 clip_gc_values.graphics_exposures = False;
440 clip_gc_values.foreground = pen_fg;
441 clip_gc_values.background = pen_bg;
442 clip_gc_values.clip_mask = clipmask[i];
444 GCGraphicsExposures | GCForeground | GCBackground | GCClipMask;
445 clip_gc[i] = XCreateGC(display,window,clip_gc_valuemask,&clip_gc_values);
449 drawto = backbuffer = pix[PIX_DB_BACK];
450 fieldbuffer = pix[PIX_DB_FIELD];
451 SetDrawtoField(DRAW_BACKBUFFER);
453 XCopyArea(display,pix[PIX_BACK],backbuffer,gc,
454 0,0, WIN_XSIZE,WIN_YSIZE, 0,0);
455 XFillRectangle(display,pix[PIX_DB_BACK],gc,
456 REAL_SX,REAL_SY, FULL_SXSIZE,FULL_SYSIZE);
457 XFillRectangle(display,pix[PIX_DB_DOOR],gc,
458 0,0, 3*DXSIZE,DYSIZE+VYSIZE);
460 for(i=0; i<MAX_BUF_XSIZE; i++)
461 for(j=0; j<MAX_BUF_YSIZE; j++)
464 redraw_mask = REDRAW_ALL;
467 void LoadGfx(int pos, struct PictureFileInfo *pic)
469 char basefilename[256];
472 #ifdef XPM_INCLUDE_FILE
473 int xpm_err, xbm_err;
474 unsigned int width,height;
478 char *picture_ext = ".gif";
480 char *picture_ext = ".xpm";
482 char *picturemask_ext = "Mask.xbm";
484 int gif_err, ilbm_err;
485 char *picture_ext = ".gif";
486 char *picturemask_ext = "Mask.ilbm";
494 if (pic->picture_filename)
501 sprintf(basefilename,"%s%s",pic->picture_filename,".gif");
502 DrawInitText(basefilename,150,FC_YELLOW);
503 sprintf(filename,"%s/%s",GFX_PATH,basefilename);
509 Read_GIF_to_Image(display, window, filename);
513 printf("GIF LOADING %s IN %.2f SECONDS\n",
514 filename,(float)(count2-count1)/100.0);
522 sprintf(basefilename,"%s%s",pic->picture_filename,picture_ext);
523 DrawInitText(basefilename,150,FC_YELLOW);
527 sprintf(filename,"%s/%s",GFX_PATH,basefilename);
533 #ifdef XPM_INCLUDE_FILE
535 xpm_att[pos].valuemask = XpmCloseness;
536 xpm_att[pos].closeness = 65535;
539 xpm_att[pos].closeness = 20000;
543 xpm_err = XpmReadFileToPixmap(display,window,filename,
544 &pix[pos],&shapemask,&xpm_att[pos]);
546 xpm_err = XpmSuccess;
552 fprintf(stderr,"Cannot open Xpm file '%s' !\n",filename);
556 fprintf(stderr,"Invalid Xpm file '%s'!\n",filename);
560 fprintf(stderr,"Not enough memory for Xpm file '%s'!\n",filename);
564 fprintf(stderr,"Can't get colors for Xpm file '%s'!\n",filename);
573 gif_err = Read_GIF_to_Pixmap(display,filename,&pix[pos]);
580 fprintf(stderr,"Cannot open GIF file '%s' !\n",filename);
584 fprintf(stderr,"Cannot read GIF file '%s' !\n",filename);
587 case GIF_FileInvalid:
588 fprintf(stderr,"Invalid GIF file '%s'!\n",filename);
592 fprintf(stderr,"Not enough memory for GIF file '%s'!\n",filename);
595 case GIF_ColorFailed:
596 fprintf(stderr,"Can't get colors for GIF file '%s'!\n",filename);
607 printf("XPM LOADING %s IN %.2f SECONDS\n",
608 filename,(float)(count2-count1)/100.0);
614 fprintf(stderr, "%s: cannot read graphics file '%s'.\n",
623 /* zugehörige Maske laden (wenn vorhanden) */
624 if (pic->picture_with_mask)
627 xbm_err = BitmapSuccess;
628 clipmask[pos] = DUMMY_MASK;
631 sprintf(basefilename,"%s%s",pic->picture_filename,picturemask_ext);
632 DrawInitText(basefilename,150,FC_YELLOW);
633 sprintf(filename,"%s/%s",GFX_PATH,basefilename);
640 #ifdef XPM_INCLUDE_FILE
643 xbm_err = XReadBitmapFile(display,window,filename,
644 &width,&height,&clipmask[pos],&hot_x,&hot_y);
646 xbm_err = BitmapSuccess;
656 case BitmapOpenFailed:
657 fprintf(stderr,"Bitmap file open failed on '%s' !\n",filename);
661 case BitmapFileInvalid:
662 fprintf(stderr,"Bitmap file invalid: '%s' !\n",filename);
667 fprintf(stderr,"No memory for file '%s' !\n",filename);
677 ilbm_err = Read_ILBM_to_Bitmap(display,filename,&clipmask[pos]);
683 case ILBM_OpenFailed:
684 fprintf(stderr,"Cannot open ILBM file '%s' !\n",filename);
687 case ILBM_ReadFailed:
688 fprintf(stderr,"Cannot read ILBM file '%s' !\n",filename);
691 case ILBM_FileInvalid:
692 fprintf(stderr,"Invalid ILBM file '%s'!\n",filename);
696 fprintf(stderr,"Not enough memory for ILBM file '%s'!\n",filename);
707 printf("LOADING %s IN %.2f SECONDS\n",
708 filename,(float)(count2-count1)/100.0);
714 fprintf(stderr, "%s: cannot read graphics file '%s'.\n",
723 pix[pos] = test_pix[test_picture_count-1];
724 clipmask[pos] = test_clipmask[test_picture_count-1];
727 void InitElementProperties()
731 static int ep_amoebalive[] =
738 static int ep_amoebalive_num = sizeof(ep_amoebalive)/sizeof(int);
740 static int ep_amoeboid[] =
748 static int ep_amoeboid_num = sizeof(ep_amoeboid)/sizeof(int);
750 static int ep_schluessel[] =
757 static int ep_schluessel_num = sizeof(ep_schluessel)/sizeof(int);
759 static int ep_pforte[] =
770 static int ep_pforte_num = sizeof(ep_pforte)/sizeof(int);
772 static int ep_solid[] =
802 static int ep_solid_num = sizeof(ep_solid)/sizeof(int);
804 static int ep_massiv[] =
822 static int ep_massiv_num = sizeof(ep_massiv)/sizeof(int);
824 static int ep_slippery[] =
846 static int ep_slippery_num = sizeof(ep_slippery)/sizeof(int);
848 static int ep_enemy[] =
859 static int ep_enemy_num = sizeof(ep_enemy)/sizeof(int);
861 static int ep_mauer[] =
880 static int ep_mauer_num = sizeof(ep_mauer)/sizeof(int);
882 static int ep_can_fall[] =
900 static int ep_can_fall_num = sizeof(ep_can_fall)/sizeof(int);
902 static int ep_can_smash[] =
921 static int ep_can_smash_num = sizeof(ep_can_smash)/sizeof(int);
923 static int ep_can_change[] =
933 static int ep_can_change_num = sizeof(ep_can_change)/sizeof(int);
935 static int ep_can_move[] =
951 static int ep_can_move_num = sizeof(ep_can_move)/sizeof(int);
953 static int ep_could_move[] =
976 static int ep_could_move_num = sizeof(ep_could_move)/sizeof(int);
978 static int ep_dont_touch[] =
985 static int ep_dont_touch_num = sizeof(ep_dont_touch)/sizeof(int);
987 static int ep_dont_go_to[] =
1000 static int ep_dont_go_to_num = sizeof(ep_dont_go_to)/sizeof(int);
1002 static int ep_mampf2[] =
1025 static int ep_mampf2_num = sizeof(ep_mampf2)/sizeof(int);
1027 static int ep_bd_element[] =
1052 static int ep_bd_element_num = sizeof(ep_bd_element)/sizeof(int);
1054 static int ep_sb_element[] =
1059 EL_SOKOBAN_FELD_LEER,
1060 EL_SOKOBAN_FELD_VOLL,
1063 static int ep_sb_element_num = sizeof(ep_sb_element)/sizeof(int);
1065 static int ep_gem[] =
1074 static int ep_gem_num = sizeof(ep_gem)/sizeof(int);
1076 static int ep_inactive[] =
1112 EL_SOKOBAN_FELD_LEER,
1113 EL_SOKOBAN_FELD_VOLL,
1126 static int ep_inactive_num = sizeof(ep_inactive)/sizeof(int);
1128 static int ep_explosive[] =
1144 static int ep_explosive_num = sizeof(ep_explosive)/sizeof(int);
1146 static int ep_mampf3[] =
1155 static int ep_mampf3_num = sizeof(ep_mampf3)/sizeof(int);
1157 static int ep_pushable[] =
1163 EL_SOKOBAN_FELD_VOLL,
1167 static int ep_pushable_num = sizeof(ep_pushable)/sizeof(int);
1169 static long ep_bit[] =
1196 static int *ep_array[] =
1223 static int *ep_num[] =
1250 static int num_properties = sizeof(ep_num)/sizeof(int *);
1252 for(i=0;i<MAX_ELEMENTS;i++)
1253 Elementeigenschaften[i] = 0;
1255 for(i=0;i<num_properties;i++)
1256 for(j=0;j<*(ep_num[i]);j++)
1257 Elementeigenschaften[(ep_array[i])[j]] |= ep_bit[i];
1258 for(i=EL_CHAR_START;i<EL_CHAR_END;i++)
1259 Elementeigenschaften[i] |= (EP_BIT_CHAR | EP_BIT_INACTIVE);
1266 if (sound_process_id)
1269 kill(sound_process_id, SIGTERM);
1270 FreeSounds(NUM_SOUNDS);
1273 for(i=0;i<NUM_PIXMAPS;i++)
1277 #ifdef XPM_INCLUDE_FILE
1278 if (i<NUM_PICTURES) /* XPM pictures */
1280 XFreeColors(display,DefaultColormap(display,screen),
1281 xpm_att[i].pixels,xpm_att[i].npixels,0);
1282 XpmFreeAttributes(&xpm_att[i]);
1285 XFreePixmap(display,pix[i]);
1288 XFreePixmap(display,clipmask[i]);
1290 XFreeGC(display, clip_gc[i]);
1294 XFreeGC(display, gc);
1296 XCloseDisplay(display);