1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
//! Operations for SDL's rectangle type.

use crate::{c_float, c_int, stdinc::*};

/// The structure that defines a point (integer)
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(C)]
#[allow(missing_docs)]
pub struct SDL_Point {
  pub x: c_int,
  pub y: c_int,
}

/// The structure that defines a point (floating)
#[derive(Debug, Clone, Copy, Default, PartialEq, PartialOrd)]
#[repr(C)]
#[allow(missing_docs)]
pub struct SDL_FPoint {
  pub x: c_float,
  pub y: c_float,
}

/// A rectangle, with the origin at the upper left (integer).
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(C)]
#[allow(missing_docs)]
pub struct SDL_Rect {
  pub x: c_int,
  pub y: c_int,
  pub w: c_int,
  pub h: c_int,
}

/// A rectangle, with the origin at the upper left (floating).
#[derive(Debug, Clone, Copy, Default, PartialEq, PartialOrd)]
#[repr(C)]
#[allow(missing_docs)]
pub struct SDL_FRect {
  pub x: f32,
  pub y: f32,
  pub w: f32,
  pub h: f32,
}

/// Returns `true` if a point resides inside a rectangle.
#[inline]
#[must_use]
pub const fn SDL_PointInRect(p: SDL_Point, r: SDL_Rect) -> bool {
  (p.x >= r.x) && (p.x < (r.x + r.w)) && (p.y >= r.y) && (p.y < (r.y + r.h))
}

/// Returns `true` if the rectangle has no area.
#[inline]
#[must_use]
pub const fn SDL_RectEmpty(r: SDL_Rect) -> bool {
  (r.w <= 0) || (r.h <= 0)
}

extern "C" {
  /// Determine whether two rectangles intersect.
  ///
  /// **Return:** `SDL_TRUE` if there is an intersection, `SDL_FALSE` otherwise.
  pub fn SDL_HasIntersection(
    A: *const SDL_Rect, B: *const SDL_Rect,
  ) -> SDL_bool;

  /// Calculate the intersection of two rectangles.
  ///
  /// **Return:** `SDL_TRUE` if there is an intersection, `SDL_FALSE` otherwise.
  pub fn SDL_IntersectRect(
    A: *const SDL_Rect, B: *const SDL_Rect, result: *mut SDL_Rect,
  ) -> SDL_bool;

  /// Calculate the union of two rectangles.
  pub fn SDL_UnionRect(
    A: *const SDL_Rect, B: *const SDL_Rect, result: *mut SDL_Rect,
  );

  /// Calculate a minimal rectangle enclosing a set of points.
  ///
  /// **Return:** `SDL_TRUE` if any points were within the clipping rect
  pub fn SDL_EnclosePoints(
    points: *const SDL_Point, count: c_int, clip: *const SDL_Rect,
    result: *mut SDL_Rect,
  ) -> SDL_bool;

  /// Calculate the intersection of a rectangle and line segment.
  ///
  /// **Return:** `SDL_TRUE` if there is an intersection, `SDL_FALSE` otherwise.
  pub fn SDL_IntersectRectAndLine(
    rect: *const SDL_Rect, X1: *mut c_int, Y1: *mut c_int, X2: *mut c_int,
    Y2: *mut c_int,
  ) -> SDL_bool;
}