diff --git a/Cargo.lock b/Cargo.lock index bb61102..1bbd6de 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -23,6 +23,15 @@ version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "aae1277d39aeec15cb388266ecc24b11c80469deae6067e17a1a7aa9e5c1f234" +[[package]] +name = "aho-corasick" +version = "0.7.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e37cfd5e7657ada45f742d6e99ca5788580b5c529dc78faf11ece6dc702656f" +dependencies = [ + "memchr", +] + [[package]] name = "android_glue" version = "0.2.3" @@ -351,6 +360,12 @@ version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9ea835d29036a4087793836fa931b08837ad5e957da9e23886b29586fb9b6650" +[[package]] +name = "either" +version = "1.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e78d4f1cc4ae33bbfc157ed5d5a5ef3bc29227303d595861deb238fcec4e9457" + [[package]] name = "fnv" version = "1.0.7" @@ -554,6 +569,15 @@ dependencies = [ "web-sys", ] +[[package]] +name = "itertools" +version = "0.10.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a9a9d19fa1e79b6215ff29b9d6880b706147f16e9b1dbb1e4e5947b5b02bc5e3" +dependencies = [ + "either", +] + [[package]] name = "itoa" version = "1.0.1" @@ -726,12 +750,28 @@ checksum = "29e2194305aa8301d5da9c1c98640047f4219f6b95c190f6860338ab9872b686" dependencies = [ "bstr", "cc", + "mlua_derive", "num-traits", "once_cell", "pkg-config", "rustc-hash", ] +[[package]] +name = "mlua_derive" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1713774a29db53a48932596dc943439dd54eb56a9efaace716719cc10fa82d5b" +dependencies = [ + "itertools", + "once_cell", + "proc-macro-error", + "proc-macro2", + "quote", + "regex", + "syn", +] + [[package]] name = "nalgebra" version = "0.30.1" @@ -1024,6 +1064,30 @@ dependencies = [ "toml", ] +[[package]] +name = "proc-macro-error" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c" +dependencies = [ + "proc-macro-error-attr", + "proc-macro2", + "quote", + "syn", + "version_check", +] + +[[package]] +name = "proc-macro-error-attr" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869" +dependencies = [ + "proc-macro2", + "quote", + "version_check", +] + [[package]] name = "proc-macro2" version = "1.0.37" @@ -1066,6 +1130,23 @@ dependencies = [ "bitflags", ] +[[package]] +name = "regex" +version = "1.5.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1a11647b6b25ff05a515cb92c365cec08801e83423a235b51e231e1808747286" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.6.25" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f497285884f3fcff424ffc933e56d7cbca511def0c9831a7f9b5f6153e3cc89b" + [[package]] name = "rustc-demangle" version = "0.1.21" @@ -1245,6 +1326,12 @@ version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3" +[[package]] +name = "version_check" +version = "0.9.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" + [[package]] name = "wasi" version = "0.11.0+wasi-snapshot-preview1" diff --git a/Cargo.toml b/Cargo.toml index a543e02..d11c772 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,4 +8,4 @@ edition = "2021" glium = "0.31.0" nalgebra-glm = "0.16.0" gltf = { version = "1.0.0", features = ["utils"] } -mlua = { version = "0.7.4", features = ["lua54"] } \ No newline at end of file +mlua = { version = "0.7.4", features = ["lua54", "macros"] } \ No newline at end of file diff --git a/main.lua b/main.lua index ee35f4e..b56179e 100644 --- a/main.lua +++ b/main.lua @@ -1 +1,9 @@ print("Hello from a lua script!") + +function init() + print("Hello from \"init\"") + couch.debug("init") +end + +function update(delta) +end diff --git a/src/main.rs b/src/main.rs index 271ef27..f10b025 100644 --- a/src/main.rs +++ b/src/main.rs @@ -35,40 +35,9 @@ fn main() { let mut script_lang = Lua::new(); script_lang.init(); - events_loop.run(move |ev, _, control_flow| { - if device_manager.is_pressed(&device::Key::W) { - offset += 0.001; - } else if device_manager.is_pressed(&device::Key::S) { - offset -= 0.001; - } - if device_manager.is_pressed(&device::Key::A) { - rotate += 0.01; - } else if device_manager.is_pressed(&device::Key::D) { - rotate -= 0.01; - } - - let mvp = glm::perspective::(4.0/3.0, 45.0_f32.to_radians(), 0.2, 100.0); - let mvp = glm::translate::(&mvp, &glm::vec3(0.0, 0.0, -10.0 + offset)); - let mvp = glm::rotate::(&mvp, 15.0_f32.to_radians() + rotate, &glm::vec3(1.0, 0.0, 0.0)); - let uniforms = uniform! { - MVP: *mvp.as_ref(), - }; + let mut last_draw = std::time::Instant::now(); - let mut target = display.draw(); - let params = glium::DrawParameters { - depth: glium::Depth { - test: glium::DepthTest::IfLess, - write: true, - .. Default::default() - }, - backface_culling: glium::draw_parameters::BackfaceCullingMode::CullClockwise, - .. Default::default() - }; - target.clear_color_and_depth((0.0, 0.0, 0.0, 1.0), 1.0); - model.draw(&mut target, &program, &uniforms, ¶ms); - target.finish().unwrap(); - - + events_loop.run(move |ev, _, control_flow| { let next_frame_time = std::time::Instant::now() + std::time::Duration::from_nanos(16_666_667); *control_flow = glutin::event_loop::ControlFlow::WaitUntil(next_frame_time); @@ -81,6 +50,45 @@ fn main() { glutin::event::Event::DeviceEvent { event, .. } => { device_manager.handle(event); }, + glutin::event::Event::MainEventsCleared => { + let now = std::time::Instant::now(); + let delta = (now - last_draw).as_secs_f32(); + last_draw = now; + + if device_manager.is_pressed(&device::Key::W) { + offset += 1.0 * delta; + } else if device_manager.is_pressed(&device::Key::S) { + offset -= 1.0 * delta; + } + if device_manager.is_pressed(&device::Key::A) { + rotate += 1.0 * delta; + } else if device_manager.is_pressed(&device::Key::D) { + rotate -= 1.0 * delta; + } + + script_lang.update(delta); + + let mvp = glm::perspective::(4.0/3.0, 45.0_f32.to_radians(), 0.2, 100.0); + let mvp = glm::translate::(&mvp, &glm::vec3(0.0, 0.0, -10.0 + offset)); + let mvp = glm::rotate::(&mvp, 15.0_f32.to_radians() + rotate, &glm::vec3(1.0, 0.0, 0.0)); + let uniforms = uniform! { + MVP: *mvp.as_ref(), + }; + + let mut target = display.draw(); + let params = glium::DrawParameters { + depth: glium::Depth { + test: glium::DepthTest::IfLess, + write: true, + .. Default::default() + }, + backface_culling: glium::draw_parameters::BackfaceCullingMode::CullClockwise, + .. Default::default() + }; + target.clear_color_and_depth((0.0, 0.0, 0.0, 1.0), 1.0); + model.draw(&mut target, &program, &uniforms, ¶ms); + target.finish().unwrap(); + } _ => (), }; }); diff --git a/src/scripting.rs b/src/scripting.rs index 5e08cd5..a1ef9f8 100644 --- a/src/scripting.rs +++ b/src/scripting.rs @@ -1,6 +1,7 @@ extern crate mlua; use mlua::Lua as LuaContext; +use mlua::chunk; pub trait ScriptLang { fn init(&mut self); @@ -13,7 +14,17 @@ pub struct Lua { impl Lua { pub fn new() -> Self { + // Create a lua context, load std libraries let ctx = LuaContext::new(); + // Create the ``couch'' api + let couch = ctx.create_table().unwrap(); + couch.set("debug", ctx.create_function(debug).unwrap()).unwrap(); + // Hook in to globals + ctx.globals().set("couch", couch).unwrap(); + let path = std::path::Path::new("main.lua"); + let buf = std::fs::read(&path).expect("Could not find main.lua"); + ctx.load(&buf).exec().unwrap(); + Lua { ctx, } @@ -22,12 +33,18 @@ impl Lua { impl ScriptLang for Lua { fn init(&mut self) { - let path = std::path::Path::new("main.lua"); - let buf = std::fs::read(&path).expect("Could not find main.lua"); - self.ctx.load(&buf).exec(); + self.ctx.load(&"init()").exec().ok(); } fn update(&mut self, delta: f32) { - todo!(); + self.ctx.load(chunk! { + update($delta) + }).exec().ok(); } } + +fn debug(_: &LuaContext, msg: String) -> Result<(), mlua::Error> { + println!("Couch Debug Message: {}", msg); + Ok(()) +} +