Skip to content

Commit e9325a2

Browse files
committed
update example to use 2 ways of using render_geometry_raw, and use render_geometry
1 parent 86f286c commit e9325a2

File tree

2 files changed

+66
-12
lines changed

2 files changed

+66
-12
lines changed

examples/render-geometry.rs

Lines changed: 58 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use sdl2::event::Event;
44
use sdl2::keyboard::Keycode;
55
use sdl2::pixels::Color;
66
use sdl2::rect::FPoint;
7-
use sdl2::render::{RenderGeometryTextureParams, VertexIndices};
7+
use sdl2::render::{RenderGeometryTextureParams, Vertex, VertexIndices};
88
use std::mem::offset_of;
99
use std::thread;
1010
use std::time::Duration;
@@ -43,6 +43,29 @@ fn main() {
4343
canvas.set_draw_color(Color::BLACK);
4444
canvas.clear();
4545

46+
// First, draw a triangle using `render_geometry`. The `tex_coord` fields are unused but
47+
// must be provided, `render_geometry` only supports `sdl2::render::Vertex`.
48+
let vertices = [
49+
Vertex {
50+
position: FPoint::new(100.0, 200.0),
51+
color: Color::RED,
52+
tex_coord: FPoint::new(0.0, 0.0),
53+
},
54+
Vertex {
55+
position: FPoint::new(200.0, 200.0),
56+
color: Color::GREEN,
57+
tex_coord: FPoint::new(0.0, 0.0),
58+
},
59+
Vertex {
60+
position: FPoint::new(150.0, 100.0),
61+
color: Color::BLUE,
62+
tex_coord: FPoint::new(0.0, 0.0),
63+
},
64+
];
65+
canvas
66+
.render_geometry(&vertices, None, VertexIndices::Sequential)
67+
.expect("render_geometry failed (probably unsupported, see error message)");
68+
4669
// `render_geometry_raw` supports any custom struct as long as it contains the needed data
4770
// (or other layout compatible of the needed data).
4871
// The struct does not need to be `repr(C)` or `Copy` for example.
@@ -59,26 +82,31 @@ fn main() {
5982
pos: FPoint,
6083
}
6184

62-
// Define the triangles
85+
// Define the vertices of a square
6386
let vertices = [
6487
MyVertex {
65-
color: [0xff, 0, 0, 0xff],
88+
color: [0, 0, 0, 0xff],
6689
foo: b"some".to_vec(),
67-
pos: FPoint::new(100.0, 500.0),
90+
pos: FPoint::new(300.0, 100.0),
6891
},
6992
MyVertex {
7093
color: [0, 0xff, 0, 0xff],
7194
foo: b"unrelated".to_vec(),
72-
pos: FPoint::new(700.0, 500.0),
95+
pos: FPoint::new(400.0, 100.0),
7396
},
7497
MyVertex {
75-
color: [0, 0, 0xff, 0xff],
98+
color: [0xff, 0, 0, 0xff],
7699
foo: b"data".to_vec(),
77-
pos: FPoint::new(400.0, 100.0),
100+
pos: FPoint::new(300.0, 200.0),
101+
},
102+
MyVertex {
103+
color: [0xff, 0xff, 0, 0xff],
104+
foo: b"!".to_vec(),
105+
pos: FPoint::new(400.0, 200.0),
78106
},
79107
];
80108

81-
// Actually render
109+
// A square is rendered as two triangles (see indices)
82110
// SAFETY: core::mem::offset_of makes sure the offsets are right.
83111
unsafe {
84112
canvas.render_geometry_raw(
@@ -87,10 +115,30 @@ fn main() {
87115
&vertices,
88116
offset_of!(MyVertex, color),
89117
None::<RenderGeometryTextureParams<()>>,
90-
VertexIndices::Sequential,
118+
&[[0, 1, 2], [1, 2, 3]],
119+
)
120+
}
121+
.expect("render_geometry_raw failed (probably unsupported, see error message)");
122+
123+
// Parameters can be reused, here only the positions are swapped out for new ones.
124+
// SAFETY: core::mem::offset_of makes sure the offsets are right.
125+
// The offset 0 is correct because the element type of positions is `[f32; 2]`.
126+
unsafe {
127+
canvas.render_geometry_raw(
128+
&[
129+
[500.0f32, 100.0],
130+
[600.0, 100.0],
131+
[500.0, 200.0],
132+
[600.0, 200.0],
133+
],
134+
0,
135+
&vertices,
136+
offset_of!(MyVertex, color),
137+
None::<RenderGeometryTextureParams<()>>,
138+
&[[0, 1, 2], [1, 2, 3]],
91139
)
92140
}
93-
.expect("render_geometry failed (probably unsupported, see error message)");
141+
.expect("render_geometry_raw failed (probably unsupported, see error message)");
94142

95143
canvas.present();
96144
thread::sleep(Duration::from_millis(16));

src/sdl2/render.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2047,19 +2047,25 @@ impl<'a> From<&'a [[i32; 3]]> for VertexIndices<'a> {
20472047
}
20482048
}
20492049

2050-
macro_rules! impl_vec_ref_into_vertex_indices {
2050+
macro_rules! impl_into_vertex_indices_forward {
20512051
($($ty:ty)*) => {
20522052
$(
20532053
impl<'a> From<&'a Vec<$ty>> for VertexIndices<'a> {
20542054
fn from(value: &'a Vec<$ty>) -> Self {
20552055
Self::from(value.as_slice())
20562056
}
20572057
}
2058+
2059+
impl<'a, const N: usize> From<&'a [$ty; N]> for VertexIndices<'a> {
2060+
fn from(value: &'a [$ty; N]) -> Self {
2061+
Self::from(value.as_slice())
2062+
}
2063+
}
20582064
)*
20592065
};
20602066
}
20612067

2062-
impl_vec_ref_into_vertex_indices!(u8 u16 u32 i32 [u8; 3] [u16; 3] [u32; 3] [i32; 3]);
2068+
impl_into_vertex_indices_forward!(u8 u16 u32 i32 [u8; 3] [u16; 3] [u32; 3] [i32; 3]);
20632069

20642070
#[derive(Clone, Copy)]
20652071
pub struct RenderGeometryTextureParams<'a, TexCoordVertex> {

0 commit comments

Comments
 (0)