1 /*
2  * main.c  --  xrobots
3  *
4  * Send comments, bug reports, etc. to brianw@Sun.COM.
5  *
6  *
7  * Copyright 1989 Brian Warkentine
8  *
9  * Permission to use, copy, modify, distribute, and sell this software and its
10  * documentation for any purpose is hereby granted without fee, provided that
11  * the above copyright notice appear in all copies and that both that
12  * copyright notice and this permission notice appear in supporting
13  * documentation, and that the author's name not be used in advertising or
14  * publicity pertaining to distribution of the software without specific,
15  * written prior permission.  The author makes no representations about the
16  * suitability of this software for any purpose.  It is provided "as is"
17  * without express or implied warranty.
18  *
19  * THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
20  * ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL THE
21  * AUTHOR BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
22  * DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
23  * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
24  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
25  *
26  * The author's current employer has no connection with this software, as it
27  * was written before being employed at Sun.  The following address is for
28  * contacting the author only and does not imply any responsibility on the
29  * behalf of Sun Microsystems.
30  *
31  * Contact the author at:
32  * 	Brian Warkentine  (brianw@Sun.COM)
33  * 	Sun Microsystems
34  * 	2550 Garcia Avenue
35  * 	Mountain View, Ca 94043-1100
36  *
37  * HP-UX next two defines
38  */
39 
40 #include <X11/X.h>
41 #include <X11/Intrinsic.h>
42 #include <X11/StringDefs.h>
43 #include <X11/cursorfont.h>
44 #include <X11/Core.h>
45 #include <X11/Shell.h>
46 
47 #ifdef R3
48 #include <X11/Label.h>
49 #include <X11/Command.h>
50 #include <X11/Box.h>
51 #else
52 #include <X11/Xaw/Label.h>
53 #include <X11/Xaw/Command.h>
54 #include <X11/Xaw/Box.h>
55 #endif
56 
57 
58 #include <stdio.h>
59 #include <stdlib.h>
60 #include <unistd.h>
61 #include "xrobots.h"
62 
63 /*----------------------------------------------------------------------*/
64 
65 Widget top_shell, top_widget, playfield_widget, score_command, sonic_command;
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 arglistteleport_command[] = {
86   {  XtNlabel, (XtArgVal) "Teleport"  },
87 };
88 
89 static Arg arglistwait_command[] = {
90   {  XtNlabel, (XtArgVal) "Wait"  },
91 };
92 
93 static Arg arglistsonic_command[] = {
94   {  XtNlabel, (XtArgVal) "Sonic Screwdriver"  },
95 };
96 
97 static Arg arglistquit_command[] = {
98   {  XtNlabel, (XtArgVal) "Quit"  },
99 };
100 
101 static Arg arglistnew_game[] = {
102   {  XtNlabel, (XtArgVal) "New Game"  },
103 };
104 
105 static Arg arglistscore_command[] = {
106   {  XtNlabel, (XtArgVal) "Score:    0"  },
107 };
108 
109 /*----------------------------------------------------------------------*/
110 
111 static char translations_str[] =
112   "<Btn1Down>:	do_nothing()		\n\
113    <Btn2Down>:	do_nothing()		\n\
114    <Btn3Down>:	do_nothing()		\n\
115    <Btn1Up>:    move()			\n\
116    :<Key>u:     move(right, up)		\n\
117    :<Key>l:     move(right)		\n\
118    :<Key>n:     move(right, down)	\n\
119    :<Key>y:     move(left, up)		\n\
120    :<Key>h:     move(left)		\n\
121    :<Key>b:     move(left, down)	\n\
122    :<Key>k:     move(up)		\n\
123    :<Key>j:     move(down)		\n\
124    :<Key>U:     jump(right, up)		\n\
125    :<Key>L:     jump(right)		\n\
126    :<Key>N:     jump(right, down)	\n\
127    :<Key>Y:     jump(left, up)		\n\
128    :<Key>H:     jump(left)		\n\
129    :<Key>B:     jump(left, down)	\n\
130    :<Key>K:     jump(up)		\n\
131    :<Key>J:     jump(down)		\n\
132    <Key>.:      move(nowhere)		\n\
133    <Key>\\ :    move(nowhere)		\n\
134    <Btn2Up>:    go_here() 		\n\
135    <Btn3Up>:    wait()	 		\n\
136    <Key>s:      sonic()			\n\
137    <Key>t:      teleport() 		\n\
138    <Key>w:      wait()			\n\
139    <Key>z:      new_game()		\n\
140    Ctrl<Key>c:  quit() ";
141 
142 static void quit_game();
143 
144 static XtActionsRec actions[] = {
145   {"wait",      (XtActionProc)wait_for_em},
146   {"teleport",  (XtActionProc)teleport},
147   {"sonic",     (XtActionProc)sonic_action},
148   {"move",      (XtActionProc)move_action},
149   {"go_here",	(XtActionProc)go_here_action},
150   {"jump",	(XtActionProc)jump_action},
151   {"quit",      (XtActionProc)quit_game},
152   {"new_game",  (XtActionProc)new_game},
153   {"do_nothing",(XtActionProc)do_nothing_action},
154 };
155 
156 AppData app_data;
157 
158 static XrmOptionDescRec options[] = {
159 	{"-spiffy",	"spiffy",	XrmoptionSepArg, NULL },
160 	{"-autoteleport","autoteleport",XrmoptionSepArg, NULL },
161 	{"-autoteleportalways","autoteleportalways",XrmoptionSepArg, NULL },
162 	{"-showmovement","showmovement",XrmoptionSepArg, NULL },
163 	{"-diewaiting","diewaiting",XrmoptionSepArg, NULL },
164 	{"-scorefile","scorefile",XrmoptionSepArg, NULL },
165 };
166 
167 static XtResource application_resources[] = {
168   {"foreground", "Foreground", XtRPixel, sizeof(Pixel),
169                 XtOffsetOf(AppData, fg), XtRString, (caddr_t) "Black"},
170   {"background", "Background", XtRPixel, sizeof(Pixel),
171                 XtOffsetOf(AppData, bg), XtRString, (caddr_t) "White"},
172   {"spiffy", "Spiffy", XtRBoolean, sizeof(Boolean),
173                 XtOffsetOf(AppData, spiffy), XtRString, (caddr_t) "True"},
174   {"autoteleport", "Autoteleport", XtRBoolean, sizeof(Boolean),
175                 XtOffsetOf(AppData, autoteleport), XtRString, (caddr_t) "False"},
176   {"autoteleportalways", "Autoteleportalways", XtRBoolean, sizeof(Boolean),
177                 XtOffsetOf(AppData, autoteleportalways), XtRString, (caddr_t) "False"},
178   {"showmovement", "Showmovement", XtRBoolean, sizeof(Boolean),
179                 XtOffsetOf(AppData, showjumps), XtRString, (caddr_t) "True"},
180   {"diewaiting", "Diewaiting", XtRBoolean, sizeof(Boolean),
181                 XtOffsetOf(AppData, diewaiting), XtRString, (caddr_t) "False"},
182   {"translations","Translations", XtRTranslationTable, sizeof(XtTranslations),
183                 XtOffsetOf(AppData, translations), XtRString, (caddr_t)translations_str},
184   {"scorefile","Scorefile", XtRString, sizeof(String),
185                 XtOffsetOf(AppData, score_filename), XtRString, (caddr_t)SCORE_FILE},
186 };
187 
188 /*----------------------------------------------------------------------*/
189 
190 int
main(argc,argv)191 main(argc, argv)
192   int argc;
193   char **argv;
194 {
195   Arg args[1];
196   XGCValues gcv;
197   Widget teleport_command,
198 	 wait_command,
199 	 quit_command,
200 	 new_game_command;
201 
202   srandom(getpid());
203 
204   top_shell = XtInitialize(argv[0], "xrobots", options, XtNumber(options),
205   (int *)&argc, argv);
206   XtSetValues(top_shell, arglisttop_shell, XtNumber(arglisttop_shell));
207 
208   XtAddActions(actions,XtNumber(actions));
209 
210   XtGetApplicationResources(top_shell, &app_data, application_resources,
211 			XtNumber(application_resources), NULL, 0 );
212 
213   top_widget = XtCreateManagedWidget(
214                                     "top_widget",
215                                     boxWidgetClass,
216                                     top_shell,
217                                     0,0);
218 
219   playfield_widget = XtCreateManagedWidget(
220                                     "playfield",
221                                     widgetClass,
222                                     top_widget,
223                                     arglistplayfield,
224                                     XtNumber(arglistplayfield));
225 
226   XtAugmentTranslations(playfield_widget,app_data.translations);
227 
228   teleport_command = XtCreateManagedWidget(
229                                     "teleport_button",
230                                     commandWidgetClass,
231                                     top_widget,
232                                     arglistteleport_command,
233                                     XtNumber(arglistteleport_command));
234 
235   XtAddCallback(teleport_command, XtNcallback, (XtCallbackProc)teleport, 0);
236 
237   wait_command =  XtCreateManagedWidget(
238                                     "wait_button",
239                                     commandWidgetClass,
240                                     top_widget,
241                                     arglistwait_command,
242                                     XtNumber(arglistwait_command));
243 
244   XtAddCallback(wait_command, XtNcallback, (XtCallbackProc)wait_for_em, 0);
245 
246   sonic_command= XtCreateManagedWidget(
247                                     "sonic_button",
248                                     commandWidgetClass,
249                                     top_widget,
250                                     arglistsonic_command,
251                                     XtNumber(arglistsonic_command));
252 
253   XtAddCallback(sonic_command, XtNcallback, (XtCallbackProc)sonic_action, 0);
254 
255   quit_command = XtCreateManagedWidget(
256                                     "quit_button",
257                                     commandWidgetClass,
258                                     top_widget,
259                                     arglistquit_command,
260                                     XtNumber(arglistquit_command));
261 
262   XtAddCallback(quit_command,     XtNcallback, (XtCallbackProc)quit_game, 0);
263 
264   new_game_command = XtCreateManagedWidget(
265                                     "new_game_button",
266                                     commandWidgetClass,
267                                     top_widget,
268                                     arglistnew_game,
269                                     XtNumber(arglistnew_game));
270 
271   XtAddCallback(new_game_command, XtNcallback, (XtCallbackProc)new_game, 0);
272 
273   score_command = XtCreateManagedWidget(
274                                     "score_button",
275                                     commandWidgetClass,
276                                     top_widget,
277                                     arglistscore_command,
278                                     XtNumber(arglistscore_command));
279 
280   XtAddCallback(score_command, XtNcallback, (XtCallbackProc)show_scores_callback, 0);
281 
282 
283   create_high_score_popup(top_widget);
284 
285   XtRealizeWidget(top_shell);
286 
287 
288   display   = XtDisplay(playfield_widget);
289   playfield = XtWindow(playfield_widget);
290   gcv.foreground = app_data.fg;
291   gcv.background = app_data.bg;
292   gcv.function = GXcopy;
293   gc = XCreateGC(display, playfield,
294  		GCForeground | GCBackground | GCFunction, &gcv);
295   gcv.foreground = app_data.bg;
296   cleargc = XCreateGC(display, playfield,
297  		 GCForeground | GCBackground | GCFunction, &gcv);
298 
299   XtAddEventHandler(playfield_widget, ExposureMask, 0,(XtEventHandler)redisplay_level, 0);
300   XtAddEventHandler(playfield_widget, PointerMotionMask, 0, (XtEventHandler)pointer_moved, 0);
301 
302   init_pixmaps(top_shell);
303 
304   new_game();
305 
306   XtMainLoop();
307 
308 }
309 
310 
311 static void
quit_game()312 quit_game()
313 {
314   free_pixmaps();
315   XtDestroyWidget(top_shell);
316   XFreeGC(display,gc);
317   XFreeGC(display,cleargc);
318 
319   exit(0);
320 }
321 
322 
323 void
update_score(score)324 update_score(score)
325   int score;
326 {
327   char text[13];
328   (void)sprintf(text,"Score: %4d",score);
329   XtSetArg(arglistscore_command[0],XtNlabel,text);
330   XtSetValues(score_command,arglistscore_command,1);
331 }
332 
333 
334 
335