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;
199 XVisualInfo vinfo_template, *vinfo;
204 /* get X server to connect to, if given as an argument */
205 for (i=1;i<argc-1;i++)
207 char *dispstr="-display";
208 int len=MAX(strlen(dispstr),strlen(argv[i]));
212 else if (!strncmp(argv[i],dispstr,len))
214 display_name=argv[i+1];
219 /* connect to X server */
220 if (!(display=XOpenDisplay(display_name)))
222 fprintf(stderr,"%s: cannot connect to X server %s\n",
223 progname, XDisplayName(display_name));
227 screen = DefaultScreen(display);
228 visual = DefaultVisual(display, screen);
229 depth = DefaultDepth(display, screen);
230 cmap = DefaultColormap(display, screen);
231 pen_fg = WhitePixel(display,screen);
232 pen_bg = BlackPixel(display,screen);
234 /* look for good enough visual */
235 vinfo_template.screen = screen;
236 vinfo_template.class = (depth == 8 ? PseudoColor : TrueColor);
237 vinfo_template.depth = depth;
238 if ((vinfo = XGetVisualInfo(display, VisualScreenMask | VisualClassMask |
239 VisualDepthMask, &vinfo_template, &num_visuals)))
241 visual = vinfo->visual;
242 XFree((void *)vinfo);
245 /* got appropriate visual? */
248 printf("Sorry, displays with less than 8 bits per pixel not supported.\n");
251 else if ((depth ==8 && visual->class != PseudoColor) ||
252 (depth > 8 && visual->class != TrueColor &&
253 visual->class != DirectColor))
255 printf("Sorry, cannot get appropriate visual.\n");
260 void InitWindow(int argc, char *argv[])
262 unsigned int border_width = 4;
263 Pixmap icon_pixmap, iconmask_pixmap;
264 unsigned int icon_width,icon_height;
265 int icon_hot_x,icon_hot_y;
266 char icon_filename[256];
267 XSizeHints size_hints;
269 XClassHint class_hints;
270 XTextProperty windowName, iconName;
272 unsigned long gc_valuemask;
273 char *window_name = WINDOWTITLE_STRING;
274 char *icon_name = WINDOWTITLE_STRING;
275 long window_event_mask;
276 Atom proto_atom = None, delete_atom = None;
277 int screen_width, screen_height;
278 int win_xpos = WIN_XPOS, win_ypos = WIN_YPOS;
281 static struct IconFileInfo icon_pic =
288 screen_width = XDisplayWidth(display, screen);
289 screen_height = XDisplayHeight(display, screen);
294 win_xpos = (screen_width - width) / 2;
295 win_ypos = (screen_height - height) / 2;
297 window = XCreateSimpleWindow(display, RootWindow(display, screen),
298 win_xpos, win_ypos, width, height, border_width,
302 proto_atom = XInternAtom(display, "WM_PROTOCOLS", FALSE);
303 delete_atom = XInternAtom(display, "WM_DELETE_WINDOW", FALSE);
304 if ((proto_atom != None) && (delete_atom != None))
305 XChangeProperty(display, window, proto_atom, XA_ATOM, 32,
306 PropModePrepend, (unsigned char *) &delete_atom, 1);
308 sprintf(icon_filename,"%s/%s",GFX_PATH,icon_pic.picture_filename);
309 XReadBitmapFile(display,window,icon_filename,
310 &icon_width,&icon_height,
311 &icon_pixmap,&icon_hot_x,&icon_hot_y);
314 fprintf(stderr, "%s: cannot read icon bitmap file '%s'.\n",
315 progname,icon_filename);
319 sprintf(icon_filename,"%s/%s",GFX_PATH,icon_pic.picturemask_filename);
320 XReadBitmapFile(display,window,icon_filename,
321 &icon_width,&icon_height,
322 &iconmask_pixmap,&icon_hot_x,&icon_hot_y);
323 if (!iconmask_pixmap)
325 fprintf(stderr, "%s: cannot read icon bitmap file '%s'.\n",
326 progname,icon_filename);
330 size_hints.width = size_hints.min_width = size_hints.max_width = width;
331 size_hints.height = size_hints.min_height = size_hints.max_height = height;
332 size_hints.flags = PSize | PMinSize | PMaxSize;
334 if (win_xpos || win_ypos)
336 size_hints.x = win_xpos;
337 size_hints.y = win_ypos;
338 size_hints.flags |= PPosition;
341 if (!XStringListToTextProperty(&window_name, 1, &windowName))
343 fprintf(stderr, "%s: structure allocation for windowName failed.\n",
348 if (!XStringListToTextProperty(&icon_name, 1, &iconName))
350 fprintf(stderr, "%s: structure allocation for iconName failed.\n",
355 wm_hints.initial_state = NormalState;
356 wm_hints.input = True;
357 wm_hints.icon_pixmap = icon_pixmap;
358 wm_hints.icon_mask = iconmask_pixmap;
359 wm_hints.flags = StateHint | IconPixmapHint | IconMaskHint | InputHint;
361 class_hints.res_name = progname;
362 class_hints.res_class = "Rocks'n'Diamonds";
364 XSetWMProperties(display, window, &windowName, &iconName,
365 argv, argc, &size_hints, &wm_hints,
368 XFree(windowName.value);
369 XFree(iconName.value);
371 /* Select event types wanted */
372 window_event_mask = ExposureMask | StructureNotifyMask | FocusChangeMask |
373 ButtonPressMask | ButtonReleaseMask | ButtonMotionMask |
374 KeyPressMask | KeyReleaseMask;
375 XSelectInput(display, window, window_event_mask);
378 /* create GC for drawing with window depth */
379 gc_values.graphics_exposures = False;
380 gc_values.foreground = pen_bg;
381 gc_values.background = pen_bg;
382 gc_valuemask = GCGraphicsExposures | GCForeground | GCBackground;
383 gc = XCreateGC(display, window, gc_valuemask, &gc_values);
386 void DrawInitText(char *text, int ypos, int color)
388 if (display && window && pix[PIX_SMALLFONT])
390 XFillRectangle(display,window,gc,0,ypos, WIN_XSIZE,FONT2_YSIZE);
391 DrawTextExt(window,gc,(WIN_XSIZE-strlen(text)*FONT2_XSIZE)/2,
392 ypos,text,FS_SMALL,color);
400 XGCValues clip_gc_values;
401 unsigned long clip_gc_valuemask;
403 static struct PictureFileInfo pic[NUM_PICTURES] =
413 static struct PictureFileInfo pic[NUM_PICTURES] =
415 { "RocksScreen", TRUE },
416 { "RocksDoor", TRUE },
417 { "RocksHeroes", TRUE },
418 { "RocksToons", TRUE },
419 { "RocksFont", FALSE },
420 { "RocksFont2", FALSE }
429 LoadGfx(PIX_SMALLFONT,&pic[PIX_SMALLFONT]);
430 DrawInitText(WINDOWTITLE_STRING,20,FC_YELLOW);
431 DrawInitText(COPYRIGHT_STRING,50,FC_RED);
433 DrawInitText("MSDOS version done by Guido Schulz",210,FC_BLUE);
436 DrawInitText("Loading graphics:",120,FC_GREEN);
438 for(i=0;i<NUM_PICTURES;i++)
439 if (i!=PIX_SMALLFONT)
444 printf("SUMMARY: %.2f SECONDS LOADING TIME\n",(float)(count2-count1)/100.0);
448 pix[PIX_DB_BACK] = XCreatePixmap(display, window,
450 XDefaultDepth(display,screen));
451 pix[PIX_DB_DOOR] = XCreatePixmap(display, window,
452 3*DXSIZE,DYSIZE+VYSIZE,
453 XDefaultDepth(display,screen));
454 pix[PIX_DB_FIELD] = XCreatePixmap(display, window,
456 XDefaultDepth(display,screen));
458 if (!pix[PIX_DB_BACK] || !pix[PIX_DB_DOOR])
460 fprintf(stderr, "%s: cannot create additional Pixmaps!\n",progname);
465 for(i=0;i<NUM_PIXMAPS;i++)
469 clip_gc_values.graphics_exposures = False;
470 clip_gc_values.foreground = pen_fg;
471 clip_gc_values.background = pen_bg;
472 clip_gc_values.clip_mask = clipmask[i];
474 GCGraphicsExposures | GCForeground | GCBackground | GCClipMask;
475 clip_gc[i] = XCreateGC(display,window,clip_gc_valuemask,&clip_gc_values);
479 drawto = backbuffer = pix[PIX_DB_BACK];
480 fieldbuffer = pix[PIX_DB_FIELD];
481 SetDrawtoField(DRAW_BACKBUFFER);
483 XCopyArea(display,pix[PIX_BACK],backbuffer,gc,
484 0,0, WIN_XSIZE,WIN_YSIZE, 0,0);
485 XFillRectangle(display,pix[PIX_DB_BACK],gc,
486 REAL_SX,REAL_SY, FULL_SXSIZE,FULL_SYSIZE);
487 XFillRectangle(display,pix[PIX_DB_DOOR],gc,
488 0,0, 3*DXSIZE,DYSIZE+VYSIZE);
490 for(i=0; i<MAX_BUF_XSIZE; i++)
491 for(j=0; j<MAX_BUF_YSIZE; j++)
494 redraw_mask = REDRAW_ALL;
497 void LoadGfx(int pos, struct PictureFileInfo *pic)
499 char basefilename[256];
502 #ifdef XPM_INCLUDE_FILE
503 int xpm_err, xbm_err;
504 unsigned int width,height;
508 char *picture_ext = ".gif";
510 char *picture_ext = ".xpm";
512 char *picturemask_ext = "Mask.xbm";
514 int gif_err, ilbm_err;
515 char *picture_ext = ".gif";
516 char *picturemask_ext = "Mask.ilbm";
524 if (pic->picture_filename)
531 sprintf(basefilename,"%s%s",pic->picture_filename,".gif");
532 DrawInitText(basefilename,150,FC_YELLOW);
533 sprintf(filename,"%s/%s",GFX_PATH,basefilename);
539 Read_GIF_to_Pixmaps(display, window, filename);
543 printf("GIF LOADING %s IN %.2f SECONDS\n",
544 filename,(float)(count2-count1)/100.0);
552 sprintf(basefilename,"%s%s",pic->picture_filename,picture_ext);
553 DrawInitText(basefilename,150,FC_YELLOW);
557 sprintf(filename,"%s/%s",GFX_PATH,basefilename);
563 #ifdef XPM_INCLUDE_FILE
565 xpm_att[pos].valuemask = XpmCloseness;
566 xpm_att[pos].closeness = 65535;
569 xpm_att[pos].closeness = 20000;
573 xpm_err = XpmReadFileToPixmap(display,window,filename,
574 &pix[pos],&shapemask,&xpm_att[pos]);
576 xpm_err = XpmSuccess;
582 fprintf(stderr,"Cannot open Xpm file '%s' !\n",filename);
586 fprintf(stderr,"Invalid Xpm file '%s'!\n",filename);
590 fprintf(stderr,"Not enough memory for Xpm file '%s'!\n",filename);
594 fprintf(stderr,"Can't get colors for Xpm file '%s'!\n",filename);
603 gif_err = Read_GIF_to_Pixmap(display,filename,&pix[pos]);
610 fprintf(stderr,"Cannot open GIF file '%s' !\n",filename);
614 fprintf(stderr,"Cannot read GIF file '%s' !\n",filename);
617 case GIF_FileInvalid:
618 fprintf(stderr,"Invalid GIF file '%s'!\n",filename);
622 fprintf(stderr,"Not enough memory for GIF file '%s'!\n",filename);
625 case GIF_ColorFailed:
626 fprintf(stderr,"Can't get colors for GIF file '%s'!\n",filename);
638 printf("XPM LOADING %s IN %.2f SECONDS\n",
639 filename,(float)(count2-count1)/100.0);
646 fprintf(stderr, "%s: cannot read graphics file '%s'.\n",
655 /* zugehörige Maske laden (wenn vorhanden) */
656 if (pic->picture_with_mask)
659 xbm_err = BitmapSuccess;
660 clipmask[pos] = DUMMY_MASK;
663 sprintf(basefilename,"%s%s",pic->picture_filename,picturemask_ext);
664 DrawInitText(basefilename,150,FC_YELLOW);
665 sprintf(filename,"%s/%s",GFX_PATH,basefilename);
672 #ifdef XPM_INCLUDE_FILE
675 xbm_err = XReadBitmapFile(display,window,filename,
676 &width,&height,&clipmask[pos],&hot_x,&hot_y);
678 xbm_err = BitmapSuccess;
688 case BitmapOpenFailed:
689 fprintf(stderr,"Bitmap file open failed on '%s' !\n",filename);
693 case BitmapFileInvalid:
694 fprintf(stderr,"Bitmap file invalid: '%s' !\n",filename);
699 fprintf(stderr,"No memory for file '%s' !\n",filename);
709 ilbm_err = Read_ILBM_to_Bitmap(display,filename,&clipmask[pos]);
715 case ILBM_OpenFailed:
716 fprintf(stderr,"Cannot open ILBM file '%s' !\n",filename);
719 case ILBM_ReadFailed:
720 fprintf(stderr,"Cannot read ILBM file '%s' !\n",filename);
723 case ILBM_FileInvalid:
724 fprintf(stderr,"Invalid ILBM file '%s'!\n",filename);
728 fprintf(stderr,"Not enough memory for ILBM file '%s'!\n",filename);
740 printf("LOADING %s IN %.2f SECONDS\n",
741 filename,(float)(count2-count1)/100.0);
748 fprintf(stderr, "%s: cannot read graphics file '%s'.\n",
757 pix[pos] = test_pix[test_picture_count-1];
758 clipmask[pos] = test_clipmask[test_picture_count-1];
761 void InitElementProperties()
765 static int ep_amoebalive[] =
772 static int ep_amoebalive_num = sizeof(ep_amoebalive)/sizeof(int);
774 static int ep_amoeboid[] =
782 static int ep_amoeboid_num = sizeof(ep_amoeboid)/sizeof(int);
784 static int ep_schluessel[] =
791 static int ep_schluessel_num = sizeof(ep_schluessel)/sizeof(int);
793 static int ep_pforte[] =
804 static int ep_pforte_num = sizeof(ep_pforte)/sizeof(int);
806 static int ep_solid[] =
836 static int ep_solid_num = sizeof(ep_solid)/sizeof(int);
838 static int ep_massiv[] =
856 static int ep_massiv_num = sizeof(ep_massiv)/sizeof(int);
858 static int ep_slippery[] =
880 static int ep_slippery_num = sizeof(ep_slippery)/sizeof(int);
882 static int ep_enemy[] =
893 static int ep_enemy_num = sizeof(ep_enemy)/sizeof(int);
895 static int ep_mauer[] =
914 static int ep_mauer_num = sizeof(ep_mauer)/sizeof(int);
916 static int ep_can_fall[] =
934 static int ep_can_fall_num = sizeof(ep_can_fall)/sizeof(int);
936 static int ep_can_smash[] =
955 static int ep_can_smash_num = sizeof(ep_can_smash)/sizeof(int);
957 static int ep_can_change[] =
967 static int ep_can_change_num = sizeof(ep_can_change)/sizeof(int);
969 static int ep_can_move[] =
985 static int ep_can_move_num = sizeof(ep_can_move)/sizeof(int);
987 static int ep_could_move[] =
1010 static int ep_could_move_num = sizeof(ep_could_move)/sizeof(int);
1012 static int ep_dont_touch[] =
1019 static int ep_dont_touch_num = sizeof(ep_dont_touch)/sizeof(int);
1021 static int ep_dont_go_to[] =
1034 static int ep_dont_go_to_num = sizeof(ep_dont_go_to)/sizeof(int);
1036 static int ep_mampf2[] =
1059 static int ep_mampf2_num = sizeof(ep_mampf2)/sizeof(int);
1061 static int ep_bd_element[] =
1086 static int ep_bd_element_num = sizeof(ep_bd_element)/sizeof(int);
1088 static int ep_sb_element[] =
1093 EL_SOKOBAN_FELD_LEER,
1094 EL_SOKOBAN_FELD_VOLL,
1097 static int ep_sb_element_num = sizeof(ep_sb_element)/sizeof(int);
1099 static int ep_gem[] =
1108 static int ep_gem_num = sizeof(ep_gem)/sizeof(int);
1110 static int ep_inactive[] =
1146 EL_SOKOBAN_FELD_LEER,
1147 EL_SOKOBAN_FELD_VOLL,
1160 static int ep_inactive_num = sizeof(ep_inactive)/sizeof(int);
1162 static int ep_explosive[] =
1178 static int ep_explosive_num = sizeof(ep_explosive)/sizeof(int);
1180 static int ep_mampf3[] =
1189 static int ep_mampf3_num = sizeof(ep_mampf3)/sizeof(int);
1191 static int ep_pushable[] =
1197 EL_SOKOBAN_FELD_VOLL,
1201 static int ep_pushable_num = sizeof(ep_pushable)/sizeof(int);
1203 static long ep_bit[] =
1230 static int *ep_array[] =
1257 static int *ep_num[] =
1284 static int num_properties = sizeof(ep_num)/sizeof(int *);
1286 for(i=0;i<MAX_ELEMENTS;i++)
1287 Elementeigenschaften[i] = 0;
1289 for(i=0;i<num_properties;i++)
1290 for(j=0;j<*(ep_num[i]);j++)
1291 Elementeigenschaften[(ep_array[i])[j]] |= ep_bit[i];
1292 for(i=EL_CHAR_START;i<EL_CHAR_END;i++)
1293 Elementeigenschaften[i] |= (EP_BIT_CHAR | EP_BIT_INACTIVE);
1300 if (sound_process_id)
1303 kill(sound_process_id, SIGTERM);
1304 FreeSounds(NUM_SOUNDS);
1307 for(i=0;i<NUM_PIXMAPS;i++)
1311 #ifdef XPM_INCLUDE_FILE
1312 if (i<NUM_PICTURES) /* XPM pictures */
1314 XFreeColors(display,DefaultColormap(display,screen),
1315 xpm_att[i].pixels,xpm_att[i].npixels,0);
1316 XpmFreeAttributes(&xpm_att[i]);
1319 XFreePixmap(display,pix[i]);
1322 XFreePixmap(display,clipmask[i]);
1324 XFreeGC(display, clip_gc[i]);
1328 XFreeGC(display, gc);
1330 XCloseDisplay(display);