From 44deaa71e1f38dc8232c6f962b8c1b52512ff74e Mon Sep 17 00:00:00 2001 From: Dane Johnson Date: Sat, 9 Apr 2022 12:42:18 -0500 Subject: [PATCH] Lua scripting --- Cargo.lock | 22 ++++++++++++++++++++++ Cargo.toml | 1 + main.lua | 1 + src/main.rs | 6 ++++++ src/scripting.rs | 32 ++++++++++++++++++++++++++++++++ 5 files changed, 62 insertions(+) create mode 100644 main.lua create mode 100644 src/scripting.rs diff --git a/Cargo.lock b/Cargo.lock index c2e883e..b3a4c8d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -257,6 +257,7 @@ version = "0.1.0" dependencies = [ "glium", "gltf", + "hlua", "nalgebra-glm", ] @@ -504,6 +505,16 @@ dependencies = [ "gl_generator", ] +[[package]] +name = "hlua" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed9db71fff2e55b83d24bbbdd9ad13f0d1ff79bc265f544370f39ee0825d54e4" +dependencies = [ + "libc", + "lua52-sys", +] + [[package]] name = "ident_case" version = "1.0.1" @@ -618,6 +629,17 @@ dependencies = [ "cfg-if 1.0.0", ] +[[package]] +name = "lua52-sys" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d451db153c94e455dc817d388f9674f6232425c28db3509e90251c55b8df2f94" +dependencies = [ + "cc", + "libc", + "pkg-config", +] + [[package]] name = "malloc_buf" version = "0.0.6" diff --git a/Cargo.toml b/Cargo.toml index 8a913dd..2d9eacd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,4 +8,5 @@ edition = "2021" glium = "0.31.0" nalgebra-glm = "0.16.0" gltf = { version = "1.0.0", features = ["utils"] } +hlua = "0.4.1" diff --git a/main.lua b/main.lua new file mode 100644 index 0000000..ee35f4e --- /dev/null +++ b/main.lua @@ -0,0 +1 @@ +print("Hello from a lua script!") diff --git a/src/main.rs b/src/main.rs index e0be298..271ef27 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,6 @@ mod device; mod model; +mod scripting; #[macro_use] extern crate glium; @@ -7,6 +8,8 @@ extern crate nalgebra_glm as glm; use model::Model; +use scripting::{ ScriptLang, Lua }; + fn main() { use glium::{glutin, Surface}; @@ -29,6 +32,9 @@ fn main() { let model = Model::new(&display, "Box.glb"); + 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; diff --git a/src/scripting.rs b/src/scripting.rs new file mode 100644 index 0000000..a9ab386 --- /dev/null +++ b/src/scripting.rs @@ -0,0 +1,32 @@ +extern crate hlua; + +use hlua::Lua as LuaContext; + +pub trait ScriptLang { + fn init(&mut self); + fn update(&mut self, delta: f32); +} + +pub struct Lua { + ctx: LuaContext<'static>, +} + +impl Lua { + pub fn new() -> Self { + Lua { + ctx: LuaContext::new(), + } + } +} + +impl ScriptLang for Lua { + fn init(&mut self) { + self.ctx.openlibs(); + //self.ctx.execute::<()>("print(\"Hello!\")").unwrap(); + self.ctx.execute_from_reader::<(), std::fs::File>(std::fs::File::open(&std::path::Path::new("main.lua")).unwrap()).unwrap(); + } + + fn update(&mut self, delta: f32) { + todo!(); + } +}