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
//! Clipping a renderer.

use crate::geo::Rect;
use crate::{bind, Sdl};

use super::Renderer;

/// A renderer that clipped from another renderer.
#[derive(Debug)]
pub struct ClippedRenderer<'renderer> {
    renderer: &'renderer mut Renderer<'renderer>,
}

impl<'renderer> ClippedRenderer<'renderer> {
    pub(super) fn new(renderer: &'renderer mut Renderer<'renderer>, rect: Rect) -> Self {
        let ret = unsafe { bind::SDL_RenderSetClipRect(renderer.as_ptr(), &rect.into()) };
        if ret != 0 {
            Sdl::error_then_panic("Setting renderer clip rect");
        }
        Self { renderer }
    }
}

impl Drop for ClippedRenderer<'_> {
    fn drop(&mut self) {
        let ret = unsafe { bind::SDL_RenderSetClipRect(self.renderer.as_ptr(), std::ptr::null()) };
        debug_assert!(ret == 0);
    }
}