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
use super::{WebGlCommon, WebGlRenderer};
use web_sys::{WebGl2RenderingContext, WebGlRenderingContext};

pub trait PartialWebGlMisc {
    fn awsm_depth_mask(&self, flag: bool);
    fn awsm_clear_color(&self, r: f32, g: f32, b: f32, a: f32);
}

macro_rules! impl_context {
    ($($type:ty { $($defs:tt)* })+) => {
        $(impl PartialWebGlMisc for $type {
            fn awsm_depth_mask(&self, flag:bool) {
                self.depth_mask(flag);
            }
            fn awsm_clear_color(&self, r: f32, g: f32, b: f32, a: f32) {
                self.clear_color(r, g, b, a);
            }
            $($defs)*
        })+
    };
}

pub struct MiscSettings {
    pub depth_mask: bool,
    pub clear_color: (f32, f32, f32, f32),
}

impl Default for MiscSettings {
    fn default() -> Self {
        Self {
            depth_mask: true,
            clear_color: (0.0, 0.0, 0.0, 0.0),
        }
    }
}

impl_context! {
    WebGlRenderingContext{}
    WebGl2RenderingContext{}
}

impl<T: WebGlCommon> WebGlRenderer<T> {
    pub fn set_depth_mask(&mut self, flag: bool) {
        if self.misc_settings.depth_mask != flag {
            self.gl.awsm_depth_mask(flag);
            self.misc_settings.depth_mask = flag;
        }
    }

    pub fn set_clear_color(&mut self, r: f32, g: f32, b: f32, a: f32) {
        let curr = self.misc_settings.clear_color;

        if curr.0 != r || curr.1 != g || curr.2 != b || curr.3 != a {
            self.gl.awsm_clear_color(r, g, b, a);
            self.misc_settings.clear_color = (r, g, b, a);
        }
    }
}