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/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 +++++++++++++++++++ 7 files changed, 120 insertions(+), 13 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/src') 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-70-g09d2