demo_view_benchmark.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #include <rtgui/dc.h>
  2. #include <rtgui/dc_hw.h>
  3. #include <rtgui/rtgui_system.h>
  4. #include <rtgui/widgets/view.h>
  5. #include "demo_view.h"
  6. #define RAND(x1, x2) ((rand() % (x2 - x1)) + x1)
  7. static rtgui_view_t* view = RT_NULL;
  8. static int running = 0;
  9. void _onidle(rtgui_widget_t* widget, rtgui_event_t *event)
  10. {
  11. rtgui_color_t color;
  12. rtgui_rect_t rect, draw_rect;
  13. struct rtgui_dc *dc;
  14. /* 获得控件所属的DC */
  15. // dc = rtgui_dc_hw_create(RTGUI_WIDGET(view));
  16. dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(view));
  17. if (dc == RT_NULL) return ;
  18. demo_view_get_rect(RTGUI_VIEW(view), &rect);
  19. draw_rect.x1 = RAND(rect.x1, rect.x2);
  20. draw_rect.y1 = RAND(rect.y1, rect.y2);
  21. draw_rect.x2 = RAND(draw_rect.x1, rect.x2);
  22. draw_rect.y2 = RAND(draw_rect.y1, rect.y2);
  23. color = RTGUI_RGB(rand() % 255, rand() % 255, rand() % 255);
  24. RTGUI_WIDGET_BACKGROUND(RTGUI_WIDGET(view)) = color;
  25. rtgui_dc_fill_rect(dc, &draw_rect);
  26. /* 绘图完成 */
  27. rtgui_dc_end_drawing(dc);
  28. }
  29. void _draw_default(rtgui_widget_t* widget, rtgui_event_t* event)
  30. {
  31. struct rtgui_dc* dc;
  32. rtgui_rect_t rect;
  33. /* 因为用的是demo view,上面本身有一部分控件,所以在绘图时先要让demo view先绘图 */
  34. rtgui_view_event_handler(widget, event);
  35. /* 获得控件所属的DC */
  36. dc = rtgui_dc_begin_drawing(widget);
  37. if (dc == RT_NULL) /* 如果不能正常获得DC,返回(如果控件或父控件是隐藏状态,DC是获取不成功的) */
  38. return ;
  39. /* 获得demo view允许绘图的区域 */
  40. demo_view_get_rect(RTGUI_VIEW(widget), &rect);
  41. /* 擦除所有 */
  42. RTGUI_WIDGET_BACKGROUND(widget) = default_background;
  43. rtgui_dc_fill_rect(dc, &rect);
  44. /* 显示提示 */
  45. rtgui_dc_draw_text(dc, "按任意键开始/停止测试...", &rect);
  46. /* 绘图完成 */
  47. rtgui_dc_end_drawing(dc);
  48. }
  49. rt_bool_t benchmark_event_handler(rtgui_widget_t* widget, rtgui_event_t *event)
  50. {
  51. if (event->type == RTGUI_EVENT_PAINT)
  52. {
  53. _draw_default(widget, event);
  54. }
  55. else if (event->type == RTGUI_EVENT_KBD)
  56. {
  57. struct rtgui_event_kbd *kbd = (struct rtgui_event_kbd*)event;
  58. if (RTGUI_KBD_IS_UP(kbd))
  59. {
  60. if (running)
  61. {
  62. /* stop */
  63. rtgui_thread_set_onidle(RT_NULL);
  64. _draw_default(widget, event);
  65. }
  66. else
  67. {
  68. /* run */
  69. rtgui_thread_set_onidle(_onidle);
  70. }
  71. running = !running;
  72. }
  73. return RT_TRUE;
  74. }
  75. else
  76. {
  77. /* 调用默认的事件处理函数 */
  78. return rtgui_view_event_handler(widget, event);
  79. }
  80. return RT_FALSE;
  81. }
  82. rtgui_view_t *demo_view_benchmark(rtgui_workbench_t* workbench)
  83. {
  84. srand(100);
  85. view = demo_view(workbench, "绘图测试");
  86. rtgui_widget_set_event_handler(RTGUI_WIDGET(view), benchmark_event_handler);
  87. return view;
  88. }