use crate::core::*;
#[derive(Debug, Copy, Clone, PartialEq)]
pub struct ClearState {
pub red: Option<f32>,
pub green: Option<f32>,
pub blue: Option<f32>,
pub alpha: Option<f32>,
pub depth: Option<f32>,
}
impl ClearState {
pub const fn none() -> Self {
Self {
red: None,
green: None,
blue: None,
alpha: None,
depth: None,
}
}
pub const fn depth(depth: f32) -> Self {
Self {
red: None,
green: None,
blue: None,
alpha: None,
depth: Some(depth),
}
}
pub const fn color(red: f32, green: f32, blue: f32, alpha: f32) -> Self {
Self {
red: Some(red),
green: Some(green),
blue: Some(blue),
alpha: Some(alpha),
depth: None,
}
}
pub const fn color_and_depth(red: f32, green: f32, blue: f32, alpha: f32, depth: f32) -> Self {
Self {
red: Some(red),
green: Some(green),
blue: Some(blue),
alpha: Some(alpha),
depth: Some(depth),
}
}
pub(in crate::core) fn apply(&self, context: &Context) {
context.set_write_mask(WriteMask {
red: self.red.is_some(),
green: self.green.is_some(),
blue: self.blue.is_some(),
alpha: self.alpha.is_some(),
depth: self.depth.is_some(),
});
unsafe {
let clear_color = self.red.is_some()
|| self.green.is_some()
|| self.blue.is_some()
|| self.alpha.is_some();
if clear_color {
context.clear_color(
self.red.unwrap_or(0.0),
self.green.unwrap_or(0.0),
self.blue.unwrap_or(0.0),
self.alpha.unwrap_or(1.0),
);
}
if let Some(depth) = self.depth {
context.clear_depth_f32(depth);
}
context.clear(if clear_color && self.depth.is_some() {
crate::context::COLOR_BUFFER_BIT | crate::context::DEPTH_BUFFER_BIT
} else if clear_color {
crate::context::COLOR_BUFFER_BIT
} else {
crate::context::DEPTH_BUFFER_BIT
});
}
}
}
impl Default for ClearState {
fn default() -> Self {
Self::color_and_depth(0.0, 0.0, 0.0, 1.0, 1.0)
}
}