/* Xroach - A game of skill. Try to find the roaches under your windows. Copyright 1991 by J.T. Anderson jta@locus.com This program may be freely distributed provided that all copyright notices are retained. To build: cc -o xroach roach.c -lX11 [-lsocketorwhatever] [-lm] [-l...] Dedicated to Greg McFarlane. (gregm@otc.otca.oz.au) */ //#include //#include //#include #include #include #include #include #include #define MWINCLUDECOLORS #include "nano-X.h" #include "XtoNX.h" char Copyright[] = "Xroach\nCopyright 1991 J.T. Anderson"; #include "roachmap.h" typedef unsigned long Pixel; typedef int ErrorHandler(); #define SCAMPER_EVENT 99 // LASTEvent+1? #if !defined(GRAB_SERVER) #define GRAB_SERVER 0 #endif STATIC_FUNCTIONS; Display *display; int screen; Window rootWin; unsigned int display_width, display_height; int center_x, center_y; GC gc; char *display_name = NULL; Pixel black, white; int done = 0; int eventBlock = 0; int errorVal = 0; typedef struct Roach { RoachMap *rp; int index; float x; float y; int intX; int intY; int hidden; int turnLeft; int steps; } Roach; Roach *roaches; int maxRoaches = 10; int curRoaches = 0; float roachSpeed = 20.0; Region rootVisible = 0; void Usage(void); void SigHandler(int sig); void AddRoach(void); void MoveRoach(int Rx); void DrawRoaches(void); void CoverRoot(void); int CalcRootVisible(void); int MarkHiddenRoaches(void); //Pixel AllocNamedColor(); int main(int ac, char **av) { //XGCValues xgcv; int ax; char *arg; RoachMap *rp; int rx; float angle; XEvent ev; char *roachColor = "black"; int nVis; int needCalc; /* Process command line options. */ for (ax=1; axpixmap = XCreateBitmapFromData(display, rootWin, //rp->roachBits, rp->width, rp->height); rp->pixmap = XCreatePixmapFromBitmapData(display, rootWin, rp->roachBits, rp->width, rp->height, BLACK, CYAN, 1); //FIXME rp->sine = sin(angle); rp->cosine = cos(angle); } roaches = (Roach *)malloc(sizeof(Roach) * maxRoaches); gc = XCreateGC(display, rootWin, 0L, &xgcv); //XSetForeground(display, gc, AllocNamedColor(roachColor, black)); //XSetFillStyle(display, gc, FillStippled); while (curRoaches < maxRoaches) AddRoach(); XSelectInput(display, rootWin, ExposureMask | SubstructureNotifyMask); needCalc = 1; while (!done) { if (XPending(display)) { XNextEvent(display, &ev); } else { if (needCalc) { needCalc = CalcRootVisible(); } nVis = MarkHiddenRoaches(); if (nVis) { ev.type = SCAMPER_EVENT; } else { DrawRoaches(); GrDelay(100); eventBlock = 1; XNextEvent(display, &ev); eventBlock = 0; } } switch (ev.type) { case SCAMPER_EVENT: for (rx=0; rxrp->width) > (x + width)) return 0; if (ry < y) return 0; if ((ry + roach->rp->height) > (y + height)) return 0; return 1; } /* Check for roach overlapping specified rectangle. */ int RoachOverRect(roach, rx, ry, x, y, width, height) Roach *roach; int rx; int ry; int x; int y; unsigned int width; unsigned int height; { if (rx >= (x + width)) return 0; if ((rx + roach->rp->width) <= x) return 0; if (ry >= (y + height)) return 0; if ((ry + roach->rp->height) <= y) return 0; return 1; } /* Give birth to a roach. */ void AddRoach(void) { Roach *r; if (curRoaches < maxRoaches) { r = &roaches[curRoaches++]; r->index = RandInt(ROACH_HEADINGS); r->rp = &roachPix[r->index]; r->x = RandInt(display_width - r->rp->width); r->y = RandInt(display_height - r->rp->height); r->intX = -1; r->intY = -1; r->hidden = 0; r->steps = RandInt(200); r->turnLeft = RandInt(100) >= 50; } } /* Turn a roach. */ void TurnRoach(roach) Roach *roach; { if (roach->index != (roach->rp - roachPix)) return; if (roach->turnLeft) { roach->index += (RandInt(30) / 10) + 1; if (roach->index >= ROACH_HEADINGS) roach->index -= ROACH_HEADINGS; } else { roach->index -= (RandInt(30) / 10) + 1; if (roach->index < 0) roach->index += ROACH_HEADINGS; } } /* Move a roach. */ void MoveRoach(int rx) { Roach *roach; Roach *r2; float newX; float newY; int ii; roach = &roaches[rx]; newX = roach->x + (roachSpeed * roach->rp->cosine); newY = roach->y - (roachSpeed * roach->rp->sine); if (RoachInRect(roach, (int)newX, (int)newY, 0, 0, display_width, display_height)) { roach->x = newX; roach->y = newY; if (roach->steps-- <= 0) { TurnRoach(roach); roach->steps = RandInt(200); } for (ii=rx+1; iiintX, r2->intY, r2->rp->width, r2->rp->height)) { TurnRoach(roach); } } } else { TurnRoach(roach); } } /* Draw all roaches. */ void DrawRoaches(void) { Roach *roach; int rx; for (rx=0; rxintX >= 0) { XClearArea(display, rootWin, roach->intX, roach->intY, roach->rp->width, roach->rp->height, False); } } for (rx=0; rxhidden) { roach->intX = roach->x; roach->intY = roach->y; roach->rp = &roachPix[roach->index]; //XSetStipple(display, gc, roach->rp->pixmap); //XSetTSOrigin(display, gc, roach->intX, roach->intY); //XFillRectangle(display, rootWin, gc, //roach->intX, roach->intY, roach->rp->width, roach->rp->height); GrCopyArea(GR_ROOT_WINDOW_ID, gc, roach->intX, roach->intY, roach->rp->width, roach->rp->height, roach->rp->pixmap, 0, 0, MWROP_SRCCOPY); } else { roach->intX = -1; } } } /* Cover root window to erase roaches. */ void CoverRoot(void) { //XSetWindowAttributes xswa; //long wamask; Window roachWin; #define CopyFromParent 0 #define InputOutput 0 //xswa.background_pixmap = ParentRelative; //xswa.override_redirect = True; //wamask = CWBackPixmap | CWOverrideRedirect; roachWin = XCreateWindow(display, rootWin, 0, 0, display_width, display_height, 0, CopyFromParent, InputOutput, CopyFromParent, wamask, &xswa); XLowerWindow(display, roachWin); XMapWindow(display, roachWin); XFlush(display); } #if !GRAB_SERVER #if 0 int RoachErrors(dpy, err) Display *dpy; XErrorEvent *err; { errorVal = err->error_code; return 0; } #endif #endif /* GRAB_SERVER */ /* Calculate Visible region of root window. */ int CalcRootVisible(void) { Region covered; Region visible; Window *children; int nChildren; Window dummy; //XWindowAttributes wa; int wx; XRectangle rect; int winX, winY; unsigned int winHeight, winWidth; unsigned int borderWidth; unsigned int depth; GR_WINDOW_INFO info; /* If we don't grab the server, the XGetWindowAttribute or XGetGeometry calls can abort us. On the other hand, the server grabs can make for some annoying delays. */ #if GRAB_SERVER //XGrabServer(display); #else //XSetErrorHandler(RoachErrors); #endif /* Get children of root. */ XQueryTree(display, rootWin, &dummy, &dummy, &children, &nChildren); /* For each mapped child, add the window rectangle to the covered region. */ covered = XCreateRegion(); for (wx=0; wx