[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,
|
||||
draw: Value,
|
||||
|
||||
state: Value,
|
||||
|
||||
gfx: graphics::GraphicsAPI,
|
||||
gfx_receive: Receiver<graphics::GraphicsCommand>,
|
||||
|
||||
|
|
@ -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();
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue