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
//! Alpha modification for a [`Surface`].

use crate::{bind, Sdl};

use super::{RawSurface, Surface};

/// An alpha modified [`Surface`].
#[derive(Debug)]
pub struct AlphaMod<S> {
    surface: S,
    alpha: u8,
}

impl<S> AlphaMod<S> {
    /// Returns the alpha modification value.
    pub fn alpha(&self) -> u8 {
        self.alpha
    }
}

impl<S: Surface> AlphaMod<S> {
    pub(super) fn new(surface: S, alpha: u8) -> Self {
        unsafe {
            let ret = bind::SDL_SetSurfaceAlphaMod(surface.as_ptr().as_ptr(), alpha);
            if ret != 0 {
                Sdl::error_then_panic("Setting surface alpha mod");
            }
        }
        Self { surface, alpha }
    }
}

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