diff options
author | natrixaeria <janng@gmx.de> | 2019-06-13 01:34:35 +0200 |
---|---|---|
committer | natrixaeria <janng@gmx.de> | 2019-06-13 01:34:35 +0200 |
commit | 5de862b4d175578ed67e177d482ac31bf811d486 (patch) | |
tree | 4a242157e34abf92f572b8f0b2135db0f44fc17e | |
parent | c3fdf122bd874bd0aedf90eff0d57cc440fc6421 (diff) |
Create a WebGl2 context
-rw-r--r-- | webhogg/wasm/Cargo.toml | 3 | ||||
-rw-r--r-- | webhogg/wasm/src/context/graphics.rs | 18 | ||||
-rw-r--r-- | webhogg/wasm/src/error.rs | 6 | ||||
-rw-r--r-- | webhogg/wasm/src/graphics.rs | 4 |
4 files changed, 25 insertions, 6 deletions
diff --git a/webhogg/wasm/Cargo.toml b/webhogg/wasm/Cargo.toml index 71b6a8e..2d69f41 100644 --- a/webhogg/wasm/Cargo.toml +++ b/webhogg/wasm/Cargo.toml @@ -18,8 +18,11 @@ lto = true wasm-bindgen = "0.2" log = "0.4" fern = "0.5" +js-sys = "0.3" [dependencies.web-sys] version = "0.3" features = [ + "OffscreenCanvas", + "WebGl2RenderingContext" ] diff --git a/webhogg/wasm/src/context/graphics.rs b/webhogg/wasm/src/context/graphics.rs index 57955d0..3476e5d 100644 --- a/webhogg/wasm/src/context/graphics.rs +++ b/webhogg/wasm/src/context/graphics.rs @@ -1,10 +1,26 @@ +use log::*; use crate::error::WasmError; +use wasm_bindgen::JsCast; +use web_sys::WebGl2RenderingContext as Gl; pub struct GraphicsContext { } impl GraphicsContext { - pub fn from_canvas() -> Result<Self, WasmError> { + pub fn from_canvas(canvas: web_sys::OffscreenCanvas) -> Result<Self, WasmError> { + let context = canvas.get_context("webgl2") + .map_err(|_| WasmError::WebGl2ContextCreation( + format!("context cration failed: getContext returned an exception")))? + .ok_or_else(|| WasmError::WebGl2ContextCreation( + format!("context cration failed: getContext returned nothing")))?; + let context = context + .dyn_into::<Gl>() + .map_err(|_| WasmError::WebGl2ContextCreation( + format!("context object is not a context")))?; + + //context.clear(Gl::COLOR_BUFFER_BIT); + //context.clear_color(0.6, 0.0, 0.6, 1.0); + Ok(Self { }) } diff --git a/webhogg/wasm/src/error.rs b/webhogg/wasm/src/error.rs index b544112..1c6ec27 100644 --- a/webhogg/wasm/src/error.rs +++ b/webhogg/wasm/src/error.rs @@ -2,7 +2,7 @@ use std::error::Error; #[derive(Debug)] pub enum WasmError { - TestError(String), + WebGl2ContextCreation(String), } impl std::fmt::Display for WasmError { @@ -14,7 +14,7 @@ impl std::fmt::Display for WasmError { impl Error for WasmError { fn description(&self) -> &str { match self { - WasmError::TestError(msg) => msg, + WasmError::WebGl2ContextCreation(msg) => msg, } } @@ -24,7 +24,7 @@ impl Error for WasmError { impl WasmError { pub fn name(&self) -> &str { match self { - WasmError::TestError(_) => "TestError", + WasmError::WebGl2ContextCreation(_) => "WebGl2ContextCreationError", } } } diff --git a/webhogg/wasm/src/graphics.rs b/webhogg/wasm/src/graphics.rs index 8169a3c..a879c5e 100644 --- a/webhogg/wasm/src/graphics.rs +++ b/webhogg/wasm/src/graphics.rs @@ -3,11 +3,11 @@ use log::*; use crate::*; #[wasm_bindgen] -pub fn start_graphics() { +pub fn start_graphics(canvas: web_sys::OffscreenCanvas) { logger::init_logger(); info!("graphics: wasm entry-point reached"); - match context::graphics::GraphicsContext::from_canvas() { + match context::graphics::GraphicsContext::from_canvas(canvas) { Ok(ctx) => context::set_graphics(ctx), Err(e) => error!("graphics {}", e) } |