[oden] Allow a "functional style" init/update/draw
This commit is contained in:
parent
7fc786e2e7
commit
642ced45f8
1 changed files with 13 additions and 4 deletions
|
|
@ -47,6 +47,8 @@ pub struct ScriptContext {
|
||||||
update: Value,
|
update: Value,
|
||||||
draw: Value,
|
draw: Value,
|
||||||
|
|
||||||
|
state: Value,
|
||||||
|
|
||||||
gfx: graphics::GraphicsAPI,
|
gfx: graphics::GraphicsAPI,
|
||||||
gfx_receive: Receiver<graphics::GraphicsCommand>,
|
gfx_receive: Receiver<graphics::GraphicsCommand>,
|
||||||
|
|
||||||
|
|
@ -87,6 +89,8 @@ impl ScriptContext {
|
||||||
.get_export(&context, "draw")
|
.get_export(&context, "draw")
|
||||||
.expect("Unable to fetch draw");
|
.expect("Unable to fetch draw");
|
||||||
|
|
||||||
|
let state = context.undefined();
|
||||||
|
|
||||||
ScriptContext {
|
ScriptContext {
|
||||||
context,
|
context,
|
||||||
|
|
||||||
|
|
@ -94,6 +98,8 @@ impl ScriptContext {
|
||||||
update,
|
update,
|
||||||
draw,
|
draw,
|
||||||
|
|
||||||
|
state,
|
||||||
|
|
||||||
gfx,
|
gfx,
|
||||||
gfx_receive,
|
gfx_receive,
|
||||||
|
|
||||||
|
|
@ -109,7 +115,8 @@ impl ScriptContext {
|
||||||
pub fn init(&mut self) {
|
pub fn init(&mut self) {
|
||||||
let _span = span!("script init");
|
let _span = span!("script init");
|
||||||
|
|
||||||
self.init
|
self.state = self
|
||||||
|
.init
|
||||||
.call(&self.context, &[])
|
.call(&self.context, &[])
|
||||||
.expect("Exception in init");
|
.expect("Exception in init");
|
||||||
}
|
}
|
||||||
|
|
@ -140,8 +147,10 @@ impl ScriptContext {
|
||||||
// Now run the update function.
|
// Now run the update function.
|
||||||
{
|
{
|
||||||
let _span = span!("javascript update");
|
let _span = span!("javascript update");
|
||||||
self.update
|
let old_state = &self.state;
|
||||||
.call(&self.context, &[])
|
self.state = self
|
||||||
|
.update
|
||||||
|
.call(&self.context, &[old_state])
|
||||||
.expect("Exception in update");
|
.expect("Exception in update");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -150,7 +159,7 @@ impl ScriptContext {
|
||||||
let _span = span!("script render");
|
let _span = span!("script render");
|
||||||
|
|
||||||
self.draw
|
self.draw
|
||||||
.call(&self.context, &[])
|
.call(&self.context, &[&self.state])
|
||||||
.expect("Exception in draw");
|
.expect("Exception in draw");
|
||||||
self.gfx.end_frame();
|
self.gfx.end_frame();
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue