From 6abe5b467e3efb706f8813b32ca5ec94d2bf57f2 Mon Sep 17 00:00:00 2001 From: Jarrod Doyle Date: Fri, 8 Mar 2024 11:27:53 +0000 Subject: [PATCH] Move script to file --- scripts/frame_counter.rn | 14 ++++++++++++++ src/main.rs | 10 ++++++---- 2 files changed, 20 insertions(+), 4 deletions(-) create mode 100644 scripts/frame_counter.rn diff --git a/scripts/frame_counter.rn b/scripts/frame_counter.rn new file mode 100644 index 0000000..95d9c1e --- /dev/null +++ b/scripts/frame_counter.rn @@ -0,0 +1,14 @@ +pub struct FrameCounter { + frame, +} + +impl FrameCounter { + pub fn new() { + return FrameCounter {frame: 0}; + } + + pub fn update(self) { + self.frame += 1; + println(`Frame: ${self.frame}`); + } +} diff --git a/src/main.rs b/src/main.rs index a93ae56..2451fbe 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,6 +7,7 @@ use anyhow::Result; use gfx::Context; use input::Input; use rune::{ + runtime::Object, termcolor::{ColorChoice, StandardStream}, Diagnostics, Source, Sources, Vm, }; @@ -31,9 +32,11 @@ pub fn main() -> Result<()> { pub fn run(event_loop: EventLoop<()>, mut context: Context) -> Result<()> { // !HACK: Temporrary scripting engine setup + let source_dir = format!("{}/scripts", env!("CARGO_MANIFEST_DIR")); let rune_context = rune::Context::with_default_modules()?; let runtime = Arc::new(rune_context.runtime()?); let mut sources = Sources::new(); + sources.insert(Source::from_path(format!("{source_dir}/frame_counter.rn"))?)?; sources.insert(Source::memory("pub fn add(a, b) { a + b }")?)?; let mut diagnostics = Diagnostics::new(); let result = rune::prepare(&mut sources) @@ -45,7 +48,7 @@ pub fn run(event_loop: EventLoop<()>, mut context: Context) -> Result<()> { diagnostics.emit(&mut writer, &sources)?; } let mut vm = Vm::new(runtime, Arc::new(result?)); - let mut frame_count = 0; + let frame_counter = vm.call(["FrameCounter", "new"], ())?; let mut input = Input::new(); @@ -70,9 +73,8 @@ pub fn run(event_loop: EventLoop<()>, mut context: Context) -> Result<()> { elwt.exit(); } - let output = vm.call(["add"], (frame_count, 1i64)).unwrap(); - frame_count = rune::from_value(output).unwrap(); - log::info!("Frame: {frame_count}"); + vm.call(["FrameCounter", "update"], (&frame_counter,)) + .unwrap(); })?; Ok(())