From 5a445814ca360befca592b939c815a21e41cb981 Mon Sep 17 00:00:00 2001 From: spuds <71292624+bananaturtlesandwich@users.noreply.github.com> Date: Thu, 15 Dec 2022 12:18:38 +0000 Subject: [PATCH 1/5] remove heavy libc dependency --- .gitignore | 2 ++ Cargo.toml | 7 ++++--- src/fixed_collections.rs | 44 ++++++++-------------------------------- src/lib.rs | 4 +--- 4 files changed, 15 insertions(+), 42 deletions(-) diff --git a/.gitignore b/.gitignore index 1b7a99b..12c06d2 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ *.vscode/ *.idea/ +*target/ +*.lock diff --git a/Cargo.toml b/Cargo.toml index 85eefa6..582c00a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,9 +9,10 @@ description = "MicroUI (immediate mode GUI) library in pure rust" repository = "https://github.com/NeoCogi/microui-rs" [lib] -name ="microui" +name = "microui" [dependencies] -libc = "0.2.138" bitflags = "1.3.2" - +sprintf = "0.1" +strtod = "0.0" +simdutf8 = { version = "0.1", default-features = false } diff --git a/src/fixed_collections.rs b/src/fixed_collections.rs index c82b0a6..60af9be 100644 --- a/src/fixed_collections.rs +++ b/src/fixed_collections.rs @@ -225,48 +225,20 @@ pub trait IString { fn append_real(&mut self, fmt: &str, v: f32) { assert!(self.capacity() - self.len() >= 32); - unsafe { - let mut fmt_ascii = [0u8; 32]; - let mut i = 0; - for c in fmt.chars() { - fmt_ascii[i] = c as u8; - i += 1; - } - fmt_ascii[i] = 0; - - let mut number_ascii = [0u8; 32]; - let number_ptr = number_ascii.as_mut_ptr(); - libc::sprintf(number_ptr as *mut libc::c_char, fmt_ascii.as_ptr() as *const libc::c_char, v as f64); - for c in 0..number_ascii.len() { - if number_ascii[c] != 0 { - self.push(number_ascii[c] as char); - } else { - break; - } + for ch in sprintf::sprintf!(fmt, v).unwrap_or_default().chars() { + match ch { + '\0' => break, + _ => self.push(ch), } } } fn append_int(&mut self, fmt: &str, v: i32) { assert!(self.capacity() - self.len() >= 32); - unsafe { - let mut fmt_ascii = [0u8; 32]; - let mut i = 0; - for c in fmt.chars() { - fmt_ascii[i] = c as u8; - i += 1; - } - fmt_ascii[i] = 0; - - let mut number_ascii = [0u8; 32]; - let number_ptr = number_ascii.as_mut_ptr(); - libc::sprintf(number_ptr as *mut libc::c_char, fmt_ascii.as_ptr() as *const libc::c_char, v); - for c in 0..number_ascii.len() { - if number_ascii[c] != 0 { - self.push(number_ascii[c] as char); - } else { - break; - } + for ch in sprintf::sprintf!(fmt, v).unwrap_or_default().chars() { + match ch { + '\0' => break, + _ => self.push(ch), } } } diff --git a/src/lib.rs b/src/lib.rs index 5b971c1..78f4baf 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -51,7 +51,6 @@ // IN THE SOFTWARE. // #![no_std] -use core::ptr; mod fixed_collections; pub use crate::fixed_collections::*; @@ -1344,8 +1343,7 @@ impl Context { i += 1; } ascii[i] = '\0' as u8; - let v = unsafe { libc::strtod(ascii.as_ptr() as *const libc::c_char, ptr::null_mut() as *mut *mut libc::c_char) }; - *value = v as Real; + *value = strtod::strtod(simdutf8::basic::from_utf8(&ascii).unwrap_or_default()).unwrap_or_default() as Real; self.number_edit = None; } else { return ResourceState::ACTIVE; From c93f07a1b5b2b2231dc798dc965568c3d098df31 Mon Sep 17 00:00:00 2001 From: spuds <71292624+bananaturtlesandwich@users.noreply.github.com> Date: Thu, 15 Dec 2022 12:47:45 +0000 Subject: [PATCH 2/5] remove heavy libc dependency for demo --- .gitignore | 1 + demo-sdl2/.idea/.gitignore | 8 ----- demo-sdl2/Cargo.toml | 1 - demo-sdl2/src/main.rs | 68 ++++++++++---------------------------- 4 files changed, 18 insertions(+), 60 deletions(-) delete mode 100644 demo-sdl2/.idea/.gitignore diff --git a/.gitignore b/.gitignore index 12c06d2..fbd6e67 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ *.idea/ *target/ *.lock +SDL2.* \ No newline at end of file diff --git a/demo-sdl2/.idea/.gitignore b/demo-sdl2/.idea/.gitignore deleted file mode 100644 index 13566b8..0000000 --- a/demo-sdl2/.idea/.gitignore +++ /dev/null @@ -1,8 +0,0 @@ -# Default ignored files -/shelf/ -/workspace.xml -# Editor-based HTTP Client requests -/httpRequests/ -# Datasource local storage ignored files -/dataSources/ -/dataSources.local.xml diff --git a/demo-sdl2/Cargo.toml b/demo-sdl2/Cargo.toml index a410578..14e3fe5 100644 --- a/demo-sdl2/Cargo.toml +++ b/demo-sdl2/Cargo.toml @@ -9,7 +9,6 @@ edition = "2021" sdl2 = "0.35.2" microui = {path = ".."} glow = "0.11.2" -libc = "0.2.138" [profile.release] panic = 'abort' diff --git a/demo-sdl2/src/main.rs b/demo-sdl2/src/main.rs index e25e1b0..90ea5c6 100644 --- a/demo-sdl2/src/main.rs +++ b/demo-sdl2/src/main.rs @@ -113,18 +113,10 @@ impl<'a> State<'a> { } fn test_window(&mut self) { - if !self - .ctx - .begin_window_ex( - "Demo Window", - rect(40 as libc::c_int, 40 as libc::c_int, 300 as libc::c_int, 450 as libc::c_int), - WidgetOption::NONE, - ) - .is_none() - { + if !self.ctx.begin_window_ex("Demo Window", rect(40, 40, 300, 450), WidgetOption::NONE).is_none() { let mut win = self.ctx.get_current_container_rect(); - win.w = if win.w > 240 as libc::c_int { win.w } else { 240 as libc::c_int }; - win.h = if win.h > 300 as libc::c_int { win.h } else { 300 as libc::c_int }; + win.w = if win.w > 240 { win.w } else { 240 }; + win.h = if win.h > 300 { win.h } else { 300 }; self.ctx.set_current_container_rect(&win); @@ -232,32 +224,14 @@ impl<'a> State<'a> { self.ctx.layout_begin_column(); self.ctx.layout_row(&[46, -1], 0); self.ctx.label("Red:"); - self.ctx.slider_ex( - &mut self.bg[0], - 0 as libc::c_int as Real, - 255 as libc::c_int as Real, - 0 as libc::c_int as Real, - "%.2", - WidgetOption::ALIGN_CENTER, - ); + self.ctx + .slider_ex(&mut self.bg[0], 0 as Real, 255 as Real, 0 as Real, "%.2", WidgetOption::ALIGN_CENTER); self.ctx.label("Green:"); - self.ctx.slider_ex( - &mut self.bg[1], - 0 as libc::c_int as Real, - 255 as libc::c_int as Real, - 0 as libc::c_int as Real, - "%.2", - WidgetOption::ALIGN_CENTER, - ); + self.ctx + .slider_ex(&mut self.bg[1], 0 as Real, 255 as Real, 0 as Real, "%.2", WidgetOption::ALIGN_CENTER); self.ctx.label("Blue:"); - self.ctx.slider_ex( - &mut self.bg[2], - 0 as libc::c_int as Real, - 255 as libc::c_int as Real, - 0 as libc::c_int as Real, - "%.2", - WidgetOption::ALIGN_CENTER, - ); + self.ctx + .slider_ex(&mut self.bg[2], 0 as Real, 255 as Real, 0 as Real, "%.2", WidgetOption::ALIGN_CENTER); self.ctx.layout_end_column(); let r: Rect = self.ctx.layout_next(); self.ctx.draw_rect(r, color(self.bg[0] as u8, self.bg[1] as u8, self.bg[2] as u8, 255)); @@ -310,32 +284,24 @@ impl<'a> State<'a> { self.ctx.push_id_from_ptr(value); let res = self .ctx - .slider_ex(&mut tmp, low as Real, high as Real, 0 as libc::c_int as Real, "%.2f", WidgetOption::ALIGN_CENTER); - *value = tmp as libc::c_uchar; + .slider_ex(&mut tmp, low as Real, high as Real, 0 as Real, "%.2f", WidgetOption::ALIGN_CENTER); + *value = tmp as u8; self.ctx.pop_id(); return res; } fn style_window(&mut self) { - if !self - .ctx - .begin_window_ex( - "Style Editor", - rect(350 as libc::c_int, 250 as libc::c_int, 300 as libc::c_int, 240 as libc::c_int), - WidgetOption::NONE, - ) - .is_none() - { - let sw: libc::c_int = (self.ctx.get_current_container_body().w as libc::c_double * 0.14f64) as libc::c_int; + if !self.ctx.begin_window_ex("Style Editor", rect(350, 250, 300, 240), WidgetOption::NONE).is_none() { + let sw = (self.ctx.get_current_container_body().w as f64 * 0.14) as i32; self.ctx.layout_row(&[80, sw, sw, sw, sw, -1], 0); let mut i = 0; while self.label_colors[i].label.len() > 0 { self.ctx.label(self.label_colors[i].label); unsafe { let color = self.ctx.style.colors.as_mut_ptr().offset(i as isize); - self.uint8_slider(&mut (*color).r, 0 as libc::c_int, 255 as libc::c_int); - self.uint8_slider(&mut (*color).g, 0 as libc::c_int, 255 as libc::c_int); - self.uint8_slider(&mut (*color).b, 0 as libc::c_int, 255 as libc::c_int); - self.uint8_slider(&mut (*color).a, 0 as libc::c_int, 255 as libc::c_int); + self.uint8_slider(&mut (*color).r, 0, 255); + self.uint8_slider(&mut (*color).g, 0, 255); + self.uint8_slider(&mut (*color).b, 0, 255); + self.uint8_slider(&mut (*color).a, 0, 255); } let next_layout = self.ctx.layout_next(); self.ctx.draw_rect(next_layout, self.ctx.style.colors[i]); From ad09a5fcfc01a57ebb6c2c62f2b806be6df218d0 Mon Sep 17 00:00:00 2001 From: spuds <71292624+bananaturtlesandwich@users.noreply.github.com> Date: Thu, 15 Dec 2022 16:47:48 +0000 Subject: [PATCH 3/5] realised there's a core::str::from_utf8 --- Cargo.toml | 3 +-- src/lib.rs | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 582c00a..ad827e1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,5 +14,4 @@ name = "microui" [dependencies] bitflags = "1.3.2" sprintf = "0.1" -strtod = "0.0" -simdutf8 = { version = "0.1", default-features = false } +fast-float = { version = "0.2", default-features = false } diff --git a/src/lib.rs b/src/lib.rs index 78f4baf..a0ea224 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1343,7 +1343,7 @@ impl Context { i += 1; } ascii[i] = '\0' as u8; - *value = strtod::strtod(simdutf8::basic::from_utf8(&ascii).unwrap_or_default()).unwrap_or_default() as Real; + *value = fast_float::parse(core::str::from_utf8(&ascii).unwrap_or_default()).unwrap_or_default(); self.number_edit = None; } else { return ResourceState::ACTIVE; From 51e5a76d330d2248d55973fa551e995ecaa6e67c Mon Sep 17 00:00:00 2001 From: spuds <71292624+bananaturtlesandwich@users.noreply.github.com> Date: Thu, 15 Dec 2022 17:57:55 +0000 Subject: [PATCH 4/5] keep closer to original impl --- src/fixed_collections.rs | 12 ++++++------ src/lib.rs | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/fixed_collections.rs b/src/fixed_collections.rs index 60af9be..3fa2592 100644 --- a/src/fixed_collections.rs +++ b/src/fixed_collections.rs @@ -225,20 +225,20 @@ pub trait IString { fn append_real(&mut self, fmt: &str, v: f32) { assert!(self.capacity() - self.len() >= 32); - for ch in sprintf::sprintf!(fmt, v).unwrap_or_default().chars() { + for ch in sprintf::sprintf!(fmt, v).unwrap_or_default().as_bytes() { match ch { - '\0' => break, - _ => self.push(ch), + 0 => break, + _ => self.push(*ch as char), } } } fn append_int(&mut self, fmt: &str, v: i32) { assert!(self.capacity() - self.len() >= 32); - for ch in sprintf::sprintf!(fmt, v).unwrap_or_default().chars() { + for ch in sprintf::sprintf!(fmt, v).unwrap_or_default().as_bytes() { match ch { - '\0' => break, - _ => self.push(ch), + 0 => break, + _ => self.push(*ch as char), } } } diff --git a/src/lib.rs b/src/lib.rs index a0ea224..2d1fa66 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -623,7 +623,7 @@ fn hash_bytes(hash_0: &mut Id, s: &[u8]) { impl Context { pub fn new() -> Self { let mut s = Self::default(); - s.draw_frame = Some(draw_frame as fn(&mut Context, Rect, ControlColor) -> ()); + s.draw_frame = Some(draw_frame); s.style = Style::default(); s } From b9f96329b3c27838e4f3936d2361c4698b30d4ac Mon Sep 17 00:00:00 2001 From: spuds <71292624+bananaturtlesandwich@users.noreply.github.com> Date: Thu, 15 Dec 2022 20:32:07 +0000 Subject: [PATCH 5/5] Revert "keep closer to original impl" This reverts commit 51e5a76d330d2248d55973fa551e995ecaa6e67c. --- src/fixed_collections.rs | 12 ++++++------ src/lib.rs | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/fixed_collections.rs b/src/fixed_collections.rs index 3fa2592..60af9be 100644 --- a/src/fixed_collections.rs +++ b/src/fixed_collections.rs @@ -225,20 +225,20 @@ pub trait IString { fn append_real(&mut self, fmt: &str, v: f32) { assert!(self.capacity() - self.len() >= 32); - for ch in sprintf::sprintf!(fmt, v).unwrap_or_default().as_bytes() { + for ch in sprintf::sprintf!(fmt, v).unwrap_or_default().chars() { match ch { - 0 => break, - _ => self.push(*ch as char), + '\0' => break, + _ => self.push(ch), } } } fn append_int(&mut self, fmt: &str, v: i32) { assert!(self.capacity() - self.len() >= 32); - for ch in sprintf::sprintf!(fmt, v).unwrap_or_default().as_bytes() { + for ch in sprintf::sprintf!(fmt, v).unwrap_or_default().chars() { match ch { - 0 => break, - _ => self.push(*ch as char), + '\0' => break, + _ => self.push(ch), } } } diff --git a/src/lib.rs b/src/lib.rs index 2d1fa66..a0ea224 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -623,7 +623,7 @@ fn hash_bytes(hash_0: &mut Id, s: &[u8]) { impl Context { pub fn new() -> Self { let mut s = Self::default(); - s.draw_frame = Some(draw_frame); + s.draw_frame = Some(draw_frame as fn(&mut Context, Rect, ControlColor) -> ()); s.style = Style::default(); s }