void getscreen(int argc, char **argv) { char *t; ARGBEGIN{ case 'a': autoindent = 1; break; case 'W': winsize = EARGF(usage()); break; default: usage(); }ARGEND if(initdraw(panic1, nil, "sam") < 0){ fprint(2, "samterm: initdraw: %r\n"); threadexitsall("init"); } t = getenv("tabstop"); if(t != nil) maxtab = strtoul(t, nil, 0); draw(screen, screen->clipr, display->white, nil, ZP); }
/* * Startup initialization */ void m_initialize(char *s){ static int first=1; int dx, dy; USED(s); if(first){ if(initdraw(0,0,"plot") < 0) sysfatal("initdraw: %r"); einit(Emouse); clipminx=mapminx=screen->r.min.x+4; clipminy=mapminy=screen->r.min.y+4; clipmaxx=mapmaxx=screen->r.max.x-5; clipmaxy=mapmaxy=screen->r.max.y-5; dx=clipmaxx-clipminx; dy=clipmaxy-clipminy; if(dx>dy){ mapminx+=(dx-dy)/2; mapmaxx=mapminx+dy; } else{ mapminy+=(dy-dx)/2; mapmaxy=mapminy+dx; } first=0; offscreen = screen; } }
void mapscreen(void) { if(initdraw(0, 0, "proof") < 0){ fprint(2, "proof: initdraw failed: %r\n"); exits("initdraw"); } einit(Ekeyboard|Emouse); }
void Overlay::drawGameOver(std::vector<Player *> players, int playerId, bool haswon) { //init glut overlay initdraw(); //first player to get camera size Player * playerone = players[0]; //Draw overlay red is lose otherwise green //glColor3f(!haswon, haswon, 0); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glColor4f(!haswon, haswon, 0, (GLfloat)0.55); //space between objects int margin = 200; //drawBackground: glBegin(GL_QUADS); glVertex2f((GLfloat)margin, (GLfloat)playerone->getCamera()->height - margin); glVertex2f((GLfloat)margin, (GLfloat)margin); glVertex2f((GLfloat)playerone->getCamera()->width - margin, (GLfloat)margin); glVertex2f((GLfloat)playerone->getCamera()->width - margin, (GLfloat)playerone->getCamera()->height - margin); glEnd(); margin += 25; //draw players for (int i = 0; i < players.size(); i++) { int x0 = margin; int y0 = margin + (i*125); int x1 = playerone->getCamera()->width - margin; int y1 = margin + 100 + (i * 125); bool isCurrentPlayer = false; //highlight if current player if (i == playerId) { isCurrentPlayer = true; } //draw player information drawPlayerInfo(players[i], x0, y0, x1, y1, isCurrentPlayer); } glutdrawstring("Game Over " , playerone->getCamera()->width / 2 - 75, 100, GLUT_BITMAP_TIMES_ROMAN_24); //reset settings: stopDrawing(); }
void main(int argc, char * argv[]) { Rectangle r1 = Rect(0, 0, 300, 200); Rectangle r2 = Rect(100, 100, 400, 300); Rectangle r3 = Rect(200, 100, 500, 300); Region reg; initdraw(0, 0, "vncviewer"); region_init(®); region_union(®, r1, r1); region_union(®, r2, r2); region_union(®, r3, r3); }
void threadmain(int argc, char **argv) { char *p; int lfd; p = "300x40@100,100"; ARGBEGIN{ case 'W': p = ARGF(); break; case 't': textmode = 1; break; case 'k': nokill = 1; break; default: usage(); }ARGEND; if(argc != 1) usage(); winsize = p; title = argv[0]; lfd = dup(0, -1); Binit(&b, lfd, OREAD); rbar = Rect(0, 0, 60, 1); if (!textmode){ if(initdraw(0, nil, "bar") < 0) sysfatal("initdraw: %r"); initcolor(); if((mc = initmouse(nil, screen)) == nil) sysfatal("initmouse: %r"); if((kc = initkeyboard(nil)) == nil) sysfatal("initkeyboard: %r"); display->locking = 1; threadcreate(resizethread, nil, STACK); threadcreate(keyboardthread, nil, STACK); threadcreate(mousethread, nil, STACK); resize(); unlockdisplay(display); } proccreate(updateproc, nil, STACK); }
void Overlay::drawHealthBar(Player * player, GateModel * gate) { //init glut overlay initdraw(); int margin = 100; Camera * cam = player->getCamera(); int x0 = cam->width - margin - 75; int y0 = margin; int x1 = cam->width - margin; int y1 = cam->height - margin; double xpbarHeight = y1 - y0; //xpbarbackground: glBegin(GL_QUADS); glColor3f((GLfloat)0, (GLfloat)1, (GLfloat)0); glVertex2f((GLfloat)x1, (GLfloat)y0); glVertex2f((GLfloat)x0, (GLfloat)y0); glColor3f((GLfloat)1, (GLfloat)0, (GLfloat)0); glVertex2f((GLfloat)x0, (GLfloat)y1); glVertex2f((GLfloat)x1, (GLfloat)y1); glEnd(); //drawindicator: float indicatorpos = (float)(xpbarHeight * gate->getHealth()) / 100 ; int indicatorx0 = x0 - 10; int indicatory0 = cam->height - (int)indicatorpos - 5 - margin; int indicatorx1 = x1 + 10; int indicatory1 = cam->height - (int)indicatorpos + 5 - margin; glColor3f(1, 1, 1); glBegin(GL_QUADS); glVertex2f((GLfloat)indicatorx1, (GLfloat)indicatory0); glVertex2f((GLfloat)indicatorx0, (GLfloat)indicatory0); glVertex2f((GLfloat)indicatorx0, (GLfloat)indicatory1); glVertex2f((GLfloat)indicatorx1, (GLfloat)indicatory1); glEnd(); //reset settings: stopDrawing(); }
void main(int argc, char *argv[]) { int delay = 1000; setrules(".d.d..b..d.d.d.d.d"); /* regular rules */ ARGBEGIN { case '3': setrules(".d.d.db.b..d.d.d.d"); break; /* 34-life */ case 'o': setrules(".d.d.db.b.b..d.d.d"); break; /* lineosc? */ case 'r': /* rules from cmdline */ setrules(EARGF(usage())); break; default: usage(); } ARGEND if (argc != 1) usage(); initdraw(g9err, 0, argv0); einit(Emouse|Ekeyboard); /* implies rawon() */ cen = divpt(subpt(addpt(screen->r.min, screen->r.max), Pt(NLIFE * PX, NLIFE * PX)), 2); box = allocimage(display, Rect(0, 0, BX, BX), RGB24, 1, DBlack); assert(box != nil); redraw(); readlife(argv[0]); do { flushimage(display, 1); idle(); sleep(delay); idle(); } while (generate()); exits(nil); }
void main(void) { int i; int ncolor; /* * A call to init draw populates extern variables found in draw.h * one of them is the display */ initdraw(0,0,"test"); /* * next part is based off of code from $PLAN9/src/cmd/draw/cmapcube.c */ ncolor=8; for(i = 0; i != ncolor; i++) { color[i] = allocimage(display, Rect(0, 0, 1, 1), CMAP8, 1, cmap2rgba(i)); } bg = allocimage(display, Rect(0,0,2,2), screen->chan, 1, DGreygreen); draw(bg, Rect(0, 0, 1, 1), color[0], nil, Pt(0,0)); exits(0); }
void threadmain(int argc, char *argv[]) { int i; char *p, *loadfile; Column *c; int ncol; Display *d; rfork(RFENVG|RFNAMEG); ncol = -1; loadfile = nil; ARGBEGIN{ case 'D': {extern int _threaddebuglevel; _threaddebuglevel = ~0; } break; case 'a': globalautoindent = TRUE; break; case 'b': bartflag = TRUE; break; case 'c': p = ARGF(); if(p == nil) goto Usage; ncol = atoi(p); if(ncol <= 0) goto Usage; break; case 'f': fontnames[0] = ARGF(); if(fontnames[0] == nil) goto Usage; break; case 'F': fontnames[1] = ARGF(); if(fontnames[1] == nil) goto Usage; break; case 'l': loadfile = ARGF(); if(loadfile == nil) goto Usage; break; case 'm': mtpt = ARGF(); if(mtpt == nil) goto Usage; break; case 'r': swapscrollbuttons = TRUE; break; case 'W': winsize = ARGF(); if(winsize == nil) goto Usage; break; default: Usage: fprint(2, "usage: acme -a -c ncol -f fontname -F fixedwidthfontname -l loadfile -W winsize\n"); threadexitsall("usage"); }ARGEND fontnames[0] = estrdup(fontnames[0]); fontnames[1] = estrdup(fontnames[1]); quotefmtinstall(); fmtinstall('t', timefmt); cputype = getenv("cputype"); objtype = getenv("objtype"); home = getenv("HOME"); acmeshell = getenv("acmeshell"); if(acmeshell && *acmeshell == '\0') acmeshell = nil; p = getenv("tabstop"); if(p != nil){ maxtab = strtoul(p, nil, 0); free(p); } if(maxtab == 0) maxtab = 4; if(loadfile) rowloadfonts(loadfile); putenv("font", fontnames[0]); snarffd = open("/dev/snarf", OREAD|OCEXEC); /* if(cputype){ sprint(buf, "/acme/bin/%s", cputype); bind(buf, "/bin", MBEFORE); } bind("/acme/bin", "/bin", MBEFORE); */ getwd(wdir, sizeof wdir); /* if(geninitdraw(nil, derror, fontnames[0], "acme", nil, Refnone) < 0){ fprint(2, "acme: can't open display: %r\n"); threadexitsall("geninitdraw"); } */ if(initdraw(derror, fontnames[0], "acme") < 0){ fprint(2, "acme: can't open display: %r\n"); threadexitsall("initdraw"); } d = display; font = d->defaultfont; /*assert(font); */ reffont.f = font; reffonts[0] = &reffont; incref(&reffont.ref); /* one to hold up 'font' variable */ incref(&reffont.ref); /* one to hold up reffonts[0] */ fontcache = emalloc(sizeof(Reffont*)); nfontcache = 1; fontcache[0] = &reffont; iconinit(); timerinit(); rxinit(); cwait = threadwaitchan(); ccommand = chancreate(sizeof(Command**), 0); ckill = chancreate(sizeof(Rune*), 0); cxfidalloc = chancreate(sizeof(Xfid*), 0); cxfidfree = chancreate(sizeof(Xfid*), 0); cnewwindow = chancreate(sizeof(Channel*), 0); cerr = chancreate(sizeof(char*), 0); cedit = chancreate(sizeof(int), 0); cexit = chancreate(sizeof(int), 0); cwarn = chancreate(sizeof(void*), 1); if(cwait==nil || ccommand==nil || ckill==nil || cxfidalloc==nil || cxfidfree==nil || cerr==nil || cexit==nil || cwarn==nil){ fprint(2, "acme: can't create initial channels: %r\n"); threadexitsall("channels"); } chansetname(ccommand, "ccommand"); chansetname(ckill, "ckill"); chansetname(cxfidalloc, "cxfidalloc"); chansetname(cxfidfree, "cxfidfree"); chansetname(cnewwindow, "cnewwindow"); chansetname(cerr, "cerr"); chansetname(cedit, "cedit"); chansetname(cexit, "cexit"); chansetname(cwarn, "cwarn"); mousectl = initmouse(nil, screen); if(mousectl == nil){ fprint(2, "acme: can't initialize mouse: %r\n"); threadexitsall("mouse"); } mouse = &mousectl->m; keyboardctl = initkeyboard(nil); if(keyboardctl == nil){ fprint(2, "acme: can't initialize keyboard: %r\n"); threadexitsall("keyboard"); } mainpid = getpid(); startplumbing(); /* plumbeditfd = plumbopen("edit", OREAD|OCEXEC); if(plumbeditfd < 0) fprint(2, "acme: can't initialize plumber: %r\n"); else{ cplumb = chancreate(sizeof(Plumbmsg*), 0); threadcreate(plumbproc, nil, STACK); } plumbsendfd = plumbopen("send", OWRITE|OCEXEC); */ fsysinit(); #define WPERCOL 8 disk = diskinit(); if(!loadfile || !rowload(&row, loadfile, TRUE)){ rowinit(&row, screen->clipr); if(ncol < 0){ if(argc == 0) ncol = 2; else{ ncol = (argc+(WPERCOL-1))/WPERCOL; if(ncol < 2) ncol = 2; } } if(ncol == 0) ncol = 2; for(i=0; i<ncol; i++){ c = rowadd(&row, nil, -1); if(c==nil && i==0) error("initializing columns"); } c = row.col[row.ncol-1]; if(argc == 0) readfile(c, wdir); else for(i=0; i<argc; i++){ p = utfrrune(argv[i], '/'); if((p!=nil && strcmp(p, "/guide")==0) || i/WPERCOL>=row.ncol) readfile(c, argv[i]); else readfile(row.col[i/WPERCOL], argv[i]); } } flushimage(display, 1); acmeerrorinit(); threadcreate(keyboardthread, nil, STACK); threadcreate(mousethread, nil, STACK); threadcreate(waitthread, nil, STACK); threadcreate(xfidallocthread, nil, STACK); threadcreate(newwindowthread, nil, STACK); /* threadcreate(shutdownthread, nil, STACK); */ threadnotify(shutdown, 1); recvul(cexit); killprocs(); threadexitsall(nil); }
void lockscreen(void) { enum { Nfld = 5, Fldlen = 12, Cursorlen = 2*4 + 2*2*16, }; char *s; char buf[Nfld*Fldlen], *flds[Nfld], newcmd[128], cbuf[Cursorlen]; int fd, dx, dy; Image *i; Point p; Rectangle r; Tm *tm; fd = open("/dev/screen", OREAD); if(fd < 0) error("can't open /dev/screen: %r"); if(read(fd, buf, Nfld*Fldlen) != Nfld*Fldlen) error("can't read /dev/screen: %r"); close(fd); buf[sizeof buf-1] = 0; if(tokenize(buf, flds, Nfld) != Nfld) error("can't tokenize /dev/screen header"); snprint(newcmd, sizeof newcmd, "-r %s %s %d %d", flds[1], flds[2], atoi(flds[3]) - 1, atoi(flds[4]) - 1); newwindow(newcmd); if (initdraw(nil, nil, "screenlock") < 0) sysfatal("initdraw failed"); if(display == nil) error("no display"); /* screen is now open and covered. grab mouse and hold on tight */ procrfork(grabmouse, nil, 4096, RFFDG); procrfork(blanker, nil, 4096, RFFDG); fd = open(pic, OREAD); if(fd > 0){ i = readimage(display, fd, 0); if(i){ r = screen->r; p = Pt(r.max.x / 2, r.max.y * 2 / 3); dx = (Dx(screen->r) - Dx(i->r)) / 2; r.min.x += dx; r.max.x -= dx; dy = (Dy(screen->r) - Dy(i->r)) / 2; r.min.y += dy; r.max.y -= dy; draw(screen, screen->r, display->black, nil, ZP); draw(screen, r, i, nil, i->r.min); flushimage(display, 1); } close(fd); /* identify the user on screen, centered */ tm = localtime(time(0)); s = smprint("user %s at %d:%02.2d", getuser(), tm->hour, tm->min); p = subpt(p, Pt(stringwidth(font, "m") * strlen(s) / 2, 0)); screenstring(p, s); } /* clear the cursor */ fd = open("/dev/cursor", OWRITE); if(fd > 0){ memset(cbuf, 0, sizeof cbuf); write(fd, cbuf, sizeof cbuf); /* leave it open */ } }
void main(void) { int c, n; char str[100], *args[10]; int jfont, curfont; if(initdraw(0, fontfile, 0) < 0){ fprint(2, "mnihongo: can't initialize display: %r\n"); exits("open"); } Binit(&bin, 0, OREAD); Binit(&bout, 1, OWRITE); jfont = -1; curfont = 1; while ((c = Bgetc(&bin)) >= 0) { switch (c) { case '\n': /* when input is text */ case ' ': case '\0': /* occasional noise creeps in */ putchar(c); break; case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': /* two motion digits plus a character */ putchar(c); /* digit 1 */ n = (c-'0')*10; c = Bgetc(&bin); putchar(c); /* digit 2 */ n += c - '0'; hmot(n); putchar(Bgetc(&bin)); /* char itself */ break; case 'c': /* single character */ c = Bgetrune(&bin); if(c==' ') /* why does this happen? it's troff - bwk */ break; else if(jfont == curfont){ Bungetrune(&bin); Bgetstr(&bin, str); kanji(str); }else{ putchar('c'); putchar(c); } break; case 'C': Bgetstr(&bin, str); Bprint(&bout, "C%s", str); break; case 'f': Bgetstr(&bin, str); curfont = atoi(str); if(curfont < 0 || curfont > 20) curfont = 1; /* sanity */ Bprint(&bout, "%c%s", c, str); break; case 'N': /* absolute character number */ case 's': case 'p': /* new page */ Bgetint(&bin, &n); Bprint(&bout, "%c%d", c, n); break; case 'H': /* absolute horizontal motion */ Bgetint(&bin, &n); Bprint(&bout, "%c%d", c, n); hgoto(n); break; case 'h': /* relative horizontal motion */ Bgetint(&bin, &n); Bprint(&bout, "%c%d", c, n); hmot(n); break; case 'V': Bgetint(&bin, &n); Bprint(&bout, "%c%d", c, n); vgoto(n); break; case 'v': Bgetint(&bin, &n); Bprint(&bout, "%c%d", c, n); vmot(n); break; case 'w': /* word space */ putchar(c); break; case 'x': /* device control */ Bgetline(&bin, str); Bprint(&bout, "%c%s", c, str); if(tokenize(str, args, 10)>2 && args[0][0]=='f' && ('0'<=args[1][0] && args[1][0]<='9')){ if(strncmp(args[2], "Jp", 2) == 0) jfont = atoi(args[1]); else if(atoi(args[1]) == jfont) jfont = -1; } break; case 'D': /* draw function */ case 'n': /* end of line */ case '#': /* comment */ Bgetline(&bin, str); Bprint(&bout, "%c%s", c, str); break; default: fprint(2, "mnihongo: unknown input character %o %c\n", c, c); exits("error"); } } }
void threadmain(int argc, char *argv[]) { int i, j, k, l; Image *dark; ramp = 0; fmt = "index %3d r %3lud g %3lud b %3lud 0x%.8luX "; ARGBEGIN{ default: goto Usage; case 'D': chattydrawclient = 1; break; case 'x': fmt = "index %2luX r %3luX g %3luX b %3luX 0x%.8luX "; break; case 'r': ramp = 1; break; }ARGEND if(argc){ Usage: fprint(2, "Usage: %s [-rx]\n", argv0); threadexitsall("usage"); } if(initdraw(0, nil, "colors") < 0) sysfatal("initdraw failed: %r"); mousectl = initmouse(nil, display->image); if(mousectl == nil) sysfatal("initmouse: %r"); keyboardctl = initkeyboard(nil); if(keyboardctl == nil) sysfatal("initkeyboard: %r"); for(i=0; i<256; i++){ if(ramp){ if(screen->chan == CMAP8){ /* dither the fine grey */ j = i-(i%17); dark = allocimage(display, Rect(0,0,1,1), screen->chan, 1, (grey(j)<<8)+0xFF); color[i] = allocimage(display, Rect(0,0,4,4), screen->chan, 1, (grey(j+17)<<8)+0xFF); for(j=0; j<16; j++){ k = j%4; l = j/4; if(dither[j] > (i%17)) draw(color[i], Rect(k, l, k+1, l+1), dark, nil, ZP); } freeimage(dark); }else color[i] = allocimage(display, Rect(0,0,1,1), screen->chan, 1, (grey(i)<<8)+0xFF); }else color[i] = allocimage(display, Rect(0,0,1,1), screen->chan, 1, (cmap2rgb(i)<<8)+0xFF); if(color[i] == nil) sysfatal("can't allocate image: %r"); } threadcreate(mousethread, nil, STACK); threadcreate(keyboardthread, nil, STACK); threadcreate(resizethread, nil, STACK); }
void main(int argc, char **argv) { int p, fd, dfd, cfd, shared; char *keypattern, *addr; Point d; TLSconn conn; keypattern = nil; shared = 0; ARGBEGIN{ case 'c': bpp12 = 1; break; case 'e': encodings = EARGF(usage()); break; case 's': shared = 1; break; case 't': tls = 1; break; case 'v': verbose = 1; break; case 'k': keypattern = EARGF(usage()); break; default: usage(); }ARGEND; if(argc != 1) usage(); addr = netmkvncaddr(argv[0]); serveraddr = argv[0]; dfd = dial(addr, nil, nil, &cfd); if(dfd < 0) sysfatal("cannot dial %s: %r", addr); if(tls){ dfd = tlsClient(dfd, &conn); if(dfd < 0) sysfatal("tlsClient: %r"); /* XXX check thumbprint */ } vnc = vncinit(dfd, cfd, nil); if(vnchandshake(vnc) < 0) sysfatal("handshake failure: %r"); if(vncauth(vnc, keypattern) < 0) sysfatal("authentication failure: %r"); if(vncstart(vnc, shared) < 0) sysfatal("init failure: %r"); initdraw(0, 0, "vncv"); display->locking = 1; unlockdisplay(display); d = addpt(vnc->dim, Pt(2*Borderwidth, 2*Borderwidth)); if(verbose) fprint(2, "screen size %P, desktop size %P\n", display->image->r.max, d); choosecolor(vnc); sendencodings(vnc); initmouse(); rfork(RFREND); atexit(shutdown); pids[0] = getpid(); switch(p = rfork(RFPROC|RFMEM)){ case -1: sysfatal("rfork: %r"); default: break; case 0: atexit(shutdown); readfromserver(vnc); exits(nil); } pids[1] = p; switch(p = rfork(RFPROC|RFMEM)){ case -1: sysfatal("rfork: %r"); default: break; case 0: atexit(shutdown); checksnarf(vnc); exits(nil); } pids[2] = p; fd = open("/dev/label", OWRITE); if(fd >= 0){ fprint(fd, "vnc %s", serveraddr); close(fd); } if(access("/dev/snarf", AEXIST) >= 0){ switch(p = rfork(RFPROC|RFMEM)){ case -1: sysfatal("rfork: %r"); default: break; case 0: atexit(shutdown); readkbd(vnc); exits(nil); } } pids[3] = p; readmouse(vnc); exits(nil); }
void threadmain(int argc, char *argv[]) { Column *c; char buf[256]; int i, ncol; rfork(RFENVG|RFNAMEG); ncol = 1; ARGBEGIN{ case 'c': ncol = atoi(EARGF(usage())); if(ncol <= 0) usage(); break; case 'm': webmountpt = EARGF(usage()); break; case 'p': procstderr++; break; case 't': charset = EARGF(usage()); break; default: usage(); break; }ARGEND snprint(buf, sizeof(buf), "%s/ctl", webmountpt); webctlfd = open(buf, ORDWR); if(webctlfd < 0) sysfatal("can't initialize webfs: %r"); snarffd = open("/dev/snarf", OREAD|OCEXEC); if(initdraw(derror, fontnames[0], "abaco") < 0) sysfatal("can't open display: %r"); memimageinit(); iconinit(); timerinit(); initfontpaths(); cexit = chancreate(sizeof(int), 0); crefresh = chancreate(sizeof(Page *), 0); if(cexit==nil || crefresh==nil) sysfatal("can't create initial channels: %r"); mousectl = initmouse(nil, screen); if(mousectl == nil) sysfatal("can't initialize mouse: %r"); mouse = mousectl; keyboardctl = initkeyboard(nil); if(keyboardctl == nil) sysfatal("can't initialize keyboard: %r"); mainpid = getpid(); plumbwebfd = plumbopen("web", OREAD|OCEXEC); if(plumbwebfd >= 0){ cplumb = chancreate(sizeof(Plumbmsg*), 0); proccreate(plumbproc, nil, STACK); } plumbsendfd = plumbopen("send", OWRITE|OCEXEC); rowinit(&row, screen->clipr); for(i=0; i<ncol; i++){ c = rowadd(&row, nil, -1); if(c==nil && i==0) error("initializing columns"); } c = row.col[row.ncol-1]; for(i=0; i<argc; i++) if(i/WPERCOL >= row.ncol) readpage(c, argv[i]); else readpage(row.col[i/WPERCOL], argv[i]); flushimage(display, 1); threadcreate(keyboardthread, nil, STACK); threadcreate(mousethread, nil, STACK); threadnotify(shutdown, 1); recvul(cexit); threadexitsall(nil); }
void main(int argc, char *argv[]) { Mouse m; int i, j; unsigned short ran, score, attempt, prev, br[2]; Image *c[2]; char *fmt; level = 16; fmt = "win in %d attempts!"; ARGBEGIN{ default: goto Usage; case 'h': level=36; break; }ARGEND if(argc){ Usage: fprint(2, "usage: %s [-h]\n", argv0); exits("usage"); } if(initdraw(0,0,"memo") < 0) sysfatal("initdraw failed: %r"); srand(time(0)); memoinit(); einit(Emouse); Start: afaces(); winflag=0; prev=level+1; score=attempt=0; for(i=0;i!=level;i++) block[i].flag = Eninit; for(i=0;i!=level/2;i++){ for(j=0;j!=2;){ ran = rand()%level; if(block[ran].flag == Eninit){ block[ran].face = face[i]; block[ran].flag = Eshow; j++; } } } eresized(0); for(;;m=emouse()) if(m.buttons) break; for(i=0;i!=level;i++) block[i].flag = Ehide; redraw(); j = 0; for(;; m=emouse()){ switch(m.buttons){ case 1: while(m.buttons){ for(i=0;i!=level;i++){ if(i!=prev && ptinrect(m.xy,block[i].r)){ if(block[i].flag==Ehide && j<2){ block[i].flag = Eshow; draw(screen, block[i].r, block[i].face, nil, ZP); c[j] = block[i].face; br[j] = prev = i; j++; } break; } } m=emouse(); } break; case 4: switch(emenuhit(3, &m, &menu)) { case 0: /* restart */ goto Start; break; case 1: level=16; goto Start; break; case 2: level=36; goto Start; break; case 3: exits(0); break; } } if(j==2){ attempt++; prev = level+1; j = 0; if(c[0] == c[1]){ score++; block[br[0]].flag = Edisc; block[br[1]].flag = Edisc; } else{ block[br[0]].flag = Ehide; block[br[1]].flag = Ehide; } redraw(); continue; } if(score == level/2){ winflag = 1; sprint(buf, fmt, attempt); redraw(); for(;;m=emouse()) if(m.buttons&1 || m.buttons&4) break; goto Start; } } }
void initialize(int argc, char **argv) { int i, blkbg; char *fontname, *p; rfork(RFNAMEG|RFNOTEG); fontname = nil; term = "vt100"; fk = vt100fk; blkbg = nocolor = 0; ARGBEGIN{ case '2': term = "vt220"; fk = vt220fk; break; case 'a': term = "ansi"; fk = ansifk; break; case 'b': blkbg = 1; /* e.g., for linux colored output */ break; case 'c': nocolor = 1; break; case 'f': fontname = EARGF(usage()); break; case 'l': p = EARGF(usage()); logfd = create(p, OWRITE, 0666); if(logfd < 0) sysfatal("could not create log file: %s: %r", p); break; case 'x': fk = xtermfk; term = "xterm"; break; default: usage(); break; }ARGEND; host_buf = malloc(host_bsize); hostp = host_buf; hostlength = 0; if(initdraw(0, fontname, term) < 0){ fprint(2, "%s: initdraw failed: %r\n", term); exits("initdraw"); } werrstr(""); /* clear spurious error messages */ ebegin(Ehost); histp = hist; menu2.item = menutext2; menu3.item = menutext3; pagemode = 0; blocked = 0; NS = font->height; CW = stringwidth(font, "m"); red = allocimage(display, Rect(0,0,1,1), screen->chan, 1, DRed); bordercol = allocimage(display, Rect(0,0,1,1), screen->chan, 1, 0xCCCCCCCC); cursback = allocimage(display, Rect(0, 0, CW+1, NS+1), screen->chan, 0, DNofill); for(i=0; i<8; i++){ colors[i] = allocimage(display, Rect(0,0,1,1), screen->chan, 1, rgbacolors[i]); hicolors[i] = allocimage(display, Rect(0,0,1,1), screen->chan, 1, rgbahicolors[i]); } bgdefault = (blkbg? display->black: display->white); fgdefault = (blkbg? display->white: display->black); bgcolor = bgdefault; fgcolor = fgdefault; resize(); if(argc > 0) { sendnchars(strlen(argv[0]),argv[0]); sendnchars(1,"\n"); } }