diff --git a/imgui-sys/Cargo.toml b/imgui-sys/Cargo.toml index cbeeeb1..97b0097 100644 --- a/imgui-sys/Cargo.toml +++ b/imgui-sys/Cargo.toml @@ -12,6 +12,7 @@ build = "build.rs" [dependencies] bitflags = "0.8" glium = { version = "0.16", default-features = false, optional = true } +gfx = { version = "0.14", optional = true } [build-dependencies] gcc = "0.3" diff --git a/imgui-sys/src/gfx_support.rs b/imgui-sys/src/gfx_support.rs new file mode 100644 index 0000000..e6adb53 --- /dev/null +++ b/imgui-sys/src/gfx_support.rs @@ -0,0 +1,58 @@ +use gfx::format::{Format, Formatted, U8Norm}; +use gfx::pso::buffer::{Element, ElemOffset, Structure}; +use gfx::traits::Pod; +use std::mem; + +use super::{ImDrawVert, ImVec2}; + +unsafe impl Pod for ImDrawVert {} + +impl Structure for ImDrawVert { + fn query(name: &str) -> Option> { + // array query hack from gfx_impl_struct_meta macro + let (sub_name, big_offset) = { + let mut split = name.split(|c| c == '[' || c == ']'); + let _ = split.next().unwrap(); + match split.next() { + Some(s) => { + let array_id: ElemOffset = s.parse().unwrap(); + let sub_name = match split.next() { + Some(s) if s.starts_with('.') => &s[1..], + _ => name, + }; + (sub_name, array_id * (mem::size_of::() as ElemOffset)) + } + None => (name, 0), + } + }; + let dummy: &ImDrawVert = unsafe { mem::transmute(0usize) }; + match sub_name { + "pos" => { + Some(Element { + format: ::get_format(), + offset: unsafe { mem::transmute::<_, usize>(&dummy.pos) } as ElemOffset + + big_offset, + }) + } + "uv" => { + Some(Element { + format: ::get_format(), + offset: unsafe { mem::transmute::<_, usize>(&dummy.uv) } as ElemOffset + + big_offset, + }) + } + "col" => { + Some(Element { + format: <[U8Norm; 4] as Formatted>::get_format(), + offset: unsafe { mem::transmute::<_, usize>(&dummy.col) } as ElemOffset + + big_offset, + }) + } + _ => None, + } + } +} + +gfx_format! { + ImVec2: R32_G32 = Vec2 +} diff --git a/imgui-sys/src/glium_support.rs b/imgui-sys/src/glium_support.rs index 492266c..0ec4248 100644 --- a/imgui-sys/src/glium_support.rs +++ b/imgui-sys/src/glium_support.rs @@ -1,7 +1,7 @@ use glium::vertex::{Attribute, AttributeType, Vertex, VertexFormat}; use std::borrow::Cow; use std::mem; -use std::os::raw::{c_float}; +use std::os::raw::c_float; use super::{ImDrawVert, ImVec2, ImVec4}; diff --git a/imgui-sys/src/lib.rs b/imgui-sys/src/lib.rs index c8e2cb3..d0e2d1d 100644 --- a/imgui-sys/src/lib.rs +++ b/imgui-sys/src/lib.rs @@ -3,6 +3,10 @@ #[macro_use] extern crate bitflags; +#[cfg(feature = "gfx")] +#[macro_use] +extern crate gfx; + #[cfg(feature = "glium")] extern crate glium; @@ -11,6 +15,9 @@ use std::mem; use std::os::raw::{c_char, c_float, c_int, c_short, c_uchar, c_uint, c_ushort, c_void}; use std::slice; +#[cfg(feature = "gfx")] +mod gfx_support; + #[cfg(feature = "glium")] mod glium_support;