ableos/shadeable/src/engine_internals.rs

58 lines
1.7 KiB
Rust
Raw Normal View History

2022-01-22 00:01:16 -06:00
use crate::pixel_format::{get_a, get_b, get_g, get_r, set_a, set_b, set_g, set_r};
use crate::pixel_format::{rgba_div, Rgba64};
2022-01-22 00:01:16 -06:00
use log::{debug, info};
2022-01-16 19:42:11 -06:00
use rhai::Engine;
2022-04-11 17:23:11 -05:00
use rhai::INT;
2022-01-22 00:01:16 -06:00
2022-01-16 19:42:11 -06:00
pub fn engine_startup() -> Engine {
let mut engine = Engine::new();
engine.on_print(|x| info!("PRINTED FROM SHADER: {}", x));
2022-01-22 00:01:16 -06:00
// Any function or closure that takes a '&str', an 'Option<&str>' and a 'Position' argument
// can be used to override 'debug'.
engine.on_debug(|x, src, pos| {
let src = src.unwrap_or("unknown");
debug!("SHADER DEBUG of {} at {:?}: {}", src, pos, x);
2022-01-22 00:01:16 -06:00
});
2022-01-16 19:42:11 -06:00
engine
2022-01-22 00:01:16 -06:00
.register_type::<Rgba64>()
.register_fn("RGBA", get_rgba_rhai)
.register_fn("RGBA", get_rgba_from_int)
.register_fn("RGBA", get_rgba_from_int)
2022-04-11 17:23:11 -05:00
.register_fn("/", rgba_div);
// .register_fn("*", rgba_mult)
// .register_fn("+", rgba_add)
// .register_fn("-", rgba_sub)
2022-01-16 19:42:11 -06:00
// engine.register_global_module(RandomPackage::new().as_shared_module());
engine
}
2022-04-11 17:23:11 -05:00
2022-01-22 00:01:16 -06:00
pub fn get_rgba_rhai(r: INT, g: INT, b: INT, a: INT) -> Rgba64 {
let mut pixel = 0;
pixel |= set_r(pixel, r as u8);
pixel |= set_g(pixel, g as u8);
pixel |= set_b(pixel, b as u8);
pixel |= set_a(pixel, a as u8);
pixel
}
pub fn get_rgba_from_int(pixel: INT) -> Rgba64 {
let mut pixel = pixel as u64;
let r = get_r(pixel.try_into().unwrap());
let g = get_g(pixel.try_into().unwrap());
let b = get_b(pixel.try_into().unwrap());
let a = get_a(pixel.try_into().unwrap());
pixel |= set_r(pixel, r as u8);
pixel |= set_g(pixel, g as u8);
pixel |= set_b(pixel, b as u8);
pixel |= set_a(pixel, a as u8);
pixel
}