More scripting stuff

This commit is contained in:
Dane Johnson 2022-04-11 16:14:09 -05:00
parent b5e4ce3ddb
commit 860654d4ba
5 changed files with 158 additions and 38 deletions

87
Cargo.lock generated
View File

@ -23,6 +23,15 @@ version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "aae1277d39aeec15cb388266ecc24b11c80469deae6067e17a1a7aa9e5c1f234" 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]] [[package]]
name = "android_glue" name = "android_glue"
version = "0.2.3" version = "0.2.3"
@ -351,6 +360,12 @@ version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9ea835d29036a4087793836fa931b08837ad5e957da9e23886b29586fb9b6650" checksum = "9ea835d29036a4087793836fa931b08837ad5e957da9e23886b29586fb9b6650"
[[package]]
name = "either"
version = "1.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e78d4f1cc4ae33bbfc157ed5d5a5ef3bc29227303d595861deb238fcec4e9457"
[[package]] [[package]]
name = "fnv" name = "fnv"
version = "1.0.7" version = "1.0.7"
@ -554,6 +569,15 @@ dependencies = [
"web-sys", "web-sys",
] ]
[[package]]
name = "itertools"
version = "0.10.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a9a9d19fa1e79b6215ff29b9d6880b706147f16e9b1dbb1e4e5947b5b02bc5e3"
dependencies = [
"either",
]
[[package]] [[package]]
name = "itoa" name = "itoa"
version = "1.0.1" version = "1.0.1"
@ -726,12 +750,28 @@ checksum = "29e2194305aa8301d5da9c1c98640047f4219f6b95c190f6860338ab9872b686"
dependencies = [ dependencies = [
"bstr", "bstr",
"cc", "cc",
"mlua_derive",
"num-traits", "num-traits",
"once_cell", "once_cell",
"pkg-config", "pkg-config",
"rustc-hash", "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]] [[package]]
name = "nalgebra" name = "nalgebra"
version = "0.30.1" version = "0.30.1"
@ -1024,6 +1064,30 @@ dependencies = [
"toml", "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]] [[package]]
name = "proc-macro2" name = "proc-macro2"
version = "1.0.37" version = "1.0.37"
@ -1066,6 +1130,23 @@ dependencies = [
"bitflags", "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]] [[package]]
name = "rustc-demangle" name = "rustc-demangle"
version = "0.1.21" version = "0.1.21"
@ -1245,6 +1326,12 @@ version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3" checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3"
[[package]]
name = "version_check"
version = "0.9.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"
[[package]] [[package]]
name = "wasi" name = "wasi"
version = "0.11.0+wasi-snapshot-preview1" version = "0.11.0+wasi-snapshot-preview1"

View File

@ -8,4 +8,4 @@ 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"] }
mlua = { version = "0.7.4", features = ["lua54"] } mlua = { version = "0.7.4", features = ["lua54", "macros"] }

View File

@ -1 +1,9 @@
print("Hello from a lua script!") print("Hello from a lua script!")
function init()
print("Hello from \"init\"")
couch.debug("init")
end
function update(delta)
end

View File

@ -35,40 +35,9 @@ fn main() {
let mut script_lang = Lua::new(); let mut script_lang = Lua::new();
script_lang.init(); script_lang.init();
events_loop.run(move |ev, _, control_flow| { let mut last_draw = std::time::Instant::now();
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::<f32>(4.0/3.0, 45.0_f32.to_radians(), 0.2, 100.0);
let mvp = glm::translate::<f32>(&mvp, &glm::vec3(0.0, 0.0, -10.0 + offset));
let mvp = glm::rotate::<f32>(&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(); events_loop.run(move |ev, _, control_flow| {
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, &params);
target.finish().unwrap();
let next_frame_time = std::time::Instant::now() + let next_frame_time = std::time::Instant::now() +
std::time::Duration::from_nanos(16_666_667); std::time::Duration::from_nanos(16_666_667);
*control_flow = glutin::event_loop::ControlFlow::WaitUntil(next_frame_time); *control_flow = glutin::event_loop::ControlFlow::WaitUntil(next_frame_time);
@ -81,6 +50,45 @@ fn main() {
glutin::event::Event::DeviceEvent { event, .. } => { glutin::event::Event::DeviceEvent { event, .. } => {
device_manager.handle(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::<f32>(4.0/3.0, 45.0_f32.to_radians(), 0.2, 100.0);
let mvp = glm::translate::<f32>(&mvp, &glm::vec3(0.0, 0.0, -10.0 + offset));
let mvp = glm::rotate::<f32>(&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, &params);
target.finish().unwrap();
}
_ => (), _ => (),
}; };
}); });

View File

@ -1,6 +1,7 @@
extern crate mlua; extern crate mlua;
use mlua::Lua as LuaContext; use mlua::Lua as LuaContext;
use mlua::chunk;
pub trait ScriptLang { pub trait ScriptLang {
fn init(&mut self); fn init(&mut self);
@ -13,7 +14,17 @@ pub struct Lua {
impl Lua { impl Lua {
pub fn new() -> Self { pub fn new() -> Self {
// Create a lua context, load std libraries
let ctx = LuaContext::new(); 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 { Lua {
ctx, ctx,
} }
@ -22,12 +33,18 @@ impl Lua {
impl ScriptLang for Lua { impl ScriptLang for Lua {
fn init(&mut self) { fn init(&mut self) {
let path = std::path::Path::new("main.lua"); self.ctx.load(&"init()").exec().ok();
let buf = std::fs::read(&path).expect("Could not find main.lua");
self.ctx.load(&buf).exec();
} }
fn update(&mut self, delta: f32) { 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(())
}