summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornatrixaeria <janng@gmx.de>2019-06-13 00:38:21 +0200
committernatrixaeria <janng@gmx.de>2019-06-13 00:38:21 +0200
commitc3fdf122bd874bd0aedf90eff0d57cc440fc6421 (patch)
tree0220c9961cd37a58d4398741aec32e6619379bbc
parent1b6a2835e57a7d399f43ecf493060e68042f9af8 (diff)
Split code into graphics and logic loop
-rw-r--r--webhogg/wasm/Cargo.toml5
-rw-r--r--webhogg/wasm/pkg/main.js22
-rw-r--r--webhogg/wasm/pkg/worker.js12
-rw-r--r--webhogg/wasm/src/context/graphics.rs11
-rw-r--r--webhogg/wasm/src/context/logic.rs11
-rw-r--r--webhogg/wasm/src/context/mod.rs24
-rw-r--r--webhogg/wasm/src/error.rs30
-rw-r--r--webhogg/wasm/src/graphics.rs19
-rw-r--r--webhogg/wasm/src/lib.rs19
-rw-r--r--webhogg/wasm/src/logic.rs19
10 files changed, 151 insertions, 21 deletions
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<Self, WasmError> {
+ 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<Self, WasmError> {
+ 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<GraphicsContext> = None;
+static mut LTX: Option<LogicContext> = 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'");
+}