1 /*
2  * main.c  - Xpipeman
3  *
4  * Send Constructive comments, bug reports, etc. to either
5  *
6  *          JANET: pavern@uk.ac.man.cs
7  *
8  *  or      INER : pavern%cs.man.ac.uk@nsfnet-relay.ac.uk
9  *
10  * All other comments > /dev/null !!
11  *
12  *
13  * Copyright 1991 Nigel Paver
14  *
15  * Permission to use, copy, modify, distribute, and sell this software and its
16  * documentation for any purpose is hereby granted without fee, provided that
17  * the above copyright notice appear in all copies and that both that
18  * copyright notice and this permission notice appear in supporting
19  * documentation, and that the author's name not be used in advertising or
20  * publicity pertaining to distribution of the software without specific,
21  * written prior permission.  The author makes no representations about the
22  * suitability of this software for any purpose.  It is provided "as is"
23  * without express or implied warranty.
24  *
25  * THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
26  * ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL THE
27  * AUTHOR BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
28  * DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
29  * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
30  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
31  *
32  *
33  *
34  * Acknowledgements to Brian Warkentine (brian@sun.COM) for his xrobots
35  * program (copyright  1989) which I cannibalized to write this program
36  */
37 
38 #include <X11/X.h>
39 #include <X11/Intrinsic.h>
40 #include <X11/StringDefs.h>
41 #include <X11/cursorfont.h>
42 #include <X11/Core.h>
43 #include <X11/Shell.h>
44 
45 #ifdef R3
46 #include <X11/Label.h>
47 #include <X11/Command.h>
48 #include <X11/Box.h>
49 #else
50 #include <X11/Xaw/Label.h>
51 #include <X11/Xaw/Command.h>
52 #include <X11/Xaw/Box.h>
53 #endif
54 
55 
56 #include <stdlib.h>
57 #include <stdio.h>
58 #include <unistd.h>
59 #include "xpipeman.h"
60 
61 /*----------------------------------------------------------------------*/
62 
63 Widget top_shell, top_widget, playfield_widget, score_command,
64        bonus_command, remain_command, level_command,info_command;
65 
66 
67 Display *display;
68 Window playfield;
69 
70 GC gc;
71 GC cleargc;
72 
73 /*----------------------------------------------------------------------*/
74 
75 static Arg arglisttop_shell[] = {
76   {  XtNinput,  (XtArgVal)True },
77 };
78 
79 static Arg arglistplayfield[] = {
80   {  XtNheight, (XtArgVal) pos_to_coord(MAXY)  },
81   {  XtNwidth,  (XtArgVal) pos_to_coord(MAXX)  },
82   {  XtNborderWidth, (XtArgVal) 4 },
83 };
84 
85 static Arg arglistquit_command[] = {
86   {  XtNlabel, (XtArgVal) "Quit"  },
87 };
88 
89 static Arg arglistnew_game[] = {
90   {  XtNlabel, (XtArgVal) "New Game"  },
91 };
92 
93 static Arg arglistscore_command[] = {
94   {  XtNlabel, (XtArgVal) "Score:    0"  },
95 };
96 
97 static Arg arglistbonus_command[] = {
98   {  XtNlabel, (XtArgVal) "Bonus:    0"  },
99 };
100 
101 static Arg arglistremain_command[] = {
102   {  XtNlabel, (XtArgVal) "Remain:    0"  },
103 };
104 
105 static Arg arglistlevel_command[] = {
106   {  XtNlabel, (XtArgVal) "Level:    0"  },
107 };
108 
109 static Arg arglistinfo_command[] = {
110   {  XtNlabel, (XtArgVal) "Info"  },
111 };
112 
113 /*----------------------------------------------------------------------*/
114 
115 static char translations_str[] =
116   "<Btn1Down>:	do_nothing()		\n\
117    <Btn2Down>:	do_nothing()		\n\
118    <Btn3Down>:	do_nothing()		\n\
119    <Btn1Up>:    move_here()		\n\
120    <Btn2Up>:    place()    		\n\
121    <Btn3Up>:    do_nothing()   		\n\
122    :<Key>x:     move(right)		\n\
123    :<Key>z:     move(left)		\n\
124    :<Key>':     move(up)		\n\
125    :<Key>/:     move(down)		\n\
126    <Key>\\ :    place()        		\n\
127    :<Key>p:     place()  		\n\
128    <Key>Tab:    fastflow() 		\n\
129    <Key>n:      new_game()		\n\
130    Ctrl<Key>c:  quit() ";
131 
132 static void quit_game();
133 
134 static XtActionsRec actions[] = {
135   {"quit",      (XtActionProc)quit_game},
136   {"move",      (XtActionProc)move_action},
137   {"move_here", (XtActionProc)move_here_action},
138   {"fastflow",   (XtActionProc)fast_flow_action},
139   {"place",     (XtActionProc)place_action},
140   {"new_game",  (XtActionProc)new_game},
141   {"do_nothing",(XtActionProc)do_nothing_action},
142 };
143 
144 AppData app_data;
145 
146 static XrmOptionDescRec options[] = {
147 	{"-scorefile","scorefile",XrmoptionSepArg, NULL },
148 };
149 
150 static XtResource application_resources[] = {
151   {"foreground", "Foreground", XtRPixel, sizeof(Pixel),
152                 XtOffsetOf(AppData, fg), XtRString, (caddr_t) "Black"},
153   {"background", "Background", XtRPixel, sizeof(Pixel),
154                 XtOffsetOf(AppData, bg), XtRString, (caddr_t) "White"},
155   {"translations","Translations", XtRTranslationTable, sizeof(XtTranslations),
156                 XtOffsetOf(AppData, translations), XtRString, (caddr_t)translations_str},
157   {"scorefile","Scorefile", XtRString, sizeof(String),
158                 XtOffsetOf(AppData, score_filename), XtRString, (caddr_t)SCORE_FILE},
159 };
160 
161 /*----------------------------------------------------------------------*/
162 
163 int
main(argc,argv)164 main(argc, argv)
165   int argc;
166   char **argv;
167 {
168   Arg args[1];
169   XGCValues gcv;
170   Widget quit_command,
171 	 new_game_command;
172 
173   srandom(getpid());
174   current_block = 0;
175   current_callback = 0;
176 
177   top_shell = XtInitialize(argv[0], "xpipeman", options, XtNumber(options), &argc, argv);
178   XtSetValues(top_shell, arglisttop_shell, XtNumber(arglisttop_shell));
179 
180   XtAddActions(actions,XtNumber(actions));
181 
182   XtGetApplicationResources(top_shell, &app_data, application_resources,
183 			XtNumber(application_resources), NULL, 0 );
184 
185   top_widget = XtCreateManagedWidget(
186                                     "top_widget",
187                                     boxWidgetClass,
188                                     top_shell,
189                                     0,0);
190 
191   playfield_widget = XtCreateManagedWidget(
192                                     "playfield",
193                                     widgetClass,
194                                     top_widget,
195                                     arglistplayfield,
196                                     XtNumber(arglistplayfield));
197 
198   XtAugmentTranslations(playfield_widget,app_data.translations);
199 
200   quit_command = XtCreateManagedWidget(
201                                     "quit_button",
202                                     commandWidgetClass,
203                                     top_widget,
204                                     arglistquit_command,
205                                     XtNumber(arglistquit_command));
206 
207   XtAddCallback(quit_command,     XtNcallback, (XtCallbackProc)quit_game, 0);
208 
209   new_game_command = XtCreateManagedWidget(
210                                     "new_game_button",
211                                     commandWidgetClass,
212                                     top_widget,
213                                     arglistnew_game,
214                                     XtNumber(arglistnew_game));
215 
216   XtAddCallback(new_game_command, XtNcallback, (XtCallbackProc)new_game, 0);
217 
218   score_command = XtCreateManagedWidget(
219                                     "score_button",
220                                     commandWidgetClass,
221                                     top_widget,
222                                     arglistscore_command,
223                                     XtNumber(arglistscore_command));
224 
225   XtAddCallback(score_command, XtNcallback, (XtCallbackProc)show_scores_callback, 0);
226   /* buttons used so that they all look the same    */
227 
228   bonus_command = XtCreateManagedWidget(
229                                     "bonus_button",
230                                     commandWidgetClass,
231                                     top_widget,
232                                     arglistbonus_command,
233                                     XtNumber(arglistbonus_command));
234 
235   remain_command = XtCreateManagedWidget(
236                                     "remain_button",
237                                     commandWidgetClass,
238                                     top_widget,
239                                     arglistremain_command,
240                                     XtNumber(arglistremain_command));
241 
242 
243   level_command = XtCreateManagedWidget(
244                                     "level_button",
245                                     commandWidgetClass,
246                                     top_widget,
247                                     arglistlevel_command,
248                                     XtNumber(arglistlevel_command));
249 
250   info_command = XtCreateManagedWidget(
251                                     "info_button",
252                                     commandWidgetClass,
253                                     top_widget,
254                                     arglistinfo_command,
255                                     XtNumber(arglistinfo_command));
256 
257   XtAddCallback(info_command, XtNcallback, (XtCallbackProc)show_info, 0);
258 
259   create_high_score_popup(top_widget);
260   create_info_popup(top_widget);
261   create_general_popups(top_widget);
262 
263   XtRealizeWidget(top_shell);
264 
265 
266   display   = XtDisplay(playfield_widget);
267   playfield = XtWindow(playfield_widget);
268   gcv.foreground = app_data.fg;
269   gcv.background = app_data.bg;
270   gcv.function = GXcopy;
271   gc = XCreateGC(display, playfield,
272  		GCForeground | GCBackground | GCFunction, &gcv);
273   gcv.foreground = app_data.bg;
274   cleargc = XCreateGC(display, playfield,
275  		 GCForeground | GCBackground | GCFunction, &gcv);
276 
277   XtAddEventHandler(playfield_widget, ExposureMask, 0, redisplay_level, 0);
278   XtAddEventHandler(playfield_widget, PointerMotionMask, 0, pointer_moved, 0);
279 
280   init_pixmaps(top_shell);
281 
282   XtMainLoop();
283 
284 }
285 
286 
287 static void
quit_game()288 quit_game()
289 {
290   free_pixmaps();
291   XtDestroyWidget(top_shell);
292   XFreeGC(display,gc);
293   XFreeGC(display,cleargc);
294 
295   exit(0);
296 }
297 
298 
299 void
update_score(score)300 update_score(score)
301   int score;
302 {
303   char text[13];
304   (void)sprintf(text,"Score: %4d",score);
305   XtSetArg(arglistscore_command[0],XtNlabel,text);
306   XtSetValues(score_command,arglistscore_command,1);
307 }
308 
309 void
update_bonus(score)310 update_bonus(score)
311   int score;
312 {
313   char text[13];
314   (void)sprintf(text,"Bonus: %4d",score);
315   XtSetArg(arglistbonus_command[0],XtNlabel,text);
316   XtSetValues(bonus_command,arglistbonus_command,1);
317 }
318 
319 void
update_remain(score)320 update_remain(score)
321   int score;
322 {
323   char text[16];
324   (void)sprintf(text,"Remaining: %4d",score);
325   XtSetArg(arglistremain_command[0],XtNlabel,text);
326   XtSetValues(remain_command,arglistremain_command,1);
327 }
328 
329 void
update_level(level)330 update_level(level)
331   int level;
332 {
333   char text[13];
334   (void)sprintf(text,"Level: %4d",level);
335   XtSetArg(arglistlevel_command[0],XtNlabel,text);
336   XtSetValues(level_command,arglistlevel_command,1);
337 }
338 
339 
340 
341