Skip to content

Commit d07044d

Browse files
committed
rework almost everything
- the macro was very hard to get sound, auto deref has to be prevented somehow - the unsafe traits could not be implemented for types defined in other crates the unsafety must be kept simple, users now only need to provide the right offsets, which core::mem::offset_of does update example
1 parent 6f10625 commit d07044d

File tree

2 files changed

+171
-281
lines changed

2 files changed

+171
-281
lines changed

examples/render-geometry.rs

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
extern crate sdl2;
22

33
use sdl2::event::Event;
4-
use sdl2::impl_as_vertex_traits;
54
use sdl2::keyboard::Keycode;
65
use sdl2::pixels::Color;
76
use sdl2::rect::FPoint;
8-
use sdl2::render::{AsVertexColor, VertexIndices};
7+
use sdl2::render::{RenderGeometryTextureParams, VertexIndices};
8+
use std::mem::offset_of;
99
use std::thread;
1010
use std::time::Duration;
1111

@@ -43,7 +43,7 @@ fn main() {
4343
canvas.set_draw_color(Color::BLACK);
4444
canvas.clear();
4545

46-
// `render_geometry` supports any custom struct as long as it contains the needed data
46+
// `render_geometry_raw` supports any custom struct as long as it contains the needed data
4747
// (or other layout compatible of the needed data).
4848
// The struct does not need to be `repr(C)` or `Copy` for example.
4949
struct MyVertex {
@@ -54,26 +54,11 @@ fn main() {
5454
#[expect(dead_code)]
5555
foo: Vec<u8>,
5656
// When defining your own vertex struct, using `FPoint` for position and tex_coord
57-
// (and `Color` for color) is the easiest way (see the trait impls below)
57+
// (and `Color` for color) is the easiest way. These are obviously layout-compatible
58+
// with `FPoint` and `Color`, respectively.
5859
pos: FPoint,
5960
}
6061

61-
// The unsafe trait to get the vertex position can simply be generated with a macro.
62-
// This macro makes sure the implementation is sound, this is only possible when the field
63-
// has the exact right type.
64-
impl_as_vertex_traits!(impl AsVertexPosition(self.pos) for MyVertex);
65-
66-
// The unsafe trait to get the vertex color must be implementated manually because the type
67-
// of the `color` field is not `sdl2::pixels::Color`.
68-
// Also make sure to not violate the contract of this unsafe trait!
69-
// SAFETY: `as_vertex_color` only returns a borrow of a field of `self`.
70-
unsafe impl AsVertexColor for MyVertex {
71-
fn as_vertex_color(&self) -> &Color {
72-
// SAFETY: [u8; 4] has the same layout as Color
73-
unsafe { &*(&self.color as *const [u8; 4] as *const Color) }
74-
}
75-
}
76-
7762
// Define the triangles
7863
let vertices = [
7964
MyVertex {
@@ -94,14 +79,18 @@ fn main() {
9479
];
9580

9681
// Actually render
97-
canvas
98-
.render_geometry(
82+
// SAFETY: core::mem::offset_of makes sure the offsets are right.
83+
unsafe {
84+
canvas.render_geometry_raw(
9985
&vertices,
86+
offset_of!(MyVertex, pos),
10087
&vertices,
101-
None::<(&sdl2::render::Texture<'_>, &[sdl2::render::Vertex])>,
88+
offset_of!(MyVertex, color),
89+
None::<RenderGeometryTextureParams<()>>,
10290
VertexIndices::Sequential,
10391
)
104-
.expect("render_geometry failed");
92+
}
93+
.expect("render_geometry failed (probably unsupported, see error message)");
10594

10695
canvas.present();
10796
thread::sleep(Duration::from_millis(16));

0 commit comments

Comments
 (0)