memo.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. /* Federico Benavento <benavento@gmail.com> */
  2. #include <u.h>
  3. #include <libc.h>
  4. #include <draw.h>
  5. #include <event.h>
  6. void memoinit(void);
  7. void redraw(void);
  8. void eresized(int);
  9. void resize(int i);
  10. void afaces(void);
  11. void allocblocks(void);
  12. Image *openface(char *path);
  13. Image *face[18];
  14. char buf[100];
  15. ushort winflag, level;
  16. Image *back;
  17. Image *fore;
  18. enum
  19. {
  20. Eninit,
  21. Eshow,
  22. Ehide,
  23. Edisc,
  24. };
  25. struct
  26. {
  27. Image *face;
  28. Rectangle r;
  29. int flag;
  30. }block[36];
  31. char *buttons[] =
  32. {
  33. "restart",
  34. "easy",
  35. "hard",
  36. "exit",
  37. 0
  38. };
  39. Menu menu =
  40. {
  41. buttons
  42. };
  43. void
  44. main(int argc, char *argv[])
  45. {
  46. Mouse m;
  47. int i, j;
  48. ushort ran, score, attempt, prev, br[2];
  49. Image *c[2];
  50. char *fmt;
  51. level = 16;
  52. fmt = "win in %d attempts!";
  53. ARGBEGIN{
  54. default:
  55. goto Usage;
  56. case 'h':
  57. level=36;
  58. break;
  59. }ARGEND
  60. if(argc){
  61. Usage:
  62. fprint(2, "usage: %s [-h]\n", argv0);
  63. exits("usage");
  64. }
  65. if(initdraw(0,0,"memo") < 0)
  66. sysfatal("initdraw failed: %r");
  67. srand(time(0));
  68. memoinit();
  69. einit(Emouse);
  70. Start:
  71. afaces();
  72. winflag=0;
  73. prev=level+1;
  74. score=attempt=0;
  75. for(i=0;i!=level;i++)
  76. block[i].flag = Eninit;
  77. for(i=0;i!=level/2;i++){
  78. for(j=0;j!=2;){
  79. ran = rand()%level;
  80. if(block[ran].flag == Eninit){
  81. block[ran].face = face[i];
  82. block[ran].flag = Eshow;
  83. j++;
  84. }
  85. }
  86. }
  87. eresized(0);
  88. for(;;m=emouse())
  89. if(m.buttons)
  90. break;
  91. for(i=0;i!=level;i++)
  92. block[i].flag = Ehide;
  93. redraw();
  94. j = 0;
  95. for(;; m=emouse()){
  96. switch(m.buttons){
  97. case 1:
  98. while(m.buttons){
  99. for(i=0;i!=level;i++){
  100. if(i!=prev && ptinrect(m.xy,block[i].r)){
  101. if(block[i].flag==Ehide && j<2){
  102. block[i].flag = Eshow;
  103. draw(screen, block[i].r, block[i].face, nil, ZP);
  104. c[j] = block[i].face;
  105. br[j] = prev = i;
  106. j++;
  107. }
  108. break;
  109. }
  110. }
  111. m=emouse();
  112. }
  113. break;
  114. case 4:
  115. switch(emenuhit(3, &m, &menu)) {
  116. case 0: /* restart */
  117. goto Start;
  118. break;
  119. case 1:
  120. level=16;
  121. goto Start;
  122. break;
  123. case 2:
  124. level=36;
  125. goto Start;
  126. break;
  127. case 3:
  128. exits(0);
  129. break;
  130. }
  131. }
  132. if(j==2){
  133. attempt++;
  134. prev = level+1;
  135. j = 0;
  136. if(c[0] == c[1]){
  137. score++;
  138. block[br[0]].flag = Edisc;
  139. block[br[1]].flag = Edisc;
  140. } else{
  141. block[br[0]].flag = Ehide;
  142. block[br[1]].flag = Ehide;
  143. }
  144. redraw();
  145. continue;
  146. }
  147. if(score == level/2){
  148. winflag = 1;
  149. sprint(buf, fmt, attempt);
  150. redraw();
  151. for(;;m=emouse())
  152. if(m.buttons&1 || m.buttons&4)
  153. break;
  154. goto Start;
  155. }
  156. }
  157. }
  158. void
  159. memoinit(void)
  160. {
  161. back = allocimagemix(display, DPalebluegreen,DWhite);
  162. fore = allocimagemix(display, 0x00DDDDFF, 0x00DDDDFF);
  163. }
  164. void
  165. eresized(int new)
  166. {
  167. if(new && getwindow(display, Refnone) < 0){
  168. fprint(2, "can't reattach to window");
  169. exits("resized");
  170. }
  171. allocblocks();
  172. draw(screen, screen->r, back, nil, ZP);
  173. redraw();
  174. }
  175. void
  176. redraw(void)
  177. {
  178. int i, nx, ny;
  179. Rectangle r;
  180. Point p;
  181. if(winflag == 1){
  182. p = Pt(Dx(screen->r)/8, Dy(screen->r)/4);
  183. r = screen->r;
  184. r.min = addpt(r.min, p);
  185. r.max = subpt(r.max, p);
  186. draw(screen, r, fore, nil, ZP);
  187. p=addpt(r.min, Pt(5,5));
  188. string(screen,p,display->black,ZP,font,buf);
  189. return;
  190. }
  191. for(i=0;i!=level;i++){
  192. r = block[i].r;
  193. switch(block[i].flag){
  194. case Eshow:
  195. draw(screen, r,block[i].face,nil,ZP);
  196. break;
  197. case Edisc:
  198. draw(screen, r, back, nil, ZP);
  199. break;
  200. case Ehide:
  201. draw(screen, r, fore, nil, ZP);
  202. break;
  203. default:
  204. fprint(2, "something went wrong!");
  205. exits("wrong");
  206. break;
  207. }
  208. }
  209. }
  210. char *facepaths[] = {
  211. /* logos */
  212. "/lib/face/48x48x4/g/glenda.1",
  213. "/lib/face/48x48x2/p/pjw+9ball.2",
  214. /* /sys/doc/9.ms authors */
  215. "/lib/face/48x48x2/k/ken.1",
  216. "/lib/face/48x48x4/b/bobf.1",
  217. "/lib/face/48x48x4/p/philw.1",
  218. "/lib/face/48x48x4/p/presotto.1",
  219. "/lib/face/48x48x4/r/rob.1",
  220. "/lib/face/48x48x4/s/sean.1",
  221. /* additional authors and luminaries for harder levels */
  222. "/lib/face/48x48x4/b/bwk.1",
  223. "/lib/face/48x48x4/c/cyoung.1",
  224. "/lib/face/48x48x4/d/dmr.1",
  225. "/lib/face/48x48x4/d/doug.1",
  226. "/lib/face/48x48x4/h/howard.1",
  227. "/lib/face/48x48x4/j/jmk.1",
  228. "/lib/face/48x48x4/s/sape.1",
  229. "/lib/face/48x48x4/s/seanq.1",
  230. "/lib/face/48x48x4/t/td.1",
  231. "/lib/face/48x48x8/l/lucent.1",
  232. };
  233. void
  234. afaces(void)
  235. {
  236. int i;
  237. for(i=0; i<18; i++)
  238. face[i] = openface(facepaths[i]);
  239. }
  240. void
  241. resize(int i)
  242. {
  243. int fd;
  244. fd = open("/dev/wctl", OWRITE);
  245. if(fd >= 0){
  246. fprint(fd, "resize -dx %d -dy %d", i, i);
  247. close(fd);
  248. }
  249. }
  250. Image *
  251. openimage(char *path)
  252. {
  253. Image *i;
  254. int fd;
  255. fd = open(path, OREAD);
  256. if(fd < 0)
  257. sysfatal("open %s: %r", path);
  258. i = readimage(display, fd, 0);
  259. if(i == nil)
  260. sysfatal("readimage %s: %r", path);
  261. close(fd);
  262. return i;
  263. }
  264. enum {
  265. Facesize = 48 };
  266. void
  267. allocblocks(void)
  268. {
  269. Rectangle r, b;
  270. ushort i, x, y, sq;
  271. sq = sqrt(level);
  272. resize(Facesize*sq+sq*4+17);
  273. r = insetrect(screen->r, 5);
  274. r.max.x = r.min.x+Facesize*sq+sq*4-1;
  275. r.max.y = r.min.y+Facesize*sq+sq*4-1;
  276. b.max.y = r.min.y;
  277. for(i=level-1, y=0; y!=sq; y++){
  278. b.min.y = b.max.y;
  279. b.max.y = r.min.y+Dy(r)*(y+1)/sq;
  280. b.max.x = r.min.x;
  281. for(x=0; x!=sq; x++, i--){
  282. b.min.x = b.max.x;
  283. b.max.x = r.min.x+Dx(r)*(x+1)/sq;
  284. block[i].r = insetrect(b, 2 );
  285. }
  286. }
  287. }
  288. Image*
  289. readbit(int fd, ulong chan, char *path)
  290. {
  291. char buf[4096], hx[4], *p;
  292. uchar data[Facesize*Facesize]; /* more than enough */
  293. int nhx, i, n, ndata, nbit;
  294. Image *img;
  295. n = readn(fd, buf, sizeof buf);
  296. if(n <= 0)
  297. return nil;
  298. if(n >= sizeof buf)
  299. n = sizeof(buf)-1;
  300. buf[n] = '\0';
  301. n = 0;
  302. nhx = 0;
  303. nbit = chantodepth(chan);
  304. ndata = (Facesize*Facesize*nbit)/8;
  305. p = buf;
  306. while(n < ndata) {
  307. p = strpbrk(p+1, "0123456789abcdefABCDEF");
  308. if(p == nil)
  309. break;
  310. if(p[0] == '0' && p[1] == 'x')
  311. continue;
  312. hx[nhx] = *p;
  313. if(++nhx == 2) {
  314. hx[nhx] = 0;
  315. i = strtoul(hx, 0, 16);
  316. data[n++] = ~i;
  317. nhx = 0;
  318. }
  319. }
  320. if(n < ndata)
  321. sysfatal("short face %s", path);
  322. img = allocimage(display, Rect(0,0,Facesize,Facesize), chan, 0, 0);
  323. if(img == nil)
  324. return nil;
  325. loadimage(img, img->r, data, ndata);
  326. return img;
  327. }
  328. Image*
  329. openface(char *path)
  330. {
  331. char *p;
  332. int fd, n;
  333. p = strstr(path, "48x48x");
  334. if(p == nil)
  335. return openimage(path);
  336. n = atoi(p+6);
  337. if(n < 4){
  338. if((fd = open(path, OREAD)) < 0)
  339. sysfatal("open %s: %r", path);
  340. return readbit(fd, n==1 ? GREY1 : GREY2, path);
  341. }
  342. return openimage(path);
  343. }