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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
use sys::rect as ll;
use std::mem;
use std::ptr;
use std::ops::{BitAnd, BitOr};

use SdlResult;
use ErrorMessage;

/// Immutable point type, consisting of x and y.
#[derive(Copy, Clone, Eq, PartialEq, Debug, Hash)]
pub struct Point {
    raw: ll::SDL_Point
}

impl From<(i32, i32)> for Point {
    fn from((x, y): (i32, i32)) -> Point {
        Point::new(x, y)
    }
}

impl Into<(i32, i32)> for Point {
    fn into(self) -> (i32, i32) {
        self.xy()
    }
}

impl Point {
    #[inline]
    pub fn raw(&self) -> *const ll::SDL_Point { &self.raw }

    #[inline]
    pub fn raw_slice(slice: &[Point]) -> *const ll::SDL_Point {
        unsafe { mem::transmute(slice.as_ptr()) }
    }

    #[inline]
    pub fn from_ll(raw: ll::SDL_Point) -> Point {
        Point::new(raw.x, raw.y)
    }

    #[inline]
    pub fn new(x: i32, y: i32) -> Point {
        Point {
            raw: ll::SDL_Point { x: x, y: y }
        }
    }

    #[inline]
    pub fn offset(&self, x: i32, y: i32) -> Point {
        Point::new(self.x() + x, self.y() + y)
    }

    #[inline]
    pub fn xy(&self) -> (i32, i32) {
        (self.raw.x, self.raw.y)
    }

    #[inline] pub fn x(&self) -> i32 { self.raw.x }
    #[inline] pub fn y(&self) -> i32 { self.raw.y }
}

/// Immutable rectangle type, consisting of x, y, width and height.
///
/// Rectangle invariants:
///
/// * `width` and `height` are positive and non-zero.
/// * `width` and `height` are less than `1<<31` (2,147,483,648).
/// * `x + width` and `y + height` do not overflow.
///
/// These invariants exist in the wrapper because many SDL functions that accept rectangles don't
/// behave predictably if the above conditions aren't met.
#[derive(Copy, Clone, Eq, PartialEq, Debug, Hash)]
pub struct Rect {
    raw: ll::SDL_Rect
}

// We can't implement From, so it's just Into for Rect.

impl Into<(i32, i32, u32, u32)> for Rect {
    fn into(self) -> (i32, i32, u32, u32) {
        self.xywh()
    }
}

impl Rect {
    #[inline]
    pub fn raw(&self) -> *const ll::SDL_Rect { &self.raw }

    #[inline]
    pub fn raw_from_option(v: Option<&Rect>) -> *const ll::SDL_Rect {
        match v {
            Some(ref r) => r.raw(),
            None => ptr::null()
        }
    }

    #[inline]
    pub fn raw_mut_from_option(v: Option<&mut Rect>) -> *mut ll::SDL_Rect {
        match v {
            Some(ref r) => r.raw() as *mut _,
            None => ptr::null_mut()
        }
    }

    #[inline]
    pub fn raw_slice(slice: &[Rect]) -> *const ll::SDL_Rect {
        unsafe { mem::transmute(slice.as_ptr()) }
    }

    #[inline]
    pub fn from_ll(raw: ll::SDL_Rect) -> SdlResult<Option<Rect>> {
        if raw.w > 0 && raw.h > 0 {
            Rect::new(raw.x, raw.y, raw.w as u32, raw.h as u32)
        } else {
            Ok(None)
        }
    }

    /// Creates a new rectangle.
    ///
    /// If `width` or `height` is zero, `Ok(None)` is returned.
    /// If the arguments violate any of the other rectangle invariants, an error is returned.
    pub fn new(x: i32, y: i32, width: u32, height: u32) -> SdlResult<Option<Rect>> {
        let width = try!(u32_to_int!(width));
        let height = try!(u32_to_int!(height));

        if x.checked_add(width).is_none() {
            Err(ErrorMessage("`x` + `width` overflows.".into()))
        } else if y.checked_add(height).is_none() {
            Err(ErrorMessage("`y` + `height` overflows.".into()))
        } else {
            match (width, height) {
                (0, _) | (_, 0) => Ok(None),
                (w, h) => Ok(Some(Rect {
                    raw: ll::SDL_Rect {
                        x: x,
                        y: y,
                        w: w,
                        h: h
                    }
                }))
            }
        }
    }

    /// Creates a new rectangle. Convenience function that unwraps the result of `Rect::new()` twice.
    ///
    /// Make sure to only use this function if you're certain the rectangle meets all the invariants.
    #[inline]
    pub fn new_unwrap(x: i32, y: i32, width: u32, height: u32) -> Rect {
        Rect::new(x, y, width, height).unwrap().expect("Rectangle is empty (width or height was 0).")
    }

    /// Offsets the rectangle's x and y coordinates.
    ///
    /// If the new rectangle violates any invariants, an error is returned.
    #[inline]
    pub fn offset(&self, x: i32, y: i32) -> SdlResult<Rect> {
        let rect = try!(Rect::new(self.x() + x, self.y() + y, self.width(), self.height()));
        Ok(rect.unwrap())
    }

    #[inline]
    pub fn xywh(&self) -> (i32, i32, u32, u32) {
        (self.raw.x, self.raw.y, self.raw.w as u32, self.raw.h as u32)
    }

    #[inline] pub fn x(&self) -> i32 { self.raw.x }
    #[inline] pub fn y(&self) -> i32 { self.raw.y }
    #[inline] pub fn width(&self) -> u32 { self.raw.w as u32 }
    #[inline] pub fn height(&self) -> u32 { self.raw.h as u32 }

    /// Calculate a minimal rectangle enclosing a set of points.
    ///
    /// Returns `Ok(None)` if there are no points, or no points within the clipping rectangle.
    /// Returns an error if the resulting rectangle's dimensions are too large for the points.
    pub fn from_enclose_points(points: &[Point], clip: Option<Rect>) -> SdlResult<Option<Rect>> {
        let mut out = unsafe { mem::uninitialized() };

        let clip_ptr = match clip.as_ref() {
            Some(r) => r.raw(),
            None => ::std::ptr::null()
        };

        let result = unsafe {
            ll::SDL_EnclosePoints(
                Point::raw_slice(points),
                points.len() as i32,
                clip_ptr,
                &mut out
            ) != 0
        };

        if result {
            // Return an error if the dimensions are too large.
            match Rect::from_ll(out).unwrap() {
                Some(r) => Ok(Some(r)),
                None => Err(ErrorMessage("Enclosed point rectangle is too large.".into()))
            }
        } else {
            Ok(None)
        }
    }

    /// Determine whether two rectangles intersect.
    pub fn has_intersection(&self, other: &Rect) -> bool {
        unsafe {
            ll::SDL_HasIntersection(self.raw(), other.raw()) != 0
        }
    }

    /// Calculate the intersection of two rectangles. The bitwise AND operator `&` can also be used.
    pub fn intersect(&self, other: &Rect) -> Option<Rect> {
        let mut out = unsafe { mem::uninitialized() };

        let result = unsafe {
            ll::SDL_IntersectRect(self.raw(), other.raw(), &mut out) != 0
        };

        if result {
            Some(Rect::from_ll(out).unwrap().unwrap())
        } else {
            None
        }
    }

    /// Calculate the union of two rectangles. The bitwise OR operator `|` can also be used.
    pub fn union(&self, other: &Rect) -> Rect {
        let mut out = unsafe { mem::uninitialized() };

        unsafe {
            // If `self` and `other` are both empty, `out` remains uninitialized.
            // Because empty rectangles aren't allowed in Rect, we don't need to worry about this.
            ll::SDL_UnionRect(self.raw(), other.raw(), &mut out)
        };

        Rect::from_ll(out).unwrap().unwrap()
    }

    /// Calculate the intersection of a rectangle and line segment. return points of intersection.
    pub fn intersect_line(&self, start: Point, end: Point) -> Option<(Point, Point)> {
        let (mut start_x, mut start_y) = start.xy();
        let (mut end_x, mut end_y) = end.xy();

        let intersected = unsafe {
            ll::SDL_IntersectRectAndLine(self.raw(), &mut start_x, &mut start_y, &mut end_x, &mut end_y) != 0
        };

        if intersected {
            Some((Point::new(start_x, start_y), Point::new(end_x, end_y)))
        } else {
            None
        }
    }
}

/// Intersect
impl BitAnd<Rect> for Rect {
    type Output = Option<Rect>;
    fn bitand(self, rhs: Rect) -> Option<Rect> { self.intersect(&rhs) }
}

/// Intersect
impl<'a> BitAnd<&'a Rect> for Rect {
    type Output = Option<Rect>;
    fn bitand(self, rhs: &Rect) -> Option<Rect> { self.intersect(rhs) }
}

/// Intersect
impl<'a> BitAnd<Rect> for &'a Rect {
    type Output = Option<Rect>;
    fn bitand(self, rhs: Rect) -> Option<Rect> { self.intersect(&rhs) }
}

/// Intersect
impl<'a> BitAnd<&'a Rect> for &'a Rect {
    type Output = Option<Rect>;
    fn bitand(self, rhs: &Rect) -> Option<Rect> { self.intersect(rhs) }
}

/// Union
impl BitOr<Rect> for Rect {
    type Output = Rect;
    fn bitor(self, rhs: Rect) -> Rect { self.union(&rhs) }
}

/// Union
impl<'a> BitOr<&'a Rect> for Rect {
    type Output = Rect;
    fn bitor(self, rhs: &Rect) -> Rect { self.union(rhs) }
}

/// Union
impl<'a> BitOr<Rect> for &'a Rect {
    type Output = Rect;
    fn bitor(self, rhs: Rect) -> Rect { self.union(&rhs) }
}

/// Union
impl<'a> BitOr<&'a Rect> for &'a Rect {
    type Output = Rect;
    fn bitor(self, rhs: &Rect) -> Rect { self.union(rhs) }
}

#[cfg(test)]
mod test {
    use super::{Rect, Point};

    fn rect(x: i32, y: i32, w: u32, h: u32) -> Rect {
        Rect::new_unwrap(x, y, w, h)
    }

    #[test]
    fn test_from_enclose_points() {
        use std::i32;

        assert_eq!(Rect::from_enclose_points(&[Point::new(2, 4), Point::new(5,9)], None),
                   Ok(Some(rect(2, 4, 4, 6))));
        assert_eq!(Rect::from_enclose_points(&[Point::new(0, 0), Point::new(10,10)],
                                          Some(rect(3, 3, 1, 1))), Ok(None));

        // Try to enclose the top-left-most and bottom-right-most points.
        // The rectangle will be too large, and the function should return an error.
        assert!(Rect::from_enclose_points(&[Point::new(i32::MIN, i32::MIN), Point::new(i32::MAX, i32::MAX)], None).is_err());
    }

    #[test]
    fn test_has_intersection() {
        assert!(rect(0, 0, 10, 10).has_intersection(&rect(9, 9, 10, 10)));
        // edge
        assert!(! rect(0, 0, 10, 10).has_intersection(&rect(10, 10, 10, 10)));
        // out
        assert!(! rect(0, 0, 10, 10).has_intersection(&rect(11, 11, 10, 10)));
    }

    #[test]
    fn test_intersection() {
        assert_eq!(rect(0, 0, 10, 10) & rect(9, 9, 10, 10), Some(rect(9, 9, 1, 1)));
        assert!((rect(0, 0, 10, 10) & rect(11, 11, 10, 10)).is_none());
    }

    #[test]
    fn test_union() {
        assert_eq!(rect(0, 0, 1, 1) | rect(9, 9, 1, 1), rect(0, 0, 10, 10));
    }

    #[test]
    fn test_intersect_line() {
        assert_eq!(rect(1, 1, 5, 5).intersect_line(Point::new(0, 0), Point::new(10, 10)),
                   Some((Point::new(1, 1), Point::new(5, 5))));
    }

    #[test]
    fn test_rect_invariants() {
        use std::i32;
        const MAX_SIZE: u32 = i32::MAX as u32;

        let pass_nonempty = &[(0, 0, 1, 1), (i32::MIN, i32::MIN, MAX_SIZE, MAX_SIZE), (0, 0, MAX_SIZE, MAX_SIZE)];
        let pass_empty = &[(0, 0, 1, 0), (i32::MAX, i32::MAX, 0, 0)];
        let fail = &[(1, 1, MAX_SIZE, MAX_SIZE), (-1, -1, MAX_SIZE+1, MAX_SIZE+1), (i32::MAX, i32::MAX, 1, 1)];

        for &(x, y, w, h) in pass_nonempty {
            // Should be non-empty.
            match Rect::new(x, y, w, h) {
                Ok(None) | Err(..) => panic!("{:?}", (x, y, w, h)),
                _ => ()
            }
        }

        for &(x, y, w, h) in pass_empty {
            // Should be empty.
            match Rect::new(x, y, w, h) {
                Ok(Some(..)) | Err(..) => panic!("{:?}", (x, y, w, h)),
                _ => ()
            }
        }

        for &(x, y, w, h) in fail {
            // Should fail.
            assert!(Rect::new(x, y, w, h).is_err(), "{:?}", (x, y, w, h));
        }
    }

    #[test]
    fn test_rect_convert() {
        // Rect into
        let r = rect(-11, 5, 50, 20);
        let r_tuple: (i32, i32, u32, u32) = r.into();
        assert_eq!(r.xywh(), r_tuple);

        // Point into
        let p = Point::new(-11, 5);
        let p_tuple = p.into();
        assert_eq!(p.xy(), p_tuple);

        // Point from
        let p_tuple = (-11, 5);
        let p: Point = Point::from(p_tuple);
        assert_eq!(p, Point::new(-11, 5));
    }
 }