Lua scripting

This commit is contained in:
Dane Johnson 2022-04-09 12:42:18 -05:00
parent 299daf4e87
commit 44deaa71e1
5 changed files with 62 additions and 0 deletions

22
Cargo.lock generated
View File

@ -257,6 +257,7 @@ version = "0.1.0"
dependencies = [ dependencies = [
"glium", "glium",
"gltf", "gltf",
"hlua",
"nalgebra-glm", "nalgebra-glm",
] ]
@ -504,6 +505,16 @@ dependencies = [
"gl_generator", "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]] [[package]]
name = "ident_case" name = "ident_case"
version = "1.0.1" version = "1.0.1"
@ -618,6 +629,17 @@ dependencies = [
"cfg-if 1.0.0", "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]] [[package]]
name = "malloc_buf" name = "malloc_buf"
version = "0.0.6" version = "0.0.6"

View File

@ -8,4 +8,5 @@ edition = "2021"
glium = "0.31.0" glium = "0.31.0"
nalgebra-glm = "0.16.0" nalgebra-glm = "0.16.0"
gltf = { version = "1.0.0", features = ["utils"] } gltf = { version = "1.0.0", features = ["utils"] }
hlua = "0.4.1"

1
main.lua Normal file
View File

@ -0,0 +1 @@
print("Hello from a lua script!")

View File

@ -1,5 +1,6 @@
mod device; mod device;
mod model; mod model;
mod scripting;
#[macro_use] #[macro_use]
extern crate glium; extern crate glium;
@ -7,6 +8,8 @@ extern crate nalgebra_glm as glm;
use model::Model; use model::Model;
use scripting::{ ScriptLang, Lua };
fn main() { fn main() {
use glium::{glutin, Surface}; use glium::{glutin, Surface};
@ -29,6 +32,9 @@ fn main() {
let model = Model::new(&display, "Box.glb"); let model = Model::new(&display, "Box.glb");
let mut script_lang = Lua::new();
script_lang.init();
events_loop.run(move |ev, _, control_flow| { events_loop.run(move |ev, _, control_flow| {
if device_manager.is_pressed(&device::Key::W) { if device_manager.is_pressed(&device::Key::W) {
offset += 0.001; offset += 0.001;

32
src/scripting.rs Normal file
View File

@ -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!();
}
}