summaryrefslogtreecommitdiff
path: root/src/contrib/SDL-3.2.20/test/testhittesting.c
diff options
context:
space:
mode:
author3gg <3gg@shellblade.net>2025-08-30 16:53:58 -0700
committer3gg <3gg@shellblade.net>2025-08-30 16:53:58 -0700
commit6aaedb813fa11ba0679c3051bc2eb28646b9506c (patch)
tree34acbfc9840e02cb4753e6306ea7ce978bf8b58e /src/contrib/SDL-3.2.20/test/testhittesting.c
parent8f228ade99dd3d4c8da9b78ade1815c9adf85c8f (diff)
Update to SDL3
Diffstat (limited to 'src/contrib/SDL-3.2.20/test/testhittesting.c')
-rw-r--r--src/contrib/SDL-3.2.20/test/testhittesting.c173
1 files changed, 173 insertions, 0 deletions
diff --git a/src/contrib/SDL-3.2.20/test/testhittesting.c b/src/contrib/SDL-3.2.20/test/testhittesting.c
new file mode 100644
index 0000000..c276d42
--- /dev/null
+++ b/src/contrib/SDL-3.2.20/test/testhittesting.c
@@ -0,0 +1,173 @@
1/*
2 Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org>
3
4 This software is provided 'as-is', without any express or implied
5 warranty. In no event will the authors be held liable for any damages
6 arising from the use of this software.
7
8 Permission is granted to anyone to use this software for any purpose,
9 including commercial applications, and to alter it and redistribute it
10 freely.
11*/
12#include <SDL3/SDL.h>
13#include <SDL3/SDL_main.h>
14#include <SDL3/SDL_test.h>
15
16#define RESIZE_BORDER 20
17
18static const SDL_Rect drag_areas[] = {
19 { 20, 20, 100, 100 },
20 { 200, 70, 100, 100 },
21 { 400, 90, 100, 100 }
22};
23static 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 }
27};
28
29static const SDL_Rect *areas = drag_areas;
30static int numareas = SDL_arraysize(drag_areas);
31
32static SDL_HitTestResult SDLCALL
33hitTest(SDL_Window *window, const SDL_Point *pt, void *data)
34{
35 int i;
36 int w, h, p_w;
37 SDL_Point adj_pt;
38 float scale;
39
40 SDL_GetWindowSize(window, &w, &h);
41 SDL_GetWindowSizeInPixels(window, &p_w, NULL);
42
43 scale = (float)p_w / (float)w;
44
45 adj_pt.x = (int)SDL_floorf(pt->x * scale);
46 adj_pt.y = (int)SDL_floorf(pt->y * scale);
47
48 for (i = 0; i < numareas; i++) {
49 if (SDL_PointInRect(&adj_pt, &areas[i])) {
50 SDL_Log("HIT-TEST: DRAGGABLE");
51 return SDL_HITTEST_DRAGGABLE;
52 }
53 }
54
55#define REPORT_RESIZE_HIT(name) \
56 { \
57 SDL_Log("HIT-TEST: RESIZE_" #name ""); \
58 return SDL_HITTEST_RESIZE_##name; \
59 }
60
61 if (pt->x < RESIZE_BORDER && pt->y < RESIZE_BORDER) {
62 REPORT_RESIZE_HIT(TOPLEFT);
63 } else if (pt->x > RESIZE_BORDER && pt->x < w - RESIZE_BORDER && pt->y < RESIZE_BORDER) {
64 REPORT_RESIZE_HIT(TOP);
65 } else if (pt->x > w - RESIZE_BORDER && pt->y < RESIZE_BORDER) {
66 REPORT_RESIZE_HIT(TOPRIGHT);
67 } else if (pt->x > w - RESIZE_BORDER && pt->y > RESIZE_BORDER && pt->y < h - RESIZE_BORDER) {
68 REPORT_RESIZE_HIT(RIGHT);
69 } else if (pt->x > w - RESIZE_BORDER && pt->y > h - RESIZE_BORDER) {
70 REPORT_RESIZE_HIT(BOTTOMRIGHT);
71 } else if (pt->x < w - RESIZE_BORDER && pt->x > RESIZE_BORDER && pt->y > h - RESIZE_BORDER) {
72 REPORT_RESIZE_HIT(BOTTOM);
73 } else if (pt->x < RESIZE_BORDER && pt->y > h - RESIZE_BORDER) {
74 REPORT_RESIZE_HIT(BOTTOMLEFT);
75 } else if (pt->x < RESIZE_BORDER && pt->y < h - RESIZE_BORDER && pt->y > RESIZE_BORDER) {
76 REPORT_RESIZE_HIT(LEFT);
77 }
78
79 SDL_Log("HIT-TEST: NORMAL");
80 return SDL_HITTEST_NORMAL;
81}
82
83int main(int argc, char **argv)
84{
85 int i;
86 int done = 0;
87 SDLTest_CommonState *state;
88
89 /* Initialize test framework */
90 state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
91 if (!state) {
92 return 1;
93 }
94
95 state->window_flags = SDL_WINDOW_BORDERLESS | SDL_WINDOW_RESIZABLE;
96
97 /* Parse commandline */
98 if (!SDLTest_CommonDefaultArgs(state, argc, argv)) {
99 return 1;
100 }
101
102 if (!SDLTest_CommonInit(state)) {
103 return 2;
104 }
105
106 for (i = 0; i < state->num_windows; i++) {
107 if (!SDL_SetWindowHitTest(state->windows[i], hitTest, NULL)) {
108 SDL_Log("Enabling hit-testing failed for window %d: %s", i, SDL_GetError());
109 SDL_Quit();
110 return 1;
111 }
112 }
113
114 while (!done) {
115 SDL_Event e;
116 int nothing_to_do = 1;
117
118 for (i = 0; i < state->num_windows; ++i) {
119 SDL_SetRenderDrawColor(state->renderers[i], 0, 0, 127, 255);
120 SDL_RenderClear(state->renderers[i]);
121 SDL_SetRenderDrawColor(state->renderers[i], 255, 0, 0, 255);
122 SDLTest_DrawString(state->renderers[i], (float)state->window_w / 2 - 80.0f, 10.0f, "Drag the red boxes");
123 SDL_RenderFillRects(state->renderers[i], render_areas, SDL_arraysize(render_areas));
124 SDL_RenderPresent(state->renderers[i]);
125 }
126
127 while (SDL_PollEvent(&e)) {
128 SDLTest_CommonEvent(state, &e, &done);
129 nothing_to_do = 0;
130
131 switch (e.type) {
132 case SDL_EVENT_MOUSE_BUTTON_DOWN:
133 SDL_Log("button down!");
134 break;
135
136 case SDL_EVENT_MOUSE_BUTTON_UP:
137 SDL_Log("button up!");
138 break;
139
140 case SDL_EVENT_WINDOW_MOVED:
141 SDL_Log("Window event moved to (%d, %d)!", (int)e.window.data1, (int)e.window.data2);
142 break;
143
144 case SDL_EVENT_KEY_DOWN:
145 if (e.key.key == SDLK_ESCAPE) {
146 done = 1;
147 } else if (e.key.key == SDLK_X) {
148 if (!areas) {
149 areas = drag_areas;
150 numareas = SDL_arraysize(drag_areas);
151 } else {
152 areas = NULL;
153 numareas = 0;
154 }
155 }
156 break;
157
158 case SDL_EVENT_QUIT:
159 done = 1;
160 break;
161 default:
162 break;
163 }
164 }
165
166 if (nothing_to_do) {
167 SDL_Delay(50);
168 }
169 }
170
171 SDLTest_CommonQuit(state);
172 return 0;
173}