From 642ced45f8440a534ee748a232b40353a02c65b5 Mon Sep 17 00:00:00 2001 From: John Doty Date: Sat, 19 Aug 2023 10:45:48 -0700 Subject: [PATCH] [oden] Allow a "functional style" init/update/draw --- src/script.rs | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/script.rs b/src/script.rs index 5d9b0c2e..56086f5d 100644 --- a/src/script.rs +++ b/src/script.rs @@ -47,6 +47,8 @@ pub struct ScriptContext { update: Value, draw: Value, + state: Value, + gfx: graphics::GraphicsAPI, gfx_receive: Receiver, @@ -87,6 +89,8 @@ impl ScriptContext { .get_export(&context, "draw") .expect("Unable to fetch draw"); + let state = context.undefined(); + ScriptContext { context, @@ -94,6 +98,8 @@ impl ScriptContext { update, draw, + state, + gfx, gfx_receive, @@ -109,7 +115,8 @@ impl ScriptContext { pub fn init(&mut self) { let _span = span!("script init"); - self.init + self.state = self + .init .call(&self.context, &[]) .expect("Exception in init"); } @@ -140,8 +147,10 @@ impl ScriptContext { // Now run the update function. { let _span = span!("javascript update"); - self.update - .call(&self.context, &[]) + let old_state = &self.state; + self.state = self + .update + .call(&self.context, &[old_state]) .expect("Exception in update"); } } @@ -150,7 +159,7 @@ impl ScriptContext { let _span = span!("script render"); self.draw - .call(&self.context, &[]) + .call(&self.context, &[&self.state]) .expect("Exception in draw"); self.gfx.end_frame();