13
#include <SDL3/SDL_main.h>
14
#include <SDL3/SDL_test.h>
16
#define RESIZE_BORDER 20
18
static const SDL_Rect drag_areas[] = {
20
{ 200, 70, 100, 100 },
23
static const SDL_FRect render_areas[] = {
24
{ 20.0f, 20.0f, 100.0f, 100.0f },
25
{ 200.0f, 70.0f, 100.0f, 100.0f },
26
{ 400.0f, 90.0f, 100.0f, 100.0f }
29
static const SDL_Rect *areas = drag_areas;
30
static int numareas = SDL_arraysize(drag_areas);
32
static SDL_HitTestResult SDLCALL
33
hitTest(SDL_Window *window, const SDL_Point *pt, void *data)
38
for (i = 0; i < numareas; i++) {
39
if (SDL_PointInRect(pt, &areas[i])) {
40
SDL_Log("HIT-TEST: DRAGGABLE\n");
41
return SDL_HITTEST_DRAGGABLE;
45
SDL_GetWindowSize(window, &w, &h);
47
#define REPORT_RESIZE_HIT(name) \
49
SDL_Log("HIT-TEST: RESIZE_" #name "\n"); \
50
return SDL_HITTEST_RESIZE_##name; \
53
if (pt->x < RESIZE_BORDER && pt->y < RESIZE_BORDER) {
54
REPORT_RESIZE_HIT(TOPLEFT);
55
} else if (pt->x > RESIZE_BORDER && pt->x < w - RESIZE_BORDER && pt->y < RESIZE_BORDER) {
56
REPORT_RESIZE_HIT(TOP);
57
} else if (pt->x > w - RESIZE_BORDER && pt->y < RESIZE_BORDER) {
58
REPORT_RESIZE_HIT(TOPRIGHT);
59
} else if (pt->x > w - RESIZE_BORDER && pt->y > RESIZE_BORDER && pt->y < h - RESIZE_BORDER) {
60
REPORT_RESIZE_HIT(RIGHT);
61
} else if (pt->x > w - RESIZE_BORDER && pt->y > h - RESIZE_BORDER) {
62
REPORT_RESIZE_HIT(BOTTOMRIGHT);
63
} else if (pt->x < w - RESIZE_BORDER && pt->x > RESIZE_BORDER && pt->y > h - RESIZE_BORDER) {
64
REPORT_RESIZE_HIT(BOTTOM);
65
} else if (pt->x < RESIZE_BORDER && pt->y > h - RESIZE_BORDER) {
66
REPORT_RESIZE_HIT(BOTTOMLEFT);
67
} else if (pt->x < RESIZE_BORDER && pt->y < h - RESIZE_BORDER && pt->y > RESIZE_BORDER) {
68
REPORT_RESIZE_HIT(LEFT);
71
SDL_Log("HIT-TEST: NORMAL\n");
72
return SDL_HITTEST_NORMAL;
75
int main(int argc, char **argv)
79
SDLTest_CommonState *state;
82
state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
87
state->window_flags = SDL_WINDOW_BORDERLESS | SDL_WINDOW_RESIZABLE;
90
SDL_SetLogPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
93
if (!SDLTest_CommonDefaultArgs(state, argc, argv)) {
97
if (!SDLTest_CommonInit(state)) {
101
for (i = 0; i < state->num_windows; i++) {
102
if (SDL_SetWindowHitTest(state->windows[i], hitTest, NULL) < 0) {
103
SDL_Log("Enabling hit-testing failed for window %d: %s", i, SDL_GetError());
111
int nothing_to_do = 1;
113
for (i = 0; i < state->num_windows; ++i) {
114
SDL_SetRenderDrawColor(state->renderers[i], 0, 0, 127, 255);
115
SDL_RenderClear(state->renderers[i]);
116
SDL_SetRenderDrawColor(state->renderers[i], 255, 0, 0, 255);
117
SDLTest_DrawString(state->renderers[i], (float)state->window_w / 2 - 80.0f, 10.0f, "Drag the red boxes");
118
SDL_RenderFillRects(state->renderers[i], render_areas, SDL_arraysize(render_areas));
119
SDL_RenderPresent(state->renderers[i]);
122
while (SDL_PollEvent(&e)) {
123
SDLTest_CommonEvent(state, &e, &done);
127
case SDL_EVENT_MOUSE_BUTTON_DOWN:
128
SDL_Log("button down!\n");
131
case SDL_EVENT_MOUSE_BUTTON_UP:
132
SDL_Log("button up!\n");
135
case SDL_EVENT_WINDOW_MOVED:
136
SDL_Log("Window event moved to (%d, %d)!\n", (int)e.window.data1, (int)e.window.data2);
139
case SDL_EVENT_KEY_DOWN:
140
if (e.key.key == SDLK_ESCAPE) {
142
} else if (e.key.key == SDLK_X) {
145
numareas = SDL_arraysize(drag_areas);
166
SDLTest_CommonQuit(state);