test/testhittesting.c
author Ryan C. Gordon <icculus@icculus.org>
Fri, 30 May 2014 01:48:08 -0400
changeset 8939 caaa7653f02b
parent 8936 59cbb9959346
child 8940 850e149d02d8
permissions -rw-r--r--
Make some printf() calls into SDL_Log() so I can see them on Windows. :)
     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             SDL_Log("HIT-TEST: DRAGGABLE\n");
    22             return SDL_HITTEST_DRAGGABLE;
    23         }
    24     }
    25 
    26     SDL_Log("HIT-TEST: NORMAL\n");
    27     return SDL_HITTEST_NORMAL;
    28 }
    29 
    30 
    31 int main(int argc, char **argv)
    32 {
    33     int done = 0;
    34     SDL_Window *window;
    35     SDL_Renderer *renderer;
    36 
    37     /* !!! FIXME: check for errors. */
    38     SDL_Init(SDL_INIT_VIDEO);
    39     window = SDL_CreateWindow("Drag the red boxes", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, SDL_WINDOW_BORDERLESS);
    40     renderer = SDL_CreateRenderer(window, -1, 0);
    41 
    42     if (SDL_SetWindowHitTest(window, hitTest, NULL) == -1) {
    43         SDL_Log("Enabling hit-testing failed!\n");
    44         SDL_Quit();
    45         return 1;
    46     }
    47 
    48     while (!done)
    49     {
    50         SDL_SetRenderDrawColor(renderer, 0, 0, 127, 255);
    51         SDL_RenderClear(renderer);
    52         SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
    53         SDL_RenderFillRects(renderer, areas, SDL_arraysize(drag_areas));
    54         SDL_RenderPresent(renderer);
    55 
    56         SDL_Event e;
    57         int nothing_to_do = 1;
    58         while (SDL_PollEvent(&e)) {
    59             nothing_to_do = 0;
    60 
    61             switch (e.type)
    62             {
    63                 case SDL_MOUSEBUTTONDOWN:
    64                     SDL_Log("button down!\n");
    65                     break;
    66 
    67                 case SDL_MOUSEBUTTONUP:
    68                     SDL_Log("button up!\n");
    69                     break;
    70 
    71                 case SDL_WINDOWEVENT:
    72                     if (e.window.event == SDL_WINDOWEVENT_MOVED) {
    73                         SDL_Log("Window event moved to (%d, %d)!\n", (int) e.window.data1, (int) e.window.data2);
    74                     }
    75                     break;
    76 
    77                 case SDL_KEYDOWN:
    78                     if (e.key.keysym.sym == SDLK_ESCAPE) {
    79                         done = 1;
    80                     } else if (e.key.keysym.sym == SDLK_x) {
    81                         if (!areas) {
    82                             areas = drag_areas;
    83                             numareas = SDL_arraysize(drag_areas);
    84                         } else {
    85                             areas = NULL;
    86                             numareas = 0;
    87                         }
    88                     }
    89                     break;
    90 
    91                 case SDL_QUIT:
    92                     done = 1;
    93                     break;
    94             }
    95         }
    96 
    97         if (nothing_to_do) {
    98             SDL_Delay(50);
    99         }
   100     }
   101 
   102     SDL_Quit();
   103     return 0;
   104 }