#[macro_use]
extern crate hlua;
fn main() {
let mut lua = hlua::Lua::new();
lua.openlibs();
{
let mut sound_namespace = lua.empty_array("Sound");
sound_namespace.set("new", hlua::function0(|| Sound::new()));
}
lua.execute::<()>(
r#"
s = Sound.new();
s:play();
print("hello world from within lua!");
print("is the sound playing:", s:is_playing());
s:stop();
print("is the sound playing:", s:is_playing());
"#,
)
.unwrap();
}
struct Sound {
playing: bool,
}
implement_lua_push!(Sound, |mut metatable| {
let mut index = metatable.empty_array("__index");
index.set("play", hlua::function1(|snd: &mut Sound| snd.play()));
index.set("stop", hlua::function1(|snd: &mut Sound| snd.stop()));
index.set(
"is_playing",
hlua::function1(|snd: &Sound| snd.is_playing()),
);
});
implement_lua_read!(Sound);
impl Sound {
pub fn new() -> Sound {
Sound { playing: false }
}
pub fn play(&mut self) {
println!("playing");
self.playing = true;
}
pub fn stop(&mut self) {
println!("stopping");
self.playing = false;
}
pub fn is_playing(&self) -> bool {
self.playing
}
}
impl Drop for Sound {
fn drop(&mut self) {
println!("`Sound` object destroyed");
}
}