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
//! Clipping an area of a [`Surface`].

use crate::bind;
use crate::geo::Rect;

use super::{RawSurface, Surface};

/// A clipped [`Surface`] with an area.
#[derive(Debug)]
pub struct Clipped<S> {
    surface: S,
    area: Rect,
}

impl<S> Clipped<S> {
    /// Returns the clipped area.
    pub fn area(&self) -> Rect {
        self.area
    }
}

impl<S: Surface> Clipped<S> {
    pub(super) fn new(surface: S, area: Rect) -> Self {
        let raw_rect = area.into();
        unsafe {
            let _ = bind::SDL_SetClipRect(surface.as_ptr().as_ptr(), &raw_rect);
        }
        Self { surface, area }
    }
}

impl<S: Surface> Surface for Clipped<S> {
    fn as_ptr(&self) -> std::ptr::NonNull<RawSurface> {
        self.surface.as_ptr()
    }
}