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