xs.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750
  1. /*
  2. * engine for 4s, 5s, etc
  3. * need to define N and pieces table before including this
  4. */
  5. enum
  6. {
  7. CNone = 0,
  8. CBounds = 1,
  9. CPiece = 2,
  10. NX = 10,
  11. NY = 20,
  12. };
  13. enum{
  14. TIMER,
  15. MOUSE,
  16. RESHAPE,
  17. KBD,
  18. NONBLOCK,
  19. NALT
  20. };
  21. #define NP (sizeof pieces/sizeof(Piece))
  22. char board[NY][NX];
  23. Rectangle rboard;
  24. Point pscore;
  25. Point scoresz;
  26. int pcsz = 32;
  27. Point pos;
  28. Image *bb, *bbmask, *bb2, *bb2mask;
  29. Rectangle br, br2;
  30. long points;
  31. int dt;
  32. int DY;
  33. int DMOUSE;
  34. int lastmx;
  35. int newscreen;
  36. Channel *timerc;
  37. Mousectl *mousectl;
  38. Keyboardctl *keyboardctl;
  39. void redraw(int);
  40. int tsleep;
  41. Piece *piece;
  42. #define NCOL 10
  43. uchar txbits[NCOL][32]={
  44. {0xDD,0xDD,0xFF,0xFF,0x77,0x77,0xFF,0xFF,
  45. 0xDD,0xDD,0xFF,0xFF,0x77,0x77,0xFF,0xFF,
  46. 0xDD,0xDD,0xFF,0xFF,0x77,0x77,0xFF,0xFF,
  47. 0xDD,0xDD,0xFF,0xFF,0x77,0x77,0xFF,0xFF},
  48. {0xDD,0xDD,0x77,0x77,0xDD,0xDD,0x77,0x77,
  49. 0xDD,0xDD,0x77,0x77,0xDD,0xDD,0x77,0x77,
  50. 0xDD,0xDD,0x77,0x77,0xDD,0xDD,0x77,0x77,
  51. 0xDD,0xDD,0x77,0x77,0xDD,0xDD,0x77,0x77},
  52. {0xAA,0xAA,0x55,0x55,0xAA,0xAA,0x55,0x55,
  53. 0xAA,0xAA,0x55,0x55,0xAA,0xAA,0x55,0x55,
  54. 0xAA,0xAA,0x55,0x55,0xAA,0xAA,0x55,0x55,
  55. 0xAA,0xAA,0x55,0x55,0xAA,0xAA,0x55,0x55},
  56. {0xAA,0xAA,0x55,0x55,0xAA,0xAA,0x55,0x55,
  57. 0xAA,0xAA,0x55,0x55,0xAA,0xAA,0x55,0x55,
  58. 0xAA,0xAA,0x55,0x55,0xAA,0xAA,0x55,0x55,
  59. 0xAA,0xAA,0x55,0x55,0xAA,0xAA,0x55,0x55},
  60. {0x22,0x22,0x88,0x88,0x22,0x22,0x88,0x88,
  61. 0x22,0x22,0x88,0x88,0x22,0x22,0x88,0x88,
  62. 0x22,0x22,0x88,0x88,0x22,0x22,0x88,0x88,
  63. 0x22,0x22,0x88,0x88,0x22,0x22,0x88,0x88},
  64. {0x22,0x22,0x00,0x00,0x88,0x88,0x00,0x00,
  65. 0x22,0x22,0x00,0x00,0x88,0x88,0x00,0x00,
  66. 0x22,0x22,0x00,0x00,0x88,0x88,0x00,0x00,
  67. 0x22,0x22,0x00,0x00,0x88,0x88,0x00,0x00},
  68. {0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x00,
  69. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x00,
  70. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x00,
  71. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x00},
  72. {0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x00,
  73. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x00,
  74. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x00,
  75. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x00},
  76. {0xCC,0xCC,0xCC,0xCC,0xCC,0xCC,0xCC,0xCC,
  77. 0xCC,0xCC,0xCC,0xCC,0xCC,0xCC,0xCC,0xCC,
  78. 0xCC,0xCC,0xCC,0xCC,0xCC,0xCC,0xCC,0xCC,
  79. 0xCC,0xCC,0xCC,0xCC,0xCC,0xCC,0xCC,0xCC},
  80. {0xCC,0xCC,0xCC,0xCC,0x33,0x33,0x33,0x33,
  81. 0xCC,0xCC,0xCC,0xCC,0x33,0x33,0x33,0x33,
  82. 0xCC,0xCC,0xCC,0xCC,0x33,0x33,0x33,0x33,
  83. 0xCC,0xCC,0xCC,0xCC,0x33,0x33,0x33,0x33},
  84. };
  85. int txpix[NCOL] = {
  86. DYellow, /* yellow */
  87. DCyan, /* cyan */
  88. DGreen, /* lime green */
  89. DGreyblue, /* slate */
  90. DRed, /* red */
  91. DGreygreen, /* olive green */
  92. DBlue, /* blue */
  93. 0xFF55AAFF, /* pink */
  94. 0xFFAAFFFF, /* lavender */
  95. 0xBB005DFF, /* maroon */
  96. };
  97. Image *tx[NCOL];
  98. Piece *
  99. rotr(Piece *p)
  100. {
  101. if(p->rot == 3)
  102. return p-3;
  103. return p+1;
  104. }
  105. Piece *
  106. rotl(Piece *p)
  107. {
  108. if(p->rot == 0)
  109. return p+3;
  110. return p-1;
  111. }
  112. int
  113. collide(Point pt, Piece *p)
  114. {
  115. int i;
  116. int c = CNone;
  117. pt.x = (pt.x - rboard.min.x) / pcsz;
  118. pt.y = (pt.y - rboard.min.y) / pcsz;
  119. for(i=0; i<N; i++){
  120. pt.x += p->d[i].x;
  121. pt.y += p->d[i].y;
  122. if(pt.x<0 || pt.x>=NX || pt.y<0 || pt.y>=NY)
  123. c |= CBounds;
  124. if(board[pt.y][pt.x])
  125. c |= CPiece;
  126. }
  127. return c;
  128. }
  129. int
  130. collider(Point pt, Point pmax)
  131. {
  132. int i, j, pi, pj, n, m;
  133. pi = (pt.x - rboard.min.x) / pcsz;
  134. pj = (pt.y - rboard.min.y) / pcsz;
  135. n = pmax.x / pcsz;
  136. m = pmax.y / pcsz + 1;
  137. for(i = pi; i < pi+n && i < NX; i++)
  138. for(j = pj; j < pj+m && j < NY; j++)
  139. if(board[j][i])
  140. return 1;
  141. return 0;
  142. }
  143. void
  144. setpiece(Piece *p){
  145. int i;
  146. Rectangle r, r2;
  147. Point op, delta;
  148. draw(bb, bb->r, display->white, nil, ZP);
  149. draw(bbmask, bbmask->r, display->transparent, nil, ZP);
  150. br = Rect(0, 0, 0, 0);
  151. br2 = br;
  152. piece = p;
  153. if(p == 0)
  154. return;
  155. r.min = bb->r.min;
  156. for(i=0; i<N; i++){
  157. r.min.x += p->d[i].x*pcsz;
  158. r.min.y += p->d[i].y*pcsz;
  159. r.max.x = r.min.x + pcsz;
  160. r.max.y = r.min.y + pcsz;
  161. if(i == 0){
  162. draw(bb, r, display->black, nil, ZP);
  163. draw(bb, insetrect(r, 1), tx[piece->tx], nil, ZP);
  164. draw(bbmask, r, display->opaque, nil, ZP);
  165. op = r.min;
  166. }else{
  167. draw(bb, r, bb, nil, op);
  168. draw(bbmask, r, bbmask, nil, op);
  169. }
  170. if(br.max.x < r.max.x)
  171. br.max.x = r.max.x;
  172. if(br.max.y < r.max.y)
  173. br.max.y = r.max.y;
  174. }
  175. br.max = subpt(br.max, bb->r.min);
  176. delta = Pt(0,DY);
  177. br2.max = addpt(br.max, delta);
  178. r = rectaddpt(br, bb2->r.min);
  179. r2 = rectaddpt(br2, bb2->r.min);
  180. draw(bb2, r2, display->white, nil, ZP);
  181. draw(bb2, rectaddpt(r,delta), bb, nil, bb->r.min);
  182. draw(bb2mask, r2, display->transparent, nil, ZP);
  183. draw(bb2mask, r, display->opaque, bbmask, bb->r.min);
  184. draw(bb2mask, rectaddpt(r,delta), display->opaque, bbmask, bb->r.min);
  185. }
  186. void
  187. drawpiece(void){
  188. draw(screen, rectaddpt(br, pos), bb, bbmask, bb->r.min);
  189. }
  190. void
  191. undrawpiece(void)
  192. {
  193. Image *mask = nil;
  194. if(collider(pos, br.max))
  195. mask = bbmask;
  196. draw(screen, rectaddpt(br, pos), display->white, mask, bb->r.min);
  197. }
  198. void
  199. rest(void)
  200. {
  201. int i;
  202. Point pt;
  203. pt = divpt(subpt(pos, rboard.min), pcsz);
  204. for(i=0; i<N; i++){
  205. pt.x += piece->d[i].x;
  206. pt.y += piece->d[i].y;
  207. board[pt.y][pt.x] = piece->tx+16;
  208. }
  209. }
  210. int
  211. canfit(Piece *p)
  212. {
  213. static int dx[]={0, -1, 1, -2, 2, -3, 3, 4, -4};
  214. int i, j;
  215. Point z;
  216. j = N + 1;
  217. if(N > 4){
  218. j = p->sz.x;
  219. if(j<p->sz.y)
  220. j = p->sz.y;
  221. j = 2*j-1;
  222. }
  223. for(i=0; i<j; i++){
  224. z.x = pos.x + dx[i]*pcsz;
  225. z.y = pos.y;
  226. if(!collide(z, p)){
  227. z.y = pos.y + pcsz-1;
  228. if(!collide(z, p)){
  229. undrawpiece();
  230. pos.x = z.x;
  231. return 1;
  232. }
  233. }
  234. }
  235. return 0;
  236. }
  237. void
  238. score(int p)
  239. {
  240. char buf[128];
  241. points += p;
  242. snprint(buf, sizeof(buf), "%.6ld", points);
  243. draw(screen, Rpt(pscore, addpt(pscore, scoresz)), display->white, nil, ZP);
  244. string(screen, pscore, display->black, ZP, font, buf);
  245. }
  246. void
  247. drawsq(Image *b, Point p, int ptx){
  248. Rectangle r;
  249. r.min = p;
  250. r.max.x = r.min.x+pcsz;
  251. r.max.y = r.min.y+pcsz;
  252. draw(b, r, display->black, nil, ZP);
  253. draw(b, insetrect(r, 1), tx[ptx], nil, ZP);
  254. }
  255. void
  256. drawboard(void)
  257. {
  258. int i, j;
  259. border(screen, insetrect(rboard, -2), 2, display->black, ZP);
  260. draw(screen, Rect(rboard.min.x, rboard.min.y-2, rboard.max.x, rboard.min.y),
  261. display->white, nil, ZP);
  262. for(i=0; i<NY; i++)
  263. for(j=0; j<NX; j++)
  264. if(board[i][j])
  265. drawsq(screen, Pt(rboard.min.x+j*pcsz, rboard.min.y+i*pcsz), board[i][j]-16);
  266. score(0);
  267. }
  268. void
  269. choosepiece(void)
  270. {
  271. int i;
  272. do{
  273. i = nrand(NP);
  274. setpiece(&pieces[i]);
  275. pos = rboard.min;
  276. pos.x += nrand(NX)*pcsz;
  277. }while(collide(Pt(pos.x, pos.y+pcsz-DY), piece));
  278. drawpiece();
  279. flushimage(display, 1);
  280. }
  281. int
  282. movepiece(void)
  283. {
  284. Image *mask = nil;
  285. if(collide(Pt(pos.x, pos.y+pcsz), piece))
  286. return 0;
  287. if(collider(pos, br2.max))
  288. mask = bb2mask;
  289. draw(screen, rectaddpt(br2, pos), bb2, mask, bb2->r.min);
  290. pos.y += DY;
  291. flushimage(display, 1);
  292. return 1;
  293. }
  294. void
  295. pause(int t)
  296. {
  297. flushimage(display, 1);
  298. while(nbrecv(timerc, nil) == 1)
  299. ;
  300. while(recv(timerc, nil) == 1)
  301. if((t -= tsleep) < 0)
  302. break;
  303. }
  304. int
  305. horiz(void)
  306. {
  307. int lev[N];
  308. int i, j, h;
  309. Rectangle r;
  310. Image *col;
  311. h = 0;
  312. for(i=0; i<NY; i++){
  313. for(j=0; board[i][j]; j++)
  314. if(j == NX-1){
  315. lev[h++] = i;
  316. break;
  317. }
  318. }
  319. if(h == 0)
  320. return 0;
  321. r = rboard;
  322. newscreen = 0;
  323. for(j=0; j<h; j++){
  324. r.min.y = rboard.min.y + lev[j]*pcsz;
  325. r.max.y = r.min.y + pcsz;
  326. draw(screen, r, display->white, nil, ZP);
  327. flushimage(display, 1);
  328. }
  329. for(i=0; i<6; i++){
  330. pause(500);
  331. if(newscreen){
  332. drawboard();
  333. break;
  334. }
  335. col = (i&1)? display->white : display->black;
  336. for(j=0; j<h; j++){
  337. r.min.y = rboard.min.y + lev[j]*pcsz;
  338. r.max.y = r.min.y + pcsz;
  339. draw(screen, r, col, nil, ZP);
  340. }
  341. flushimage(display, 1);
  342. }
  343. r = rboard;
  344. for(j=0; j<h; j++){
  345. i = NY - lev[j] - 1;
  346. score(250+10*i*i);
  347. r.min.y = rboard.min.y;
  348. r.max.y = rboard.min.y+lev[j]*pcsz;
  349. draw(screen, rectaddpt(r, Pt(0,pcsz)), screen, nil, r.min);
  350. r.max.y = rboard.min.y+pcsz;
  351. draw(screen, r, display->white, nil, ZP);
  352. memcpy(&board[1][0], &board[0][0], NX*lev[j]);
  353. memset(&board[0][0], 0, NX);
  354. }
  355. flushimage(display, 1);
  356. return 1;
  357. }
  358. void
  359. mright(void)
  360. {
  361. if(!collide(Pt(pos.x+pcsz, pos.y), piece))
  362. if(!collide(Pt(pos.x+pcsz, pos.y+pcsz-DY), piece)){
  363. undrawpiece();
  364. pos.x += pcsz;
  365. drawpiece();
  366. flushimage(display, 1);
  367. }
  368. }
  369. void
  370. mleft(void)
  371. {
  372. if(!collide(Pt(pos.x-pcsz, pos.y), piece))
  373. if(!collide(Pt(pos.x-pcsz, pos.y+pcsz-DY), piece)){
  374. undrawpiece();
  375. pos.x -= pcsz;
  376. drawpiece();
  377. flushimage(display, 1);
  378. }
  379. }
  380. void
  381. rright(void)
  382. {
  383. if(canfit(rotr(piece))){
  384. setpiece(rotr(piece));
  385. drawpiece();
  386. flushimage(display, 1);
  387. }
  388. }
  389. void
  390. rleft(void)
  391. {
  392. if(canfit(rotl(piece))){
  393. setpiece(rotl(piece));
  394. drawpiece();
  395. flushimage(display, 1);
  396. }
  397. }
  398. int fusst = 0;
  399. int
  400. drop(int f)
  401. {
  402. Mouse mouse;
  403. if(f){
  404. score(5L*(rboard.max.y-pos.y)/pcsz);
  405. do; while(movepiece());
  406. }
  407. fusst = 0;
  408. rest();
  409. if(pos.y==rboard.min.y && !horiz())
  410. return 1;
  411. horiz();
  412. setpiece(0);
  413. pause(1500);
  414. choosepiece();
  415. while(nbrecv(mousectl->c, &mouse) == 1)
  416. lastmx = mouse.xy.x;
  417. while(nbrecv(keyboardctl->c, nil) == 1)
  418. ;
  419. return 0;
  420. }
  421. int
  422. play(void)
  423. {
  424. int i;
  425. int suspended;
  426. Mouse om;
  427. Mouse mouse;
  428. Rune r;
  429. Alt alts[NALT+1];
  430. alts[TIMER].c = timerc;
  431. alts[TIMER].v = nil;
  432. alts[TIMER].op = CHANRCV;
  433. alts[MOUSE].c = mousectl->c;
  434. alts[MOUSE].v = &mouse;
  435. alts[MOUSE].op = CHANRCV;
  436. alts[RESHAPE].c = mousectl->resizec;
  437. alts[RESHAPE].v = nil;
  438. alts[RESHAPE].op = CHANRCV;
  439. alts[KBD].c = keyboardctl->c;
  440. alts[KBD].v = &r;
  441. alts[KBD].op = CHANRCV;
  442. alts[NONBLOCK].c = nil;
  443. alts[NONBLOCK].v = nil;
  444. alts[NONBLOCK].op = CHANNOBLK;
  445. alts[NALT].op = CHANEND;
  446. /* flush the pipe */
  447. while(alt(alts) != NONBLOCK)
  448. ;
  449. alts[NONBLOCK].op = CHANEND;
  450. dt = 64;
  451. suspended = 0;
  452. choosepiece();
  453. for(;;)
  454. switch(alt(alts)){
  455. case MOUSE:
  456. if(suspended)
  457. break;
  458. if(lastmx < 0)
  459. lastmx = mouse.xy.x;
  460. if(mouse.xy.x > lastmx+DMOUSE){
  461. mright();
  462. lastmx = mouse.xy.x;
  463. }
  464. if(mouse.xy.x < lastmx-DMOUSE){
  465. mleft();
  466. lastmx = mouse.xy.x;
  467. }
  468. if(mouse.buttons&1 && !(om.buttons&1))
  469. rleft();
  470. if(mouse.buttons&2 && !(om.buttons&2))
  471. if(drop(1))
  472. return 1;
  473. if(mouse.buttons&4 && !(om.buttons&4))
  474. rright();
  475. om = mouse;
  476. break;
  477. case RESHAPE:
  478. redraw(1);
  479. break;
  480. case KBD:
  481. if(suspended) {
  482. suspended = 0;
  483. break;
  484. }
  485. switch(r){
  486. case 'q':
  487. case 'Q':
  488. case 0x04:
  489. return 0;
  490. case 'f':
  491. case ';':
  492. mright();
  493. break;
  494. case 'a':
  495. case 'j':
  496. mleft();
  497. break;
  498. case 'd':
  499. case 'l':
  500. rright();
  501. break;
  502. case 's':
  503. case 'k':
  504. rleft();
  505. break;
  506. case ' ':
  507. if(drop(1))
  508. return 1;
  509. break;
  510. case 'z':
  511. suspended = 1;
  512. break;
  513. }
  514. break;
  515. case TIMER:
  516. if(suspended)
  517. break;
  518. dt -= tsleep;
  519. if(dt < 0){
  520. i = 1;
  521. dt = 16 * (points+nrand(10000)-5000) / 10000;
  522. if(dt >= 32){
  523. i += (dt-32)/16;
  524. dt = 32;
  525. }
  526. dt = 52-dt;
  527. while(i-- > 0)
  528. if(movepiece()==0 && ++fusst==40){
  529. if(drop(0))
  530. return 1;
  531. break;
  532. }
  533. }
  534. break;
  535. }
  536. return 0; /* not reached */
  537. }
  538. void
  539. setparms(void)
  540. {
  541. char buf[32];
  542. int fd, n;
  543. tsleep = 50;
  544. fd = open("/dev/hz", OREAD);
  545. if(fd < 0)
  546. return;
  547. n = read(fd, buf, sizeof buf - 1);
  548. close(fd);
  549. if(n < 0)
  550. return;
  551. buf[n] = '\0';
  552. tsleep = strtoul(buf, 0, 10);
  553. tsleep = (1000 + tsleep - 1) / tsleep;
  554. }
  555. void
  556. timerproc(void *v)
  557. {
  558. Channel *c;
  559. void **arg;
  560. arg = v;
  561. c = (Channel*)arg;
  562. for(;;){
  563. sleep(tsleep);
  564. send(c, nil);
  565. }
  566. }
  567. void
  568. redraw(int new)
  569. {
  570. Rectangle r;
  571. long dx, dy;
  572. if(new && getwindow(display, Refmesg) < 0){
  573. fprint(2, "can't reattach to window");
  574. exits("redraw");
  575. }
  576. r = screen->r;
  577. pos.x = (pos.x - rboard.min.x) / pcsz;
  578. pos.y = (pos.y - rboard.min.y) / pcsz;
  579. dx = r.max.x - r.min.x;
  580. dy = r.max.y - r.min.y - 2*32;
  581. DY = dx / NX;
  582. if(DY > dy / NY)
  583. DY = dy / NY;
  584. DY /= 8;
  585. if(DY > 4)
  586. DY = 4;
  587. pcsz = DY*8;
  588. DMOUSE = pcsz/3;
  589. if(pcsz < 8){
  590. fprint(2, "screen too small: %d\n", pcsz);
  591. threadexitsall(nil);
  592. exits("too small");
  593. }
  594. rboard = screen->r;
  595. rboard.min.x += (dx-pcsz*NX)/2;
  596. rboard.min.y += (dy-pcsz*NY)/2+32;
  597. rboard.max.x = rboard.min.x+NX*pcsz;
  598. rboard.max.y = rboard.min.y+NY*pcsz;
  599. pscore.x = rboard.min.x+8;
  600. pscore.y = rboard.min.y-32;
  601. scoresz = stringsize(font, "000000");
  602. pos.x = pos.x*pcsz + rboard.min.x;
  603. pos.y = pos.y*pcsz + rboard.min.y;
  604. if(bb){
  605. freeimage(bb);
  606. freeimage(bbmask);
  607. freeimage(bb2);
  608. freeimage(bb2mask);
  609. }
  610. bb = allocimage(display, Rect(0,0,N*pcsz,N*pcsz), screen->chan, 0, 0);
  611. bbmask = allocimage(display, Rect(0,0,N*pcsz,N*pcsz), GREY1, 0, 0);
  612. bb2 = allocimage(display, Rect(0,0,N*pcsz,N*pcsz+DY), screen->chan, 0, 0);
  613. bb2mask = allocimage(display, bb2->r, GREY1, 0, 0);
  614. if(bb==0 || bbmask==0 || bb2==0 || bb2mask==0){
  615. fprint(2, "allocimage fail (bb)\n");
  616. exits("allocimage");
  617. }
  618. draw(screen, screen->r, display->white, nil, ZP);
  619. drawboard();
  620. setpiece(piece);
  621. if(piece)
  622. drawpiece();
  623. lastmx = -1;
  624. newscreen = 1;
  625. flushimage(display, 1);
  626. }
  627. void
  628. threadmain(int argc, char *argv[])
  629. {
  630. Image *tb;
  631. char buf[200];
  632. int i, scores;
  633. long starttime, endtime;
  634. ARGBEGIN{
  635. }ARGEND
  636. setparms();
  637. snprint(buf, sizeof(buf), "%ds", N);
  638. initdraw(0, 0, buf);
  639. mousectl = initmouse(nil, display->image); /* BUG? */
  640. if(mousectl == nil){
  641. fprint(2, "[45]s: mouse init failed: %r\n");
  642. exits("mouse");
  643. }
  644. keyboardctl = initkeyboard(nil); /* BUG? */
  645. if(keyboardctl == nil){
  646. fprint(2, "[45]s: keyboard init failed: %r\n");
  647. exits("kbd");
  648. }
  649. starttime = time(0);
  650. srand(starttime);
  651. snprint(buf, sizeof(buf), "/sys/games/lib/%dscores", N);
  652. scores = open(buf, OWRITE);
  653. if(scores < 0){
  654. fprint(2, "can't open %s\n", buf);
  655. exits("scores file");
  656. }
  657. tb = 0;
  658. if(screen->depth < 3){
  659. tb = allocimage(display, Rect(0,0,16,16), 0, 1, -1);
  660. if(tb == 0){
  661. fprint(2, "allocimage fail (tb)\n");
  662. exits("allocimage");
  663. }
  664. }
  665. for(i = 0; i<NCOL; i++){
  666. tx[i] = allocimage(display, Rect(0, 0, 16, 16), screen->chan, 1, txpix[i]);
  667. if(tx[i] == 0){
  668. fprint(2, "allocimage fail (tx)\n");
  669. exits("allocimage");
  670. }
  671. if(screen->depth < 3){
  672. loadimage(tb, tb->r, txbits[i], 32);
  673. draw(tx[i], tx[i]->r, tb, nil, ZP);
  674. }
  675. }
  676. if(tb != 0)
  677. freeimage(tb);
  678. threadsetname("4s-5s");
  679. timerc= chancreate(sizeof(int), 0);
  680. proccreate(timerproc, timerc, 1024);
  681. points = 0;
  682. memset(board, 0, sizeof(board));
  683. redraw(0);
  684. if(play()){
  685. endtime = time(0);
  686. fprint(scores, "%ld\t%s\t%lud\t%ld\n",
  687. points, getuser(), starttime, endtime-starttime);
  688. }
  689. threadexitsall(nil);
  690. exits(0);
  691. }