Skip to content

Commit 503d18c

Browse files
Add geometry raw functions
1 parent 1d3e114 commit 503d18c

File tree

1 file changed

+79
-9
lines changed

1 file changed

+79
-9
lines changed

src/sdl2/render.rs

+79-9
Original file line numberDiff line numberDiff line change
@@ -1578,16 +1578,25 @@ impl<T: RenderTarget> Canvas<T> {
15781578
&mut self,
15791579
texture: &Texture,
15801580
vertices: &[Vertex],
1581-
indices: &[u32],
1581+
indices: Option<&[u32]>,
15821582
) -> Result<(), String> {
1583+
// API expects i32, however the underlying SDL_RenderGeometryRaw call casts to u32
1584+
let indices_ptr: *const c_int = match indices {
1585+
Some(i) => i.as_ptr() as *const c_int,
1586+
None => std::ptr::null(),
1587+
};
1588+
let indices_count: c_int = match indices {
1589+
Some(i) => i.len() as c_int,
1590+
None => 0,
1591+
};
15831592
let result = unsafe {
15841593
sys::SDL_RenderGeometry(
15851594
self.context.raw,
15861595
texture.raw,
15871596
Vertex::raw_slice(vertices),
15881597
vertices.len() as c_int,
1589-
indices.as_ptr() as *const i32,
1590-
indices.len() as c_int,
1598+
indices_ptr,
1599+
indices_count,
15911600
)
15921601
};
15931602

@@ -1598,9 +1607,9 @@ impl<T: RenderTarget> Canvas<T> {
15981607
}
15991608
}
16001609

1601-
/// Render a list of triangles, optionally using a texture and indices into the vertex arrays.
1610+
/// Render a list of triangles, using separate arrays for position, color and UV.
16021611
#[doc(alias = "SDL_RenderGeometryRaw")]
1603-
pub fn geometry_raw<I: VertexIndex>(
1612+
pub fn geometry_split<I: VertexIndex>(
16041613
&mut self,
16051614
texture: &Texture,
16061615
xy: &[f32],
@@ -1610,8 +1619,20 @@ impl<T: RenderTarget> Canvas<T> {
16101619
uv: &[f32],
16111620
uv_stride: i32,
16121621
num_vertices: i32,
1613-
indices: &[I],
1622+
indices: Option<&[I]>,
16141623
) -> Result<(), String> {
1624+
let indices_ptr: *const c_void = match indices {
1625+
Some(i) => i.as_ptr() as *const c_void,
1626+
None => std::ptr::null(),
1627+
};
1628+
let indices_count: c_int = match indices {
1629+
Some(i) => i.len() as c_int,
1630+
None => 0,
1631+
};
1632+
let indices_size: c_int = match indices {
1633+
Some(_) => std::mem::size_of::<I>() as c_int,
1634+
None => 0,
1635+
};
16151636
let result = unsafe {
16161637
sys::SDL_RenderGeometryRaw(
16171638
self.context.raw,
@@ -1623,9 +1644,58 @@ impl<T: RenderTarget> Canvas<T> {
16231644
uv.as_ptr(),
16241645
uv_stride as c_int,
16251646
num_vertices as c_int,
1626-
indices.as_ptr() as *const c_void,
1627-
indices.len() as c_int,
1628-
std::mem::size_of::<I>() as i32,
1647+
indices_ptr,
1648+
indices_count,
1649+
indices_size,
1650+
)
1651+
};
1652+
1653+
if result != 0 {
1654+
Err(get_error())
1655+
} else {
1656+
Ok(())
1657+
}
1658+
}
1659+
1660+
/// Render a list of triangles, optionally using a texture and indices into the vertex arrays.
1661+
/// It is recommended that you use the "memoffset" crate to determine the offsets of fields in your struct.
1662+
#[doc(alias = "SDL_RenderGeometryRaw")]
1663+
pub fn geometry_struct<V, I: VertexIndex>(
1664+
&mut self,
1665+
texture: &Texture,
1666+
vertices: &[V],
1667+
xy_offset: usize,
1668+
color_offset: usize,
1669+
uv_offset: usize,
1670+
indices: Option<&[I]>,
1671+
) -> Result<(), String> {
1672+
let stride = std::mem::size_of::<V>() as c_int;
1673+
let indices_ptr: *const c_void = match indices {
1674+
Some(i) => i.as_ptr() as *const c_void,
1675+
None => std::ptr::null(),
1676+
};
1677+
let indices_count: c_int = match indices {
1678+
Some(i) => i.len() as c_int,
1679+
None => 0,
1680+
};
1681+
let indices_size: c_int = match indices {
1682+
Some(_) => std::mem::size_of::<I>() as c_int,
1683+
None => 0,
1684+
};
1685+
let result = unsafe {
1686+
sys::SDL_RenderGeometryRaw(
1687+
self.context.raw,
1688+
texture.raw,
1689+
(vertices.as_ptr() as *const u8).add(xy_offset) as *const f32,
1690+
stride,
1691+
(vertices.as_ptr() as *const u8).add(color_offset) as *const sys::SDL_Color,
1692+
stride,
1693+
(vertices.as_ptr() as *const u8).add(uv_offset) as *const f32,
1694+
stride,
1695+
vertices.len() as c_int,
1696+
indices_ptr,
1697+
indices_count,
1698+
indices_size,
16291699
)
16301700
};
16311701

0 commit comments

Comments
 (0)