tetris_ui.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. * File : tetris_ui.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 <rtgui/rtgui.h>
  15. #include <rtgui/rtgui_system.h>
  16. #include <rtgui/widgets/view.h>
  17. #include <rtgui/widgets/label.h>
  18. #include <rtgui/widgets/button.h>
  19. #include <rtgui/widgets/window.h>
  20. #include <rtgui/widgets/workbench.h>
  21. #include <rtgui/dc.h>
  22. #include "tetris.h"
  23. struct app_info
  24. {
  25. struct rtgui_view* home_view;
  26. struct rtgui_workbench* workbench;
  27. rt_tetris_t * tetris;
  28. rt_tetris_view_t* tetris_view;
  29. rtgui_timer_t* _timer;
  30. };
  31. typedef struct app_info app_info;
  32. static app_info g_app_info;
  33. static void _game_over(void)
  34. {
  35. rtgui_timer_stop(g_app_info._timer);
  36. rt_tetris_pause(g_app_info.tetris);
  37. rt_tetris_destory(g_app_info.tetris);
  38. rt_tetris_view_destroy(g_app_info.tetris_view);
  39. rtgui_view_destroy(g_app_info.home_view);
  40. rtgui_workbench_close(g_app_info.workbench);
  41. rt_kprintf("GAME OVER\n");
  42. }
  43. static rt_bool_t home_view_event_handler(struct rtgui_widget* widget, struct rtgui_event* event)
  44. {
  45. if (event->type == RTGUI_EVENT_PAINT)
  46. {
  47. struct rtgui_dc* dc;
  48. rtgui_rect_t rect;
  49. /* draw child */
  50. rtgui_view_event_handler(widget, event);
  51. dc = rtgui_dc_begin_drawing(widget);
  52. if (dc == RT_NULL) return -RT_ERROR;
  53. rect.x1 = 96;
  54. rect.y1 = 0;
  55. rect.x2 = 128;
  56. rect.y2 = 16;
  57. rtgui_dc_draw_text(dc, "next", &rect);
  58. rect.y1 += 30;
  59. rect.y2 = rect.y1 + 16;
  60. rtgui_dc_draw_text(dc, "level", &rect);
  61. rect.y1 += 22;
  62. rect.y2 = rect.y1 + 16;
  63. rtgui_dc_draw_text(dc, "lines", &rect);
  64. rect.y1 += 22;
  65. rect.y2 = rect.y1 + 16;
  66. rtgui_dc_draw_text(dc, "score", &rect);
  67. rtgui_dc_end_drawing(dc);
  68. /* start tetris game, removed later */
  69. rt_tetris_start(g_app_info.tetris);
  70. return RT_FALSE;
  71. }
  72. else if ((event->type == RTGUI_EVENT_KBD))
  73. {
  74. struct rtgui_event_kbd* ekbd = (struct rtgui_event_kbd*)event;
  75. if (ekbd->type == RTGUI_KEYDOWN)
  76. {
  77. if (ekbd->key == RTGUIK_RIGHT)
  78. {
  79. rt_tetris_right(g_app_info.tetris);
  80. }
  81. else if (ekbd->key == RTGUIK_LEFT)
  82. {
  83. rt_tetris_left(g_app_info.tetris);
  84. }
  85. else if (ekbd->key == RTGUIK_UP)
  86. {
  87. rt_tetris_rotate(g_app_info.tetris, RT_EOK);
  88. }
  89. else if (ekbd->key == RTGUIK_DOWN)
  90. {
  91. if( rt_tetris_drop(g_app_info.tetris) == -RT_ETIMEOUT
  92. && rt_tetris_status(g_app_info.tetris) != RT_FALSE)
  93. {
  94. _game_over();
  95. }
  96. }
  97. }
  98. }
  99. return rtgui_view_event_handler(widget, event);
  100. }
  101. static void _timer_timeout(rtgui_timer_t* timer, void* parameter)
  102. {
  103. if( rt_tetris_down(g_app_info.tetris) == -RT_ETIMEOUT)
  104. {
  105. _game_over();
  106. }
  107. }
  108. static rt_bool_t workbench_event_handler(rtgui_widget_t *widget, rtgui_event_t *event)
  109. {
  110. if (event->type == RTGUI_EVENT_KBD)
  111. {
  112. struct rtgui_event_kbd* ekbd = (struct rtgui_event_kbd*)event;
  113. if (((ekbd->type == RTGUI_KEYUP) && ekbd->key == RTGUIK_HOME)
  114. && !RTGUI_WORKBENCH_IS_MODAL_MODE(g_app_info.workbench))
  115. {
  116. /* active home view */
  117. if (g_app_info.workbench->current_view != g_app_info.home_view)
  118. {
  119. rtgui_view_show(g_app_info.home_view, RT_FALSE);
  120. return RT_TRUE;
  121. }
  122. }
  123. }
  124. return rtgui_workbench_event_handler(widget, event);
  125. }
  126. void tetris_ui_entry(void* parameter)
  127. {
  128. rt_mq_t mq;
  129. mq = rt_mq_create("tetris_ui", 256, 4, RT_IPC_FLAG_FIFO);
  130. rtgui_thread_register(rt_thread_self(), mq);
  131. g_app_info.workbench = rtgui_workbench_create("main", "workbench");
  132. if (g_app_info.workbench == RT_NULL)
  133. {
  134. rt_kprintf("can't find panel 'main'\n");
  135. return;
  136. }
  137. rtgui_widget_set_event_handler(RTGUI_WIDGET(g_app_info.workbench), workbench_event_handler);
  138. /* add home view */
  139. g_app_info.home_view = rtgui_view_create("Home");
  140. rtgui_widget_set_event_handler(RTGUI_WIDGET(g_app_info.home_view), home_view_event_handler);
  141. rtgui_workbench_add_view(g_app_info.workbench, g_app_info.home_view);
  142. /* this view can be focused */
  143. RTGUI_WIDGET(g_app_info.home_view)->flag |= RTGUI_WIDGET_FLAG_FOCUSABLE;
  144. /* set widget focus */
  145. rtgui_widget_focus(RTGUI_WIDGET(g_app_info.home_view));
  146. RTGUI_WIDGET_BACKGROUND(RTGUI_WIDGET(g_app_info.home_view)) = RTGUI_RGB(0xff, 0xff, 0xff);
  147. rtgui_view_show(g_app_info.home_view, RT_FALSE);
  148. /* create tetris modal instance */
  149. g_app_info.tetris = rt_tetris_create(16, 17);
  150. /* create tetris view instance */
  151. g_app_info.tetris_view = rt_tetris_view_create(RTGUI_WIDGET(g_app_info.home_view));
  152. /* register tetris view to tetris modal */
  153. rt_tetris_add_view(g_app_info.tetris, g_app_info.tetris_view);
  154. g_app_info._timer = rtgui_timer_create(40, RT_TIMER_FLAG_PERIODIC, _timer_timeout, RT_NULL);
  155. rtgui_timer_start(g_app_info._timer);
  156. rtgui_workbench_event_loop(g_app_info.workbench);
  157. rtgui_workbench_destroy(g_app_info.workbench);
  158. rtgui_thread_deregister(rt_thread_self());
  159. rt_mq_delete(mq);
  160. }