tetris_ui.c 5.0 KB

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