demo_view_benchmark.c 2.6 KB

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