testhittesting.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #include <stdio.h>
  2. #include "SDL.h"
  3. /* !!! FIXME: rewrite this to be wired in to test framework. */
  4. #define RESIZE_BORDER 20
  5. const SDL_Rect drag_areas[] = {
  6. { 20, 20, 100, 100 },
  7. { 200, 70, 100, 100 },
  8. { 400, 90, 100, 100 }
  9. };
  10. static const SDL_Rect *areas = drag_areas;
  11. static int numareas = SDL_arraysize(drag_areas);
  12. static SDL_HitTestResult SDLCALL
  13. hitTest(SDL_Window *window, const SDL_Point *pt, void *data)
  14. {
  15. int i;
  16. int w, h;
  17. for (i = 0; i < numareas; i++) {
  18. if (SDL_PointInRect(pt, &areas[i])) {
  19. SDL_Log("HIT-TEST: DRAGGABLE\n");
  20. return SDL_HITTEST_DRAGGABLE;
  21. }
  22. }
  23. SDL_GetWindowSize(window, &w, &h);
  24. #define REPORT_RESIZE_HIT(name) { \
  25. SDL_Log("HIT-TEST: RESIZE_" #name "\n"); \
  26. return SDL_HITTEST_RESIZE_##name; \
  27. }
  28. if (pt->x < RESIZE_BORDER && pt->y < RESIZE_BORDER) {
  29. REPORT_RESIZE_HIT(TOPLEFT);
  30. } else if (pt->x > RESIZE_BORDER && pt->x < w - RESIZE_BORDER && pt->y < RESIZE_BORDER) {
  31. REPORT_RESIZE_HIT(TOP);
  32. } else if (pt->x > w - RESIZE_BORDER && pt->y < RESIZE_BORDER) {
  33. REPORT_RESIZE_HIT(TOPRIGHT);
  34. } else if (pt->x > w - RESIZE_BORDER && pt->y > RESIZE_BORDER && pt->y < h - RESIZE_BORDER) {
  35. REPORT_RESIZE_HIT(RIGHT);
  36. } else if (pt->x > w - RESIZE_BORDER && pt->y > h - RESIZE_BORDER) {
  37. REPORT_RESIZE_HIT(BOTTOMRIGHT);
  38. } else if (pt->x < w - RESIZE_BORDER && pt->x > RESIZE_BORDER && pt->y > h - RESIZE_BORDER) {
  39. REPORT_RESIZE_HIT(BOTTOM);
  40. } else if (pt->x < RESIZE_BORDER && pt->y > h - RESIZE_BORDER) {
  41. REPORT_RESIZE_HIT(BOTTOMLEFT);
  42. } else if (pt->x < RESIZE_BORDER && pt->y < h - RESIZE_BORDER && pt->y > RESIZE_BORDER) {
  43. REPORT_RESIZE_HIT(LEFT);
  44. }
  45. SDL_Log("HIT-TEST: NORMAL\n");
  46. return SDL_HITTEST_NORMAL;
  47. }
  48. int main(int argc, char **argv)
  49. {
  50. int done = 0;
  51. SDL_Window *window;
  52. SDL_Renderer *renderer;
  53. /* !!! FIXME: check for errors. */
  54. SDL_Init(SDL_INIT_VIDEO);
  55. window = SDL_CreateWindow("Drag the red boxes", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, SDL_WINDOW_BORDERLESS | SDL_WINDOW_RESIZABLE);
  56. renderer = SDL_CreateRenderer(window, -1, 0);
  57. if (SDL_SetWindowHitTest(window, hitTest, NULL) == -1) {
  58. SDL_Log("Enabling hit-testing failed!\n");
  59. SDL_Quit();
  60. return 1;
  61. }
  62. while (!done)
  63. {
  64. SDL_Event e;
  65. int nothing_to_do = 1;
  66. SDL_SetRenderDrawColor(renderer, 0, 0, 127, 255);
  67. SDL_RenderClear(renderer);
  68. SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
  69. SDL_RenderFillRects(renderer, areas, SDL_arraysize(drag_areas));
  70. SDL_RenderPresent(renderer);
  71. while (SDL_PollEvent(&e)) {
  72. nothing_to_do = 0;
  73. switch (e.type)
  74. {
  75. case SDL_MOUSEBUTTONDOWN:
  76. SDL_Log("button down!\n");
  77. break;
  78. case SDL_MOUSEBUTTONUP:
  79. SDL_Log("button up!\n");
  80. break;
  81. case SDL_WINDOWEVENT:
  82. if (e.window.event == SDL_WINDOWEVENT_MOVED) {
  83. SDL_Log("Window event moved to (%d, %d)!\n", (int) e.window.data1, (int) e.window.data2);
  84. }
  85. break;
  86. case SDL_KEYDOWN:
  87. if (e.key.keysym.sym == SDLK_ESCAPE) {
  88. done = 1;
  89. } else if (e.key.keysym.sym == SDLK_x) {
  90. if (!areas) {
  91. areas = drag_areas;
  92. numareas = SDL_arraysize(drag_areas);
  93. } else {
  94. areas = NULL;
  95. numareas = 0;
  96. }
  97. }
  98. break;
  99. case SDL_QUIT:
  100. done = 1;
  101. break;
  102. }
  103. }
  104. if (nothing_to_do) {
  105. SDL_Delay(50);
  106. }
  107. }
  108. SDL_Quit();
  109. return 0;
  110. }