1 /*********************************************************
2 
3   X De Block
4 
5        main.c file
6 
7   *********************************************************/
8 
9 #include "gameheader.h"
10 
11 #include "xpm/playB.xpm"
12 #include "xpm/contB.xpm"
13 #include "xpm/mapB.xpm"
14 #include "xpm/nameB.xpm"
15 #include "xpm/scoreB.xpm"
16 #include "xpm/quitB.xpm"
17 #include "xpm/aboutB.xpm"
18 
19 #include "xpm/back3.xpm"
20 
21 #include "xpm/x.xpm"
22 #include "xpm/x.xbm"
23 #include "xpm/d.xpm"
24 #include "xpm/d.xbm"
25 #include "xpm/e.xpm"
26 #include "xpm/e.xbm"
27 #include "xpm/b.xpm"
28 #include "xpm/b.xbm"
29 #include "xpm/l.xpm"
30 #include "xpm/l.xbm"
31 #include "xpm/o.xpm"
32 #include "xpm/o.xbm"
33 #include "xpm/c.xpm"
34 #include "xpm/c.xbm"
35 #include "xpm/k.xpm"
36 #include "xpm/k.xbm"
37 
38 Display     *display;                   /* global display      */
39 Window      mWin;                       /* global main window  */
40 GC          gc;                         /* global gc           */
41 Pixmap      offPixmap,backPicPixmap;    /* global offscreen pixmaps */
42 XFontStruct *xfont;                     /* global font         */
43 XFontStruct *minifont;                  /* global mini font    */
44 Colormap    cmap;                       /* global cmap         */
45 
46 
47 XColor      white,black,blue,yellow,red,green,skyblue,rgb;
48 Pixmap      xpms,xpmMs;
49 
50 allDataRec  nowDataRec;
51 mapFilePtr  nowMapFile;
52 flgs        gameMainFlg;
53 
54 
checkOption(argc,argv)55 int checkOption(argc,argv)
56      int  argc;
57      char *argv[];
58 {
59   register i,j;
60   int      flg = False;
61   char     temp[256];
62 
63   void     initGame();
64   void     freeGame();
65 
66   /***  set s_map  ***/
67   gameMainFlg.s_map = False;
68   nowDataRec.nowMap.gameSpeed = 90;
69   strcpy(nowDataRec.nowMap.dirname, "/usr/local/lib/X11/xdeblock/mapf");
70 
71   for(i =1; i  < argc; i++)
72     {
73       if(!strcmp(argv[i], "-d"))
74 	{
75 	  i++;
76 	  strcpy(temp,argv[i]);
77 	  strcat(temp,":0.0");
78 	  if((display = XOpenDisplay(temp))==NULL)
79 	    {fprintf(stderr,"XDeBlock: cannot open display[%s]\n",temp);
80 	     exit(1);}
81 	  flg = True;
82 	}
83       else if(!strcmp(argv[i],"-speed"))
84 	{
85 	  i++;
86 	  nowDataRec.nowMap.gameSpeed = atoi(argv[i]);
87 	}
88       else if(!strcmp(argv[i],"-mdir"))
89 	{
90 	  i++;
91 	  strcpy(nowDataRec.nowMap.dirname,argv[i]);
92 	}
93       else if(!((strcmp(argv[i],"-help"))&&(strcmp(argv[i],"-h"))
94 	      &&(strcmp(argv[i],"-?"))))
95 	{
96 	  i++;
97 	  printf("Usage: %s [option] [mapfile]\n",argv[0]);
98 	  printf("Option:\n\t-h ... help\n\t-d [display] ... display name\n");
99 	  printf("\t-speed [speed] ... game speed <0(fast) to 100(slow)>\n");
100 	  printf("\t-mdir [mapfile directry] ... input mapfile directory\n");
101 	  printf("\t-init [mapfile(binary)] ... initialize data in mapfile\n");
102 	  printf("\t-txt2bin [mapfile(text)] ... convert text to binary\n");
103 	  printf("\t-bin2txt [mapfile(binary)] ... convert binary to text\n");
104 	  printf("\nex: %s -mdir ./mapf -speed 3 standard.mapf &\n",argv[0]);
105 
106 	  exit(0);
107 	}
108       else if(!strcmp(argv[i],"-init"))
109 	{
110 	  i++;
111 	  initGame();
112 	  strcpy(nowDataRec.nowMap.filename,argv[i]);
113 	  if(!loadMapData(False))
114 	    {
115 	      fprintf(stderr,"XDeBlock:cannot open file [%s]\n",argv[i]);
116 	      exit(0);
117 	    }
118 	  clearHiScore();
119 	  freeGame();
120 	  exit(0);
121 	}
122       else if(!strcmp(argv[i],"-bin2txt"))
123 	{
124 	  i++;
125 	  initGame();
126 	  strcpy(nowDataRec.nowMap.filename,argv[i]);
127 	  if(!loadMapData(False))
128 	    {
129 	      fprintf(stderr,"XDeBlock: cannot open file [%s]\n",argv[i]);
130 	      exit(0);
131 	    }
132 
133 	  saveMapText();
134 	  freeGame();
135 	  exit(0);
136 	}
137       else if(!strcmp(argv[i],"-txt2bin"))
138 	{
139 	  i++;
140 	  initGame();
141 	  strcpy(nowDataRec.nowMap.filename,argv[i]);
142 	  if(!loadMapData(False))
143 	    {
144 	      fprintf(stderr,"XDeBlock: cannot open file [%s]\n",argv[i]);
145 	      exit(0);
146 	    }
147 
148 	  fileUpData();
149 	  freeGame();
150 	  exit(0);
151 	}
152       else if(!strcmp(argv[i],"-stg"))
153 	{
154 	  i++;
155 	  nowDataRec.nowMap.s_map = atoi(argv[i]);
156 	  gameMainFlg.s_map = True;
157 	}
158       else if(strstr(argv[i],".mapf")||strstr(argv[i],".mapf.txt"))
159 	{
160 	  strcpy(nowDataRec.nowMap.filename,argv[i]);
161 	  gameMainFlg.use_argv = True;
162 #ifdef DEBUG
163 	  printf("line : filename = %s\n",nowDataRec.nowMap.filename);
164 #endif
165 	}
166       else
167 	{
168 	  printf("XDeBlock: Bad Option !\n");
169 	  printf("Usage: %s [option] [mapfile]\n",argv[0]);
170 	  printf("Option:\n\t-h ... help\n\t-d [display] ... display name\n");
171 	  printf("\t-speed [speed] ... game speed <0(fast) to 100(slow)>\n");
172 	  printf("\t-mdir [mapfile directry] ... input mapfile directory\n");
173 	  printf("\t-init [mapfile(binary)] ... initialize data in mapfile\n");
174 	  printf("\t-txt2bin [mapfile(text)] ... convert text to binary\n");
175 	  printf("\t-bin2txt [mapfile(binary)] ... convert binary to text\n");
176 	  printf("\nex: %s -mdir ./mapf -speed 3 standard.mapf &\n",argv[0]);
177 
178 	  exit(0);
179 	}
180     }
181 
182   if(!flg)
183       display = XOpenDisplay(NULL);
184 
185 }
186 
187 
initResources()188 void initResources()
189 {
190   XpmAttributes   attributes;
191   int             err;
192   int             screen_num;
193   XVisualInfo     v_info;
194   XSizeHints      hints;
195   int             useDefaultColourmap;
196 
197   /**** create main window ****/
198 
199   mWin = XCreateSimpleWindow(display,DefaultRootWindow(display),
200 			    0,0,dWinWidth,dWinHeight,2,
201 			    BlackPixel(display,DefaultScreen(display)),
202 			    WhitePixel(display,DefaultScreen(display)));
203   XSelectInput(display,mWin,
204 	       KeyPressMask | KeyReleaseMask | ButtonPressMask |
205 	       ButtonReleaseMask | ExposureMask | ButtonMotionMask);
206 
207   hints.flags      = PMinSize | PMaxSize;
208   hints.min_width  = dWinWidth;
209   hints.min_height = dWinHeight;
210   hints.max_width  = dWinWidth;
211   hints.max_height = dWinHeight;
212 
213   XSetNormalHints(display,mWin,&hints);
214 
215   XStoreName(display,mWin,"X De Block");
216   XMapWindow(display,mWin);
217 
218 
219   /**** create off-screen pixmap ****/
220 
221   offPixmap = XCreatePixmap(display,mWin,dWinWidth,dWinHeight,
222 			    DefaultDepth(display,DefaultScreen(display)));
223 
224   /**** wall paper screen pixmap ****/
225 
226   backPicPixmap = XCreatePixmap(display,mWin,dBackPmWidth,dBackPmHeight,
227 				DefaultDepth(display,DefaultScreen(display)));
228 
229 
230   /**** create global gc ****/
231 
232   gc = XCreateGC(display,mWin, 0,0);
233 
234   XSetForeground(display,gc,WhitePixel(display,0));
235   XFillRectangle(display,offPixmap,gc,0,0,dWinWidth,dWinHeight);
236 
237   XSetForeground(display,gc,BlackPixel(display,0));
238   XSetBackground(display,gc,WhitePixel(display,0));
239 
240   /**** set global font ****/
241 
242   xfont = XLoadQueryFont(display,dDefaultFont);
243   XSetFont(display,gc,xfont->fid);
244 
245   minifont = XLoadQueryFont(display,dMiniFont);
246 
247 
248   /**** set global colormap ****/
249 
250   screen_num = DefaultScreen(display);
251 
252   if (!XMatchVisualInfo(display,screen_num,DefaultDepth(display,screen_num),
253 			PseudoColor,&v_info))
254     if (!XMatchVisualInfo(display,screen_num,
255 			  DefaultDepth(display,screen_num),
256 			  DirectColor,&v_info))
257       if (!XMatchVisualInfo(display,screen_num,
258 			    DefaultDepth(display,screen_num),
259 			    TrueColor,&v_info))
260 	{
261 	  fprintf(stderr,"Sorry, you must have a colour display.");
262 	  exit(1);
263 	}
264 
265 #ifdef DEBUG
266   printf("XDeBlock: depth = %d,cmap_size = %d\n",
267 	 v_info.depth,v_info.colormap_size);
268 #endif
269 
270   cmap = XCreateColormap(display,mWin,DefaultVisual(display,screen_num),
271 			 AllocNone);
272   XInstallColormap(display,cmap);
273   XSetWindowColormap(display,mWin,cmap);
274 
275   XAllocNamedColor(display,cmap,"black",&black,&rgb);
276   XAllocNamedColor(display,cmap,"white",&white,&rgb);
277   XAllocNamedColor(display,cmap,"blue",&blue,&rgb);
278   XAllocNamedColor(display,cmap,"yellow",&yellow,&rgb);
279   XAllocNamedColor(display,cmap,"green",&green,&rgb);
280   XAllocNamedColor(display,cmap,"red",&red,&rgb);
281   XAllocNamedColor(display,cmap,"skyblue",&skyblue,&rgb);
282 }
283 
initGameMainFlg(void)284 void initGameMainFlg(void)
285 {
286   gameMainFlg.life      = True;		/**	can play game flg	****/
287   gameMainFlg.openMap   = False;        /**     map file open flg	****/
288   gameMainFlg.reDraw    = True;		/**		???		  **/
289   gameMainFlg.hidePen   = True;		/**	cursor view mode 	****/
290   gameMainFlg.saveFlg   = False;	/**     score .etc upData flg     **/
291   gameMainFlg.inputName = False;	/**	player name input ?	****/
292 /*  gameMainFlg.useSound  = False;              play Game Sound	****/
293   gameMainFlg.reDraw2   = True;		/**	osEvt cursor flg	****/
294   gameMainFlg.errPatch1 = True;	        /**	active window flg	****/
295   gameMainFlg.useEffect = False;	/**	use many kind effect     ***/
296   gameMainFlg.useWinOut = True;	        /** use wait cursor out of Window **/
297   gameMainFlg.demoFlg   = False;        /**     show game Demo !!	****/
298   gameMainFlg.bloDlg    = False;       	/**	use block edit mode	****/
299   gameMainFlg.getMode   = False;        /**     use title button	****/
300 
301 }
302 
303 
initGame(void)304 void initGame(void)
305 {
306   nowMapFile = (mapFilePtr)malloc(sizeof(mapFileRec));
307 
308   nowDataRec.nowMap.nowTime.tv_sec = 0;	/** use Titel loop and Game loop */
309   nowDataRec.nowMap.nowTime.tv_usec= 0;	/** use Titel loop and Game loop */
310   nowDataRec.nowMap.itemNum   = 0;	/**    use Title item domo      **/
311   nowDataRec.nowMap.prevScore = 0;	/**    demo insted of use	**/
312   nowDataRec.nowMap.continueCount = -1;	/**	set continueCount	**/
313   nowDataRec.nowMap.gameCount = 0;  	/**	use Title button	**/
314 
315   strcpy(nowDataRec.nowMap.playerName,getenv("USER"));
316 
317 }
318 
freeGame(void)319 void freeGame(void)
320 {
321   free(nowMapFile);
322 }
323 
setDemo(void)324 void setDemo(void)
325 {
326   register    i,num;
327   XRectangle  box;
328   char	      saveFlg1;
329   char        *text[20]={"Score +100","Score +200","Score +500",
330 			   "Score -100","Score -200","Score -400",
331 			   "Bar +","Bar -","Bigger Ball",
332 			   "Smaller Ball","Ball 1UP","Ball 1DOWN",
333 			   "Ball x2","Reverse","Ball Speed UP",
334 			   "Ball Speed DOWN","Wall Through","Weight Ball"};
335 
336   if(nowDataRec.nowMap.prevScore == 0)
337     ;
338   else if(nowDataRec.nowMap.prevScore <= 3)
339     {
340       XSetForeground(display,gc,blue.pixel);
341       XDrawRectangle(display,mWin,gc,240,120,330,300);
342 
343       drawShadowText(display,mWin,300,95,
344 		     "****  items  ****",blue.pixel);
345 
346       for(i^=i; i < dItemMax; i++)
347 	{
348 	  num = i+1+(nowDataRec.nowMap.prevScore-1)*6;
349 	  setRect(&box,250,130+(45*i),24,24);
350 	  serveItem(&box,num);
351 
352           drawShadowText(display,mWin,310,145+(45*i),
353 		         text[num-1],blue.pixel);
354 	}
355     }
356   else
357     {
358       saveFlg1 = gameMainFlg.inputName;
359       gameMainFlg.inputName = True;
360       gameMain();
361       gameMainFlg.inputName = saveFlg1;
362       nowDataRec.nowMap.prevScore = 0;
363       gameMainFlg.demoFlg = False;
364     }
365 }
366 
drawTitle(void)367 void drawTitle(void)
368 {
369   register        i;
370   XpmAttributes   attributes;
371   static Pixmap   tempXpm,tempXpmMs,tPix;
372   int             err,pic_x,pic_y,x,y;
373   long            color;
374 
375 
376   /***  set attributes  ***/
377   attributes.valuemask = XpmColormap;
378   attributes.colormap = cmap;
379 
380   /***  set window background   ***/
381 
382   err = XpmCreatePixmapFromData(display,mWin,back3_xpm,
383 				&tempXpm,&tempXpmMs,&attributes);
384   handleXPMError(err,"title back");
385 
386   XSetWindowBackgroundPixmap(display,mWin,tempXpm);
387   XFreePixmap(display,tempXpm);
388 
389   putBackPattarn(offPixmap,dWinWidth,dWinHeight,303);
390 
391   /***  draw Title      ***/
392 
393   for(i^=i; i< 8;i++)
394     {
395       switch(i)
396 	{
397 	case 0:
398 	  sscanf(x_xpm[0],"%d%d",&pic_x,&pic_y);
399 	  err = XpmCreatePixmapFromData(display,mWin,x_xpm,
400 					&tempXpm,&tempXpmMs,&attributes);
401 	  tPix = XCreateBitmapFromData(display,mWin,x_bits,x_width,x_height);
402 	  x = 180; y = 70; color = blue.pixel;
403 	  break;
404 	case 1:
405 	  sscanf(d_xpm[0],"%d%d",&pic_x,&pic_y);
406 	  err = XpmCreatePixmapFromData(display,mWin,d_xpm,
407 					&tempXpm,&tempXpmMs,&attributes);
408 	  tPix = XCreateBitmapFromData(display,mWin,d_bits,d_width,d_height);
409 	  x = 360; y = 140; color = yellow.pixel;
410 	  break;
411 	case 2:
412 	  sscanf(e_xpm[0],"%d%d",&pic_x,&pic_y);
413 	  err = XpmCreatePixmapFromData(display,mWin,e_xpm,
414 					&tempXpm,&tempXpmMs,&attributes);
415 	  tPix = XCreateBitmapFromData(display,mWin,e_bits,e_width,e_height);
416 	  x = 450; y = 160; color = white.pixel;
417 	  break;
418 	case 3:
419 	  sscanf(b_xpm[0],"%d%d",&pic_x,&pic_y);
420 	  err = XpmCreatePixmapFromData(display,mWin,b_xpm,
421 					&tempXpm,&tempXpmMs,&attributes);
422 	  tPix = XCreateBitmapFromData(display,mWin,b_bits,b_width,b_height);
423 	  x = 220; y = 270; color = red.pixel;
424 	  break;
425 	case 4:
426 	  sscanf(l_xpm[0],"%d%d",&pic_x,&pic_y);
427 	  err = XpmCreatePixmapFromData(display,mWin,l_xpm,
428 					&tempXpm,&tempXpmMs,&attributes);
429 	  tPix = XCreateBitmapFromData(display,mWin,l_bits,l_width,l_height);
430 	  x = 300; y = 270; color = green.pixel;
431 	  break;
432 	case 5:
433 	  sscanf(o_xpm[0],"%d%d",&pic_x,&pic_y);
434 	  err = XpmCreatePixmapFromData(display,mWin,o_xpm,
435 					&tempXpm,&tempXpmMs,&attributes);
436 	  tPix = XCreateBitmapFromData(display,mWin,o_bits,o_width,o_height);
437 	  x = 355; y = 290; color = blue.pixel;
438 	  break;
439 	case 6:
440 	  sscanf(c_xpm[0],"%d%d",&pic_x,&pic_y);
441 	  err = XpmCreatePixmapFromData(display,mWin,c_xpm,
442 					&tempXpm,&tempXpmMs,&attributes);
443 	  tPix = XCreateBitmapFromData(display,mWin,c_bits,c_width,c_height);
444 	  x = 420; y = 290; color = yellow.pixel;
445 	  break;
446 	case 7:
447 	  sscanf(k_xpm[0],"%d%d",&pic_x,&pic_y);
448 	  err = XpmCreatePixmapFromData(display,mWin,k_xpm,
449 					&tempXpm,&tempXpmMs,&attributes);
450 	  tPix = XCreateBitmapFromData(display,mWin,k_bits,k_width,k_height);
451 	  x = 480; y = 270; color = skyblue.pixel;
452 	  break;
453 
454 	}
455       drawWithMask(display,offPixmap,tempXpm,tempXpmMs,x+10,y+10,pic_x,pic_y);
456 
457       XSetForeground(display,gc,color);
458       XCopyPlane(display,tPix,tempXpm,gc,0,0,pic_x,pic_y,0,0,1);
459       drawWithMask(display,offPixmap,tempXpm,tempXpmMs,x,y,pic_x,pic_y);
460 
461       XFreePixmap(display,tempXpm);
462       XFreePixmap(display,tPix);
463     }
464 
465   XSetForeground(display,gc,BlackPixel(display,0));
466   XSetBackground(display,gc,WhitePixel(display,0));
467 
468   /***  title texts    ***/
469 
470   drawMiniText(display,offPixmap,170,360,
471 	       "XDeBlock ver.1.0 (C)CopyRight 1996 Takuto Okuno",
472 	       yellow.pixel,True);
473   drawShadowText(display,offPixmap,170,420,
474 		 "Map Select:",green.pixel);
475 
476   /***  title button    ***/
477   for(i^=i; i < 7; i++)
478     {
479       switch(i)
480 	{
481 	case 0:
482 	  err = XpmCreatePixmapFromData(display,mWin,playB_xpm,
483 					&tempXpm,&tempXpmMs,&attributes);
484 	  handleXPMError(err,"play button");
485 	  break;
486 	case 1:
487 	  err = XpmCreatePixmapFromData(display,mWin,contB_xpm,
488 					&tempXpm,&tempXpmMs,&attributes);
489 	  handleXPMError(err,"cont button");
490 	  break;
491 	case 2:
492 	  err = XpmCreatePixmapFromData(display,mWin,mapB_xpm,
493 					&tempXpm,&tempXpmMs,&attributes);
494 	  handleXPMError(err,"map button");
495 	  break;
496 	case 3:
497 	  err = XpmCreatePixmapFromData(display,mWin,nameB_xpm,
498 					&tempXpm,&tempXpmMs,&attributes);
499 	  handleXPMError(err,"name button");
500 	  break;
501 	case 4:
502 	  err = XpmCreatePixmapFromData(display,mWin,scoreB_xpm,
503 					&tempXpm,&tempXpmMs,&attributes);
504 	  handleXPMError(err,"score button");
505 	  break;
506 	case 5:
507 	  err = XpmCreatePixmapFromData(display,mWin,quitB_xpm,
508 					&tempXpm,&tempXpmMs,&attributes);
509 	  handleXPMError(err,"quit button");
510 	  break;
511 	case 6:
512 	  err = XpmCreatePixmapFromData(display,mWin,aboutB_xpm,
513 					&tempXpm,&tempXpmMs,&attributes);
514 	  handleXPMError(err,"quit button");
515 	  break;
516 	}
517 
518       drawWithMask(display,offPixmap,tempXpm,tempXpmMs,54+105*i,480,96,48);
519 
520       XFreePixmap(display,tempXpm);
521     }
522 
523   XCopyArea(display,offPixmap,mWin,gc,0,0,dWinWidth,dWinHeight,0,0);
524   XpmFreeAttributes(&attributes);
525 }
526 
titleLoop(void)527 void titleLoop(void)
528 {
529   register  i,iNum;
530 
531   if(!gameMainFlg.demoFlg)
532     {
533       if(nowDataRec.nowMap.itemNum)
534 	for(i = nowDataRec.nowMap.itemNum -1; i >= 0; i--)
535 	  {
536 	    iNum = nowDataRec.nowMap.mvItemBox[i];
537 	    disposeItem(iNum,i);
538 	  }
539 
540       drawTitle();
541 
542       gameMainFlg.demoFlg = True;
543 
544       nowDataRec.nowMap.nowTime.tv_sec  = 0;
545       nowDataRec.nowMap.nowTime.tv_usec = 0;
546       gettimeofday(&nowDataRec.nowMap.prevTime,NULL);
547 
548       setDemo();
549     }
550 
551   for(i ^=i; i < nowDataRec.nowMap.itemNum;i++)
552     {
553       iNum = nowDataRec.nowMap.mvItemBox[i];
554       nowDataRec.nowItem[iNum].prevRect = nowDataRec.nowItem[iNum].nowRect;
555       drawItem(iNum);
556     }
557 
558   gameSleep(display,10);
559   countTime();
560 
561   if(nowDataRec.nowMap.nowTime.tv_sec > 5)
562     {
563       nowDataRec.nowMap.nowTime.tv_sec  = 0;
564       nowDataRec.nowMap.nowTime.tv_usec = 0;
565       gettimeofday(&nowDataRec.nowMap.prevTime,NULL);
566       nowDataRec.nowMap.prevScore ++;
567       gameMainFlg.demoFlg = False;
568     }
569 
570 }
571 
checkTitleButton(num)572 int checkTitleButton(num)
573      int num;
574 {
575   switch(num)
576     {
577     case 0:		/****	game start     	****/
578       gameMainFlg.demoFlg             = False;
579       gameMainFlg.useContinue         = False;
580       nowDataRec.nowMap.continueCount = 1;
581 
582       gameMain();
583       drawTitle();
584       break;
585     case 1:		/****	game continue	****/
586       if(nowDataRec.nowMap.continueCount < 0)
587 	return;
588       gameMainFlg.demoFlg             = False;
589       gameMainFlg.useContinue         = True;
590       nowDataRec.nowMap.continueCount --;
591 
592       gameMain();
593       drawTitle();
594       break;
595 
596     case 2:
597       loadMapData(True);
598       break;
599 
600     case 3:
601       nameInputDLG();
602       break;
603 
604     case 4:
605       if(!gameMainFlg.openMap)
606 	break;
607       gameMainFlg.demoFlg = False;
608       hiScoreDLG();
609       break;
610 
611     case 5:
612     /*** quit game ***/
613       return(True);
614 
615     case 6:
616       aboutDLG();
617       break;
618 
619     }
620 
621   nowDataRec.nowMap.prevScore = 0;
622   gameMainFlg.demoFlg = False;
623 
624   return(False);
625 }
626 
627 
handleTitleButton(x,y,updown)628 int handleTitleButton(x,y,updown)
629      short x,y;
630      int   updown;
631 {
632   register	i;
633   XRectangle	buttonRect;
634   int		flg = False;
635 
636   for(i^=i; i < 7; i++)
637     {
638       setRect(&buttonRect,54 +105*i,480,96,48);
639       if(ptInRect(&buttonRect,x,y))
640 	{
641 	  flg                         = True;
642 	  gameMainFlg.demoFlg         = False;
643 	  nowDataRec.nowMap.prevScore = 0;
644 
645 	  if(!gameMainFlg.getMode)
646 	    {
647 	      XClearArea(display,mWin,54+105*i,480,96,48,False);
648 	      XCopyArea(display,offPixmap,mWin,gc,54+105*i,480,96,48,
649 			57+105*i,483);
650 	      gameMainFlg.getMode = True;
651 	      nowDataRec.nowBar.nowAct = i;
652 	    }
653 	}
654     }
655 
656   if(gameMainFlg.getMode)
657     if(flg)
658       {
659 	if(updown)
660 	  {
661 	    gameMainFlg.getMode = False;
662 	    XClearArea(display,mWin,57+105*nowDataRec.nowBar.nowAct,
663 		       483,96,48,False);
664 	    setRect(&buttonRect,54+105*nowDataRec.nowBar.nowAct,480,96,48);
665 	    drawBack_Off(&buttonRect);
666 	    return(checkTitleButton(nowDataRec.nowBar.nowAct));
667 	  }
668       }
669     else
670       {
671 	gameMainFlg.getMode = False;
672 	XClearArea(display,mWin,57+105*nowDataRec.nowBar.nowAct,
673 		   483,96,48,False);
674 	setRect(&buttonRect,54+105*nowDataRec.nowBar.nowAct,480,96,48);
675 	drawBack_Off(&buttonRect);
676       }
677 
678   return(False);
679 }
680 
handleTitleMouseRelease(event)681 int handleTitleMouseRelease(event)
682      XButtonEvent *event;
683 {
684 
685   switch(event->button)
686     {
687     case Button1:
688       return(handleTitleButton(event->x,event->y,True));
689       break;
690 
691     case Button2:
692       return(handleTitleButton(event->x,event->y,True));
693       break;
694 
695     case Button3:
696       break;
697     }
698 
699   return(False);
700 
701 }
702 
handleTitleMouseMotion(event)703 void handleTitleMouseMotion(event)
704      XPointerMovedEvent *event;
705 {
706 
707   handleTitleButton(event->x,event->y,False);
708 
709 }
710 
handleTitleMouseDown(event)711 void handleTitleMouseDown(event)
712      XButtonEvent *event;
713 {
714 
715   switch(event->button)
716     {
717     case Button1:
718       handleTitleButton(event->x,event->y,False);
719       break;
720 
721     case Button2:
722       handleTitleButton(event->x,event->y,False);
723       break;
724 
725     case Button3:
726       break;
727     }
728 }
729 
reDraw(event)730 void reDraw(event)
731      XExposeEvent *event;
732 {
733   /***    expose event ***/
734   drawBack_Offs(event->x,event->y,event->width,event->height);
735 
736 }
737 
titleText(void)738 void titleText(void)
739 {
740 
741   if(gameMainFlg.openMap)
742     drawShadowText(display,mWin,310,420,
743 		   nowDataRec.nowMap.filename,red.pixel);
744   else
745     drawShadowText(display,mWin,310,420,
746 		   "non select",red.pixel);
747 
748 }
749 
750 
handleTitleEventLoop()751 void handleTitleEventLoop()
752 {
753   XEvent    event;
754   int       flg,i,j,k,evt_count;
755   str255    nowfile;
756 
757   while(True)
758     {
759       flg = True;
760       if(gameMainFlg.use_argv)
761 	{
762 	  if(nowDataRec.nowMap.dirname[0] != 0x00)
763 	    {
764 	      strcpy(nowfile,nowDataRec.nowMap.dirname);
765 	      strcat(nowfile,"/");
766 	      strcat(nowfile,nowDataRec.nowMap.filename);
767 	      strcpy(nowDataRec.nowMap.filename,nowfile);
768 	    }
769 
770 	  if(!loadMapData(False))
771 	    {
772 	      fprintf(stderr,"XDeBlock: cannot open file [%s]\n",
773 		      nowDataRec.nowMap.filename);
774 	      exit(0);
775 	    }
776 	  gameMainFlg.openMap  = True;
777 
778 	  gameMainFlg.demoFlg             = False;
779 	  gameMainFlg.useContinue         = False;
780 	  nowDataRec.nowMap.continueCount = 1;
781 
782 	  gameMain();
783 	  gameMainFlg.use_argv = False;
784 	}
785 
786       while(flg)
787 	{
788 	  titleLoop();
789 
790 	  for(evt_count = XEventsQueued(display,QueuedAfterReading);
791 	      evt_count > 0;evt_count--)
792 	    {
793 	      XNextEvent(display,&event);
794 
795 	      switch(event.type)
796 		{
797 		case Expose:
798 #ifdef EVENT_DEBUG
799 		  printf("Title:Expose :%d\n",evt_count);
800 #endif
801 		  reDraw(&event);
802 		  titleText();
803 		  break;
804 
805 		case ButtonRelease:
806 #ifdef EVENT_DEBUG
807 		  printf("Title:ButtonRelease :%d\n",evt_count);
808 #endif
809 		  if(handleTitleMouseRelease(&event))
810 		    return;
811 
812 		  XSelectInput(display,mWin,
813 			       KeyPressMask | KeyReleaseMask |
814 			       ButtonPressMask | ButtonReleaseMask |
815 			       ExposureMask | ButtonMotionMask);
816 		  break;
817 
818 		case ButtonPress:
819 #ifdef EVENT_DEBUG
820 		  printf("Title:ButtonPress :%d\n",evt_count);
821 #endif
822 		  handleTitleMouseDown(&event);
823 		  break;
824 
825 		case MotionNotify:
826 #ifdef EVENT_DEBUG
827 		  printf("Title:MotionNotify :%d\n",evt_count);
828 #endif
829 		  handleTitleMouseMotion(&event);
830 		  titleText();
831 		  break;
832 
833 		default:
834 #ifdef EVENT_DEBUG
835 		  printf("Title:default :%d\n",evt_count);
836 #endif
837 		  break;
838 
839 		}
840 	    }
841 	}
842     }
843 }
844 
clearResources()845 void clearResources()
846 {
847   XFreeColormap(display,cmap);
848   XFreeFont(display,xfont);
849   XFreeGC(display,gc);
850   XFreePixmap(display,offPixmap);
851   XFreePixmap(display,backPicPixmap);
852   XDestroyWindow(display,mWin);
853   XCloseDisplay(display);
854 
855 }
856 
857 
main(argc,argv)858 void main(argc,argv)
859      int   argc;
860      char  *argv[];
861 {
862 /***   check option   ***/
863 
864   checkOption(argc,argv);
865 
866 /***   initialize resources   ***/
867 
868   initResources();
869   initGameMainFlg();
870   initGame();
871   initItemPix();
872   initBlockPix();
873 
874 /***   main loop   ***/
875 
876   handleTitleEventLoop();
877 
878 /***   clear resources   ***/
879 
880   freeItem();
881   freeBlock();
882   freeGame();
883   clearResources();
884 }
885 
886