jgoldman@parc.xerox.com (Jonny Goldman) (08/04/90)
Submitted-by: Jonny Goldman <jgoldman@parc.xerox.com> Posting-number: Volume 8, Issue 64 Archive-name: xinvaders/part03 X X for(i=0; i< NUMBUILDINGS; i++) { X buildings[i].x = i*((gamewidth ? X (VWIDTH -(3*(base->width+3))-70) : X (gamewidth-(3*(base->width+3))-70)))/4+35; X for (j=0; j<HUNKROWS; j++) X for (k = 0; k < NUMHUNKS; k++) X buildings[i].hunks[j][k] = TRUE; X } X j--; X X for(i=0; i< NUMBUILDINGS; i++) { X buildings[i].hunks[0][0] = FALSE; X buildings[i].hunks[0][NUMHUNKS-1] = FALSE; X for (k = 3; k < NUMHUNKS-3; k++) X buildings[i].hunks[j][k] = FALSE; X } X} X Xvoid DrawBuildings() X{ X int i, j, k; X X for(i=0; i< NUMBUILDINGS; i++) { X for (j=0; j<HUNKROWS; j++) X for (k = 0; k < NUMHUNKS; k++) X if(buildings[i].hunks[j][k]) DrawBuildingHunk(&buildings[i], j, k, buildinggc); X } X} END_OF_FILE if test 6969 -ne `wc -c <'base.c'`; then echo shar: \"'base.c'\" unpacked with wrong size! fi # end of 'base.c' fi if test -f 'widget.c' -a "${1}" != "-c" ; then echo shar: Will not clobber existing file \"'widget.c'\" else echo shar: Extracting \"'widget.c'\" \(8425 characters\) sed "s/^X//" >'widget.c' <<'END_OF_FILE' X/* XCopyright notice: X XThis is mine. I'm only letting you use it. Period. Feel free to rip off Xany of the code you see fit, but have the courtesy to give me credit. XOtherwise great hairy beasties will rip your eyes out and eat your flesh Xwhen you least expect it. X XJonny Goldman <jgoldman@parc.xerox.com> X XTue Jul 17 1990 X*/ X X/* widget.c -- handle things that make the playing field behave as a widget. */ X X#include "vaders.h" X#include <X11/Xaw/Paned.h> X Xtypedef struct _BaseRec { X int x; /* Location. */ X int v; /* velocity */ X int width, height; /* box of this base. */ X XImage *shape_image; /* an XImage for the spaceship */ X} BaseRec, *Base; X Xextern Base base; X Xvoid HandleFocus(); Xvoid HandleStruct(); Xvoid Pause(); X Xstatic XtActionsRec actions[] = { X {"grab-focus", (XtActionProc) TakeFocus}, X {"left", MoveLeft}, X {"right", MoveRight}, X {"stop", Stop}, X {"fire", Fire}, X {"pause", Pause}, X {"quit", Quit}, X}; X Xstatic char defaultTranslation[] = X "<Btn1Down>: left()\n\ X <Btn1Up>: stop()\n\ X <Btn2Down>: fire()\n\ X <Btn3Down>: right()\n\ X <Btn3Up>: stop()\n\ X <KeyDown>z: fire()\n\ X <KeyDown>\\,: left()\n\ X <KeyUp>\\,: stop()\n\ X <KeyDown>.: right()\n\ X <KeyUp>.: stop()\n\ X <KeyDown>\\ : fire()\n\ X <KeyDown>p: pause()\n\ X <KeyDown>q: quit()"; X X Xstatic void ClassInitialize() {} X Xstatic void Initialize(request, w) XWidget request, w; X{ X gamewidget = (VadersWidget) w; X XtAddEventHandler(toplevel, (EventMask) FocusChangeMask, FALSE, X HandleFocus, (Opaque) NULL); X XtAddEventHandler(toplevel, (EventMask) StructureNotifyMask, FALSE, X HandleStruct, (Opaque) NULL); X} X X Xstatic void Realize(w, valueMask, attributes) XWidget w; XMask *valueMask; XXSetWindowAttributes *attributes; X{ X int i; X XGCValues backvalues; X XGCValues basevalues; X XGCValues buildingvalues; X XGCValues vader1values; X XGCValues vader2values; X XGCValues vader3values; X XGCValues shotvalues; X XGCValues scorevalues; X XGCValues spacervalues; X X /* X * Safety check. X */ X X if (w->core.width == 0) w->core.width = 1; X if (w->core.height == 0) w->core.height = 1; X X XtCreateWindow(w, (unsigned int) InputOutput, (Visual *) CopyFromParent, X *valueMask, attributes); X X gamewidth = w->core.width; X gameheight = w->core.height; X gamewindow = w->core.window; X X backvalues.background = defaultback; X backvalues.foreground = defaultback; X backgc = XCreateGC(dpy, gamewindow, X (XtGCMask) GCForeground | GCBackground, &backvalues); X XSetFunction(dpy, backgc, GXcopy); X basevalues.background = defaultback; X basevalues.foreground = basepixel; X basegc = XCreateGC(dpy, gamewindow, X (XtGCMask) GCForeground | GCBackground, &basevalues); X XSetFunction(dpy, basegc, GXcopy); X buildingvalues.background = defaultback; X buildingvalues.foreground = buildingpixel; X buildinggc = XCreateGC(dpy, gamewindow, X (XtGCMask) GCForeground | GCBackground, &buildingvalues); X XSetFunction(dpy, buildinggc, GXcopy); X vader1values.background = defaultback; X vader1values.foreground = vader1pixel; X vadergc[0] = XCreateGC(dpy, gamewindow, X (XtGCMask) GCForeground | GCBackground, &vader1values); X XSetFunction(dpy, vadergc[0], GXcopy); X vader2values.background = defaultback; X vader2values.foreground = vader2pixel; X vadergc[1] = XCreateGC(dpy, gamewindow, X (XtGCMask) GCForeground | GCBackground, &vader2values); X XSetFunction(dpy, vadergc[1], GXcopy); X vader3values.background = defaultback; X vader3values.foreground = vader3pixel; X vadergc[2] = XCreateGC(dpy, gamewindow, X (XtGCMask) GCForeground | GCBackground, &vader3values); X XSetFunction(dpy, vadergc[2], GXcopy); X shotvalues.background = defaultback; X shotvalues.foreground = shotpixel; X shotgc = XCreateGC(dpy, gamewindow, X (XtGCMask) GCForeground | GCBackground, &shotvalues); X XSetFunction(dpy, shotgc, GXcopy); X scorevalues.background = defaultback; X scorevalues.foreground = scorepixel; X scoregc = XCreateGC(dpy, gamewindow, X (XtGCMask) GCForeground | GCBackground, &scorevalues); X XSetFunction(dpy, scoregc, GXcopy); X shotvalues.background = defaultback; X shotvalues.foreground = vshotpixel; X vshotgc = XCreateGC(dpy, gamewindow, X (XtGCMask) GCForeground | GCBackground, &shotvalues); X XSetFunction(dpy, vshotgc, GXcopy); X spacervalues.background = defaultback; X spacervalues.foreground = spacerpixel; X spacergc = XCreateGC(dpy, gamewindow, X (XtGCMask) GCForeground | GCBackground, &spacervalues); X XSetFunction(dpy, spacergc, GXcopy); X X InitBase(); X InitBuildings(); X InitVaders(); X InitSpacers(); X InitShot(); X InitScore(); X} X Xvoid SuspendTimers() X{ X if (basetimerid) X XtRemoveTimeOut(basetimerid); X basetimerid = NULL; X if (vadertimerid) X XtRemoveTimeOut(vadertimerid); X vadertimerid = NULL; X if (shottimerid) X XtRemoveTimeOut(shottimerid); X shottimerid = NULL; X if (vshottimerid) X XtRemoveTimeOut(vshottimerid); X vshottimerid = NULL; X if (spacertimerid) X XtRemoveTimeOut(spacertimerid); X spacertimerid = NULL; X} X Xvoid EnableTimers() X{ X if (basetimerid == NULL) X basetimerid = XtAddTimeOut(basewait, MoveBase, (Opaque) MoveBase); X if (vadertimerid == NULL) X vadertimerid = XtAddTimeOut(vaderwait, MoveVaders, X (Opaque) MoveVaders); X if(spacertimerid == NULL) X spacertimerid = XtAddTimeOut(spacerwait, MoveSpacer, X (Opaque) MoveSpacer); X if (shottimerid == NULL) X shottimerid = XtAddTimeOut(shotwait, MoveShots, X (Opaque) MoveShots); X if (vshottimerid == NULL) X vshottimerid = XtAddTimeOut(vshotwait, MoveVshots, X (Opaque) MoveVshots); X} X Xstatic void HandleExpose(w, event) XWidget w; XXEvent *event; X{ X /* if (event->xexpose.count) return; */ X XSync(dpy, 0); X /* suspend events */ X SuspendTimers(); X /* rebuild the display */ X XSync(dpy, 0); X XClearWindow(dpy, gamewindow); X if (!basedestroyed) X PaintBase(basegc); X if(spacer_shown) X PaintSpacer(spacergc); X PaintAllVaders(); X PaintAllShots(); X PaintBasesLeft(); X DrawBuildings(); X PaintScore(); X XSync(dpy, 0); X /* enable events */ X if(!paused) X EnableTimers(); X} X Xvoid Quit() X{ X XCloseDisplay(dpy); X exit(0); X} X Xint paused = 1; X Xstatic void HandleFocus(w, closure, event) XWidget w; XOpaque closure; XXEvent *event; X{ X} X Xvoid Pause(w, closure, event) XWidget w; XOpaque closure; XXEvent *event; X{ X if (paused) { X paused = 0; X EnableTimers(); X } else { X paused = 1; X SuspendTimers(); X } X} X X Xstatic void HandleStruct(w, closure, event) XWidget w; XOpaque closure; XXEvent *event; X{ X} X Xstatic void Destroy() {} X Xstatic void Resize(w) XWidget w; X{ X /* X * Resize the playing field. X */ X gamewidth = w->core.width-3*(base->width+2); X gameheight = w->core.height; X} X Xstatic Boolean SetValues() {} X Xstatic Boolean TakeFocus() X{ X XSetInputFocus(dpy, gamewindow, RevertToPointerRoot, CurrentTime); X return TRUE; X} X XVadersClassRec vadersClassRec = { X { X/* core_class fields */ X /* superclass */ (WidgetClass) &widgetClassRec, X /* class_name */ "XVaders", X /* widget_size */ sizeof(VadersRec), X /* class_initialize */ ClassInitialize, X /* class_part_initiali*/ NULL, X /* class_inited */ FALSE, X /* initialize */ Initialize, X /* initialize_hook */ NULL, X /* realize */ Realize, X /* actions */ actions, X /* num_actions */ XtNumber(actions), X /* resources */ NULL, X /* num_resources */ (Cardinal) 0, X /* xrm_class */ NULLQUARK, X /* compress_motion */ TRUE, X /* compress_exposure */ TRUE, X /* compress_enterleave*/ TRUE, X /* visible_interest */ FALSE, X /* destroy */ Destroy, X /* resize */ Resize, X /* expose */ HandleExpose, X /* set_values */ SetValues, X /* set_values_hook */ NULL, X /* set_values_almost */ NULL, X /* get_values_hook */ NULL, X /* accept_focus */ TakeFocus, X /* version */ XtVersion, X /* callback_private */ NULL, X /* tm_table */ defaultTranslation, X /* query_geometry */ NULL, X /* display_accelerator*/ NULL, X /* extension` */ NULL, X },{ X /* mumble */ 0 /* Make C compiler happy */ X } X}; X XWidgetClass vadersWidgetClass = (WidgetClass)&vadersClassRec; END_OF_FILE if test 8425 -ne `wc -c <'widget.c'`; then echo shar: \"'widget.c'\" unpacked with wrong size! fi # end of 'widget.c' fi if test -f 'main.c' -a "${1}" != "-c" ; then echo shar: Will not clobber existing file \"'main.c'\" else echo shar: Extracting \"'main.c'\" \(5151 characters\) sed "s/^X//" >'main.c' <<'END_OF_FILE' X/* XCopyright notice: X XThis is mine. I'm only letting you use it. Period. Feel free to rip off Xany of the code you see fit, but have the courtesy to give me credit. XOtherwise great hairy beasties will rip your eyes out and eat your flesh Xwhen you least expect it. X XJonny Goldman <jgoldman@parc.xerox.com> X XTue Jul 17 1990 X*/ X X/* main.c -- create our windows and initialize things. */ X X#define MAIN X#include "vaders.h" X#include <X11/Xaw/Label.h> X#include <X11/Xaw/Paned.h> X Xstatic int width, height; /* Size of window. */ Xstatic Pixel defaultfore; /* Default foreground */ XPixel defaultback; /* Default background */ Xstatic Pixel scorepixel; /* Color for score. */ X Xstatic XrmOptionDescRec table[] = { X {"-debug", "*debug", XrmoptionNoArg, NULL}, X}; X Xstatic XtResource resources[] = { X {XtNwidth, XtCWidth, XtRInt, sizeof(int), X (Cardinal)&width, XtRImmediate, (caddr_t) VWIDTH}, X {XtNheight, XtCHeight, XtRInt, sizeof(int), X (Cardinal)&height, XtRImmediate, (caddr_t) VHEIGHT}, X {"debug", "Debug", XtRBoolean, sizeof(Boolean), X (Cardinal)&debug, XtRString, "off"}, X {"basewait", "BaseWait", XtRInt, sizeof(int), X (Cardinal)&basewait, XtRImmediate, (caddr_t) 10}, X {"vaderwait", "VaderWait", XtRInt, sizeof(int), X (Cardinal)&vaderwait, XtRImmediate, (caddr_t) 300}, X {"spacerwait", "SpacerWait", XtRInt, sizeof(int), X (Cardinal)&spacerwait, XtRImmediate, (caddr_t) 50}, X {"shotwait", "ShotWait", XtRInt, sizeof(int), X (Cardinal)&shotwait, XtRImmediate, (caddr_t) 10}, X {"vshotwait", "VshotWait", XtRInt, sizeof(int), X (Cardinal)&vshotwait, XtRImmediate, (caddr_t) 30}, X {"basecolor", "BaseColor", XtRPixel, sizeof(Pixel), X (Cardinal)&basepixel, XtRString, "cyan"}, X {"spacercolor", "SpacerColor", XtRPixel, sizeof(Pixel), X (Cardinal)&spacerpixel, XtRString, "gray"}, X {"buildingcolor", "BuildingColor", XtRPixel, sizeof(Pixel), X (Cardinal)&buildingpixel, XtRString, "yellow"}, X {"vader1color", "Vader1Color", XtRPixel, sizeof(Pixel), X (Cardinal)&vader1pixel, XtRString, "blue"}, X {"vader2color", "Vader2Color", XtRPixel, sizeof(Pixel), X (Cardinal)&vader2pixel, XtRString, "green"}, X {"vader3color", "Vader3Color", XtRPixel, sizeof(Pixel), X (Cardinal)&vader3pixel, XtRString, "red"}, X {"shotcolor", "ShotColor", XtRPixel, sizeof(Pixel), X (Cardinal)&shotpixel, XtRString, "lavender"}, X {"vshotcolor", "VshotColor", XtRPixel, sizeof(Pixel), X (Cardinal)&vshotpixel, XtRString, "orange"}, X {"scorecolor", "ScoreColor", XtRPixel, sizeof(Pixel), X (Cardinal)&scorepixel, XtRString, "white"}, X {"maxshots", "MaxShots", XtRInt, sizeof(int), X (Cardinal) &maxshots, XtRImmediate, (caddr_t) 1}, X {"maxvshots", "MaxVshots", XtRInt, sizeof(int), X (Cardinal) &maxvshots, XtRImmediate, (caddr_t) 6}, X {"defaultfore", "DefaultFore", XtRPixel, sizeof(Pixel), X (Cardinal) &defaultfore, XtRString, "white"}, X {"defaultback", "DefaultBack", XtRPixel, sizeof(Pixel), X (Cardinal) &defaultback, XtRString, "black"}, X}; X X Xstatic void CvtStringToFloat(args, num_args, fromVal, toVal) XArgList args; XCardinal num_args; XXrmValue *fromVal; XXrmValue *toVal; X{ X static float i; X X if (sscanf((char *)fromVal->addr, "%f", &i) == 1) { X toVal->size = sizeof(float); X toVal->addr = (caddr_t) &i; X } else { X toVal->size = 0; X toVal->addr = NULL; X } X} X Xstatic void AddResource(r, p) Xchar *r; XPixel *p; X{ X XrmValue value; X XrmDatabase db = XtDatabase(dpy); X value.size = sizeof(Pixel); X value.addr = (caddr_t) p; X XrmPutResource(&db, r, XtRPixel, &value); X} X Xmain(argc, argv) XCardinal argc; Xchar **argv; X{ X static Arg args[] = { X {XtNwidth, NULL}, X {XtNheight, NULL}, X }; X static Arg scoreargs[] = { X {XtNforeground, NULL}, X }; X Widget pane; X extern WidgetClass labelwidgetclass; X srandom(time(0)); X toplevel = XtInitialize(argv[0], "Vaders", table, XtNumber(table), X &argc, argv); X dpy = XtDisplay(toplevel); X XtAddConverter(XtRString, XtRFloat, CvtStringToFloat, NULL, 0); X XtGetApplicationResources(toplevel, (caddr_t) NULL, X resources, XtNumber(resources), X NULL, (Cardinal) 0); X AddResource("*background", &defaultback); X if (DisplayCells(dpy, DefaultScreen(dpy)) <= 2) X { X basepixel = defaultfore; X buildingpixel = defaultfore; dan ---------------------------------------------------- O'Reilly && Associates argv@sun.com / argv@ora.com Opinions expressed reflect those of the author only.