fermium/
rect.rs

1//! Operations for SDL's rectangle type.
2
3use crate::{c_float, c_int, stdinc::*};
4
5/// The structure that defines a point (integer)
6#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
7#[repr(C)]
8#[allow(missing_docs)]
9pub struct SDL_Point {
10  pub x: c_int,
11  pub y: c_int,
12}
13
14/// The structure that defines a point (floating)
15#[derive(Debug, Clone, Copy, Default, PartialEq, PartialOrd)]
16#[repr(C)]
17#[allow(missing_docs)]
18pub struct SDL_FPoint {
19  pub x: c_float,
20  pub y: c_float,
21}
22
23/// A rectangle, with the origin at the upper left (integer).
24#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
25#[repr(C)]
26#[allow(missing_docs)]
27pub struct SDL_Rect {
28  pub x: c_int,
29  pub y: c_int,
30  pub w: c_int,
31  pub h: c_int,
32}
33
34/// A rectangle, with the origin at the upper left (floating).
35#[derive(Debug, Clone, Copy, Default, PartialEq, PartialOrd)]
36#[repr(C)]
37#[allow(missing_docs)]
38pub struct SDL_FRect {
39  pub x: f32,
40  pub y: f32,
41  pub w: f32,
42  pub h: f32,
43}
44
45/// Returns `true` if a point resides inside a rectangle.
46#[inline]
47#[must_use]
48pub const fn SDL_PointInRect(p: SDL_Point, r: SDL_Rect) -> bool {
49  (p.x >= r.x) && (p.x < (r.x + r.w)) && (p.y >= r.y) && (p.y < (r.y + r.h))
50}
51
52/// Returns `true` if the rectangle has no area.
53#[inline]
54#[must_use]
55pub const fn SDL_RectEmpty(r: SDL_Rect) -> bool {
56  (r.w <= 0) || (r.h <= 0)
57}
58
59extern "C" {
60  /// Determine whether two rectangles intersect.
61  ///
62  /// **Return:** `SDL_TRUE` if there is an intersection, `SDL_FALSE` otherwise.
63  pub fn SDL_HasIntersection(
64    A: *const SDL_Rect, B: *const SDL_Rect,
65  ) -> SDL_bool;
66
67  /// Calculate the intersection of two rectangles.
68  ///
69  /// **Return:** `SDL_TRUE` if there is an intersection, `SDL_FALSE` otherwise.
70  pub fn SDL_IntersectRect(
71    A: *const SDL_Rect, B: *const SDL_Rect, result: *mut SDL_Rect,
72  ) -> SDL_bool;
73
74  /// Calculate the union of two rectangles.
75  pub fn SDL_UnionRect(
76    A: *const SDL_Rect, B: *const SDL_Rect, result: *mut SDL_Rect,
77  );
78
79  /// Calculate a minimal rectangle enclosing a set of points.
80  ///
81  /// **Return:** `SDL_TRUE` if any points were within the clipping rect
82  pub fn SDL_EnclosePoints(
83    points: *const SDL_Point, count: c_int, clip: *const SDL_Rect,
84    result: *mut SDL_Rect,
85  ) -> SDL_bool;
86
87  /// Calculate the intersection of a rectangle and line segment.
88  ///
89  /// **Return:** `SDL_TRUE` if there is an intersection, `SDL_FALSE` otherwise.
90  pub fn SDL_IntersectRectAndLine(
91    rect: *const SDL_Rect, X1: *mut c_int, Y1: *mut c_int, X2: *mut c_int,
92    Y2: *mut c_int,
93  ) -> SDL_bool;
94}