1 /* Federico Benavento <benavento@gmail.com> */
17 void allocblocks(void);
18 Image *openface(char *path);
22 ushort winflag, level;
56 main(int argc, char *argv[])
60 ushort ran, score, attempt, prev, br[2];
65 fmt = "win in %d attempts!";
77 fprint(2, "usage: %s [-h]\n", argv0);
80 if(initdraw(0,0,"memo") < 0)
81 sysfatal("initdraw failed: %r");
92 block[i].flag = Eninit;
94 for(i=0;i!=level/2;i++){
97 if(block[ran].flag == Eninit){
98 block[ran].face = face[i];
99 block[ran].flag = Eshow;
109 for(i=0;i!=level;i++)
110 block[i].flag = Ehide;
118 for(i=0;i!=level;i++){
119 if(i!=prev && ptinrect(m.xy,block[i].r)){
120 if(block[i].flag==Ehide && j<2){
121 block[i].flag = Eshow;
122 draw(screen, block[i].r, block[i].face, nil, ZP);
123 c[j] = block[i].face;
134 switch(emenuhit(3, &m, &menu)) {
135 case 0: /* restart */
157 block[br[0]].flag = Edisc;
158 block[br[1]].flag = Edisc;
160 block[br[0]].flag = Ehide;
161 block[br[1]].flag = Ehide;
166 if(score == level/2){
168 sprint(buf, fmt, attempt);
171 if(m.buttons&1 || m.buttons&4)
181 back = allocimagemix(display, DPalebluegreen,DWhite);
182 fore = allocimagemix(display, 0x00DDDDFF, 0x00DDDDFF);
191 if(new && getwindow(display, Refnone) < 0){
192 fprint(2, "can't reattach to window");
197 p = Pt(Dx(screen->r)+8, Dy(screen->r)+8);
198 if(!new || !eqpt(p, Pt(Facesize*sq+sq*4+17, Facesize*sq+sq*4+17)))
199 resize(Facesize*sq+sq*4+17);
202 draw(screen, screen->r, back, nil, ZP);
214 p = Pt(Dx(screen->r)/8, Dy(screen->r)/4);
216 r.min = addpt(r.min, p);
217 r.max = subpt(r.max, p);
218 draw(screen, r, fore, nil, ZP);
219 p=addpt(r.min, Pt(5,5));
220 string(screen,p,display->black,ZP,font,buf);
224 for(i=0;i!=level;i++){
226 switch(block[i].flag){
228 draw(screen, r,block[i].face,nil,ZP);
231 draw(screen, r, back, nil, ZP);
234 draw(screen, r, fore, nil, ZP);
237 fprint(2, "something went wrong!");
244 char *facepaths[] = {
246 "/lib/face/48x48x4/g/glenda.1",
247 "/lib/face/48x48x2/p/pjw+9ball.2",
249 /* /sys/doc/9.ms authors */
250 "/lib/face/48x48x2/k/ken.1",
251 "/lib/face/48x48x4/b/bobf.1",
252 "/lib/face/48x48x4/p/philw.1",
253 "/lib/face/48x48x4/p/presotto.1",
254 "/lib/face/48x48x4/r/rob.1",
255 "/lib/face/48x48x4/s/sean.1",
257 /* additional authors and luminaries for harder levels */
258 "/lib/face/48x48x4/b/bwk.1",
259 "/lib/face/48x48x4/c/cyoung.1",
260 "/lib/face/48x48x4/d/dmr.1",
261 "/lib/face/48x48x4/d/doug.1",
262 "/lib/face/48x48x4/h/howard.1",
263 "/lib/face/48x48x4/j/jmk.1",
264 "/lib/face/48x48x4/s/sape.1",
265 "/lib/face/48x48x4/s/seanq.1",
266 "/lib/face/48x48x4/t/td.1",
267 "/lib/face/48x48x8/l/lucent.1",
276 face[i] = openface(facepaths[i]);
284 fd = open("/dev/wctl", OWRITE);
286 fprint(fd, "resize -dx %d -dy %d", i, i);
292 openimage(char *path)
297 fd = open(path, OREAD);
299 sysfatal("open %s: %r", path);
300 i = readimage(display, fd, 0);
302 sysfatal("readimage %s: %r", path);
314 r = insetrect(screen->r, 5);
315 r.max.x = r.min.x+Facesize*sq+sq*4-1;
316 r.max.y = r.min.y+Facesize*sq+sq*4-1;
318 for(i=level-1, y=0; y!=sq; y++){
320 b.max.y = r.min.y+Dy(r)*(y+1)/sq;
322 for(x=0; x!=sq; x++, i--){
324 b.max.x = r.min.x+Dx(r)*(x+1)/sq;
325 block[i].r = insetrect(b, 2 );
331 readbit(int fd, ulong chan, char *path)
333 char buf[4096], hx[4], *p;
334 uchar data[Facesize*Facesize]; /* more than enough */
335 int nhx, i, n, ndata, nbit;
338 n = readn(fd, buf, sizeof buf);
347 nbit = chantodepth(chan);
348 ndata = (Facesize*Facesize*nbit)/8;
351 p = strpbrk(p+1, "0123456789abcdefABCDEF");
354 if(p[0] == '0' && p[1] == 'x')
360 i = strtoul(hx, 0, 16);
366 sysfatal("short face %s", path);
368 img = allocimage(display, Rect(0,0,Facesize,Facesize), chan, 0, 0);
372 loadimage(img, img->r, data, ndata);
382 p = strstr(path, "48x48x");
384 return openimage(path);
387 if((fd = open(path, OREAD)) < 0)
388 sysfatal("open %s: %r", path);
389 return readbit(fd, n==1 ? GREY1 : GREY2, path);
391 return openimage(path);