testhittesting.c File Reference
`#include <stdio.h>`
`#include "SDL.h"`
Include dependency graph for testhittesting.c:

Go to the source code of this file.

## Macros

#define | RESIZE_BORDER 20
#define | REPORT_RESIZE_HIT(name)

## Functions

static SDL_HitTestResult | hitTest (SDL_Window *window, const SDL_Point *pt, void *data)
int | main (int argc, char **argv)

## Variables

const SDL_Rect | drag_areas []
static const SDL_Rect * | areas = drag_areas
static int | numareas = SDL_arraysize(drag_areas)

## Macro Definition Documentation

## REPORT_RESIZE_HIT

#define REPORT_RESIZE_HIT | ( |  | name | ) |

Value:
{ \
SDL_Log("HIT-TEST: RESIZE_" #name "\n"); \
return SDL_HITTEST_RESIZE_##name; \
}
name
GLuint const GLchar * name
Definition: SDL_opengl_glext.h:660

Referenced by hitTest().

## RESIZE_BORDER

#define RESIZE_BORDER 20

Definition at line 6 of file testhittesting.c.

Referenced by hitTest(), and SDLTest_ExampleHitTestCallback().

## Function Documentation

## hitTest()

static SDL_HitTestResult hitTest | ( | SDL_Window * | window,
|  | const SDL_Point * | pt,
|  | void * | data
| ) |  |

static

Definition at line 18 of file testhittesting.c.

References i, numareas, REPORT_RESIZE_HIT, RESIZE_BORDER, SDL_GetWindowSize, SDL_HITTEST_DRAGGABLE, SDL_HITTEST_NORMAL, SDL_Log, SDL_PointInRect(), SDL_Point::x, and SDL_Point::y.

Referenced by main().

{
int i;
int w, h;
for (i = 0; i < numareas; i++) {
if (SDL_PointInRect(pt, &areas[i])) {
SDL_Log("HIT-TEST: DRAGGABLE\n");
return SDL_HITTEST_DRAGGABLE;
}
}
SDL_GetWindowSize(window, &w, &h);
#define REPORT_RESIZE_HIT(name) { \
SDL_Log("HIT-TEST: RESIZE_" #name "\n"); \
return SDL_HITTEST_RESIZE_##name; \
}
if (pt->x < RESIZE_BORDER && pt->y < RESIZE_BORDER) {
REPORT_RESIZE_HIT(TOPLEFT);
} else if (pt->x > RESIZE_BORDER && pt->x < w - RESIZE_BORDER && pt->y < RESIZE_BORDER) {
REPORT_RESIZE_HIT(TOP);
} else if (pt->x > w - RESIZE_BORDER && pt->y < RESIZE_BORDER) {
REPORT_RESIZE_HIT(TOPRIGHT);
} else if (pt->x > w - RESIZE_BORDER && pt->y > RESIZE_BORDER && pt->y < h - RESIZE_BORDER) {
REPORT_RESIZE_HIT(RIGHT);
} else if (pt->x > w - RESIZE_BORDER && pt->y > h - RESIZE_BORDER) {
REPORT_RESIZE_HIT(BOTTOMRIGHT);
} else if (pt->x < w - RESIZE_BORDER && pt->x > RESIZE_BORDER && pt->y > h - RESIZE_BORDER) {
REPORT_RESIZE_HIT(BOTTOM);
} else if (pt->x < RESIZE_BORDER && pt->y > h - RESIZE_BORDER) {
REPORT_RESIZE_HIT(BOTTOMLEFT);
} else if (pt->x < RESIZE_BORDER && pt->y < h - RESIZE_BORDER && pt->y > RESIZE_BORDER) {
REPORT_RESIZE_HIT(LEFT);
}
SDL_Log("HIT-TEST: NORMAL\n");
return SDL_HITTEST_NORMAL;
}
SDL_HITTEST_NORMAL
Definition: SDL_video.h:995
numareas
static int numareas
Definition: testhittesting.c:15
h
GLfloat GLfloat GLfloat GLfloat h
Definition: SDL_opengl_glext.h:1946
SDL_PointInRect
SDL_FORCE_INLINE SDL_bool SDL_PointInRect(const SDL_Point *p, const SDL_Rect *r)
Returns true if point resides inside a rectangle.
Definition: SDL_rect.h:73
SDL_Point::x
int x
Definition: SDL_rect.h:50
SDL_GetWindowSize
#define SDL_GetWindowSize
Definition: SDL_dynapi_overrides.h:527
SDL_Log
#define SDL_Log
Definition: SDL_dynapi_overrides.h:31
SDL_Point::y
int y
Definition: SDL_rect.h:51
w
GLubyte GLubyte GLubyte GLubyte w
Definition: SDL_opengl_glext.h:731
SDL_HITTEST_DRAGGABLE
Definition: SDL_video.h:996
i
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int in i)
Definition: SDL_x11sym.h:50
RESIZE_BORDER
#define RESIZE_BORDER
Definition: testhittesting.c:6
REPORT_RESIZE_HIT
#define REPORT_RESIZE_HIT(name)
areas
static const SDL_Rect * areas
Definition: testhittesting.c:14

## main()

int main | ( | int | argc,
|  | char ** | argv
| ) |  |

Definition at line 60 of file testhittesting.c.

References SDL_WindowEvent::data1, SDL_WindowEvent::data2, done, drag_areas, e, SDL_WindowEvent::event, hitTest(), SDL_Event::key, SDL_KeyboardEvent::keysym, NULL, numareas, renderer, SDL_arraysize, SDL_CreateRenderer, SDL_CreateWindow, SDL_Delay, SDL_Init, SDL_INIT_VIDEO, SDL_KEYDOWN, SDL_Log, SDL_MOUSEBUTTONDOWN, SDL_MOUSEBUTTONUP, SDL_PollEvent, SDL_Quit, SDL_QUIT, SDL_RenderClear, SDL_RenderFillRects, SDL_RenderPresent, SDL_SetRenderDrawColor, SDL_SetWindowHitTest, SDL_WINDOW_BORDERLESS, SDL_WINDOW_RESIZABLE, SDL_WINDOWEVENT, SDL_WINDOWEVENT_MOVED, SDL_WINDOWPOS_CENTERED, SDLK_ESCAPE, SDLK_x, SDL_Keysym::sym, SDL_Event::type, and SDL_Event::window.

{
int done = 0;
SDL_Window *window;
SDL_Renderer *renderer;
/* !!! FIXME: check for errors. */
SDL_Init(SDL_INIT_VIDEO);
window = SDL_CreateWindow("Drag the red boxes", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, SDL_WINDOW_BORDERLESS | SDL_WINDOW_RESIZABLE);
renderer = SDL_CreateRenderer(window, -1, 0);
if (SDL_SetWindowHitTest(window, hitTest, NULL) == -1) {
SDL_Log("Enabling hit-testing failed!\n");
SDL_Quit();
return 1;
}
while (!done)
{
SDL_Event e;
int nothing_to_do = 1;
SDL_SetRenderDrawColor(renderer, 0, 0, 127, 255);
SDL_RenderClear(renderer);
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
SDL_RenderFillRects(renderer, areas, SDL_arraysize(drag_areas));
SDL_RenderPresent(renderer);
while (SDL_PollEvent(&e)) {
nothing_to_do = 0;
switch (e.type)
{
case SDL_MOUSEBUTTONDOWN:
SDL_Log("button down!\n");
break;
case SDL_MOUSEBUTTONUP:
SDL_Log("button up!\n");
break;
case SDL_WINDOWEVENT:
if (e.window.event == SDL_WINDOWEVENT_MOVED) {
SDL_Log("Window event moved to (%d, %d)!\n", (int) e.window.data1, (int) e.window.data2);
}
break;
case SDL_KEYDOWN:
if (e.key.keysym.sym == SDLK_ESCAPE) {
done = 1;
} else if (e.key.keysym.sym == SDLK_x) {
if (!areas) {
areas = drag_areas;
numareas = SDL_arraysize(drag_areas);
} else {
areas = NULL;
numareas = 0;
}
}
break;
case SDL_QUIT:
done = 1;
break;
}
}
if (nothing_to_do) {
SDL_Delay(50);
}
}
SDL_Quit();
return 0;
}
SDL_WINDOWPOS_CENTERED
#define SDL_WINDOWPOS_CENTERED
Definition: SDL_video.h:139
SDL_PollEvent
#define SDL_PollEvent
Definition: SDL_dynapi_overrides.h:122
SDL_KEYDOWN
Definition: SDL_events.h:93
e
SDL_PRINTF_FORMAT_STRING const char int SDL_PRINTF_FORMAT_STRING const char int SDL_PRINTF_FORMAT_STRING const char int SDL_PRINTF_FORMAT_STRING const char const char SDL_SCANF_FORMAT_STRING const char return SDL_ThreadFunction const char void return Uint32 return Uint32 SDL_AssertionHandler void SDL_SpinLock SDL_atomic_t int int return SDL_atomic_t return void void void return void return int return SDL_AudioSpec SDL_AudioSpec return int int return return int SDL_RWops int SDL_AudioSpec Uint8 Uint32 * e
Definition: SDL_dynapi_procs.h:117
SDL_MOUSEBUTTONUP
Definition: SDL_events.h:104
numareas
static int numareas
Definition: testhittesting.c:15
SDL_MOUSEBUTTONDOWN
Definition: SDL_events.h:103
SDL_QUIT
Definition: SDL_events.h:60
SDL_Renderer
Definition: SDL_sysrender.h:76
SDL_CreateWindow
#define SDL_CreateWindow
Definition: SDL_dynapi_overrides.h:514
drag_areas
const SDL_Rect drag_areas[]
Definition: testhittesting.c:8
SDL_Event::window
SDL_WindowEvent window
Definition: SDL_events.h:529
SDL_Log
#define SDL_Log
Definition: SDL_dynapi_overrides.h:31
SDL_RenderFillRects
#define SDL_RenderFillRects
Definition: SDL_dynapi_overrides.h:342
SDLK_x
Definition: SDL_keycode.h:123
renderer
static SDL_Renderer * renderer
Definition: testaudiocapture.c:21
SDL_Quit
#define SDL_Quit
Definition: SDL_dynapi_overrides.h:58
done
int done
Definition: checkkeys.c:28
SDL_WindowEvent::data2
Sint32 data2
Definition: SDL_events.h:184
SDL_WINDOWEVENT
Definition: SDL_events.h:89
SDL_WindowEvent::event
Uint8 event
Definition: SDL_events.h:179
SDL_KeyboardEvent::keysym
SDL_Keysym keysym
Definition: SDL_events.h:199
hitTest
static SDL_HitTestResult hitTest(SDL_Window *window, const SDL_Point *pt, void *data)
Definition: testhittesting.c:18
SDL_Delay
#define SDL_Delay
Definition: SDL_dynapi_overrides.h:486
NULL
#define NULL
Definition: begin_code.h:164
SDL_WINDOW_RESIZABLE
Definition: SDL_video.h:105
SDL_WINDOW_BORDERLESS
Definition: SDL_video.h:104
SDL_RenderClear
#define SDL_RenderClear
Definition: SDL_dynapi_overrides.h:334
window
EGLSurface EGLNativeWindowType * window
Definition: eglext.h:1025
SDL_Event::key
SDL_KeyboardEvent key
Definition: SDL_events.h:530
SDL_SetWindowHitTest
#define SDL_SetWindowHitTest
Definition: SDL_dynapi_overrides.h:585
SDL_Window
The type used to identify a window.
Definition: SDL_sysvideo.h:73
SDL_Keysym::sym
SDL_Keycode sym
Definition: SDL_keyboard.h:50
SDL_Init
#define SDL_Init
Definition: SDL_dynapi_overrides.h:54
SDL_arraysize
#define SDL_arraysize(array)
Definition: SDL_stdinc.h:93
SDL_Event
General event structure.
Definition: SDL_events.h:525
SDL_SetRenderDrawColor
#define SDL_SetRenderDrawColor
Definition: SDL_dynapi_overrides.h:330
areas
static const SDL_Rect * areas
Definition: testhittesting.c:14
SDLK_ESCAPE
Definition: SDL_keycode.h:55
SDL_WindowEvent::data1
Sint32 data1
Definition: SDL_events.h:183
SDL_WINDOWEVENT_MOVED
Definition: SDL_video.h:153
SDL_INIT_VIDEO
#define SDL_INIT_VIDEO
Definition: SDL.h:78
SDL_CreateRenderer
#define SDL_CreateRenderer
Definition: SDL_dynapi_overrides.h:301
SDL_RenderPresent
#define SDL_RenderPresent
Definition: SDL_dynapi_overrides.h:346
SDL_Event::type
Uint32 type
Definition: SDL_events.h:527

## Variable Documentation

## areas

const SDL_Rect* areas = drag_areas

static

Definition at line 14 of file testhittesting.c.

## drag_areas

const SDL_Rect drag_areas[]

Initial value:
= {
{ 20, 20, 100, 100 },
{ 200, 70, 100, 100 },
{ 400, 90, 100, 100 }
}

Definition at line 8 of file testhittesting.c.

Referenced by main().

## numareas

int numareas = SDL_arraysize(drag_areas)

static

Definition at line 15 of file testhittesting.c.

Referenced by hitTest(), and main().