From 1b6a2835e57a7d399f43ecf493060e68042f9af8 Mon Sep 17 00:00:00 2001 From: natrixaeria Date: Wed, 12 Jun 2019 16:59:09 +0200 Subject: Clone the colored logging from old commit --- webhogg/wasm/pkg/main.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) (limited to 'webhogg/wasm/pkg/main.js') diff --git a/webhogg/wasm/pkg/main.js b/webhogg/wasm/pkg/main.js index eec740d..ce96ee5 100644 --- a/webhogg/wasm/pkg/main.js +++ b/webhogg/wasm/pkg/main.js @@ -1,9 +1,13 @@ async function main() { let fetchingSource = fetch('bin/webhogg-wasm.wasm'); let fetchedSource = await fetchingSource; - let source = await fetchedSource.text(); - //alert(source) - let workerGraphics = new Worker('pkg/worker-graphics.js'); -} + source = await fetchedSource.arrayBuffer(); + let workers = []; + for (var type of ['graphics', 'logic']) { + let worker = new Worker('pkg/worker.js'); + worker.postMessage([type, source]); + workers.push(worker); + } +} main(); -- cgit v1.2.3-54-g00ecf From c3fdf122bd874bd0aedf90eff0d57cc440fc6421 Mon Sep 17 00:00:00 2001 From: natrixaeria Date: Thu, 13 Jun 2019 00:38:21 +0200 Subject: Split code into graphics and logic loop --- webhogg/wasm/Cargo.toml | 5 +++++ webhogg/wasm/pkg/main.js | 22 +++++++++++++++++++--- webhogg/wasm/pkg/worker.js | 12 +++++++----- webhogg/wasm/src/context/graphics.rs | 11 +++++++++++ webhogg/wasm/src/context/logic.rs | 11 +++++++++++ webhogg/wasm/src/context/mod.rs | 24 ++++++++++++++++++++++++ webhogg/wasm/src/error.rs | 30 ++++++++++++++++++++++++++++++ webhogg/wasm/src/graphics.rs | 19 +++++++++++++++++++ webhogg/wasm/src/lib.rs | 19 ++++++------------- webhogg/wasm/src/logic.rs | 19 +++++++++++++++++++ 10 files changed, 151 insertions(+), 21 deletions(-) create mode 100644 webhogg/wasm/src/context/graphics.rs create mode 100644 webhogg/wasm/src/context/logic.rs create mode 100644 webhogg/wasm/src/context/mod.rs create mode 100644 webhogg/wasm/src/error.rs create mode 100644 webhogg/wasm/src/graphics.rs create mode 100644 webhogg/wasm/src/logic.rs (limited to 'webhogg/wasm/pkg/main.js') diff --git a/webhogg/wasm/Cargo.toml b/webhogg/wasm/Cargo.toml index f4042c5..71b6a8e 100644 --- a/webhogg/wasm/Cargo.toml +++ b/webhogg/wasm/Cargo.toml @@ -18,3 +18,8 @@ lto = true wasm-bindgen = "0.2" log = "0.4" fern = "0.5" + +[dependencies.web-sys] +version = "0.3" +features = [ +] diff --git a/webhogg/wasm/pkg/main.js b/webhogg/wasm/pkg/main.js index ce96ee5..f16a775 100644 --- a/webhogg/wasm/pkg/main.js +++ b/webhogg/wasm/pkg/main.js @@ -1,12 +1,28 @@ +workers = []; + +function exit() { + for (var worker of workers) { + worker.terminate(); + } + console.clear(); +} + async function main() { let fetchingSource = fetch('bin/webhogg-wasm.wasm'); + + let canvasElement = document.getElementById('c'); + let offCanvas = canvasElement.transferControlToOffscreen(); + let fetchedSource = await fetchingSource; source = await fetchedSource.arrayBuffer(); - let workers = []; - for (var type of ['graphics', 'logic']) { + const modules = [ + ['graphics', source, [offCanvas], 100], + ['logic', source, [], 1000] + ]; + for (var module of modules) { let worker = new Worker('pkg/worker.js'); - worker.postMessage([type, source]); + worker.postMessage(module, module[2]); workers.push(worker); } } diff --git a/webhogg/wasm/pkg/worker.js b/webhogg/wasm/pkg/worker.js index bd11676..0b68374 100644 --- a/webhogg/wasm/pkg/worker.js +++ b/webhogg/wasm/pkg/worker.js @@ -1,9 +1,11 @@ onmessage = async function (e) { importScripts('../bin/webhogg-wasm.js'); - let ctx = await wasm_bindgen(e.data[1]); + 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); - if (e.data[0] === 'graphics') - ctx.start_graphics(); - else if (e.data[0] === 'logic') - ctx.start_logic(); + 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 new file mode 100644 index 0000000..57955d0 --- /dev/null +++ b/webhogg/wasm/src/context/graphics.rs @@ -0,0 +1,11 @@ +use crate::error::WasmError; + +pub struct GraphicsContext { +} + +impl GraphicsContext { + pub fn from_canvas() -> Result { + Ok(Self { + }) + } +} diff --git a/webhogg/wasm/src/context/logic.rs b/webhogg/wasm/src/context/logic.rs new file mode 100644 index 0000000..71dfea4 --- /dev/null +++ b/webhogg/wasm/src/context/logic.rs @@ -0,0 +1,11 @@ +use crate::error::WasmError; + +pub struct LogicContext { +} + +impl LogicContext { + pub fn new() -> Result { + Ok(Self { + }) + } +} diff --git a/webhogg/wasm/src/context/mod.rs b/webhogg/wasm/src/context/mod.rs new file mode 100644 index 0000000..3e8261b --- /dev/null +++ b/webhogg/wasm/src/context/mod.rs @@ -0,0 +1,24 @@ +pub mod graphics; +pub mod logic; + +use graphics::GraphicsContext; +use logic::LogicContext; + +static mut GTX: Option = None; +static mut LTX: Option = None; + +pub fn get_graphics() -> &'static mut GraphicsContext { + unsafe { GTX.as_mut().unwrap() } +} + +pub fn get_logic() -> &'static mut LogicContext { + unsafe { LTX.as_mut().unwrap() } +} + +pub fn set_graphics(gtx: GraphicsContext) { + unsafe { GTX = Some(gtx) } +} + +pub fn set_logic(ltx: LogicContext) { + unsafe { LTX = Some(ltx) } +} diff --git a/webhogg/wasm/src/error.rs b/webhogg/wasm/src/error.rs new file mode 100644 index 0000000..b544112 --- /dev/null +++ b/webhogg/wasm/src/error.rs @@ -0,0 +1,30 @@ +use std::error::Error; + +#[derive(Debug)] +pub enum WasmError { + TestError(String), +} + +impl std::fmt::Display for WasmError { + fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> { + write!(f, "{}: {}", self.name(), self.description()) + } +} + +impl Error for WasmError { + fn description(&self) -> &str { + match self { + WasmError::TestError(msg) => msg, + } + } + + fn source(&self) -> Option<&'static dyn Error> { None } +} + +impl WasmError { + pub fn name(&self) -> &str { + match self { + WasmError::TestError(_) => "TestError", + } + } +} diff --git a/webhogg/wasm/src/graphics.rs b/webhogg/wasm/src/graphics.rs new file mode 100644 index 0000000..8169a3c --- /dev/null +++ b/webhogg/wasm/src/graphics.rs @@ -0,0 +1,19 @@ +use wasm_bindgen::prelude::*; +use log::*; +use crate::*; + +#[wasm_bindgen] +pub fn start_graphics() { + logger::init_logger(); + info!("graphics: wasm entry-point reached"); + + match context::graphics::GraphicsContext::from_canvas() { + Ok(ctx) => context::set_graphics(ctx), + Err(e) => error!("graphics {}", e) + } +} + +#[wasm_bindgen] +pub fn loop_graphics() { + debug!("graphics: loopin'"); +} diff --git a/webhogg/wasm/src/lib.rs b/webhogg/wasm/src/lib.rs index bed779b..7aa4e86 100644 --- a/webhogg/wasm/src/lib.rs +++ b/webhogg/wasm/src/lib.rs @@ -1,16 +1,9 @@ -use wasm_bindgen::prelude::*; -use log::*; - mod logger; +pub mod error; +pub mod context; -#[wasm_bindgen] -pub fn start_graphics() { - logger::init_logger(); - info!("hello from wasm graphics"); -} +pub mod logic; +pub mod graphics; -#[wasm_bindgen] -pub fn start_logic() { - logger::init_logger(); - debug!("hello from wasm logic"); -} +pub use logic::*; +pub use graphics::*; diff --git a/webhogg/wasm/src/logic.rs b/webhogg/wasm/src/logic.rs new file mode 100644 index 0000000..14272d9 --- /dev/null +++ b/webhogg/wasm/src/logic.rs @@ -0,0 +1,19 @@ +use wasm_bindgen::prelude::*; +use log::*; +use crate::*; + +#[wasm_bindgen] +pub fn start_logic() { + logger::init_logger(); + info!("logic: wasm entry-point reached"); + + match context::logic::LogicContext::new() { + Ok(ctx) => context::set_logic(ctx), + Err(e) => error!("logic {}", e) + } +} + +#[wasm_bindgen] +pub fn loop_logic() { + debug!("logic: loopin'"); +} -- cgit v1.2.3-54-g00ecf 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(-) (limited to 'webhogg/wasm/pkg/main.js') 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 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/pkg/main.js') 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-54-g00ecf