Skip to content

Commit

Permalink
Merge pull request #1 from bananaturtlesandwich/master
Browse files Browse the repository at this point in the history
remove heavy libc dependency
  • Loading branch information
eloraiby committed Jan 3, 2023
2 parents c173c82 + b9f9632 commit 33c955f
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 102 deletions.
3 changes: 3 additions & 0 deletions .gitignore
@@ -1,2 +1,5 @@
*.vscode/
*.idea/
*target/
*.lock
SDL2.*
6 changes: 3 additions & 3 deletions Cargo.toml
Expand Up @@ -9,9 +9,9 @@ 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"
fast-float = { version = "0.2", default-features = false }
8 changes: 0 additions & 8 deletions demo-sdl2/.idea/.gitignore

This file was deleted.

1 change: 0 additions & 1 deletion demo-sdl2/Cargo.toml
Expand Up @@ -9,7 +9,6 @@ edition = "2021"
sdl2 = "0.35.2"
microui = {path = ".."}
glow = "0.11.2"
libc = "0.2.138"

[profile.release]
panic = 'abort'
Expand Down
68 changes: 17 additions & 51 deletions demo-sdl2/src/main.rs
Expand Up @@ -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);

Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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]);
Expand Down
44 changes: 8 additions & 36 deletions src/fixed_collections.rs
Expand Up @@ -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),
}
}
}
Expand Down
4 changes: 1 addition & 3 deletions src/lib.rs
Expand Up @@ -51,7 +51,6 @@
// IN THE SOFTWARE.
//
#![no_std]
use core::ptr;

mod fixed_collections;
pub use crate::fixed_collections::*;
Expand Down Expand Up @@ -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 = fast_float::parse(core::str::from_utf8(&ascii).unwrap_or_default()).unwrap_or_default();
self.number_edit = None;
} else {
return ResourceState::ACTIVE;
Expand Down

0 comments on commit 33c955f

Please sign in to comment.