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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
use crate::bind;
mod rect;
pub use rect::*;
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
#[must_use]
pub struct Point {
pub x: i32,
pub y: i32,
}
impl From<bind::SDL_Point> for Point {
fn from(bind::SDL_Point { x, y }: bind::SDL_Point) -> Self {
Self {
x: x as i32,
y: y as i32,
}
}
}
impl From<Point> for bind::SDL_Point {
fn from(Point { x, y }: Point) -> Self {
use std::os::raw::c_int;
Self {
x: x as c_int,
y: y as c_int,
}
}
}
impl Point {
pub fn offset(self, x: i32, y: i32) -> Self {
Self {
x: self.x + x,
y: self.y + y,
}
}
#[must_use]
pub fn is_in(&self, rect: Rect) -> bool {
let bottom_right = rect.bottom_right();
rect.up_left.x <= self.x
&& self.x <= bottom_right.x
&& rect.up_left.y <= self.y
&& self.y <= bottom_right.y
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
#[must_use]
pub struct Size {
pub width: u32,
pub height: u32,
}
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd)]
#[must_use]
pub struct Scale {
pub horizontal: f32,
pub vertical: f32,
}
impl Default for Scale {
fn default() -> Self {
Self {
horizontal: 1.0,
vertical: 1.0,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
#[must_use]
pub struct Line {
pub start: Point,
pub end: Point,
}
impl Line {
pub fn clip_with(mut self, rect: Rect) -> Self {
unsafe {
bind::SDL_IntersectRectAndLine(
&(rect.into()),
&mut self.start.x,
&mut self.start.y,
&mut self.end.x,
&mut self.end.y,
);
}
self
}
}