test/testhittesting.c
author Sam Lantinga <slouken@libsdl.org>
Thu, 07 Dec 2017 16:08:09 -0800
changeset 11730 ac6c607e065c
parent 11272 d60ede5a0c2e
permissions -rw-r--r--
Enable building the Metal renderer by default, and weak link the Metal framework so the SDL library is safe to use on older Macs
Also generate iOS versions of the Metal shaders
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
ionut@8946
     6
#define RESIZE_BORDER 20
ionut@8946
     7
icculus@8935
     8
const SDL_Rect drag_areas[] = {
icculus@8935
     9
    { 20, 20, 100, 100 },
icculus@8935
    10
    { 200, 70, 100, 100 },
icculus@8935
    11
    { 400, 90, 100, 100 }
icculus@8935
    12
};
icculus@8935
    13
icculus@8935
    14
static const SDL_Rect *areas = drag_areas;
icculus@8935
    15
static int numareas = SDL_arraysize(drag_areas);
icculus@8935
    16
slouken@11272
    17
static SDL_HitTestResult SDLCALL
icculus@8935
    18
hitTest(SDL_Window *window, const SDL_Point *pt, void *data)
icculus@8935
    19
{
icculus@8935
    20
    int i;
ionut@8946
    21
    int w, h;
ionut@8946
    22
icculus@8935
    23
    for (i = 0; i < numareas; i++) {
icculus@8935
    24
        if (SDL_PointInRect(pt, &areas[i])) {
icculus@8939
    25
            SDL_Log("HIT-TEST: DRAGGABLE\n");
icculus@8935
    26
            return SDL_HITTEST_DRAGGABLE;
icculus@8935
    27
        }
icculus@8935
    28
    }
icculus@8935
    29
ionut@8946
    30
    SDL_GetWindowSize(window, &w, &h);
icculus@8947
    31
icculus@8947
    32
    #define REPORT_RESIZE_HIT(name) { \
icculus@8947
    33
        SDL_Log("HIT-TEST: RESIZE_" #name "\n"); \
icculus@8947
    34
        return SDL_HITTEST_RESIZE_##name; \
icculus@8947
    35
    }
icculus@8947
    36
icculus@8947
    37
    if (pt->x < RESIZE_BORDER && pt->y < RESIZE_BORDER) {
icculus@8947
    38
        REPORT_RESIZE_HIT(TOPLEFT);
icculus@8947
    39
    } else if (pt->x > RESIZE_BORDER && pt->x < w - RESIZE_BORDER && pt->y < RESIZE_BORDER) {
icculus@8947
    40
        REPORT_RESIZE_HIT(TOP);
icculus@8947
    41
    } else if (pt->x > w - RESIZE_BORDER && pt->y < RESIZE_BORDER) {
icculus@8947
    42
        REPORT_RESIZE_HIT(TOPRIGHT);
icculus@8947
    43
    } else if (pt->x > w - RESIZE_BORDER && pt->y > RESIZE_BORDER && pt->y < h - RESIZE_BORDER) {
icculus@8947
    44
        REPORT_RESIZE_HIT(RIGHT);
icculus@8947
    45
    } else if (pt->x > w - RESIZE_BORDER && pt->y > h - RESIZE_BORDER) {
icculus@8947
    46
        REPORT_RESIZE_HIT(BOTTOMRIGHT);
icculus@8947
    47
    } else if (pt->x < w - RESIZE_BORDER && pt->x > RESIZE_BORDER && pt->y > h - RESIZE_BORDER) {
icculus@8947
    48
        REPORT_RESIZE_HIT(BOTTOM);
icculus@8947
    49
    } else if (pt->x < RESIZE_BORDER && pt->y > h - RESIZE_BORDER) {
icculus@8947
    50
        REPORT_RESIZE_HIT(BOTTOMLEFT);
icculus@8947
    51
    } else if (pt->x < RESIZE_BORDER && pt->y < h - RESIZE_BORDER && pt->y > RESIZE_BORDER) {
icculus@8947
    52
        REPORT_RESIZE_HIT(LEFT);
icculus@8947
    53
    }
ionut@8946
    54
icculus@8939
    55
    SDL_Log("HIT-TEST: NORMAL\n");
icculus@8935
    56
    return SDL_HITTEST_NORMAL;
icculus@8935
    57
}
icculus@8935
    58
icculus@8935
    59
icculus@8931
    60
int main(int argc, char **argv)
icculus@8931
    61
{
icculus@8931
    62
    int done = 0;
icculus@8931
    63
    SDL_Window *window;
icculus@8931
    64
    SDL_Renderer *renderer;
icculus@8931
    65
icculus@8931
    66
    /* !!! FIXME: check for errors. */
icculus@8931
    67
    SDL_Init(SDL_INIT_VIDEO);
ionut@8946
    68
    window = SDL_CreateWindow("Drag the red boxes", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, SDL_WINDOW_BORDERLESS | SDL_WINDOW_RESIZABLE);
icculus@8931
    69
    renderer = SDL_CreateRenderer(window, -1, 0);
icculus@8931
    70
icculus@8935
    71
    if (SDL_SetWindowHitTest(window, hitTest, NULL) == -1) {
icculus@8939
    72
        SDL_Log("Enabling hit-testing failed!\n");
icculus@8931
    73
        SDL_Quit();
icculus@8931
    74
        return 1;
icculus@8931
    75
    }
icculus@8931
    76
icculus@8931
    77
    while (!done)
icculus@8931
    78
    {
icculus@8940
    79
        SDL_Event e;
icculus@8940
    80
        int nothing_to_do = 1;
icculus@8940
    81
icculus@8931
    82
        SDL_SetRenderDrawColor(renderer, 0, 0, 127, 255);
icculus@8931
    83
        SDL_RenderClear(renderer);
icculus@8931
    84
        SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
icculus@8931
    85
        SDL_RenderFillRects(renderer, areas, SDL_arraysize(drag_areas));
icculus@8931
    86
        SDL_RenderPresent(renderer);
icculus@8931
    87
icculus@8931
    88
        while (SDL_PollEvent(&e)) {
icculus@8931
    89
            nothing_to_do = 0;
icculus@8931
    90
icculus@8931
    91
            switch (e.type)
icculus@8931
    92
            {
icculus@8931
    93
                case SDL_MOUSEBUTTONDOWN:
icculus@8939
    94
                    SDL_Log("button down!\n");
icculus@8931
    95
                    break;
icculus@8931
    96
icculus@8931
    97
                case SDL_MOUSEBUTTONUP:
icculus@8939
    98
                    SDL_Log("button up!\n");
icculus@8931
    99
                    break;
icculus@8931
   100
icculus@8931
   101
                case SDL_WINDOWEVENT:
icculus@8931
   102
                    if (e.window.event == SDL_WINDOWEVENT_MOVED) {
icculus@8939
   103
                        SDL_Log("Window event moved to (%d, %d)!\n", (int) e.window.data1, (int) e.window.data2);
icculus@8931
   104
                    }
icculus@8931
   105
                    break;
icculus@8931
   106
icculus@8931
   107
                case SDL_KEYDOWN:
icculus@8931
   108
                    if (e.key.keysym.sym == SDLK_ESCAPE) {
icculus@8931
   109
                        done = 1;
icculus@8931
   110
                    } else if (e.key.keysym.sym == SDLK_x) {
icculus@8931
   111
                        if (!areas) {
icculus@8931
   112
                            areas = drag_areas;
icculus@8931
   113
                            numareas = SDL_arraysize(drag_areas);
icculus@8931
   114
                        } else {
icculus@8931
   115
                            areas = NULL;
icculus@8931
   116
                            numareas = 0;
icculus@8931
   117
                        }
icculus@8931
   118
                    }
icculus@8931
   119
                    break;
icculus@8931
   120
icculus@8931
   121
                case SDL_QUIT:
icculus@8931
   122
                    done = 1;
icculus@8931
   123
                    break;
icculus@8931
   124
            }
icculus@8931
   125
        }
icculus@8931
   126
icculus@8931
   127
        if (nothing_to_do) {
icculus@8931
   128
            SDL_Delay(50);
icculus@8931
   129
        }
icculus@8931
   130
    }
icculus@8931
   131
icculus@8931
   132
    SDL_Quit();
icculus@8931
   133
    return 0;
icculus@8931
   134
}