tetris_modal.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730
  1. /*
  2. * File : tetris_modal.c
  3. * This file is part of RTGUI in RT-Thread RTOS
  4. * COPYRIGHT (C) 2010, RT-Thread Development Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2010-08-14 Yi.Qiu first version
  13. */
  14. #include <rtthread.h>
  15. #include <stdlib.h>
  16. #include "tetris.h"
  17. struct rt_tetris
  18. {
  19. rt_uint32_t width; /* the width of the tetris */
  20. rt_uint32_t height; /* the height of the tetris */
  21. rt_uint16_t* panel; /* the panel of the tetris */
  22. rt_uint32_t* brick; /* the current brick of the tetris */
  23. rt_uint32_t* next_brick; /* the next brick of the tetris */
  24. rt_tetris_view_t* view; /* the view on which the tetris show */
  25. rt_uint32_t level; /* game level */
  26. rt_uint32_t lines; /* released lines count */
  27. rt_uint32_t score; /* total scores statistic */
  28. rt_bool_t status; /* game status, pause or runing */
  29. };
  30. static const rt_uint32_t g_brick[][4] =
  31. {
  32. {23,7,8,22},
  33. {23,7,8,24},
  34. {24,7,8,25},
  35. {8,7,9,23},
  36. {8,7,9,24},
  37. {8,7,9,25},
  38. {7,6,8,9},
  39. };
  40. static rt_err_t rt_tetris_append_brick(rt_tetris_t* thiz, rt_uint32_t brick[]);
  41. static rt_err_t rt_tetris_delete_brick(rt_tetris_t* thiz, rt_uint32_t brick[]);
  42. static rt_err_t rt_tetris_release_lines(rt_tetris_t* thiz, rt_uint32_t brick[]);
  43. static rt_err_t rt_tetris_is_reach_top(rt_tetris_t* thiz, rt_uint32_t brick[]);
  44. static rt_err_t rt_tetris_update_brick(rt_tetris_t* thiz);
  45. /**
  46. * this function create a tetris instance
  47. *
  48. * @param width the width of tetris.
  49. * @param height the height of tetris.
  50. *
  51. * @return the tetris instance
  52. */
  53. rt_tetris_t* rt_tetris_create(rt_uint32_t width, rt_uint32_t height)
  54. {
  55. int index;
  56. rt_tetris_t* thiz = (rt_tetris_t*)rt_malloc(sizeof(rt_tetris_t));
  57. RT_ASSERT(thiz != RT_NULL);
  58. thiz->height = height;
  59. thiz->width = width;
  60. thiz->panel = rt_malloc(thiz->height * sizeof(rt_uint32_t));
  61. rt_memset(thiz->panel, 0, thiz->height * sizeof(rt_uint32_t));
  62. thiz->brick = (rt_uint32_t*)rt_malloc(4 * sizeof(rt_uint32_t));
  63. index = (int)(7.0 * rand()/(RAND_MAX + 1.0));
  64. rt_memcpy(thiz->brick, g_brick[index], 4 * sizeof(rt_uint32_t));
  65. thiz->next_brick= (rt_uint32_t*)rt_malloc(4 * sizeof(rt_uint32_t));
  66. index = (int)(7.0 * rand()/(RAND_MAX + 1.0));
  67. rt_memcpy(thiz->next_brick, g_brick[index], 4 * sizeof(rt_uint32_t));
  68. thiz->view = RT_NULL;
  69. thiz->level = 0;
  70. thiz->lines = 0;
  71. thiz->score = 0;
  72. thiz->status = RT_FALSE;
  73. return thiz;
  74. }
  75. /**
  76. * this function destory a tetris instance
  77. *
  78. * @param thiz the tetris instance.
  79. *
  80. * @return RT_EOK
  81. */
  82. rt_err_t rt_tetris_destory(rt_tetris_t* thiz)
  83. {
  84. RT_ASSERT(thiz->panel && thiz->brick && thiz->next_brick);
  85. rt_free(thiz->panel);
  86. rt_free(thiz->brick);
  87. rt_free(thiz->next_brick);
  88. rt_free(thiz);
  89. return RT_EOK;
  90. }
  91. /**
  92. * this function start tetris game
  93. *
  94. * @param thiz the tetris instance.
  95. *
  96. * @return RT_EOK
  97. */
  98. rt_err_t rt_tetris_start(rt_tetris_t* thiz)
  99. {
  100. RT_ASSERT(thiz != RT_NULL);
  101. /* update next brick on view */
  102. thiz->view->update_next_brick(thiz->view, thiz);
  103. /* update level */
  104. thiz->view->update_level(thiz->view, thiz);
  105. /* update lines and score */
  106. thiz->view->update_score_and_lines(thiz->view, thiz);
  107. thiz->status = RT_TRUE;
  108. return RT_EOK;
  109. }
  110. /**
  111. * this function pause tetris game
  112. *
  113. * @param thiz the tetris instance.
  114. *
  115. * @return RT_EOK
  116. */
  117. rt_err_t rt_tetris_pause(rt_tetris_t* thiz)
  118. {
  119. RT_ASSERT(thiz != RT_NULL);
  120. thiz->status = RT_FALSE;
  121. return RT_EOK;
  122. }
  123. /**
  124. * this function get width of a tetris instance
  125. *
  126. * @param thiz the tetris instance.
  127. *
  128. * @return the width of the tetris instance
  129. */
  130. rt_uint32_t rt_tetris_width(rt_tetris_t* thiz)
  131. {
  132. RT_ASSERT(thiz != RT_NULL);
  133. return thiz->width;
  134. }
  135. /**
  136. * this function get next brick of a tetris instance
  137. *
  138. * @param thiz the tetris instance.
  139. *
  140. * @return the next brick of the tetris instance
  141. */
  142. rt_uint32_t* rt_tetris_next_brick(rt_tetris_t* thiz)
  143. {
  144. RT_ASSERT(thiz != RT_NULL);
  145. return thiz->next_brick;
  146. }
  147. /**
  148. * this function get level of the tetris instance
  149. *
  150. * @param thiz the tetris instance.
  151. *
  152. * @return the level of the tetris instance
  153. */
  154. rt_uint32_t rt_tetris_level(rt_tetris_t* thiz)
  155. {
  156. RT_ASSERT(thiz != RT_NULL);
  157. return thiz->level;
  158. }
  159. /**
  160. * this function get released lines of the tetris instance
  161. *
  162. * @param thiz the tetris instance.
  163. *
  164. * @return the released lines of the tetris instance
  165. */
  166. rt_uint32_t rt_tetris_lines(rt_tetris_t* thiz)
  167. {
  168. RT_ASSERT(thiz != RT_NULL);
  169. return thiz->lines;
  170. }
  171. /**
  172. * this function get score of the tetris instance
  173. *
  174. * @param thiz the tetris instance.
  175. *
  176. * @return the score of the tetris instance
  177. */
  178. rt_uint32_t rt_tetris_score(rt_tetris_t* thiz)
  179. {
  180. RT_ASSERT(thiz != RT_NULL);
  181. return thiz->score;
  182. }
  183. /**
  184. * this function get height of a tetris instance
  185. *
  186. * @param thiz the tetris instance.
  187. *
  188. * @return the height of the tetris instance
  189. */
  190. rt_uint32_t rt_tetris_height(rt_tetris_t* thiz)
  191. {
  192. RT_ASSERT(thiz != RT_NULL);
  193. return thiz->height;
  194. }
  195. /**
  196. * this function get status of a tetris instance
  197. *
  198. * @param thiz the tetris instance.
  199. *
  200. * @return the status of the tetris instance
  201. */
  202. rt_bool_t rt_tetris_status(rt_tetris_t* thiz)
  203. {
  204. RT_ASSERT(thiz != RT_NULL);
  205. return thiz->status;
  206. }
  207. /**
  208. * this function makes current brick move down
  209. *
  210. * @param thiz the tetris instance.
  211. *
  212. * @return RT_EOK on success, -RT_ERROR on fail
  213. */
  214. rt_err_t rt_tetris_down(rt_tetris_t* thiz)
  215. {
  216. int i;
  217. RT_ASSERT(thiz != RT_NULL);
  218. if(thiz->status == RT_FALSE) return -RT_ERROR;
  219. /* delete the brick from tetris panel */
  220. rt_tetris_delete_brick(thiz, thiz->brick);
  221. for(i=0; i<4; i++)
  222. {
  223. /* check collision and bottom*/
  224. if((thiz->brick[i] >= thiz->width * (thiz->height - 1))
  225. || rt_tetris_check_collision(thiz, thiz->brick[i] + thiz->width) == RT_EOK)
  226. {
  227. /* restore the deleted brick */
  228. rt_tetris_append_brick(thiz, thiz->brick);
  229. if(rt_tetris_is_reach_top(thiz, thiz->brick) == RT_EOK)
  230. {
  231. rt_memset(thiz->panel, 0xff, thiz->height * sizeof(rt_uint32_t));
  232. /* update view */
  233. thiz->view->update(thiz->view, thiz);
  234. /* game over */
  235. return -RT_ETIMEOUT;
  236. }
  237. if(rt_tetris_release_lines(thiz, thiz->brick) == RT_EOK)
  238. {
  239. /* update view */
  240. thiz->view->update(thiz->view, thiz);
  241. }
  242. rt_tetris_update_brick(thiz);
  243. return -RT_ERROR;
  244. }
  245. }
  246. for(i=0; i<4; i++)
  247. {
  248. /* increase one line */
  249. thiz->brick[i] += thiz->width;
  250. }
  251. /* append the brick to tetris panel */
  252. rt_tetris_append_brick(thiz, thiz->brick);
  253. /* update view */
  254. thiz->view->update(thiz->view, thiz);
  255. return RT_EOK;
  256. }
  257. /**
  258. * this function makes current brick move left
  259. *
  260. * @param thiz the tetris instance.
  261. *
  262. * @return RT_EOK on success, -RT_ERROR on fail
  263. */
  264. rt_err_t rt_tetris_left(rt_tetris_t* thiz)
  265. {
  266. int i;
  267. RT_ASSERT(thiz != RT_NULL);
  268. if(thiz->status == RT_FALSE) return -RT_ERROR;
  269. /* delete the brick from tetris panel */
  270. rt_tetris_delete_brick(thiz, thiz->brick);
  271. for(i=0; i<4; i++)
  272. {
  273. /* check left board */
  274. if((thiz->brick[i] % thiz->width) == 0)
  275. {
  276. /* restore the deleted brick */
  277. rt_tetris_append_brick(thiz, thiz->brick);
  278. return -RT_ERROR;
  279. }
  280. if(rt_tetris_check_collision(thiz, thiz->brick[i] - 1) == RT_EOK)
  281. {
  282. /* restore the deleted brick */
  283. rt_tetris_append_brick(thiz, thiz->brick);
  284. return -RT_ERROR;
  285. }
  286. }
  287. for(i=0; i<4; i++)
  288. {
  289. /* move one step to left */
  290. thiz->brick[i] --;;
  291. }
  292. /* append the brick to tetris panel */
  293. rt_tetris_append_brick(thiz, thiz->brick);
  294. /* update view */
  295. thiz->view->update(thiz->view, thiz);
  296. return RT_EOK;
  297. }
  298. /**
  299. * this function makes current brick move right
  300. *
  301. * @param thiz the tetris instance.
  302. *
  303. * @return RT_EOK on success, -RT_ERROR on fail
  304. */
  305. rt_err_t rt_tetris_right(rt_tetris_t* thiz)
  306. {
  307. int i;
  308. RT_ASSERT(thiz != RT_NULL);
  309. if(thiz->status == RT_FALSE) return -RT_ERROR;
  310. /* delete the brick from tetris panel */
  311. rt_tetris_delete_brick(thiz, thiz->brick);
  312. for(i=0; i<4; i++)
  313. {
  314. /* check left board */
  315. if(((thiz->brick[i] + 1) % thiz->width) == 0)
  316. {
  317. /* restore the deleted brick */
  318. rt_tetris_append_brick(thiz, thiz->brick);
  319. return -RT_ERROR;
  320. }
  321. /* check collision */
  322. if(rt_tetris_check_collision(thiz, thiz->brick[i] + 1) == RT_EOK)
  323. {
  324. /* restore the deleted brick */
  325. rt_tetris_append_brick(thiz, thiz->brick);
  326. return -RT_ERROR;
  327. }
  328. }
  329. for(i=0; i<4; i++)
  330. {
  331. /* move one step to right */
  332. thiz->brick[i] ++;;
  333. }
  334. /* append the brick to tetris panel */
  335. rt_tetris_append_brick(thiz, thiz->brick);
  336. /* update view */
  337. thiz->view->update(thiz->view, thiz);
  338. return RT_EOK;
  339. }
  340. /**
  341. * this function makes current brick drop quickly
  342. *
  343. * @param thiz the tetris instance.
  344. *
  345. * @return RT_EOK on success, -RT_ERROR on fail
  346. */
  347. rt_err_t rt_tetris_drop(rt_tetris_t* thiz)
  348. {
  349. rt_err_t ret;
  350. RT_ASSERT(thiz != RT_NULL);
  351. if(thiz->status == RT_FALSE) return -RT_ETIMEOUT;
  352. /* move down until blocked */
  353. while((ret = rt_tetris_down(thiz)) == RT_EOK);
  354. return ret;
  355. }
  356. /**
  357. * this function makes current brick do rotation
  358. *
  359. * @param thiz the tetris instance.
  360. *
  361. * @return RT_EOK on success, -RT_ERROR on fail
  362. */
  363. rt_err_t rt_tetris_rotate(rt_tetris_t* thiz, rt_bool_t direction)
  364. {
  365. int i;
  366. rt_uint32_t tmp[4];
  367. RT_ASSERT(thiz != RT_NULL);
  368. if(thiz->status == RT_FALSE) return -RT_ERROR;
  369. rt_tetris_delete_brick(thiz, thiz->brick);
  370. tmp[0] = thiz->brick[0];
  371. for(i=1; i<4; i++)
  372. {
  373. int diff = thiz->brick[0] - thiz->brick[i];
  374. if(diff == 1)
  375. {
  376. tmp[i] = thiz->brick[0] - thiz->width;
  377. }
  378. else if(diff == -1)
  379. {
  380. tmp[i] = thiz->brick[0] + thiz->width;
  381. }
  382. else if(diff == 2)
  383. {
  384. tmp[i] = thiz->brick[0] - 2 * thiz->width;
  385. }
  386. else if(diff == -2)
  387. {
  388. tmp[i] = thiz->brick[0] + 2 * thiz->width;
  389. }
  390. else if(diff == thiz->width - 1)
  391. {
  392. tmp[i] = thiz->brick[0] + thiz->width + 1;
  393. }
  394. else if(diff == 1 - thiz->width)
  395. {
  396. tmp[i] = thiz->brick[0] - thiz->width - 1;
  397. }
  398. else if(diff == thiz->width)
  399. {
  400. if((thiz->brick[0] + 1) % thiz->width == 0)
  401. {
  402. /* restore the deleted brick */
  403. rt_tetris_append_brick(thiz, thiz->brick);
  404. return -RT_ERROR;
  405. }
  406. else tmp[i] = thiz->brick[0] + 1;
  407. }
  408. else if(diff == -1 * (thiz->width))
  409. {
  410. if(thiz->brick[0] % thiz->width == 0)
  411. {
  412. /* restore the deleted brick */
  413. rt_tetris_append_brick(thiz, thiz->brick);
  414. return -RT_ERROR;
  415. }
  416. else tmp[i] = thiz->brick[0] - 1;
  417. }
  418. else if(diff == thiz->width + 1)
  419. {
  420. tmp[i] = thiz->brick[0] - thiz->width + 1;
  421. }
  422. else if(diff == -1 - thiz->width)
  423. {
  424. tmp[i] = thiz->brick[0] + thiz->width - 1;
  425. }
  426. else if(diff == 2 * thiz->width)
  427. {
  428. if((thiz->brick[0] % thiz->width) >= (thiz->width - 2))
  429. {
  430. /* restore the deleted brick */
  431. rt_tetris_append_brick(thiz, thiz->brick);
  432. return -RT_ERROR;
  433. }
  434. else tmp[i] = thiz->brick[0] + 2;
  435. }
  436. else if(diff == -2 * thiz->width)
  437. {
  438. if((thiz->brick[0] % thiz->width) < 2)
  439. {
  440. /* restore the deleted brick */
  441. rt_tetris_append_brick(thiz, thiz->brick);
  442. return -RT_ERROR;
  443. }
  444. else tmp[i] = thiz->brick[0] - 2;
  445. }
  446. if(tmp[i] > (thiz->height) * thiz->width)
  447. {
  448. /* restore the deleted brick */
  449. rt_tetris_append_brick(thiz, thiz->brick);
  450. return -RT_ERROR;
  451. }
  452. if(rt_tetris_check_collision(thiz, tmp[i]) == RT_EOK)
  453. {
  454. /* restore the deleted brick */
  455. rt_tetris_append_brick(thiz, thiz->brick);
  456. return -RT_ERROR;
  457. }
  458. }
  459. /* do roration */
  460. for(i=0; i<4; i++)
  461. {
  462. thiz->brick[i] = tmp[i];
  463. }
  464. /* append the brick to tetris panel */
  465. rt_tetris_append_brick(thiz, thiz->brick);
  466. /* update view */
  467. thiz->view->update(thiz->view, thiz);
  468. return RT_EOK;
  469. }
  470. /**
  471. * this function add a view to the tetris
  472. *
  473. * @param thiz the tetris instance.
  474. * @param view the view instance.
  475. *
  476. * @return RT_EOK on success, -RT_ERROR on fail
  477. */
  478. rt_err_t rt_tetris_add_view(rt_tetris_t* thiz, rt_tetris_view_t* view)
  479. {
  480. RT_ASSERT(thiz != RT_NULL);
  481. /* Only suppurt single view now */
  482. thiz->view = view;
  483. return RT_EOK;
  484. }
  485. /**
  486. * this function delete a view from the tetris
  487. *
  488. * @param thiz the tetris instance.
  489. * @param view the view instance.
  490. *
  491. * @return RT_EOK on success, -RT_ERROR on fail
  492. */
  493. rt_err_t rt_tetris_delete_view(rt_tetris_t* thiz, rt_tetris_view_t* view)
  494. {
  495. RT_ASSERT(thiz != RT_NULL);
  496. thiz->view = RT_NULL;
  497. return RT_EOK;
  498. }
  499. /**
  500. * this function used to check collision
  501. *
  502. * @param thiz the tetris instance.
  503. * @param block the block to be checked.
  504. *
  505. * @return RT_EOK on collision, -RT_ERROR on not collision
  506. */
  507. rt_err_t rt_tetris_check_collision(rt_tetris_t* thiz, rt_uint32_t block)
  508. {
  509. RT_ASSERT(thiz != RT_NULL);
  510. RT_ASSERT(block < thiz->height * thiz->width);
  511. if((thiz->panel[block/thiz->width] & (1 << (block % thiz->width)))
  512. == (1 << (block % thiz->width)))
  513. {
  514. return RT_EOK;
  515. }
  516. else
  517. {
  518. return -RT_ERROR;
  519. }
  520. }
  521. static rt_err_t rt_tetris_update_brick(rt_tetris_t* thiz)
  522. {
  523. int index;
  524. RT_ASSERT(thiz != RT_NULL);
  525. index = (int)(7.0 * rand()/(RAND_MAX + 1.0));
  526. rt_memcpy(thiz->brick, thiz->next_brick, 4 * sizeof(rt_uint32_t));
  527. rt_memcpy(thiz->next_brick, g_brick[index], 4 * sizeof(rt_uint32_t));
  528. /* update next brick on view */
  529. thiz->view->update_next_brick(thiz->view, thiz);
  530. return RT_EOK;
  531. }
  532. static rt_err_t rt_tetris_append_brick(rt_tetris_t* thiz, rt_uint32_t brick[])
  533. {
  534. int i;
  535. RT_ASSERT(thiz != RT_NULL);
  536. RT_ASSERT(brick != RT_NULL);
  537. for(i=0; i<4; i++)
  538. {
  539. int y = brick[i]/thiz->width;
  540. int x = brick[i]%thiz->width;
  541. thiz->panel[y] |= (1<<x);
  542. }
  543. return RT_EOK;
  544. }
  545. static rt_err_t rt_tetris_delete_brick(rt_tetris_t* thiz, rt_uint32_t brick[])
  546. {
  547. int i;
  548. RT_ASSERT(thiz != RT_NULL);
  549. RT_ASSERT(brick != RT_NULL);
  550. for(i=0; i<4; i++)
  551. {
  552. int y = brick[i]/thiz->width;
  553. int x = brick[i]%thiz->width;
  554. thiz->panel[y] &= ~(1<<x);
  555. }
  556. return RT_EOK;
  557. }
  558. static rt_err_t rt_tetris_is_reach_top(rt_tetris_t* thiz, rt_uint32_t brick[])
  559. {
  560. int i;
  561. RT_ASSERT(thiz != RT_NULL);
  562. RT_ASSERT(brick != RT_NULL);
  563. for(i=0; i<4; i++)
  564. {
  565. if(brick[i] / thiz->width == 0)
  566. return RT_EOK;
  567. }
  568. return -RT_ERROR;
  569. }
  570. static rt_err_t rt_tetris_release_lines(rt_tetris_t* thiz, rt_uint32_t brick[])
  571. {
  572. int i, j, check_line = 0;
  573. rt_bool_t line_released = -RT_ERROR;
  574. RT_ASSERT(thiz != RT_NULL);
  575. RT_ASSERT(brick != RT_NULL);
  576. for(i=0; i<4; i++)
  577. {
  578. /* choose a line */
  579. check_line = brick[i]/thiz->width;
  580. if((thiz->panel[check_line]) == ((1 << thiz->width) - 1))
  581. {
  582. for(j=check_line; j>0; j--)
  583. {
  584. thiz->panel[j] = thiz->panel[j-1];
  585. }
  586. /* clear the first line */
  587. thiz->panel[0] = 0;
  588. for(j=i+1; j<4; j++)
  589. {
  590. if(brick[j] < brick[i])
  591. {
  592. brick[j] += thiz->width;
  593. }
  594. }
  595. thiz->lines++;
  596. thiz->score += 100;
  597. line_released = RT_EOK;
  598. }
  599. }
  600. if(line_released == RT_EOK)
  601. {
  602. /* update view */
  603. thiz->view->update_score_and_lines(thiz->view, thiz);
  604. return RT_EOK;
  605. }
  606. else
  607. {
  608. return -RT_ERROR;
  609. }
  610. }