1
1
extern crate sdl2;
2
2
3
3
use sdl2:: event:: Event ;
4
- use sdl2:: impl_as_vertex_traits;
5
4
use sdl2:: keyboard:: Keycode ;
6
5
use sdl2:: pixels:: Color ;
7
6
use sdl2:: rect:: FPoint ;
8
- use sdl2:: render:: { AsVertexColor , VertexIndices } ;
7
+ use sdl2:: render:: { RenderGeometryTextureParams , VertexIndices } ;
8
+ use std:: mem:: offset_of;
9
9
use std:: thread;
10
10
use std:: time:: Duration ;
11
11
@@ -43,7 +43,7 @@ fn main() {
43
43
canvas. set_draw_color ( Color :: BLACK ) ;
44
44
canvas. clear ( ) ;
45
45
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
47
47
// (or other layout compatible of the needed data).
48
48
// The struct does not need to be `repr(C)` or `Copy` for example.
49
49
struct MyVertex {
@@ -54,26 +54,11 @@ fn main() {
54
54
#[ expect( dead_code) ]
55
55
foo : Vec < u8 > ,
56
56
// 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.
58
59
pos : FPoint ,
59
60
}
60
61
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
-
77
62
// Define the triangles
78
63
let vertices = [
79
64
MyVertex {
@@ -94,14 +79,18 @@ fn main() {
94
79
] ;
95
80
96
81
// 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 (
99
85
& vertices,
86
+ offset_of ! ( MyVertex , pos) ,
100
87
& vertices,
101
- None :: < ( & sdl2:: render:: Texture < ' _ > , & [ sdl2:: render:: Vertex ] ) > ,
88
+ offset_of ! ( MyVertex , color) ,
89
+ None :: < RenderGeometryTextureParams < ( ) > > ,
102
90
VertexIndices :: Sequential ,
103
91
)
104
- . expect ( "render_geometry failed" ) ;
92
+ }
93
+ . expect ( "render_geometry failed (probably unsupported, see error message)" ) ;
105
94
106
95
canvas. present ( ) ;
107
96
thread:: sleep ( Duration :: from_millis ( 16 ) ) ;
0 commit comments