From 7884252333cc102a8144e4eafc36f9ef605b1be7 Mon Sep 17 00:00:00 2001 From: natrixaeria Date: Thu, 13 Jun 2019 03:21:53 +0200 Subject: Draw violet background --- webhogg/wasm/index.html | 4 +++- webhogg/wasm/pkg/main.js | 16 +++++++++++++--- webhogg/wasm/pkg/worker.js | 22 ++++++++++++++-------- webhogg/wasm/src/context/graphics.rs | 10 ++++++++-- webhogg/wasm/src/graphics.rs | 1 + 5 files changed, 39 insertions(+), 14 deletions(-) diff --git a/webhogg/wasm/index.html b/webhogg/wasm/index.html index eae1cc2..b6c8b9b 100644 --- a/webhogg/wasm/index.html +++ b/webhogg/wasm/index.html @@ -5,7 +5,9 @@ webhogg - your browser is incompetent + + your browser is incompetent + diff --git a/webhogg/wasm/pkg/main.js b/webhogg/wasm/pkg/main.js index f16a775..76afa1c 100644 --- a/webhogg/wasm/pkg/main.js +++ b/webhogg/wasm/pkg/main.js @@ -17,12 +17,22 @@ async function main() { source = await fetchedSource.arrayBuffer(); const modules = [ - ['graphics', source, [offCanvas], 100], - ['logic', source, [], 1000] + { type: 'graphics', + source: source, + canvas: offCanvas, + dt: 10000 }, + { type: 'logic', + source: source, + canvas: [], + dt: 10000 }, ]; for (var module of modules) { let worker = new Worker('pkg/worker.js'); - worker.postMessage(module, module[2]); + if (module.type === 'graphics') { + worker.postMessage(module, [module.canvas]); + } else { + worker.postMessage(module); + } workers.push(worker); } } diff --git a/webhogg/wasm/pkg/worker.js b/webhogg/wasm/pkg/worker.js index 0b68374..780ea6f 100644 --- a/webhogg/wasm/pkg/worker.js +++ b/webhogg/wasm/pkg/worker.js @@ -1,11 +1,17 @@ -onmessage = async function (e) { +let data = null; + +onmessage = function (e) { + data = e.data; + importScripts('../bin/webhogg-wasm.js'); - let type = e.data[0]; - let source = e.data[1]; - let args = e.data[2]; - let dt = e.data[3]; - let ctx = await wasm_bindgen(source); + wasm_bindgen(data.source).then(ctx => { + if (data.type === 'graphics') { + wasm_bindgen.start_graphics(data.canvas); + setInterval(wasm_bindgen.loop_graphics, data.dt); + } else if (data.type === 'logic') { + wasm_bindgen.start_logic(); + setInterval(wasm_bindgen.loop_logic, data.dt); + } - ctx['start_' + type].apply(args); - setInterval(ctx['loop_' + type], dt); + }); } diff --git a/webhogg/wasm/src/context/graphics.rs b/webhogg/wasm/src/context/graphics.rs index 3476e5d..853e1e1 100644 --- a/webhogg/wasm/src/context/graphics.rs +++ b/webhogg/wasm/src/context/graphics.rs @@ -8,6 +8,12 @@ pub struct GraphicsContext { 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")))? @@ -18,8 +24,8 @@ impl GraphicsContext { .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); + context.clear_color(0.6, 0.0, 0.6, 1.0); + context.clear(Gl::COLOR_BUFFER_BIT); Ok(Self { }) diff --git a/webhogg/wasm/src/graphics.rs b/webhogg/wasm/src/graphics.rs index a879c5e..690b4cd 100644 --- a/webhogg/wasm/src/graphics.rs +++ b/webhogg/wasm/src/graphics.rs @@ -6,6 +6,7 @@ use crate::*; pub fn start_graphics(canvas: web_sys::OffscreenCanvas) { logger::init_logger(); info!("graphics: wasm entry-point reached"); + //debug!("js value is?: undefined: {}", canvas.is_undefined()); match context::graphics::GraphicsContext::from_canvas(canvas) { Ok(ctx) => context::set_graphics(ctx), -- cgit v1.2.3-54-g00ecf