home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / Games / xpipeman / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-03  |  10.0 KB  |  339 lines

  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 "xpipeman.h"
  57.  
  58. /*----------------------------------------------------------------------*/
  59.  
  60. Widget top_shell, top_widget, playfield_widget, score_command,
  61.        bonus_command, remain_command, level_command,info_command;
  62.  
  63.  
  64. Display *display;
  65. Window playfield;
  66.  
  67. GC gc;
  68. GC cleargc;
  69.  
  70. /*----------------------------------------------------------------------*/
  71.  
  72. static Arg arglisttop_shell[] = {
  73.   {  XtNinput,  (XtArgVal)True },
  74. };
  75.  
  76. static Arg arglistplayfield[] = {
  77.   {  XtNheight, (XtArgVal) pos_to_coord(MAXY)  },
  78.   {  XtNwidth,  (XtArgVal) pos_to_coord(MAXX)  },
  79.   {  XtNborderWidth, (XtArgVal) 4 },
  80. };
  81.  
  82. static Arg arglistquit_command[] = {
  83.   {  XtNlabel, (XtArgVal) "Quit"  }, 
  84. };
  85.  
  86. static Arg arglistnew_game[] = {
  87.   {  XtNlabel, (XtArgVal) "New Game"  }, 
  88. };
  89.  
  90. static Arg arglistscore_command[] = {
  91.   {  XtNlabel, (XtArgVal) "Score:    0"  }, 
  92. };
  93.  
  94. static Arg arglistbonus_command[] = {
  95.   {  XtNlabel, (XtArgVal) "Bonus:    0"  }, 
  96. };
  97.  
  98. static Arg arglistremain_command[] = {
  99.   {  XtNlabel, (XtArgVal) "Remain:    0"  }, 
  100. };
  101.  
  102. static Arg arglistlevel_command[] = {
  103.   {  XtNlabel, (XtArgVal) "Level:    0"  }, 
  104. };
  105.  
  106. static Arg arglistinfo_command[] = {
  107.   {  XtNlabel, (XtArgVal) "Info"  }, 
  108. };
  109.  
  110. /*----------------------------------------------------------------------*/
  111.  
  112. static char translations_str[] = 
  113.   "<Btn1Down>:    do_nothing()        \n\
  114.    <Btn2Down>:    do_nothing()        \n\
  115.    <Btn3Down>:    do_nothing()        \n\
  116.    <Btn1Up>:    move_here()        \n\
  117.    <Btn2Up>:    place()            \n\
  118.    <Btn3Up>:    do_nothing()           \n\
  119.    :<Key>x:     move(right)        \n\
  120.    :<Key>z:     move(left)        \n\
  121.    :<Key>':     move(up)        \n\
  122.    :<Key>/:     move(down)        \n\
  123.    <Key>\\ :    place()                \n\
  124.    :<Key>p:     place()          \n\
  125.    <Key>Tab:    fastflow()         \n\
  126.    <Key>n:      new_game()        \n\
  127.    Ctrl<Key>c:  quit() ";
  128.  
  129. static void quit_game();
  130.  
  131. static XtActionsRec actions[] = {
  132.   {"quit",      (XtActionProc)quit_game},
  133.   {"move",      (XtActionProc)move_action},
  134.   {"move_here", (XtActionProc)move_here_action},
  135.   {"fastflow",   (XtActionProc)fast_flow_action},
  136.   {"place",     (XtActionProc)place_action},
  137.   {"new_game",  (XtActionProc)new_game},
  138.   {"do_nothing",(XtActionProc)do_nothing_action},
  139. };
  140.  
  141. Pixel fg, bg;
  142.  
  143. XtTranslations translations;
  144.  
  145. static XrmOptionDescRec options[] = {
  146.     {"-scorefile","scorefile",XrmoptionSepArg, NULL },
  147. };
  148.  
  149. static XtResource application_resources[] = {
  150.   {"foreground", "Foreground", XtRPixel, sizeof(Pixel),
  151.                 (Cardinal)&fg, XtRString, (caddr_t) "Black"},
  152.   {"background", "Background", XtRPixel, sizeof(Pixel),
  153.                 (Cardinal)&bg, XtRString, (caddr_t) "White"},
  154.   {"translations","Translations", XtRTranslationTable, sizeof(XtTranslations),
  155.                 (Cardinal)&translations, XtRString, (caddr_t)translations_str},
  156.   {"scorefile","Scorefile", XtRString, sizeof(String),
  157.                 (Cardinal)&score_filename, XtRString, (caddr_t)SCORE_FILE},
  158. };
  159.  
  160. /*----------------------------------------------------------------------*/
  161.  
  162. main(argc, argv)
  163.   unsigned int argc;
  164.   char **argv;
  165. {
  166.   Arg args[1];
  167.   XGCValues gcv;
  168.   Widget quit_command,
  169.      new_game_command;
  170.  
  171.   srandom(getpid());
  172.   current_block = 0;
  173.   current_callback = NULL;
  174.  
  175.   top_shell = XtInitialize(argv[0], "xpipeman", options, XtNumber(options), &argc, argv);
  176.   XtSetValues(top_shell, arglisttop_shell, XtNumber(arglisttop_shell));
  177.  
  178.   XtAddActions(actions,XtNumber(actions));
  179.  
  180.   XtGetApplicationResources(top_shell, 0, application_resources, 
  181.             XtNumber(application_resources), NULL, 0 );
  182.  
  183.   top_widget = XtCreateManagedWidget(
  184.                                     "top_widget",
  185.                                     boxWidgetClass,
  186.                                     top_shell,
  187.                                     0,0);
  188.  
  189.   playfield_widget = XtCreateManagedWidget(
  190.                                     "playfield",
  191.                                     widgetClass,
  192.                                     top_widget,
  193.                                     arglistplayfield,
  194.                                     XtNumber(arglistplayfield));
  195.  
  196.   XtAugmentTranslations(playfield_widget,translations);
  197.  
  198.   quit_command = XtCreateManagedWidget(
  199.                                     "quit_button",
  200.                                     commandWidgetClass,
  201.                                     top_widget,
  202.                                     arglistquit_command,
  203.                                     XtNumber(arglistquit_command));
  204.  
  205.   XtAddCallback(quit_command,     XtNcallback, (XtCallbackProc)quit_game, 0);
  206.  
  207.   new_game_command = XtCreateManagedWidget(
  208.                                     "new_game_button",
  209.                                     commandWidgetClass,
  210.                                     top_widget,
  211.                                     arglistnew_game,
  212.                                     XtNumber(arglistnew_game));
  213.  
  214.   XtAddCallback(new_game_command, XtNcallback, (XtCallbackProc)new_game, 0);
  215.  
  216.   score_command = XtCreateManagedWidget(
  217.                                     "score_button",
  218.                                     commandWidgetClass,
  219.                                     top_widget,
  220.                                     arglistscore_command,
  221.                                     XtNumber(arglistscore_command));
  222.  
  223.   XtAddCallback(score_command, XtNcallback, (XtCallbackProc)show_scores_callback, 0);
  224.   /* buttons used so that they all look the same    */
  225.  
  226.   bonus_command = XtCreateManagedWidget(
  227.                                     "bonus_button",
  228.                                     commandWidgetClass,
  229.                                     top_widget,
  230.                                     arglistbonus_command,
  231.                                     XtNumber(arglistbonus_command));
  232.  
  233.   remain_command = XtCreateManagedWidget(
  234.                                     "remain_button",
  235.                                     commandWidgetClass,
  236.                                     top_widget,
  237.                                     arglistremain_command,
  238.                                     XtNumber(arglistremain_command));
  239.  
  240.  
  241.   level_command = XtCreateManagedWidget(
  242.                                     "level_button",
  243.                                     commandWidgetClass,
  244.                                     top_widget,
  245.                                     arglistlevel_command,
  246.                                     XtNumber(arglistlevel_command));
  247.  
  248.   info_command = XtCreateManagedWidget(
  249.                                     "info_button",
  250.                                     commandWidgetClass,
  251.                                     top_widget,
  252.                                     arglistinfo_command,
  253.                                     XtNumber(arglistinfo_command));
  254.  
  255.   XtAddCallback(info_command, XtNcallback, (XtCallbackProc)show_info, 0);
  256.  
  257.   create_high_score_popup(top_widget);
  258.   create_info_popup(top_widget);
  259.   create_general_popups(top_widget);
  260.  
  261.   XtRealizeWidget(top_shell);
  262.  
  263.  
  264.   display   = XtDisplay(playfield_widget);
  265.   playfield = XtWindow(playfield_widget);
  266.   gcv.foreground = fg;
  267.   gcv.background = bg;
  268.   gcv.function = GXcopy;
  269.   gc = XCreateGC(display, playfield, 
  270.          GCForeground | GCBackground | GCFunction, &gcv);
  271.   gcv.foreground = bg;
  272.   cleargc = XCreateGC(display, playfield,
  273.           GCForeground | GCBackground | GCFunction, &gcv);
  274.  
  275.   XtAddEventHandler(playfield_widget, ExposureMask, 0, redisplay_level, 0);
  276.   XtAddEventHandler(playfield_widget, PointerMotionMask, 0, pointer_moved, 0);
  277.  
  278.   init_pixmaps(top_shell);
  279.  
  280.   XtMainLoop();
  281.  
  282. }
  283.  
  284.  
  285. static void
  286. quit_game()
  287. {
  288.   free_pixmaps();
  289.   XtDestroyWidget(top_shell);
  290.   XFreeGC(display,gc);
  291.   XFreeGC(display,cleargc);
  292.  
  293.   exit(0);
  294. }
  295.  
  296.  
  297. void
  298. update_score(score)
  299.   int score;
  300. {
  301.   char text[13];
  302.   (void)sprintf(text,"Score: %4d",score);
  303.   XtSetArg(arglistscore_command[0],XtNlabel,text);
  304.   XtSetValues(score_command,arglistscore_command,1);
  305. }
  306.  
  307. void
  308. update_bonus(score)
  309.   int score;
  310. {
  311.   char text[13];
  312.   (void)sprintf(text,"Bonus: %4d",score);
  313.   XtSetArg(arglistbonus_command[0],XtNlabel,text);
  314.   XtSetValues(bonus_command,arglistbonus_command,1);
  315. }
  316.  
  317. void
  318. update_remain(score)
  319.   int score;
  320. {
  321.   char text[13];
  322.   (void)sprintf(text,"Remaining: %4d",score);
  323.   XtSetArg(arglistremain_command[0],XtNlabel,text);
  324.   XtSetValues(remain_command,arglistremain_command,1);
  325. }
  326.  
  327. void
  328. update_level(level)
  329.   int level;
  330. {
  331.   char text[13];
  332.   (void)sprintf(text,"Level: %4d",level);
  333.   XtSetArg(arglistlevel_command[0],XtNlabel,text);
  334.   XtSetValues(level_command,arglistlevel_command,1);
  335. }
  336.  
  337.  
  338.  
  339.