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