[oden] Oh boy here we go

This commit is contained in:
John Doty 2023-06-21 21:57:32 -07:00
parent 8218b88820
commit 14f9eb655f
4 changed files with 206 additions and 31 deletions

View file

@ -1,12 +1,17 @@
use oden_js::{Context, Runtime, Value};
use std::sync::mpsc::{channel, Receiver};
mod graphics;
pub mod graphics;
use graphics::GraphicsCommand;
pub struct ScriptContext {
context: Context,
init: Value,
update: Value,
draw: Value,
gfx: graphics::GraphicsAPI,
gfx_receive: Receiver<graphics::GraphicsCommand>,
}
impl ScriptContext {
@ -18,7 +23,10 @@ impl ScriptContext {
context.add_intrinsic_bigdecimal();
context.add_intrinsic_operators();
graphics::GraphicsAPI::define(&context).expect("Graphics module should load without error");
let (gfx_send, gfx_receive) = channel();
let gfx = graphics::GraphicsAPI::define(&context, gfx_send)
.expect("Graphics module should load without error");
let js = include_str!("main.js");
let module = context
@ -37,12 +45,20 @@ impl ScriptContext {
ScriptContext {
context,
init,
update,
draw,
gfx,
gfx_receive,
}
}
// TODO: The script could really be on a background thread you know.
// We would want a bi-directional gate for frames to not let the
// game thread go to fast probably? And to discard whole frames &c.
pub fn init(&self) {
self.init.call(&self.context).expect("Exception in init");
}
@ -53,7 +69,17 @@ impl ScriptContext {
.expect("Exception in update");
}
pub fn render(&self) {
pub fn render(&self) -> Vec<graphics::GraphicsCommand> {
self.draw.call(&self.context).expect("Exception in draw");
self.gfx.end_frame();
let mut commands = Vec::new();
loop {
match self.gfx_receive.recv().unwrap() {
GraphicsCommand::EndFrame => break,
other => commands.push(other),
}
}
commands
}
}