test/testhittesting.c
changeset 8935 9d2f0236322b
parent 8931 44d8a2f4b431
child 8936 59cbb9959346
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/testhittesting.c	Wed May 28 01:22:47 2014 -0400
     1.3 @@ -0,0 +1,102 @@
     1.4 +#include <stdio.h>
     1.5 +#include "SDL.h"
     1.6 +
     1.7 +/* !!! FIXME: rewrite this to be wired in to test framework. */
     1.8 +
     1.9 +const SDL_Rect drag_areas[] = {
    1.10 +    { 20, 20, 100, 100 },
    1.11 +    { 200, 70, 100, 100 },
    1.12 +    { 400, 90, 100, 100 }
    1.13 +};
    1.14 +
    1.15 +static const SDL_Rect *areas = drag_areas;
    1.16 +static int numareas = SDL_arraysize(drag_areas);
    1.17 +
    1.18 +static SDL_HitTestResult
    1.19 +hitTest(SDL_Window *window, const SDL_Point *pt, void *data)
    1.20 +{
    1.21 +    int i;
    1.22 +    for (i = 0; i < numareas; i++) {
    1.23 +        if (SDL_PointInRect(pt, &areas[i])) {
    1.24 +            return SDL_HITTEST_DRAGGABLE;
    1.25 +        }
    1.26 +    }
    1.27 +
    1.28 +    return SDL_HITTEST_NORMAL;
    1.29 +}
    1.30 +
    1.31 +
    1.32 +int main(int argc, char **argv)
    1.33 +{
    1.34 +    int done = 0;
    1.35 +    SDL_Window *window;
    1.36 +    SDL_Renderer *renderer;
    1.37 +
    1.38 +    /* !!! FIXME: check for errors. */
    1.39 +    SDL_Init(SDL_INIT_VIDEO);
    1.40 +    window = SDL_CreateWindow("Drag the red boxes", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, SDL_WINDOW_BORDERLESS);
    1.41 +    renderer = SDL_CreateRenderer(window, -1, 0);
    1.42 +
    1.43 +    if (SDL_SetWindowHitTest(window, hitTest, NULL) == -1) {
    1.44 +        fprintf(stderr, "Enabling hit-testing failed!\n");
    1.45 +        SDL_Quit();
    1.46 +        return 1;
    1.47 +    }
    1.48 +
    1.49 +    while (!done)
    1.50 +    {
    1.51 +        SDL_SetRenderDrawColor(renderer, 0, 0, 127, 255);
    1.52 +        SDL_RenderClear(renderer);
    1.53 +        SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
    1.54 +        SDL_RenderFillRects(renderer, areas, SDL_arraysize(drag_areas));
    1.55 +        SDL_RenderPresent(renderer);
    1.56 +
    1.57 +        SDL_Event e;
    1.58 +        int nothing_to_do = 1;
    1.59 +        while (SDL_PollEvent(&e)) {
    1.60 +            nothing_to_do = 0;
    1.61 +
    1.62 +            switch (e.type)
    1.63 +            {
    1.64 +                case SDL_MOUSEBUTTONDOWN:
    1.65 +                    printf("button down!\n");
    1.66 +                    break;
    1.67 +
    1.68 +                case SDL_MOUSEBUTTONUP:
    1.69 +                    printf("button up!\n");
    1.70 +                    break;
    1.71 +
    1.72 +                case SDL_WINDOWEVENT:
    1.73 +                    if (e.window.event == SDL_WINDOWEVENT_MOVED) {
    1.74 +                        printf("Window event moved to (%d, %d)!\n", (int) e.window.data1, (int) e.window.data2);
    1.75 +                    }
    1.76 +                    break;
    1.77 +
    1.78 +                case SDL_KEYDOWN:
    1.79 +                    if (e.key.keysym.sym == SDLK_ESCAPE) {
    1.80 +                        done = 1;
    1.81 +                    } else if (e.key.keysym.sym == SDLK_x) {
    1.82 +                        if (!areas) {
    1.83 +                            areas = drag_areas;
    1.84 +                            numareas = SDL_arraysize(drag_areas);
    1.85 +                        } else {
    1.86 +                            areas = NULL;
    1.87 +                            numareas = 0;
    1.88 +                        }
    1.89 +                    }
    1.90 +                    break;
    1.91 +
    1.92 +                case SDL_QUIT:
    1.93 +                    done = 1;
    1.94 +                    break;
    1.95 +            }
    1.96 +        }
    1.97 +
    1.98 +        if (nothing_to_do) {
    1.99 +            SDL_Delay(50);
   1.100 +        }
   1.101 +    }
   1.102 +
   1.103 +    SDL_Quit();
   1.104 +    return 0;
   1.105 +}