summaryrefslogtreecommitdiff
path: root/WebInterface/wasm/webhogg/src
diff options
context:
space:
mode:
Diffstat (limited to 'WebInterface/wasm/webhogg/src')
-rw-r--r--WebInterface/wasm/webhogg/src/app.rs22
-rw-r--r--WebInterface/wasm/webhogg/src/canvas.rs24
-rw-r--r--WebInterface/wasm/webhogg/src/lib.rs36
-rw-r--r--WebInterface/wasm/webhogg/src/page.rs26
4 files changed, 22 insertions, 86 deletions
diff --git a/WebInterface/wasm/webhogg/src/app.rs b/WebInterface/wasm/webhogg/src/app.rs
deleted file mode 100644
index 7931418..0000000
--- a/WebInterface/wasm/webhogg/src/app.rs
+++ /dev/null
@@ -1,22 +0,0 @@
-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
index a0ef7d1..5f819e9 100644
--- a/WebInterface/wasm/webhogg/src/canvas.rs
+++ b/WebInterface/wasm/webhogg/src/canvas.rs
@@ -1,32 +1,26 @@
use web_sys::WebGl2RenderingContext as WebGl2;
use wasm_bindgen::JsCast;
use crate::webhogg_exception::WebhoggException;
-use crate::page::Page;
+use web_sys::OffscreenCanvas as ECanvas;
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()))?
+ pub fn from_existing(canvas: &ECanvas) -> Result<Self, WebhoggException> {
+ info!("messages lol");
+ let ctx = canvas.get_context("webgl2")
+ .map_err(|x| WebhoggException::WebGlContextError(
+ format!("obtained invalid webgl2 context js value: {:?}", x)))?
.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");
+ info!("successfully obtained webgl2 context");
+ ctx.clear_color(1.0, 0.0, 0.0, 1.0);
+ ctx.clear(WebGl2::COLOR_BUFFER_BIT);
Ok(Self {
ctx,
})
diff --git a/WebInterface/wasm/webhogg/src/lib.rs b/WebInterface/wasm/webhogg/src/lib.rs
index 2ee7267..6d9fca9 100644
--- a/WebInterface/wasm/webhogg/src/lib.rs
+++ b/WebInterface/wasm/webhogg/src/lib.rs
@@ -1,54 +1,44 @@
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;
+use web_sys::OffscreenCanvas as ECanvas;
#[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: web_sys::Worker) {
client_logger::init_logger();
info!("hello from game logic wasm");
- info!("begin long calculation in game logic thread");
worker.post_message(&wasm_bindgen::JsValue::from_str("premsg frm wasm_gLe"))
.unwrap();
- info!("killed game logic");
+ info!("game logic terminated");
}
#[wasm_bindgen]
pub fn graphics_entry(worker: web_sys::DedicatedWorkerGlobalScope,
- canvas: web_sys::OffscreenCanvas) {
+ ecanvas: JsValue) {
client_logger::init_logger();
- info!("hello from graphics wasm {:?}", canvas);
+ let ecanvas: ECanvas = js_sys::Reflect::get(&ecanvas,
+ &wasm_bindgen::JsValue::from_str("canvas"))
+ .map_err(|e| error!("could not load canvas"))
+ .unwrap().into();
+
+ info!("hello from graphics wasm {:?}", ecanvas);
let handler = wasm_bindgen::closure::Closure::once_into_js(
(|e: web_sys::MessageEvent| {
info!("things are getting wired: {:?}", e.data());
}));
-
worker.set_onmessage(Some(&js_sys::Function::from(handler)));
- entry2();
- info!("killed graphics");
-}
-
-pub fn entry2() {
- // client_logger::init_logger();
- info!("begin running wasm application");
+ let canvas = canvas::Canvas::from_existing(&ecanvas)
+ .map_err(|e| error!("{}", e))
+ .unwrap();
- run_application()
+ info!("graphics terminated");
}
diff --git a/WebInterface/wasm/webhogg/src/page.rs b/WebInterface/wasm/webhogg/src/page.rs
deleted file mode 100644
index cb9ee3f..0000000
--- a/WebInterface/wasm/webhogg/src/page.rs
+++ /dev/null
@@ -1,26 +0,0 @@
-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)
- }
-}