1 
2 //          Copyright 2018 - 2021 Michael D. Parker
3 // Distributed under the Boost Software License, Version 1.0.
4 //    (See accompanying file LICENSE_1_0.txt or copy at
5 //          http://www.boost.org/LICENSE_1_0.txt)
6 
7 module bindbc.sdl.bind.sdlrect;
8 
9 import bindbc.sdl.config;
10 import bindbc.sdl.bind.sdlstdinc : SDL_bool;
11 
12 struct SDL_Point {
13     int x;
14     int y;
15 }
16 
17 struct SDL_Rect {
18     int x, y;
19     int w, h;
20 }
21 
22 static if(sdlSupport >= SDLSupport.sdl2010) {
23     struct SDL_FPoint {
24         float x, y;
25     }
26 
27     struct SDL_FRect {
28         float x, y;
29         float w, h;
30     }
31 }
32 
33 @nogc nothrow pure {
34     // This macro was added to SDL_rect.h in 2.0.4, but hurts nothing to implement for
35     // all versions.
36     bool SDL_PointInRect(const SDL_Point *p, const SDL_Rect *r) {
37         pragma(inline, true);
38         return ((p.x >= r.x) && (p.x < (r.x + r.w)) &&
39                 (p.y >= r.y) && (p.y < (r.y + r.h)));
40     }
41 
42     bool SDL_RectEmpty(const(SDL_Rect)* X) {
43         pragma(inline, true);
44         return !X || (X.w <= 0) || (X.h <= 0);
45     }
46 
47     bool SDL_RectEquals(const(SDL_Rect)* A, const(SDL_Rect)* B) {
48         pragma(inline, true);
49         return A && B &&
50             (A.x == B.x) && (A.y == B.y) &&
51             (A.w == B.w) && (A.h == B.h);
52     }
53 }
54 
55 static if(staticBinding) {
56     extern(C) @nogc nothrow {
57         SDL_bool SDL_HasIntersection(const(SDL_Rect)* A, const(SDL_Rect)* B);
58         SDL_bool SDL_IntersectRect(const(SDL_Rect)* A, const(SDL_Rect)* B,SDL_Rect* result);
59         void SDL_UnionRect(const(SDL_Rect)* A, const(SDL_Rect)* B, SDL_Rect* result);
60         SDL_bool SDL_EnclosePoints(const(SDL_Point)* points, int count, const(SDL_Rect)* clip, SDL_Rect* result);
61         SDL_bool SDL_IntersectRectAndLine(const(SDL_Rect)* rect, int* X1, int* Y1, int* X2, int* Y2);
62     }
63 }
64 else {
65     extern(C) @nogc nothrow {
66         alias pSDL_HasIntersection = SDL_bool function(const(SDL_Rect)* A, const(SDL_Rect)* B);
67         alias pSDL_IntersectRect = SDL_bool function(const(SDL_Rect)* A, const(SDL_Rect)* B, SDL_Rect* result);
68         alias pSDL_UnionRect = void function(const(SDL_Rect)* A, const(SDL_Rect)* B,SDL_Rect* result);
69         alias pSDL_EnclosePoints = SDL_bool function(const(SDL_Point)* points, int count, const(SDL_Rect)* clip, SDL_Rect* result);
70         alias pSDL_IntersectRectAndLine = SDL_bool function(const(SDL_Rect)* rect, int* X1, int* Y1, int* X2, int* Y2);
71     }
72 
73     __gshared {
74         pSDL_HasIntersection SDL_HasIntersection;
75         pSDL_IntersectRect SDL_IntersectRect;
76         pSDL_UnionRect SDL_UnionRect;
77         pSDL_EnclosePoints SDL_EnclosePoints;
78         pSDL_IntersectRectAndLine SDL_IntersectRectAndLine;
79     }
80 }