diff --git a/crates/mun_runtime/examples/buoyancy.rs b/crates/mun_runtime/examples/buoyancy.rs new file mode 100644 index 000000000..9472fce66 --- /dev/null +++ b/crates/mun_runtime/examples/buoyancy.rs @@ -0,0 +1,40 @@ +use mun_runtime::{invoke_fn, RetryResultExt, RuntimeBuilder, StructRef}; +use std::{env, time}; + +extern "C" fn log_f32(value: f32) { + println!("{}", value); +} + +// How to run? +// 1. On the CLI, navigate to the `crates/mun_runtime/examples` directory. +// 2. Run the compiler daemon from the CLI: `/path/to/mun build resources/buoyancy.mun --watch` +// 3. Run the application from the CLI: cargo run --example buoyancy -- buoyancy.munlib +fn main() { + let lib_dir = env::args().nth(1).expect("Expected path to a Mun library."); + + let runtime = RuntimeBuilder::new(lib_dir) + .insert_fn("log_f32", log_f32 as extern "C" fn(f32)) + .spawn() + .expect("Failed to spawn Runtime"); + + let ctx: StructRef = invoke_fn!(runtime, "new_sim").wait(); + + let mut previous = time::Instant::now(); + const FRAME_TIME: time::Duration = time::Duration::from_millis(40); + loop { + let now = time::Instant::now(); + let elapsed = now.duration_since(previous); + + let elapsed_secs = if elapsed < FRAME_TIME { + std::thread::sleep(FRAME_TIME - elapsed); + FRAME_TIME.as_secs_f32() + } else { + elapsed.as_secs_f32() + }; + + let _: () = invoke_fn!(runtime, "sim_update", ctx.clone(), elapsed_secs).wait(); + previous = now; + + runtime.borrow_mut().update(); + } +} diff --git a/crates/mun_runtime/examples/hot_reloading.rs b/crates/mun_runtime/examples/fibonacci.rs similarity index 88% rename from crates/mun_runtime/examples/hot_reloading.rs rename to crates/mun_runtime/examples/fibonacci.rs index 8090e33f3..afa4ab493 100644 --- a/crates/mun_runtime/examples/hot_reloading.rs +++ b/crates/mun_runtime/examples/fibonacci.rs @@ -4,7 +4,7 @@ use std::env; // How to run? // 1. On the CLI, navigate to the `crates/mun_runtime/examples` directory. // 2. Run the compiler daemon from the CLI: `/path/to/mun build resources/fibonacci.mun --watch` -// 3. Run the application from the CLI: cargo run --example hot_reloading -- fibonacci.dll +// 3. Run the application from the CLI: cargo run --example fibonacci -- fibonacci.munlib fn main() { let lib_dir = env::args().nth(1).expect("Expected path to a Mun library."); println!("lib: {}", lib_dir); diff --git a/crates/mun_runtime/examples/resources/buoyancy.mun b/crates/mun_runtime/examples/resources/buoyancy.mun new file mode 100644 index 000000000..50dab368d --- /dev/null +++ b/crates/mun_runtime/examples/resources/buoyancy.mun @@ -0,0 +1,90 @@ +extern fn log_f32(value: f32); + +struct SimContext { + sphere: Sphere, + water: Water, + gravity: f32, +} + +struct Sphere { + radius: f32, + mass: f32, // density: f32, + height: f32, + velocity: f32, +} + +struct Water { + density: f32, +} + +pub fn new_sim() -> SimContext { + SimContext { + sphere: new_sphere(), + water: new_water(), + gravity: 9.81, + } +} + +fn new_sphere() -> Sphere { + let radius = 1.0; + let density = 250.0; + + let volume = calc_sphere_volume(radius); + let mass = density * volume; + + Sphere { + radius, + mass, + height: 1.0, + velocity: 0.0, + } +} + +fn new_water() -> Water { + Water { + density: 1000.0, + } +} + +fn calc_submerged_ratio(s: Sphere) -> f32 { + let bottom = s.height - s.radius; + if bottom >= 0.0 { + 0.0 + } else if bottom <= -1.0 { + 1.0 + } else { + -bottom + } +} + +fn calc_sphere_volume(radius: f32) -> f32 { + let pi = 3.1415926535897; + let r = radius; + + 3.0/4.0 * pi * r * r * r +} + +fn calc_buoyancy_force(s: Sphere, w: Water, gravity: f32, submerged_ratio: f32) -> f32 { + let volume = calc_sphere_volume(s.radius); + volume * submerged_ratio * w.density * gravity +} + +pub fn sim_update(ctx: SimContext, elapsed_secs: f32) { + let submerged_ratio = calc_submerged_ratio(ctx.sphere); + if submerged_ratio > 0.0 { + let buoyancy_force = calc_buoyancy_force( + ctx.sphere, + ctx.water, + ctx.gravity, + submerged_ratio + ); + let buoyancy_acc = buoyancy_force / ctx.sphere.mass; + ctx.sphere.velocity += buoyancy_acc * elapsed_secs; + } + + ctx.sphere.velocity -= ctx.gravity * elapsed_secs; + + ctx.sphere.height += ctx.sphere.velocity * elapsed_secs; + + log_f32(ctx.sphere.height); +} diff --git a/crates/mun_runtime/examples/resources/fibonacci.mun b/crates/mun_runtime/examples/resources/fibonacci.mun index 9d9a84c46..c06694edf 100644 --- a/crates/mun_runtime/examples/resources/fibonacci.mun +++ b/crates/mun_runtime/examples/resources/fibonacci.mun @@ -1,8 +1,8 @@ -fn nth() -> int { +pub fn nth() -> i64 { 3 } -fn fibonacci(n:int) -> int { +pub fn fibonacci(n:i64) -> i64 { if n <= 1 { n } else {