diff --git a/CHANGELOG.md b/CHANGELOG.md index 3b7abf5..193d550 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ ## [Unreleased] - ReleaseDate +### Added + +- Implement GetDrawColor + ### Fixed - Avoid redundant update after initial download diff --git a/src/api/rendering.rs b/src/api/rendering.rs index df692bf..8621eae 100644 --- a/src/api/rendering.rs +++ b/src/api/rendering.rs @@ -34,6 +34,7 @@ pub fn register_globals(lua: &Lua) -> LuaResult<()> { // thousands of times per frame. C functions are used to get raw access to // the lua stack without the overhead. unsafe { globals.set("SetDrawColor", lua.create_c_function(set_draw_color)?)? }; + unsafe { globals.set("GetDrawColor", lua.create_c_function(get_draw_color)?)? }; unsafe { globals.set("SetViewport", lua.create_c_function(set_viewport)?)? }; unsafe { globals.set("SetDrawLayer", lua.create_c_function(set_draw_layer)?)?; @@ -160,6 +161,20 @@ unsafe extern "C-unwind" fn set_draw_color(state: *mut ffi::lua_State) -> c_int 0 } +unsafe extern "C-unwind" fn get_draw_color(state: *mut ffi::lua_State) -> c_int { + //profiling::scope!("get_draw_color"); + let lua_instance = unsafe { Lua::get_or_init_from_ptr(state) }; + let ctx = lua_instance.app_data_ref::<&'static Context>().unwrap(); + + let color: [f32; 4] = ctx.layers().get_draw_color().into(); + unsafe { ffi::lua_pushnumber(state, color[0] as f64) }; + unsafe { ffi::lua_pushnumber(state, color[1] as f64) }; + unsafe { ffi::lua_pushnumber(state, color[2] as f64) }; + unsafe { ffi::lua_pushnumber(state, color[3] as f64) }; + + 4 +} + unsafe extern "C-unwind" fn set_viewport(state: *mut ffi::lua_State) -> c_int { //profiling::scope!("set_viewport"); let lua_instance = unsafe { Lua::get_or_init_from_ptr(state) }; diff --git a/src/color.rs b/src/color.rs index a79dec8..dedc5ce 100644 --- a/src/color.rs +++ b/src/color.rs @@ -67,8 +67,20 @@ impl From for image::Rgba { } } +impl From for [f32; 4] { + fn from(value: Srgba) -> Self { + value.0.map(u8_to_f32) + } +} + /// f32 [0.0, 1.0] -> u8 [0, 255] #[inline(always)] const fn f32_to_u8(c: f32) -> u8 { ((c * 255.0) + 0.5) as u8 } + +/// u8 [0, 255] -> f32 [0.0, 1.0] +#[inline(always)] +const fn u8_to_f32(c: u8) -> f32 { + c as f32 / 255.0 +}