summaryrefslogtreecommitdiff
path: root/src/context.rs
blob: c6c68bc8883803b236d581f4f2408dc5c44a4a8b (plain)
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
use glow::HasContext;

#[cfg(target_arch = "wasm32")]
use winit::platform::web::WindowExtWebSys;

#[cfg(target_arch = "wasm32")]
#[link(wasm_import_module = "./helpers.js")]
extern {
    fn js_track_resized_setup();
    fn js_poll_resized() -> bool;
}

pub const RENDER_WIDTH: f32 = 240.0;
pub const RENDER_HEIGHT: f32 = 160.0;

pub fn compute_upscale(windoww: u32, windowh: u32) -> u32 {
    let mut ratio = 1;
    loop {
        if (RENDER_WIDTH as u32) * ratio > windoww
            || (RENDER_HEIGHT as u32) * ratio > windowh
        {
            break;
        }
        ratio += 1;
    }
    (ratio - 1).max(1)
}

pub struct Context {
    pub window: winit::window::Window,
    pub gl: glow::Context,
    pub emptyvao: glow::VertexArray,

    #[cfg(target_arch = "wasm32")]
    pub performance: web_sys::Performance,
}

impl Context {
    pub fn new(window: winit::window::Window, gl: glow::Context) -> Self {
        unsafe {
            gl.clear_color(0.1, 0.1, 0.1, 1.0);
            gl.clear_depth_f32(1.0);

            gl.enable(glow::DEPTH_TEST);
            gl.depth_func(glow::LEQUAL);

            gl.enable(glow::BLEND);
            gl.blend_func(glow::SRC_ALPHA, glow::ONE_MINUS_SRC_ALPHA);

            gl.enable(glow::STENCIL_TEST);

            gl.cull_face(glow::FRONT);
        }

        let emptyvao = unsafe {
            gl.create_vertex_array().expect("failed to initialize vao")
        };

        #[cfg(target_arch = "wasm32")]
        unsafe { js_track_resized_setup(); }

        Self {
            window,
            gl,
            emptyvao,

            #[cfg(target_arch = "wasm32")]
            performance: web_sys::window().expect("failed to find window")
                .performance().expect("failed to get performance"),
        }
    }

    #[cfg(target_arch = "wasm32")]
    pub fn maximize_canvas(&self) {
        web_sys::window()
            .and_then(|win| win.document())
            .and_then(|doc| {
                let inner_size = {
                    let browser_window = doc.default_view()
                        .or_else(web_sys::window)
                        .unwrap();
                    winit::dpi::PhysicalSize::new(
                        browser_window.inner_width().unwrap().as_f64().unwrap(),
                        browser_window.inner_height().unwrap().as_f64().unwrap(),
                    )
                };
                self.window.canvas().unwrap().set_width(inner_size.width as _);
                self.window.canvas().unwrap().set_height(inner_size.height as _);
                let _ = self.window.request_inner_size(inner_size);
                Some(())
            })
            .expect("failed to resize canvas");
    }

    #[cfg(target_arch = "wasm32")]
    pub fn resize_necessary(&self) -> bool {
        unsafe {
            js_poll_resized()
        }
    }

    #[cfg(not(target_arch = "wasm32"))]
    pub fn resize_necessary(&self) -> bool {
        false
    }

    pub fn clear_color(&self, color: glam::Vec4) {
        unsafe {
            self.gl.clear_color(color.x, color.y, color.z, color.w);
        }
    }

    pub fn clear_depth(&self) {
        unsafe {
            self.gl.clear(glow::DEPTH_BUFFER_BIT);
        }
    }

    pub fn clear(&self) {
        unsafe {
            self.gl.stencil_mask(0xff);
            self.gl.clear(glow::COLOR_BUFFER_BIT | glow::DEPTH_BUFFER_BIT | glow::STENCIL_BUFFER_BIT);
        }
    }

    pub fn begin_stencil(&self) {
        unsafe {
            self.gl.stencil_func(glow::ALWAYS, 1, 0xff);
            self.gl.stencil_op(glow::KEEP, glow::KEEP, glow::REPLACE);
        }
    }

    pub fn use_stencil(&self) {
        unsafe {
            self.gl.stencil_func(glow::EQUAL, 1, 0xff);
            self.gl.stencil_op(glow::KEEP, glow::KEEP, glow::KEEP);
        }
    }

    pub fn end_stencil(&self) {
        unsafe {
            self.gl.stencil_func(glow::ALWAYS, 1, 0xff);
            self.gl.stencil_op(glow::KEEP, glow::KEEP, glow::KEEP);
        }
    }

    pub fn render_no_geometry(&self) {
        unsafe {
            self.gl.bind_vertex_array(Some(self.emptyvao));
            self.gl.draw_arrays(glow::TRIANGLE_STRIP, 0, 4);
        }
    }

    pub fn reset_blend(&self) {
        unsafe {
            self.gl.blend_func(glow::SRC_ALPHA, glow::ONE_MINUS_SRC_ALPHA);
        }
    }

    pub fn inverse_blend(&self) {
        unsafe {
            self.gl.blend_func(
                glow::ONE_MINUS_DST_COLOR,
                glow::ZERO,
            );
        }
    }

    pub fn enable_culling(&self) {
        unsafe {
            self.gl.enable(glow::CULL_FACE);
        }
    }

    pub fn disable_culling(&self) {
        unsafe {
            self.gl.disable(glow::CULL_FACE);
        }
    }
}