test/testhittesting.c
author Ryan C. Gordon <icculus@icculus.org>
Fri, 30 May 2014 01:48:26 -0400
changeset 8940 850e149d02d8
parent 8939 caaa7653f02b
child 8946 c9592936dffc
permissions -rw-r--r--
Patched to compile on Visual Studio.
     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_Event e;
    51         int nothing_to_do = 1;
    52 
    53         SDL_SetRenderDrawColor(renderer, 0, 0, 127, 255);
    54         SDL_RenderClear(renderer);
    55         SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
    56         SDL_RenderFillRects(renderer, areas, SDL_arraysize(drag_areas));
    57         SDL_RenderPresent(renderer);
    58 
    59         while (SDL_PollEvent(&e)) {
    60             nothing_to_do = 0;
    61 
    62             switch (e.type)
    63             {
    64                 case SDL_MOUSEBUTTONDOWN:
    65                     SDL_Log("button down!\n");
    66                     break;
    67 
    68                 case SDL_MOUSEBUTTONUP:
    69                     SDL_Log("button up!\n");
    70                     break;
    71 
    72                 case SDL_WINDOWEVENT:
    73                     if (e.window.event == SDL_WINDOWEVENT_MOVED) {
    74                         SDL_Log("Window event moved to (%d, %d)!\n", (int) e.window.data1, (int) e.window.data2);
    75                     }
    76                     break;
    77 
    78                 case SDL_KEYDOWN:
    79                     if (e.key.keysym.sym == SDLK_ESCAPE) {
    80                         done = 1;
    81                     } else if (e.key.keysym.sym == SDLK_x) {
    82                         if (!areas) {
    83                             areas = drag_areas;
    84                             numareas = SDL_arraysize(drag_areas);
    85                         } else {
    86                             areas = NULL;
    87                             numareas = 0;
    88                         }
    89                     }
    90                     break;
    91 
    92                 case SDL_QUIT:
    93                     done = 1;
    94                     break;
    95             }
    96         }
    97 
    98         if (nothing_to_do) {
    99             SDL_Delay(50);
   100         }
   101     }
   102 
   103     SDL_Quit();
   104     return 0;
   105 }