ucmd_mines.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677
  1. /* mines: a small Minesweeper for a canvas window.
  2. *
  3. * The board is 9x9 with 10 mines. The first reveal is always safe: mines are
  4. * placed after it, outside the clicked cell and its eight neighbours. Left
  5. * click reveals, right click toggles a flag, R starts a new game and ESC
  6. * closes the window. The window can be resized; the board is re-laid out.
  7. */
  8. #include "ucmd_lib.h"
  9. #include "ngui.h"
  10. #define MW 9
  11. #define MH 9
  12. #define MINE_COUNT 10
  13. #define TOTAL_CELLS (MW * MH)
  14. #define START_W 300
  15. #define START_H 360
  16. #define PAD 10
  17. #define HEADER_H 42
  18. #define FOOTER_H 20
  19. #define C_BG NGUI_RGB(24, 28, 40)
  20. #define C_PANEL NGUI_RGB(38, 43, 58)
  21. #define C_TILE NGUI_RGB(62, 72, 96)
  22. #define C_TILE_HOT NGUI_RGB(78, 90, 120)
  23. #define C_TILE_HI NGUI_RGB(112, 126, 160)
  24. #define C_TILE_LO NGUI_RGB(28, 32, 44)
  25. #define C_OPEN NGUI_RGB(42, 48, 62)
  26. #define C_GRID NGUI_RGB(30, 35, 46)
  27. #define C_BORDER NGUI_RGB(92, 99, 124)
  28. #define C_DIM NGUI_RGB(145, 151, 170)
  29. #define C_FLAG NGUI_RGB(238, 116, 116)
  30. #define C_MINE NGUI_RGB(255, 110, 110)
  31. #define C_EXPLODED NGUI_RGB(130, 36, 44)
  32. #define C_FACE NGUI_RGB(238, 198, 88)
  33. #define C_FACE_DK NGUI_RGB(125, 88, 20)
  34. #define C_WIN NGUI_RGB(120, 220, 140)
  35. #define C_LOSE NGUI_RGB(238, 116, 116)
  36. struct mines_layout {
  37. int cell;
  38. int bx, by, bw, bh;
  39. int face_x, face_y, face_w, face_h;
  40. int counter_x, counter_y;
  41. int timer_x, timer_y;
  42. int footer_y;
  43. };
  44. static unsigned char mine[TOTAL_CELLS];
  45. static unsigned char opened[TOTAL_CELLS];
  46. static unsigned char flagged[TOTAL_CELLS];
  47. static unsigned char adj[TOTAL_CELLS];
  48. static int started;
  49. static int over;
  50. static int won;
  51. static int exploded = -1;
  52. static int opened_count;
  53. static int flag_count;
  54. static int last_second;
  55. static int quit;
  56. static unsigned int start_ms;
  57. static unsigned int end_ms;
  58. static unsigned int rng_state = 0x6D2B79F5u;
  59. static unsigned int game_seq;
  60. static unsigned int prev_right;
  61. static struct ngui_rect rect_make(int x, int y, int w, int h)
  62. {
  63. struct ngui_rect r;
  64. r.x = x;
  65. r.y = y;
  66. r.w = w;
  67. r.h = h;
  68. return r;
  69. }
  70. static int point_in(const struct ngui_rect *r, int x, int y)
  71. {
  72. return x >= r->x && y >= r->y &&
  73. x < r->x + r->w && y < r->y + r->h;
  74. }
  75. static unsigned int rng_next(void)
  76. {
  77. unsigned int x = rng_state;
  78. x ^= x << 13;
  79. x ^= x >> 17;
  80. x ^= x << 5;
  81. rng_state = x ? x : 0x6D2B79F5u;
  82. return rng_state;
  83. }
  84. static void reset_game(void)
  85. {
  86. unsigned int i;
  87. for (i = 0; i < TOTAL_CELLS; i++) {
  88. mine[i] = 0;
  89. opened[i] = 0;
  90. flagged[i] = 0;
  91. adj[i] = 0;
  92. }
  93. started = 0;
  94. over = 0;
  95. won = 0;
  96. exploded = -1;
  97. opened_count = 0;
  98. flag_count = 0;
  99. last_second = 0;
  100. start_ms = 0;
  101. end_ms = 0;
  102. game_seq++;
  103. rng_state = (u_ticks_ms() * 2654435761u) ^
  104. (game_seq * 2246822519u) ^ 0x9E3779B9u;
  105. if (!rng_state)
  106. rng_state = 0x6D2B79F5u;
  107. }
  108. static void compute_adj(void)
  109. {
  110. int y, x, dy, dx;
  111. for (y = 0; y < MH; y++) {
  112. for (x = 0; x < MW; x++) {
  113. int n = 0;
  114. for (dy = -1; dy <= 1; dy++) {
  115. for (dx = -1; dx <= 1; dx++) {
  116. int nx = x + dx;
  117. int ny = y + dy;
  118. if ((!dx && !dy) || nx < 0 || nx >= MW ||
  119. ny < 0 || ny >= MH)
  120. continue;
  121. if (mine[ny * MW + nx])
  122. n++;
  123. }
  124. }
  125. adj[y * MW + x] = (unsigned char)n;
  126. }
  127. }
  128. }
  129. static void place_mines(int safe)
  130. {
  131. int sx = safe % MW;
  132. int sy = safe / MW;
  133. int placed = 0;
  134. unsigned int guard = 0;
  135. while (placed < MINE_COUNT && guard++ < 100000u) {
  136. int idx = (int)(rng_next() % TOTAL_CELLS);
  137. int x = idx % MW;
  138. int y = idx / MW;
  139. if (mine[idx])
  140. continue;
  141. if (x >= sx - 1 && x <= sx + 1 &&
  142. y >= sy - 1 && y <= sy + 1)
  143. continue;
  144. mine[idx] = 1;
  145. placed++;
  146. }
  147. compute_adj();
  148. }
  149. static int reveal_cell(int idx)
  150. {
  151. int queue[TOTAL_CELLS];
  152. int head = 0, tail = 0;
  153. if (idx < 0 || idx >= TOTAL_CELLS)
  154. return 0;
  155. if (over || opened[idx] || flagged[idx])
  156. return 0;
  157. if (!started) {
  158. place_mines(idx);
  159. started = 1;
  160. start_ms = u_ticks_ms();
  161. last_second = 0;
  162. }
  163. if (mine[idx]) {
  164. over = 1;
  165. exploded = idx;
  166. end_ms = u_ticks_ms();
  167. return 1;
  168. }
  169. opened[idx] = 1;
  170. opened_count++;
  171. queue[tail++] = idx;
  172. while (head < tail) {
  173. int cur = queue[head++];
  174. int y = cur / MW;
  175. int x = cur % MW;
  176. int dy, dx;
  177. if (adj[cur] != 0)
  178. continue;
  179. for (dy = -1; dy <= 1; dy++) {
  180. for (dx = -1; dx <= 1; dx++) {
  181. int nx = x + dx;
  182. int ny = y + dy;
  183. int n;
  184. if ((!dx && !dy) || nx < 0 || nx >= MW ||
  185. ny < 0 || ny >= MH)
  186. continue;
  187. n = ny * MW + nx;
  188. if (opened[n] || flagged[n] || mine[n])
  189. continue;
  190. opened[n] = 1;
  191. opened_count++;
  192. queue[tail++] = n;
  193. }
  194. }
  195. }
  196. if (opened_count >= TOTAL_CELLS - MINE_COUNT) {
  197. int i;
  198. over = 1;
  199. won = 1;
  200. end_ms = u_ticks_ms();
  201. for (i = 0; i < TOTAL_CELLS; i++) {
  202. if (mine[i] && !flagged[i]) {
  203. flagged[i] = 1;
  204. flag_count++;
  205. }
  206. }
  207. }
  208. return 1;
  209. }
  210. static int toggle_flag(int idx)
  211. {
  212. if (idx < 0 || idx >= TOTAL_CELLS)
  213. return 0;
  214. if (over || opened[idx])
  215. return 0;
  216. if (flagged[idx]) {
  217. flagged[idx] = 0;
  218. flag_count--;
  219. } else {
  220. flagged[idx] = 1;
  221. flag_count++;
  222. }
  223. return 1;
  224. }
  225. static int elapsed_seconds(void)
  226. {
  227. unsigned int now, ms;
  228. if (!started)
  229. return 0;
  230. now = over ? end_ms : u_ticks_ms();
  231. ms = now - start_ms;
  232. if (ms / 1000 > 999u)
  233. return 999;
  234. return (int)(ms / 1000);
  235. }
  236. static void format3(int v, char *out)
  237. {
  238. if (v < 0) {
  239. out[0] = '-';
  240. if (v < -99)
  241. v = -99;
  242. v = -v;
  243. out[1] = (char)('0' + v / 10);
  244. out[2] = (char)('0' + v % 10);
  245. out[3] = '\0';
  246. return;
  247. }
  248. if (v > 999)
  249. v = 999;
  250. out[0] = (char)('0' + v / 100);
  251. out[1] = (char)('0' + (v / 10) % 10);
  252. out[2] = (char)('0' + v % 10);
  253. out[3] = '\0';
  254. }
  255. static void compute_layout(struct ngui *ui, struct mines_layout *l)
  256. {
  257. int region_h = ui->h - HEADER_H - FOOTER_H;
  258. int avail_w = ui->w - 2 * PAD;
  259. int avail_h = region_h - 2 * PAD;
  260. int cell;
  261. if (avail_w < 1)
  262. avail_w = 1;
  263. if (avail_h < 1)
  264. avail_h = 1;
  265. cell = avail_w / MW;
  266. if (cell > avail_h / MH)
  267. cell = avail_h / MH;
  268. if (cell < 10)
  269. cell = 10;
  270. if (cell > 40)
  271. cell = 40;
  272. l->cell = cell;
  273. l->bw = cell * MW;
  274. l->bh = cell * MH;
  275. l->bx = (ui->w - l->bw) / 2;
  276. if (l->bx < 0)
  277. l->bx = 0;
  278. l->by = HEADER_H + (region_h - l->bh) / 2;
  279. if (l->by < HEADER_H)
  280. l->by = HEADER_H;
  281. l->face_w = 28;
  282. l->face_h = 28;
  283. l->face_x = (ui->w - l->face_w) / 2;
  284. l->face_y = (HEADER_H - l->face_h) / 2;
  285. l->counter_x = PAD;
  286. l->counter_y = (HEADER_H - 16) / 2;
  287. l->timer_x = ui->w - PAD - 36;
  288. if (l->timer_x < l->counter_x + 44)
  289. l->timer_x = l->counter_x + 44;
  290. l->timer_y = l->counter_y;
  291. l->footer_y = ui->h - FOOTER_H + 2;
  292. }
  293. static struct ngui_rect face_rect(const struct mines_layout *l)
  294. {
  295. return rect_make(l->face_x, l->face_y, l->face_w, l->face_h);
  296. }
  297. static int cell_at(const struct mines_layout *l, int px, int py)
  298. {
  299. int cx, cy;
  300. if (l->cell <= 0 || px < l->bx || py < l->by)
  301. return -1;
  302. cx = (px - l->bx) / l->cell;
  303. cy = (py - l->by) / l->cell;
  304. if (cx < 0 || cx >= MW || cy < 0 || cy >= MH)
  305. return -1;
  306. return cy * MW + cx;
  307. }
  308. static void fill_circle(struct ngui *ui, int cx, int cy, int r,
  309. unsigned short color)
  310. {
  311. int dy;
  312. for (dy = -r; dy <= r; dy++) {
  313. int dx = 0;
  314. while ((dx + 1) * (dx + 1) + dy * dy <= r * r)
  315. dx++;
  316. if (dx > 0)
  317. ngui_fill_rect(ui,
  318. rect_make(cx - dx, cy + dy,
  319. dx * 2 + 1, 1), color);
  320. }
  321. }
  322. static void draw_mine_icon(struct ngui *ui, int cx, int cy, int r,
  323. unsigned short color)
  324. {
  325. int s = r + 2;
  326. ngui_line(ui, cx - s, cy, cx + s, cy, color);
  327. ngui_line(ui, cx, cy - s, cx, cy + s, color);
  328. ngui_line(ui, cx - s + 2, cy - s + 2, cx + s - 2, cy + s - 2, color);
  329. ngui_line(ui, cx - s + 2, cy + s - 2, cx + s - 2, cy - s + 2, color);
  330. fill_circle(ui, cx, cy, r, color);
  331. }
  332. static void draw_flag_icon(struct ngui *ui, struct ngui_rect r,
  333. unsigned short color)
  334. {
  335. int cx = r.x + r.w / 2;
  336. int top = r.y + r.h / 2 - 7;
  337. int pole_h = 13;
  338. int fh, i;
  339. if (pole_h > r.h - 6)
  340. pole_h = r.h - 6;
  341. if (pole_h < 6)
  342. pole_h = 6;
  343. fh = pole_h * 3 / 4;
  344. if (fh < 4)
  345. fh = 4;
  346. for (i = 0; i < fh; i++)
  347. ngui_fill_rect(ui, rect_make(cx + 1, top + i,
  348. fh - i, 1), color);
  349. ngui_line(ui, cx, top, cx, top + pole_h, color);
  350. ngui_fill_rect(ui, rect_make(cx - 2, top + pole_h, 5, 2), color);
  351. }
  352. static void draw_cross(struct ngui *ui, struct ngui_rect r,
  353. unsigned short color)
  354. {
  355. int x0 = r.x + r.w / 2 - 5;
  356. int x1 = r.x + r.w / 2 + 5;
  357. int y0 = r.y + r.h / 2 - 5;
  358. int y1 = r.y + r.h / 2 + 5;
  359. ngui_line(ui, x0, y0, x1, y1, color);
  360. ngui_line(ui, x0, y1, x1, y0, color);
  361. }
  362. static unsigned short number_color(int n)
  363. {
  364. switch (n) {
  365. case 1: return NGUI_RGB(120, 180, 255);
  366. case 2: return NGUI_RGB(120, 220, 140);
  367. case 3: return NGUI_RGB(255, 120, 120);
  368. case 4: return NGUI_RGB(180, 140, 255);
  369. case 5: return NGUI_RGB(255, 180, 100);
  370. case 6: return NGUI_RGB(100, 220, 220);
  371. case 7: return NGUI_RGB(230, 230, 230);
  372. default: return NGUI_RGB(160, 160, 170);
  373. }
  374. }
  375. static void draw_hidden(struct ngui *ui, struct ngui_rect r, int hot)
  376. {
  377. int b = (r.w < 20) ? 1 : 2;
  378. ngui_fill_rect(ui, r, hot ? C_TILE_HOT : C_TILE);
  379. ngui_fill_rect(ui, rect_make(r.x, r.y, r.w, b), C_TILE_HI);
  380. ngui_fill_rect(ui, rect_make(r.x, r.y, b, r.h), C_TILE_HI);
  381. ngui_fill_rect(ui, rect_make(r.x, r.y + r.h - b, r.w, b), C_TILE_LO);
  382. ngui_fill_rect(ui, rect_make(r.x + r.w - b, r.y, b, r.h), C_TILE_LO);
  383. }
  384. static void draw_open(struct ngui *ui, struct ngui_rect r)
  385. {
  386. ngui_fill_rect(ui, r, C_OPEN);
  387. ngui_fill_rect(ui, rect_make(r.x, r.y, r.w, 1), C_GRID);
  388. ngui_fill_rect(ui, rect_make(r.x, r.y, 1, r.h), C_GRID);
  389. }
  390. static void draw_face_icon(struct ngui *ui, struct ngui_rect r)
  391. {
  392. int cx = r.x + r.w / 2;
  393. int cy = r.y + r.h / 2;
  394. int rad = (r.w < r.h ? r.w : r.h) / 2 - 3;
  395. if (rad < 6)
  396. rad = 6;
  397. fill_circle(ui, cx, cy, rad, C_FACE);
  398. if (won) {
  399. ngui_fill_rect(ui, rect_make(cx - 7, cy - 5, 6, 4), C_FACE_DK);
  400. ngui_fill_rect(ui, rect_make(cx + 1, cy - 5, 6, 4), C_FACE_DK);
  401. ngui_fill_rect(ui, rect_make(cx - 1, cy - 4, 2, 1), C_FACE_DK);
  402. ngui_line(ui, cx - 5, cy + 4, cx - 2, cy + 6, C_FACE_DK);
  403. ngui_line(ui, cx - 2, cy + 6, cx + 2, cy + 6, C_FACE_DK);
  404. ngui_line(ui, cx + 2, cy + 6, cx + 5, cy + 4, C_FACE_DK);
  405. } else if (over) {
  406. ngui_line(ui, cx - 6, cy - 5, cx - 2, cy - 1, C_FACE_DK);
  407. ngui_line(ui, cx - 2, cy - 5, cx - 6, cy - 1, C_FACE_DK);
  408. ngui_line(ui, cx + 2, cy - 5, cx + 6, cy - 1, C_FACE_DK);
  409. ngui_line(ui, cx + 6, cy - 5, cx + 2, cy - 1, C_FACE_DK);
  410. ngui_line(ui, cx - 5, cy + 6, cx - 2, cy + 4, C_FACE_DK);
  411. ngui_line(ui, cx - 2, cy + 4, cx + 2, cy + 4, C_FACE_DK);
  412. ngui_line(ui, cx + 2, cy + 4, cx + 5, cy + 6, C_FACE_DK);
  413. } else {
  414. ngui_fill_rect(ui, rect_make(cx - 5, cy - 4, 3, 4), C_FACE_DK);
  415. ngui_fill_rect(ui, rect_make(cx + 3, cy - 4, 3, 4), C_FACE_DK);
  416. ngui_line(ui, cx - 5, cy + 4, cx - 2, cy + 6, C_FACE_DK);
  417. ngui_line(ui, cx - 2, cy + 6, cx + 2, cy + 6, C_FACE_DK);
  418. ngui_line(ui, cx + 2, cy + 6, cx + 5, cy + 4, C_FACE_DK);
  419. }
  420. }
  421. static void draw_header(struct ngui *ui, const struct mines_layout *l)
  422. {
  423. struct ngui_rect fr = face_rect(l);
  424. int hot = ui->inside && point_in(&fr, ui->mouse_x, ui->mouse_y);
  425. int down = hot && (ui->buttons & 1);
  426. unsigned short bg = down ? ui->theme.button_down :
  427. hot ? ui->theme.button_hot : ui->theme.button;
  428. char buf[4];
  429. ngui_fill_rect(ui, rect_make(0, 0, ui->w, HEADER_H), C_PANEL);
  430. ngui_fill_rect(ui, rect_make(0, HEADER_H - 1, ui->w, 1), C_BORDER);
  431. format3(MINE_COUNT - flag_count, buf);
  432. ngui_text(ui, l->counter_x, l->counter_y, "M", C_DIM);
  433. ngui_text(ui, l->counter_x + 12, l->counter_y, buf, C_FLAG);
  434. format3(elapsed_seconds(), buf);
  435. ngui_text(ui, l->timer_x, l->timer_y, buf, C_WIN);
  436. ngui_text(ui, l->timer_x + 28, l->timer_y, "T", C_DIM);
  437. ngui_fill_rect(ui, fr, bg);
  438. ngui_frame(ui, fr, hot ? ui->theme.accent : C_BORDER);
  439. draw_face_icon(ui, rect_make(fr.x + 2, fr.y + 2,
  440. fr.w - 4, fr.h - 4));
  441. }
  442. static void draw_board(struct ngui *ui, const struct mines_layout *l)
  443. {
  444. int y, x;
  445. ngui_push_clip(ui, rect_make(l->bx, l->by, l->bw, l->bh));
  446. for (y = 0; y < MH; y++) {
  447. for (x = 0; x < MW; x++) {
  448. int idx = y * MW + x;
  449. struct ngui_rect r = rect_make(l->bx + x * l->cell,
  450. l->by + y * l->cell,
  451. l->cell, l->cell);
  452. int hot = ui->inside && point_in(&r, ui->mouse_x,
  453. ui->mouse_y);
  454. if (opened[idx]) {
  455. draw_open(ui, r);
  456. if (mine[idx]) {
  457. draw_mine_icon(ui, r.x + r.w / 2,
  458. r.y + r.h / 2,
  459. r.w / 5, C_MINE);
  460. } else if (adj[idx]) {
  461. char nb[2];
  462. nb[0] = (char)('0' + adj[idx]);
  463. nb[1] = '\0';
  464. ngui_push_clip(ui, r);
  465. ngui_text(ui, r.x + (r.w - 8) / 2,
  466. r.y + (r.h - 16) / 2, nb,
  467. number_color(adj[idx]));
  468. ngui_pop_clip(ui);
  469. }
  470. } else {
  471. draw_hidden(ui, r, hot);
  472. if (flagged[idx]) {
  473. draw_flag_icon(ui, r, C_FLAG);
  474. if (over && !mine[idx])
  475. draw_cross(ui, r, C_LOSE);
  476. } else if (over && mine[idx]) {
  477. if (idx == exploded)
  478. ngui_fill_rect(ui, r, C_EXPLODED);
  479. draw_mine_icon(ui, r.x + r.w / 2,
  480. r.y + r.h / 2,
  481. r.w / 5, C_MINE);
  482. }
  483. }
  484. }
  485. }
  486. ngui_pop_clip(ui);
  487. }
  488. static void draw_footer(struct ngui *ui, const struct mines_layout *l)
  489. {
  490. const char *msg;
  491. unsigned short color;
  492. int tw, x;
  493. if (won) {
  494. msg = "You win! R: play again";
  495. color = C_WIN;
  496. } else if (over) {
  497. msg = "Boom! R: play again";
  498. color = C_LOSE;
  499. } else if (started) {
  500. msg = "R: new right-click: flag";
  501. color = C_DIM;
  502. } else {
  503. msg = "left: reveal right: flag";
  504. color = C_DIM;
  505. }
  506. tw = (int)ngui_text_width(msg);
  507. x = (ui->w - tw) / 2;
  508. if (x < 4)
  509. x = 4;
  510. ngui_text(ui, x, l->footer_y, msg, color);
  511. }
  512. static void draw_app(struct ngui *ui, const struct mines_layout *l)
  513. {
  514. ngui_fill_rect(ui, rect_make(0, 0, ui->w, ui->h), C_BG);
  515. ngui_fill_rect(ui, rect_make(0, ui->h - FOOTER_H, ui->w, 1), C_BORDER);
  516. draw_header(ui, l);
  517. draw_board(ui, l);
  518. draw_footer(ui, l);
  519. }
  520. static int handle_input(struct ngui *ui, const struct mines_layout *l)
  521. {
  522. int changed = 0;
  523. int k;
  524. while ((k = ngui_key(ui)) != 0) {
  525. if (k == NGUI_KEY_ESC) {
  526. quit = 1;
  527. } else if (k == 'r' || k == 'R') {
  528. reset_game();
  529. changed = 1;
  530. }
  531. }
  532. if (ui->pressed) {
  533. struct ngui_rect fr = face_rect(l);
  534. if (point_in(&fr, ui->press_x, ui->press_y)) {
  535. reset_game();
  536. changed = 1;
  537. } else {
  538. int idx = cell_at(l, ui->press_x, ui->press_y);
  539. if (idx >= 0 && reveal_cell(idx))
  540. changed = 1;
  541. }
  542. }
  543. {
  544. unsigned int right = ui->buttons & 2u;
  545. if (right && !prev_right && ui->inside) {
  546. int idx = cell_at(l, ui->mouse_x, ui->mouse_y);
  547. if (idx >= 0 && toggle_flag(idx))
  548. changed = 1;
  549. }
  550. prev_right = right;
  551. }
  552. if (started && !over) {
  553. int sec = elapsed_seconds();
  554. if (sec != last_second) {
  555. last_second = sec;
  556. changed = 1;
  557. }
  558. }
  559. return changed;
  560. }
  561. void _start(const char *arg)
  562. {
  563. struct win_info info;
  564. struct ngui ui;
  565. (void)arg;
  566. if (u_win_create(START_W, START_H, "mines", &info) != 0) {
  567. u_puts("mines: no graphics window\n");
  568. u_exit(1);
  569. }
  570. ngui_init(&ui, (unsigned short *)info.fb, (int)info.width,
  571. (int)info.height);
  572. reset_game();
  573. quit = 0;
  574. prev_right = 0;
  575. for (;;) {
  576. struct mines_layout l;
  577. int want, changed;
  578. want = ngui_begin(&ui);
  579. if (quit)
  580. break;
  581. compute_layout(&ui, &l);
  582. changed = handle_input(&ui, &l);
  583. if (want || changed) {
  584. draw_app(&ui, &l);
  585. ngui_end(&ui);
  586. } else {
  587. ngui_end_ex(&ui, 0);
  588. }
  589. if (quit)
  590. break;
  591. u_sleep_ms(10);
  592. }
  593. u_win_close();
  594. u_exit(0);
  595. }