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 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