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

  1. /* 
  2. Copyright notice:
  3.  
  4. This is mine.  I'm only letting you use it.  Period.  Feel free to rip off
  5. any of the code you see fit, but have the courtesy to give me credit.
  6. Otherwise great hairy beasties will rip your eyes out and eat your flesh
  7. when you least expect it.
  8.  
  9. Jonny Goldman <jonathan@think.com>
  10.  
  11. Wed May  8 1991
  12. */
  13.  
  14. /* widget.c -- handle things that make the playing field behave as a widget. */
  15.  
  16. #include "vaders.h"
  17. #include <X11/Xaw/Form.h>
  18.  
  19. typedef struct _BaseRec {
  20.   int x;            /* Location. */
  21.   int v;            /* velocity */
  22.   int width, height;        /* box of this base. */
  23.   XImage *shape_image;        /* an XImage for the spaceship */
  24. } BaseRec, *Base;
  25.  
  26. extern Base base;
  27.  
  28. void HandleFocus();
  29. void HandleStruct();
  30. void Pause();
  31.  
  32. static XtActionsRec actions[] = {
  33.     {"grab-focus",    (XtActionProc) TakeFocus},
  34.     {"left",        MoveLeft},
  35.     {"right",        MoveRight},
  36.     {"stop",        Stop},
  37.     {"fire",        Fire},
  38.     {"pause",        Pause},
  39.     {"quit",        Quit},
  40. };
  41.  
  42. static char defaultTranslation[] =
  43.      "<Btn1Down>:    left()\n\
  44.      <Btn1Up>:        stop()\n\
  45.      <Btn2Down>:    fire()\n\
  46.      <Btn3Down>:    right()\n\
  47.      <Btn3Up>:        stop()\n\
  48.      <KeyDown>z:    fire()\n\
  49.      <KeyDown>\\,:    left()\n\
  50.      <KeyUp>\\,:    stop()\n\
  51.      <KeyDown>.:    right()\n\
  52.      <KeyUp>.:        stop()\n\
  53.      <KeyDown>\\ :    fire()\n\
  54.      <KeyDown>p:    pause()\n\
  55.      <KeyDown>q:    quit()";
  56.  
  57.  
  58. static void ClassInitialize() {}
  59.  
  60. /*ARGSUSED*/
  61. static void Initialize(request, w)
  62. Widget request, w;
  63. {
  64.     XtAddEventHandler(toplevel, (EventMask) FocusChangeMask, FALSE,
  65.               HandleFocus, (Opaque) NULL);
  66.     XtAddEventHandler(toplevel, (EventMask) StructureNotifyMask, FALSE,
  67.               HandleStruct, (Opaque) NULL);
  68. }
  69.  
  70.  
  71. static void Realize(w, valueMask, attributes)
  72. Widget w;
  73. Mask *valueMask;
  74. XSetWindowAttributes *attributes;
  75. {
  76.   XGCValues forevalues;
  77.   XGCValues backvalues;
  78.   XGCValues basevalues;
  79.   XGCValues buildingvalues;
  80.   XGCValues vader1values;
  81.   XGCValues vader2values;
  82.   XGCValues vader3values;
  83.   XGCValues shotvalues;
  84.   XGCValues scorevalues;
  85.   XGCValues spacervalues;
  86.  
  87.   /*
  88.    * Safety check.
  89.    */
  90.  
  91.   if (w->core.width == 0) w->core.width = 1;
  92.   if (w->core.height == 0) w->core.height = 1;
  93.  
  94.   XtCreateWindow(w, (unsigned int) InputOutput, (Visual *) CopyFromParent,
  95.          *valueMask, attributes);
  96.  
  97.   gamewidth = scale*VWIDTH;
  98.   gameheight = scale*VHEIGHT;
  99.   gamewindow = XtWindow(gamewidget);
  100.   labelwindow = XtWindow(labelwidget);
  101.  
  102.   forevalues.background = WhitePixel(dpy, DefaultScreen(dpy));
  103.   forevalues.foreground = BlackPixel(dpy, DefaultScreen(dpy));
  104.   foregc = XCreateGC(dpy, XtWindow(w),
  105.              (XtGCMask) GCForeground | GCBackground, &forevalues);
  106.   XSetFunction(dpy, foregc, GXcopy);
  107.   backvalues.background = defaultback;
  108.   backvalues.foreground = defaultback;
  109.   backgc = XCreateGC(dpy, XtWindow(w),
  110.              (XtGCMask) GCForeground | GCBackground, &backvalues);
  111.   XSetFunction(dpy, backgc, GXcopy);
  112.   basevalues.background = defaultback;
  113.   basevalues.foreground = basepixel;
  114.   basegc = XCreateGC(dpy, XtWindow(w),
  115.              (XtGCMask) GCForeground | GCBackground, &basevalues);
  116.   XSetFunction(dpy, basegc, GXcopy);
  117.   buildingvalues.background = defaultback;
  118.   buildingvalues.foreground = buildingpixel;
  119.   buildinggc = XCreateGC(dpy, XtWindow(w),
  120.              (XtGCMask) GCForeground | GCBackground, &buildingvalues);
  121.   XSetFunction(dpy, buildinggc, GXcopy);
  122.   vader1values.background = defaultback;
  123.   vader1values.foreground = vader1pixel;
  124.   vadergc[0] = XCreateGC(dpy, XtWindow(w),
  125.              (XtGCMask) GCForeground | GCBackground, &vader1values);
  126.   XSetFunction(dpy, vadergc[0], GXcopy);
  127.   vader2values.background = defaultback;
  128.   vader2values.foreground = vader2pixel;
  129.   vadergc[1] = XCreateGC(dpy, XtWindow(w),
  130.              (XtGCMask) GCForeground | GCBackground, &vader2values);
  131.   XSetFunction(dpy, vadergc[1], GXcopy);
  132.   vader3values.background = defaultback;
  133.   vader3values.foreground = vader3pixel;
  134.   vadergc[2] = XCreateGC(dpy, XtWindow(w),
  135.              (XtGCMask) GCForeground | GCBackground, &vader3values);
  136.   XSetFunction(dpy, vadergc[2], GXcopy);
  137.   shotvalues.background = defaultback;
  138.   shotvalues.foreground = shotpixel;
  139.   shotgc = XCreateGC(dpy, XtWindow(w),
  140.              (XtGCMask) GCForeground | GCBackground, &shotvalues);
  141.   XSetFunction(dpy, shotgc, GXcopy);
  142.   scorevalues.background = defaultback;
  143.   scorevalues.foreground = scorepixel;
  144.   scorevalues.font = XLoadFont(dpy, vaderfont);
  145.   scoregc = XCreateGC(dpy, XtWindow(w),
  146.              (XtGCMask) GCForeground | GCBackground | GCFont, &scorevalues);
  147.   XSetFunction(dpy, scoregc, GXcopy);
  148.   shotvalues.background = defaultback;
  149.   shotvalues.foreground = vshotpixel;
  150.   vshotgc = XCreateGC(dpy, XtWindow(w),
  151.               (XtGCMask) GCForeground | GCBackground, &shotvalues);
  152.   XSetFunction(dpy, vshotgc, GXcopy);
  153.   spacervalues.background = defaultback;
  154.   spacervalues.foreground = spacerpixel;
  155.   spacergc = XCreateGC(dpy, XtWindow(w),
  156.                (XtGCMask) GCForeground | GCBackground, &spacervalues);
  157.   XSetFunction(dpy, spacergc, GXcopy);
  158.  
  159.   InitBase();
  160.   InitBuildings();
  161.   InitVaders();
  162.   InitSpacers();
  163.   InitShot();
  164.   InitScore();
  165. }
  166.  
  167. void SuspendTimers()
  168. {
  169.   if (basetimerid) 
  170.     XtRemoveTimeOut(basetimerid);
  171.   basetimerid = NULL;
  172.   if (vadertimerid)
  173.     XtRemoveTimeOut(vadertimerid);
  174.   vadertimerid = NULL;
  175.   if (shottimerid)
  176.     XtRemoveTimeOut(shottimerid);
  177.   shottimerid = NULL;
  178.   if (vshottimerid)
  179.     XtRemoveTimeOut(vshottimerid);
  180.   vshottimerid = NULL;
  181.   if (spacertimerid)
  182.     XtRemoveTimeOut(spacertimerid);
  183.   spacertimerid = NULL;
  184. }
  185.  
  186. void EnableTimers()
  187. {
  188.   if (basetimerid == NULL)
  189.     basetimerid = XtAddTimeOut(basewait, MoveBase, (Opaque) MoveBase);
  190.   if (vadertimerid == NULL)
  191.     vadertimerid = XtAddTimeOut(vaderwait, MoveVaders,
  192.                    (Opaque) MoveVaders);
  193.   if(spacertimerid == NULL)
  194.     spacertimerid = XtAddTimeOut(spacerwait, MoveSpacer,
  195.                  (Opaque) MoveSpacer);
  196.   if (shottimerid == NULL)
  197.     shottimerid = XtAddTimeOut(shotwait, MoveShots,
  198.                    (Opaque) MoveShots);
  199.   if (vshottimerid == NULL)
  200.     vshottimerid = XtAddTimeOut(vshotwait, MoveVshots,
  201.                 (Opaque) MoveVshots);
  202. }
  203.  
  204. /*ARGSUSED*/
  205. static void HandleExpose(w, event)
  206. Widget w;
  207. XEvent *event;
  208. {
  209.  /*  if (event->xexpose.count) return; */
  210.   XSync(dpy, 0);
  211.   /* suspend events */
  212.   SuspendTimers();
  213.   /* rebuild the display */
  214.   XSync(dpy, 0);
  215.   XClearWindow(dpy, gamewindow);
  216.   if (!basedestroyed)
  217.     PaintBase(basegc);
  218.   if(spacer_shown)
  219.     PaintSpacer(spacergc);
  220.   PaintAllVaders();
  221.   PaintAllShots();
  222.   PaintBasesLeft();
  223.   DrawBuildings();
  224.   PaintScore();
  225.   XSync(dpy, 0);
  226.   /* enable events */
  227.   if(!paused) 
  228.     EnableTimers();
  229. }
  230.  
  231. /*ARGSUSED*/
  232. void Quit(w, closure, event)
  233. Widget w;
  234. Opaque closure;
  235. XEvent *event;
  236. {
  237.     XCloseDisplay(dpy);
  238.     exit(0);
  239. }
  240.  
  241. int paused = 1;
  242.  
  243. /*ARGSUSED*/
  244. static void HandleFocus(w, closure, event)
  245. Widget w;
  246. Opaque closure;
  247. XEvent *event;
  248. {
  249. }
  250.  
  251. /*ARGSUSED*/
  252. void Pause(w, closure, event)
  253. Widget w;
  254. Opaque closure;
  255. XEvent *event;
  256. {
  257.   static Arg args[1];
  258.   if (paused) {
  259.     paused = 0;
  260.     EnableTimers();
  261.     XtSetArg(args[0], XtNlabel, "Pause");
  262.     XtSetValues(pausebutton, args, 1);
  263.     HandleExpose(NULL, NULL, NULL);
  264.   } else {
  265.     paused = 1;
  266.     SuspendTimers();
  267.     XtSetArg(args[0], XtNlabel, "Resume");
  268.     XtSetValues(pausebutton, args, 1);
  269.   }
  270. }
  271.  
  272. /*ARGSUSED*/
  273. static void HandleStruct(w, closure, event)
  274. Widget w;
  275. Opaque closure;
  276. XEvent *event;
  277. {
  278. }
  279.  
  280. static void Destroy() {}
  281.  
  282. static void Resize() {}
  283.  
  284. static Boolean SetValues() 
  285. {
  286.   return FALSE;
  287. }
  288.  
  289. static Boolean TakeFocus()
  290. {
  291.     XSetInputFocus(dpy, gamewindow, RevertToPointerRoot, CurrentTime);
  292.     return TRUE;
  293. }
  294.  
  295. #include "me.h"
  296.  
  297. void ShowInfo(w, closure, event)
  298. Widget w;
  299. Opaque closure;
  300. XEvent *event;
  301. {
  302.   int xloc, yloc;
  303.  
  304.   paused = 0;
  305.   Pause(NULL, NULL, NULL);
  306.   /* rebuild the display */
  307.   XSync(dpy, 0);
  308.   XClearWindow(dpy, gamewindow);
  309.   if (me_image == NULL) {
  310.     me_image = XCreateImage(dpy,
  311.                 DefaultVisual(dpy, DefaultScreen(dpy)),
  312.                 1,
  313.                 XYBitmap,
  314.                 0,
  315.                 (scale == 1) ? me1_bits:me2_bits,
  316.                 (scale == 1) ? me1_width : me2_width,
  317.                 (scale == 1) ? me1_height: me2_height,
  318.                 8, 0);
  319.     me_image->bitmap_bit_order = LSBFirst;
  320.     me_image->byte_order = LSBFirst;
  321.   }
  322.   
  323.   xloc = (gamewidth-((scale == 1) ? me1_width : me2_width))/2;
  324.   yloc = ((scale == 1) ? me1_width : me2_width) + 10;
  325.  
  326.   XPutImage(dpy, gamewindow, foregc, me_image,
  327.         0, 0, 
  328.         xloc, 0,
  329.         (scale == 1) ? me1_width : me2_width,
  330.         (scale == 1) ? me1_height: me2_height);
  331.  
  332.   if (scale == 2)
  333.     XDrawImageString(dpy, gamewindow, scoregc, xloc, yloc, "Xinvaders, by Jonny Goldman", 27);
  334.   else {
  335.     XDrawImageString(dpy, gamewindow, scoregc, xloc+30, yloc, "Xinvaders", 9);
  336.     XDrawImageString(dpy, gamewindow, scoregc, xloc, yloc+15, "by Jonny Goldman", 16);
  337.   }
  338.  
  339.   XSync(dpy, 0);
  340. }
  341.  
  342.  
  343. VadersClassRec vadersClassRec = {
  344.   {
  345. /* core_class fields      */
  346.     /* superclass         */    (WidgetClass) &widgetClassRec,
  347.     /* class_name         */    "Xinvaders",
  348.     /* widget_size        */    sizeof(VadersRec),
  349.     /* class_initialize   */    ClassInitialize,
  350.     /* class_part_initiali*/    NULL,
  351.     /* class_inited       */    FALSE,
  352.     /* initialize         */    Initialize,
  353.     /* initialize_hook      */    NULL,
  354.     /* realize            */    Realize,
  355.     /* actions            */    actions,
  356.     /* num_actions        */    XtNumber(actions),
  357.     /* resources          */    NULL,
  358.     /* num_resources      */    (Cardinal) 0,
  359.     /* xrm_class          */    NULLQUARK,
  360.     /* compress_motion    */    TRUE,
  361.     /* compress_exposure  */    TRUE,
  362.     /* compress_enterleave*/    TRUE,
  363.     /* visible_interest   */    FALSE,
  364.     /* destroy            */    Destroy,
  365.     /* resize             */    Resize,
  366.     /* expose             */    HandleExpose,
  367.     /* set_values         */    SetValues,
  368.     /* set_values_hook      */    NULL,
  369.     /* set_values_almost  */    NULL,
  370.     /* get_values_hook      */    NULL,
  371.     /* accept_focus       */    TakeFocus,
  372.     /* version          */    XtVersion,
  373.     /* callback_private      */    NULL,
  374.     /* tm_table          */    defaultTranslation,
  375.     /* query_geometry      */    NULL,
  376.     /* display_accelerator*/    NULL,
  377.     /* extension`      */    NULL,
  378.   },{
  379.     /* mumble             */    0       /* Make C compiler happy   */
  380.   }
  381. };
  382.  
  383. WidgetClass vadersWidgetClass = (WidgetClass)&vadersClassRec;
  384.