home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / x / volume4 / xrobots / part01 / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-06-04  |  8.5 KB  |  296 lines

  1. /*
  2.  * main.c  --  xrobots v1.0
  3.  * 
  4.  * Author: Brian Warkentine, 1989.
  5.  *
  6.  * Permission to use, copy, modify and distribute (without charge) this
  7.  *   software, documentation, images, etc. is granted, provided that this 
  8.  *   comment and the author's name is retained.  The author assumes no 
  9.  *   responsibility for lost sleep as a consequence of use of this software.
  10.  *
  11.  * Send comments, bug reports, etc. to bwarkent@polyslo.CalPoly.EDU.
  12.  *   (Soon to be at @Sun.COM, so if the previous address doesn't work,
  13.  *   try brian@csufres.CSUFresno.EDU.)
  14.  *
  15.  * Portions of this code generated by wedgimatic on Jan 21 14:24:10 PST 1989.
  16.  * 
  17.  */
  18.  
  19. #include <X11/X.h>
  20. #include <X11/Intrinsic.h>
  21. #include <X11/StringDefs.h>
  22. #include <X11/cursorfont.h>
  23. #include <X11/Core.h>
  24. #include <X11/Label.h>
  25. #include <X11/Command.h>
  26. #include <X11/Box.h>
  27.  
  28. #include "callbacks.h"
  29. #include "graphics.h"
  30. #include "game.h"
  31. #include "score.h"
  32. #include "help.h"
  33.  
  34. /*----------------------------------------------------------------------*/
  35.  
  36. Widget top_shell, top_widget, playfield_widget, score_widget, sonic_command;
  37.  
  38. Display *display;
  39. Window playfield;
  40.  
  41. GC gc;
  42. GC cleargc;
  43.  
  44. /*----------------------------------------------------------------------*/
  45.  
  46.  
  47. static XtCallbackRec teleport_callbackList[] = {
  48.   {  (XtCallbackProc)teleport_callback, NULL  },
  49.   {  NULL, NULL  }
  50. };
  51.  
  52. static XtCallbackRec wait_callbackList[] = {
  53.   {  (XtCallbackProc)wait_callback, NULL  },
  54.   {  NULL, NULL  }
  55. };
  56.  
  57. static XtCallbackRec sonic_callbackList[] = {
  58.   {  (XtCallbackProc)sonic_callback, NULL  },
  59.   {  NULL, NULL  }
  60. };
  61.  
  62. static XtCallbackRec quitList[] = {
  63.   {  (XtCallbackProc)quit_callback, NULL  },
  64.   {  NULL, NULL  }
  65. };
  66.  
  67. static XtCallbackRec new_gameList[] = {
  68.   {  (XtCallbackProc)new_game_callback, NULL  },
  69.   {  NULL, NULL  }
  70. };
  71.  
  72. static XtCallbackRec show_helpList[] = {
  73.   {  (XtCallbackProc)show_help_callback, NULL  },
  74.   {  NULL, NULL  }
  75. };
  76.  
  77. /*----------------------------------------------------------------------*/
  78.  
  79.  
  80. static Arg arglistplayfield[] = {
  81.   {  XtNheight, (XtArgVal) pos_to_coord(MAXY)  },
  82.   {  XtNwidth,  (XtArgVal) pos_to_coord(MAXX)  },
  83.   {  XtNborderWidth, (XtArgVal) 4 },
  84. };
  85.  
  86. static Arg arglistteleport_command[] = {
  87.   {  XtNlabel, (XtArgVal) "Teleport"  },
  88.   {  XtNcallback, (XtArgVal) teleport_callbackList }
  89. };
  90.  
  91. static Arg arglistwait_command[] = {
  92.   {  XtNlabel, (XtArgVal) "Wait"  },
  93.   {  XtNcallback, (XtArgVal) wait_callbackList }
  94. };
  95.  
  96. static Arg arglistsonic_command[] = {
  97.   {  XtNlabel, (XtArgVal) "Sonic Screwdriver"  },
  98.   {  XtNcallback, (XtArgVal) sonic_callbackList }
  99. };
  100.  
  101. static Arg arglistquit_command[] = {
  102.   {  XtNlabel, (XtArgVal) "Quit"  }, 
  103.   {  XtNcallback, (XtArgVal) quitList }
  104. };
  105.  
  106. static Arg arglistnew_game[] = {
  107.   {  XtNlabel, (XtArgVal) "New Game"  }, 
  108.   {  XtNcallback, (XtArgVal) new_gameList }
  109. };
  110.  
  111. static Arg arglistscore_widget[] = {
  112.   {  XtNlabel, (XtArgVal) "Score:    0"  }, 
  113. };
  114.  
  115. static Arg arglisthelp_button[] = {
  116.   {  XtNlabel, (XtArgVal) "Help"  }, 
  117.   {  XtNcallback, (XtArgVal) show_helpList }
  118. };
  119.  
  120. /*----------------------------------------------------------------------*/
  121.  
  122. /* these can be overriden -- of course */
  123. static char translations_str[] = 
  124.   "<Btn1Down>:    do_nothing()        \n\
  125.    <Btn2Down>:    do_nothing()        \n\
  126.    <Btn3Down>:    do_nothing()        \n\
  127.    <Btn1Up>:    move()            \n\
  128.    <Key>u:      move(right, up)        \n\
  129.    <Key>l:      move(right)        \n\
  130.    <Key>n:      move(right, down)    \n\
  131.    <Key>y:      move(left, up)        \n\
  132.    <Key>h:      move(left)        \n\
  133.    <Key>b:      move(left, down)    \n\
  134.    <Key>k:      move(up)        \n\
  135.    <Key>j:      move(down)        \n\
  136.    <Key>.:      move(nowhere)        \n\
  137.    <Key>\\ :    move(nowhere)        \n\
  138.    <Btn2Up>:    go_here()         \n\
  139.    <Btn3Up>:    wait()             \n\
  140.    <Key>s:      sonic()            \n\
  141.    <Key>t:      teleport()         \n\
  142.    <Key>w:      wait()            \n\
  143.    <Key>z:      new_game()        ";
  144. /* <Key>q:      quit() "; */
  145.  
  146. Pixel fg, bg;
  147. Boolean spiffy;
  148. XtTranslations translations;
  149.  
  150. static XtResource application_resources[] = {
  151.   {"foreground", "Foreground", XtRPixel, sizeof(Pixel),
  152.                 (Cardinal)&fg, XtRString, (caddr_t) "Black"},
  153.   {"background", "Background", XtRPixel, sizeof(Pixel),
  154.                 (Cardinal)&bg, XtRString, (caddr_t) "White"},
  155.   {"spiffy", "Spiffy", XtRBoolean, sizeof(Boolean),
  156.                 (Cardinal)&spiffy, XtRString, (caddr_t) "True"},
  157.   {"translations","Translations", XtRTranslationTable, sizeof(XtTranslations),
  158.                 (Cardinal)&translations, XtRString, (caddr_t)translations_str},
  159. };
  160.  
  161. /*----------------------------------------------------------------------*/
  162.  
  163. main(argc, argv)
  164.   unsigned int argc;
  165.   char **argv;
  166. {
  167.   Arg args[1];
  168.   XGCValues gcv;
  169.  
  170.   srandom(getpid());
  171.  
  172.   top_shell = XtInitialize(argv[0], "xrobots", 0, 0, &argc, argv);
  173.  
  174.   init_actions();
  175.  
  176.   XtGetApplicationResources(top_shell, 0, application_resources, 
  177.             XtNumber(application_resources), NULL, 0 );
  178.  
  179.   top_widget = XtCreateManagedWidget(
  180.                                     "top_widget",
  181.                                     boxWidgetClass,
  182.                                     top_shell,
  183.                                     0,0);
  184.  
  185.   playfield_widget = XtCreateManagedWidget(
  186.                                     "playfield",
  187.                                     widgetClass,
  188.                                     top_widget,
  189.                                     arglistplayfield,
  190.                                     XtNumber(arglistplayfield));
  191.  
  192.   XtAugmentTranslations(playfield_widget,translations);
  193.  
  194.   (void) XtCreateManagedWidget(
  195.                                     "teleport_button",
  196.                                     commandWidgetClass,
  197.                                     top_widget,
  198.                                     arglistteleport_command,
  199.                                     XtNumber(arglistteleport_command));
  200.  
  201.   (void) XtCreateManagedWidget(
  202.                                     "wait_button",
  203.                                     commandWidgetClass,
  204.                                     top_widget,
  205.                                     arglistwait_command,
  206.                                     XtNumber(arglistwait_command));
  207.  
  208.   sonic_command= XtCreateManagedWidget(
  209.                                     "sonic_button",
  210.                                     commandWidgetClass,
  211.                                     top_widget,
  212.                                     arglistsonic_command,
  213.                                     XtNumber(arglistsonic_command));
  214.  
  215.   (void) XtCreateManagedWidget(
  216.                                     "quit_button",
  217.                                     commandWidgetClass,
  218.                                     top_widget,
  219.                                     arglistquit_command,
  220.                                     XtNumber(arglistquit_command));
  221.   (void) XtCreateManagedWidget(
  222.                                     "new_game_button",
  223.                                     commandWidgetClass,
  224.                                     top_widget,
  225.                                     arglistnew_game,
  226.                                     XtNumber(arglistnew_game));
  227.  
  228.   (void) XtCreateManagedWidget(
  229.                                     "help_button",
  230.                                     commandWidgetClass,
  231.                                     top_widget,
  232.                                     arglisthelp_button,
  233.                     XtNumber(arglisthelp_button));
  234.  
  235.   score_widget = XtCreateManagedWidget(
  236.                                     "score_button",
  237.                                     labelWidgetClass,
  238.                                     top_widget,
  239.                                     arglistscore_widget,
  240.                                     XtNumber(arglistscore_widget));
  241.  
  242.  
  243.   create_high_score_popup(top_widget);
  244.   create_help_popup(top_widget);
  245.  
  246.   XtRealizeWidget(top_shell);
  247.  
  248.   display   = XtDisplay(playfield_widget);
  249.   playfield = XtWindow(playfield_widget);
  250.   gcv.foreground = fg;
  251.   gcv.background = bg;
  252.   gcv.function = GXcopy;
  253.   gc = XCreateGC(display, playfield, 
  254.          GCForeground | GCBackground | GCFunction, &gcv);
  255.   gcv.foreground = bg;
  256.   cleargc = XCreateGC(display, playfield,
  257.           GCForeground | GCBackground | GCFunction, &gcv);
  258.  
  259.   XtAddEventHandler(playfield_widget, ExposureMask, 0, redisplay_level, 0);
  260.   XtAddEventHandler(playfield_widget, PointerMotionMask, 0, pointer_moved, 0);
  261.  
  262.   init_pixmaps(top_shell);
  263.  
  264.   new_game();
  265.  
  266.   XtMainLoop();
  267.  
  268. }
  269.  
  270.  
  271. void
  272. quit_game()
  273. {
  274.   free_pixmaps();
  275.   XtDestroyWidget(top_shell);
  276.   XFreeGC(display,gc);
  277.   XFreeGC(display,cleargc);
  278.  
  279. /*  XtDestroyApplicationContext(); */
  280.   exit(0);
  281. }
  282.  
  283.  
  284. void
  285. update_score(score)
  286.   int score;
  287. {
  288.   char text[13];
  289.   (void)sprintf(text,"Score: %4d",score);
  290.   XtSetArg(arglistscore_widget[0],XtNlabel,text);
  291.   XtSetValues(score_widget,arglistscore_widget,1);
  292. }
  293.  
  294.  
  295.  
  296.