1 
2 //          Copyright Michael D. Parker 2018.
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.bind.sdlstdinc : SDL_bool;
10 
11 struct SDL_Point {
12     int x;
13     int y;
14 }
15 
16 struct SDL_Rect {
17     int x, y;
18     int w, h;
19 }
20 
21 @nogc nothrow pure {
22     // This macro was added to SDL_rect.h in 2.0.4, but hurts nothing to implement for
23     // all versions.
24     bool SDL_PointInRect(const SDL_Point *p, const SDL_Rect *r) {
25         pragma(inline, true);
26         return ((p.x >= r.x) && (p.x < (r.x + r.w)) &&
27                 (p.y >= r.y) && (p.y < (r.y + r.h)));
28     }
29 
30     bool SDL_RectEmpty(const(SDL_Rect)* X) {
31         pragma(inline, true);
32         return !X || (X.w <= 0) || (X.h <= 0);
33     }
34 
35     bool SDL_RectEquals(const(SDL_Rect)* A, const(SDL_Rect)* B) {
36         pragma(inline, true);
37         return A && B &&
38             (A.x == B.x) && (A.y == B.y) &&
39             (A.w == B.w) && (A.h == B.h);
40     }
41 }
42 
43 version(BindSDL_Static) {
44     extern(C) @nogc nothrow {
45         SDL_bool SDL_HasIntersection(const(SDL_Rect)*,const(SDL_Rect)*);
46         SDL_bool SDL_IntersectRect(const(SDL_Rect)*,const(SDL_Rect)*,SDL_Rect*);
47         void SDL_UnionRect(const(SDL_Rect)*,const(SDL_Rect)*,SDL_Rect*);
48         SDL_bool SDL_EnclosePoints(const(SDL_Point)*,int,const(SDL_Rect)*,SDL_Rect*);
49         SDL_bool SDL_IntersectRectAndLine(const(SDL_Rect)*,int*,int*,int*,int*);
50     }
51 }
52 else {
53     extern(C) @nogc nothrow {
54         alias pSDL_HasIntersection = SDL_bool function(const(SDL_Rect)*,const(SDL_Rect)*);
55         alias pSDL_IntersectRect = SDL_bool function(const(SDL_Rect)*,const(SDL_Rect)*,SDL_Rect*);
56         alias pSDL_UnionRect = void function(const(SDL_Rect)*,const(SDL_Rect)*,SDL_Rect*);
57         alias pSDL_EnclosePoints = SDL_bool function(const(SDL_Point)*,int,const(SDL_Rect)*,SDL_Rect*);
58         alias pSDL_IntersectRectAndLine = SDL_bool function(const(SDL_Rect)*,int*,int*,int*,int*);
59     }
60 
61     __gshared {
62         pSDL_HasIntersection SDL_HasIntersection;
63         pSDL_IntersectRect SDL_IntersectRect;
64         pSDL_UnionRect SDL_UnionRect;
65         pSDL_EnclosePoints SDL_EnclosePoints;
66         pSDL_IntersectRectAndLine SDL_IntersectRectAndLine;
67     }
68 }