From 03cedca74429c326fcca176576702873e32a8455 Mon Sep 17 00:00:00 2001 From: natrixaeria Date: Thu, 13 Jun 2019 17:21:44 +0200 Subject: Add rendering loop --- webhogg/wasm/index.html | 4 ++-- webhogg/wasm/pkg/main.js | 2 +- webhogg/wasm/src/context/graphics.rs | 29 ++++++++++++++++++++--------- webhogg/wasm/src/graphics.rs | 4 +++- 4 files changed, 26 insertions(+), 13 deletions(-) (limited to 'webhogg/wasm') diff --git a/webhogg/wasm/index.html b/webhogg/wasm/index.html index b6c8b9b..315d9cb 100644 --- a/webhogg/wasm/index.html +++ b/webhogg/wasm/index.html @@ -4,8 +4,8 @@ webhogg - - + + your browser is incompetent diff --git a/webhogg/wasm/pkg/main.js b/webhogg/wasm/pkg/main.js index 76afa1c..90b3956 100644 --- a/webhogg/wasm/pkg/main.js +++ b/webhogg/wasm/pkg/main.js @@ -20,7 +20,7 @@ async function main() { { type: 'graphics', source: source, canvas: offCanvas, - dt: 10000 }, + dt: 16 }, { type: 'logic', source: source, canvas: [], diff --git a/webhogg/wasm/src/context/graphics.rs b/webhogg/wasm/src/context/graphics.rs index 853e1e1..b94441d 100644 --- a/webhogg/wasm/src/context/graphics.rs +++ b/webhogg/wasm/src/context/graphics.rs @@ -4,16 +4,12 @@ use wasm_bindgen::JsCast; use web_sys::WebGl2RenderingContext as Gl; pub struct GraphicsContext { + gl: Gl, + frame_nr: u64, } impl GraphicsContext { pub fn from_canvas(canvas: web_sys::OffscreenCanvas) -> Result { - /*debug!("canvas object usw.: {:?}", canvas); - let canvas: web_sys::OffscreenCanvas = js_sys::Reflect::get(&canvas, - &wasm_bindgen::JsValue::from_str("canvas")) - .map_err(|_| WasmError::WebGl2ContextCreation( - format!("canvas object is not defined")))? - .into();*/ let context = canvas.get_context("webgl2") .map_err(|_| WasmError::WebGl2ContextCreation( format!("context cration failed: getContext returned an exception")))? @@ -23,11 +19,26 @@ impl GraphicsContext { .dyn_into::() .map_err(|_| WasmError::WebGl2ContextCreation( format!("context object is not a context")))?; - - context.clear_color(0.6, 0.0, 0.6, 1.0); - context.clear(Gl::COLOR_BUFFER_BIT); Ok(Self { + gl: context, + frame_nr: 0, }) } + + pub fn update(&mut self) -> Result<(), WasmError> { + let light = 0.5; + + let a = (self.frame_nr as f32) / 60.0; + let a = f32::abs(f32::sin(a)); + let b = f32::abs(f32::cos(a)); + let (a, b) = (a * light, b * light); + + self.gl.clear_color(a, light - a, b, 1.0); + self.gl.clear(Gl::COLOR_BUFFER_BIT); + + self.frame_nr += 1; + + Ok(()) + } } diff --git a/webhogg/wasm/src/graphics.rs b/webhogg/wasm/src/graphics.rs index 690b4cd..c4902ae 100644 --- a/webhogg/wasm/src/graphics.rs +++ b/webhogg/wasm/src/graphics.rs @@ -16,5 +16,7 @@ pub fn start_graphics(canvas: web_sys::OffscreenCanvas) { #[wasm_bindgen] pub fn loop_graphics() { - debug!("graphics: loopin'"); + context::get_graphics().update() + .map_err(|e| error!("gaphics loop {}", e)) + .unwrap(); } -- cgit v1.2.3-70-g09d2