diff options
Diffstat (limited to 'WebInterface/wasm/webhogg/src')
-rw-r--r-- | WebInterface/wasm/webhogg/src/app.rs | 22 | ||||
-rw-r--r-- | WebInterface/wasm/webhogg/src/canvas.rs | 42 | ||||
-rw-r--r-- | WebInterface/wasm/webhogg/src/client_logger.rs | 46 | ||||
-rw-r--r-- | WebInterface/wasm/webhogg/src/lib.rs | 41 | ||||
-rw-r--r-- | WebInterface/wasm/webhogg/src/page.rs | 26 | ||||
-rw-r--r-- | WebInterface/wasm/webhogg/src/webhogg_exception.rs | 39 |
6 files changed, 216 insertions, 0 deletions
diff --git a/WebInterface/wasm/webhogg/src/app.rs b/WebInterface/wasm/webhogg/src/app.rs new file mode 100644 index 0000000..7931418 --- /dev/null +++ b/WebInterface/wasm/webhogg/src/app.rs @@ -0,0 +1,22 @@ +use crate::webhogg_exception::WebhoggException; +use crate::page::Page; +use crate::canvas::Canvas; + +pub(crate) struct WebhoggApplication { + page: Page, + canvas: Canvas, +} + +impl WebhoggApplication { + pub fn new() -> Result<Self, WebhoggException> { + let page = Page::obtain()?; + let canvas = Canvas::from_existing("canvas", &page)?; + Ok(Self { + page, canvas, + }) + } + + pub fn run(self) -> Result<(), WebhoggException> { + Ok(()) + } +} diff --git a/WebInterface/wasm/webhogg/src/canvas.rs b/WebInterface/wasm/webhogg/src/canvas.rs new file mode 100644 index 0000000..a0ef7d1 --- /dev/null +++ b/WebInterface/wasm/webhogg/src/canvas.rs @@ -0,0 +1,42 @@ +use web_sys::WebGl2RenderingContext as WebGl2; +use wasm_bindgen::JsCast; +use crate::webhogg_exception::WebhoggException; +use crate::page::Page; + +pub struct Canvas { + ctx: WebGl2, +} + +impl Canvas { + pub fn from_existing(id: &str, page: &Page) -> Result<Self, WebhoggException> { + let canvas_element = page.get_element(id) + .ok_or(WebhoggException::DomError( + "could not obtain canvas element (id=canvas)" + .to_string()))?; + let canvas_element = canvas_element + .dyn_into::<web_sys::HtmlCanvasElement>() + .map_err(|_| WebhoggException::DomError( + "id=canvas is not a canvas element".to_string()))?; + debug!("successfully obtained canvas element"); + let ctx = canvas_element.get_context("webgl2") + .map_err(|_| WebhoggException::WebGlContextError( + "obtained invalid webgl2 context js value".to_string()))? + .ok_or(WebhoggException::WebGlContextError( + "could not obtaine webgl2 context".to_string()))? + .dyn_into::<WebGl2>() + .map_err(|_| WebhoggException::WebGlContextError( + "obtained invalid webgl2 context js object".to_string()))?; + debug!("successfully obtained webgl2 context"); + Ok(Self { + ctx, + }) + } + + pub fn gl<'a>(&'a self) -> &'a WebGl2 { + &self.ctx + } + + pub fn gl_mut<'a>(&'a mut self) -> &'a mut WebGl2 { + &mut self.ctx + } +} diff --git a/WebInterface/wasm/webhogg/src/client_logger.rs b/WebInterface/wasm/webhogg/src/client_logger.rs new file mode 100644 index 0000000..f71918f --- /dev/null +++ b/WebInterface/wasm/webhogg/src/client_logger.rs @@ -0,0 +1,46 @@ +use wasm_bindgen::prelude::*; + +#[wasm_bindgen] +extern "C" { + #[wasm_bindgen(js_namespace=console, js_name=debug)] + fn __console_debug_colored2(f: &str, c1: &str, c2: &str); + #[wasm_bindgen(js_namespace=console, js_name=info)] + fn __console_info_colored2(f: &str, c1: &str, c2: &str); + #[wasm_bindgen(js_namespace=console, js_name=warn)] + fn __console_warn_colored2(f: &str, c1: &str, c2: &str); + #[wasm_bindgen(js_namespace=console, js_name=error)] + fn __console_error_colored2(f: &str, c1: &str, c2: &str); +} + +fn log(rec: &log::Record) { + let log_fn = match rec.level() { + log::Level::Trace | log::Level::Debug => __console_debug_colored2, + log::Level::Info => __console_info_colored2, + log::Level::Warn => __console_warn_colored2, + log::Level::Error => __console_error_colored2, + }; + log_fn(&format!("{}", rec.args()), + &format!("color: {}", match rec.level() { + log::Level::Trace => "violet", + log::Level::Debug => "blue", + log::Level::Info => "green", + log::Level::Warn => "orange", + log::Level::Error => "red" + }), ""); +} + +pub fn init_logger() { + fern::Dispatch::new().format(|out, message, record|{ + out.finish(format_args!( + "%c{}%c {} > {}", + record.level(), + record.target(), + message + ) + ) + }) + .level(log::LevelFilter::Debug) + .chain(fern::Output::call(log)) + .apply().unwrap(); +} + diff --git a/WebInterface/wasm/webhogg/src/lib.rs b/WebInterface/wasm/webhogg/src/lib.rs new file mode 100644 index 0000000..4a79a24 --- /dev/null +++ b/WebInterface/wasm/webhogg/src/lib.rs @@ -0,0 +1,41 @@ +mod client_logger; +mod webhogg_exception; +mod page; +mod canvas; +mod app; + +use wasm_bindgen::prelude::*; +use app::WebhoggApplication as App; +use web_sys::Worker; + +#[macro_use] +extern crate log; + +fn run_application() { + match App::new().and_then(|app| app.run()) { + Ok(_) => info!("program terminated successfully"), + Err(e) => error!("program terminated with failure > {}", e) + } +} + +#[wasm_bindgen] +pub fn game_logic_entry(worker: Worker) { + client_logger::init_logger(); + + info!("game logic initialisation"); +} + +#[wasm_bindgen] +pub fn graphics_entry(worker: Worker) { + client_logger::init_logger(); + + info!("graphics initialisation"); +} + +pub fn entry2() { + client_logger::init_logger(); + + info!("begin running wasm application"); + + run_application() +} diff --git a/WebInterface/wasm/webhogg/src/page.rs b/WebInterface/wasm/webhogg/src/page.rs new file mode 100644 index 0000000..cb9ee3f --- /dev/null +++ b/WebInterface/wasm/webhogg/src/page.rs @@ -0,0 +1,26 @@ +use web_sys; + +use crate::webhogg_exception::WebhoggException; + +pub struct Page { + window: web_sys::Window, + document: web_sys::Document, +} + +impl Page { + pub fn obtain() -> Result<Self, WebhoggException> { + let window = web_sys::window() + .ok_or(WebhoggException::DomError("could not obtain window".to_string()))?; + let document = window.document() + .ok_or(WebhoggException::DomError("could not obtain document".to_string()))?; + debug!("initialised page"); + Ok(Self { + window, + document, + }) + } + + pub fn get_element(&self, id: &str) -> Option<web_sys::Element> { + self.document.get_element_by_id(id) + } +} diff --git a/WebInterface/wasm/webhogg/src/webhogg_exception.rs b/WebInterface/wasm/webhogg/src/webhogg_exception.rs new file mode 100644 index 0000000..46eedd6 --- /dev/null +++ b/WebInterface/wasm/webhogg/src/webhogg_exception.rs @@ -0,0 +1,39 @@ +use std::error::Error; + +#[derive(Debug)] +pub enum WebhoggException { + DomError(String), + WebGlContextError(String), +} + +impl WebhoggException { + fn err_name(&self) -> &str { + match self { + WebhoggException::DomError(_) => "DomError", + WebhoggException::WebGlContextError(_) => "WebGlContextError", + } + } +} + +impl Error for WebhoggException { + fn description(&self) -> &str { + match self { + WebhoggException::DomError(desc) => desc, + WebhoggException::WebGlContextError(desc) => desc, + } + } + + fn cause(&self) -> Option<&dyn Error> { + self.source() + } + + fn source(&self) -> Option<&(dyn Error + 'static)> { + None + } +} + +impl std::fmt::Display for WebhoggException { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + write!(f, "WebhoggException::{} {}", self.err_name(), self.description()) + } +} |