test/testhittesting.c
author Ryan C. Gordon
Wed, 28 May 2014 01:22:47 -0400
changeset 8935 9d2f0236322b
parent 8931 test/testdragareas.c@44d8a2f4b431
child 8936 59cbb9959346
permissions -rw-r--r--
Changed drag area API to a hit-testing API.

There were several good arguments for this: it's how Windows works with
WM_NCHITTEST, SDL doesn't need to manage a list of rects, it allows more
control over the regions (how do you use rects to cleanly surround a circular
button?), the callback can be more optimized than a iterating a list of
rects, and you don't have to send an updated list of rects whenever the
window resizes or layout changes.
     1 #include <stdio.h>
     2 #include "SDL.h"
     3 
     4 /* !!! FIXME: rewrite this to be wired in to test framework. */
     5 
     6 const SDL_Rect drag_areas[] = {
     7     { 20, 20, 100, 100 },
     8     { 200, 70, 100, 100 },
     9     { 400, 90, 100, 100 }
    10 };
    11 
    12 static const SDL_Rect *areas = drag_areas;
    13 static int numareas = SDL_arraysize(drag_areas);
    14 
    15 static SDL_HitTestResult
    16 hitTest(SDL_Window *window, const SDL_Point *pt, void *data)
    17 {
    18     int i;
    19     for (i = 0; i < numareas; i++) {
    20         if (SDL_PointInRect(pt, &areas[i])) {
    21             return SDL_HITTEST_DRAGGABLE;
    22         }
    23     }
    24 
    25     return SDL_HITTEST_NORMAL;
    26 }
    27 
    28 
    29 int main(int argc, char **argv)
    30 {
    31     int done = 0;
    32     SDL_Window *window;
    33     SDL_Renderer *renderer;
    34 
    35     /* !!! FIXME: check for errors. */
    36     SDL_Init(SDL_INIT_VIDEO);
    37     window = SDL_CreateWindow("Drag the red boxes", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, SDL_WINDOW_BORDERLESS);
    38     renderer = SDL_CreateRenderer(window, -1, 0);
    39 
    40     if (SDL_SetWindowHitTest(window, hitTest, NULL) == -1) {
    41         fprintf(stderr, "Enabling hit-testing failed!\n");
    42         SDL_Quit();
    43         return 1;
    44     }
    45 
    46     while (!done)
    47     {
    48         SDL_SetRenderDrawColor(renderer, 0, 0, 127, 255);
    49         SDL_RenderClear(renderer);
    50         SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
    51         SDL_RenderFillRects(renderer, areas, SDL_arraysize(drag_areas));
    52         SDL_RenderPresent(renderer);
    53 
    54         SDL_Event e;
    55         int nothing_to_do = 1;
    56         while (SDL_PollEvent(&e)) {
    57             nothing_to_do = 0;
    58 
    59             switch (e.type)
    60             {
    61                 case SDL_MOUSEBUTTONDOWN:
    62                     printf("button down!\n");
    63                     break;
    64 
    65                 case SDL_MOUSEBUTTONUP:
    66                     printf("button up!\n");
    67                     break;
    68 
    69                 case SDL_WINDOWEVENT:
    70                     if (e.window.event == SDL_WINDOWEVENT_MOVED) {
    71                         printf("Window event moved to (%d, %d)!\n", (int) e.window.data1, (int) e.window.data2);
    72                     }
    73                     break;
    74 
    75                 case SDL_KEYDOWN:
    76                     if (e.key.keysym.sym == SDLK_ESCAPE) {
    77                         done = 1;
    78                     } else if (e.key.keysym.sym == SDLK_x) {
    79                         if (!areas) {
    80                             areas = drag_areas;
    81                             numareas = SDL_arraysize(drag_areas);
    82                         } else {
    83                             areas = NULL;
    84                             numareas = 0;
    85                         }
    86                     }
    87                     break;
    88 
    89                 case SDL_QUIT:
    90                     done = 1;
    91                     break;
    92             }
    93         }
    94 
    95         if (nothing_to_do) {
    96             SDL_Delay(50);
    97         }
    98     }
    99 
   100     SDL_Quit();
   101     return 0;
   102 }