@@ -4,7 +4,7 @@ use sdl2::event::Event;
4
4
use sdl2:: keyboard:: Keycode ;
5
5
use sdl2:: pixels:: Color ;
6
6
use sdl2:: rect:: FPoint ;
7
- use sdl2:: render:: { RenderGeometryTextureParams , VertexIndices } ;
7
+ use sdl2:: render:: { RenderGeometryTextureParams , Vertex , VertexIndices } ;
8
8
use std:: mem:: offset_of;
9
9
use std:: thread;
10
10
use std:: time:: Duration ;
@@ -43,6 +43,29 @@ fn main() {
43
43
canvas. set_draw_color ( Color :: BLACK ) ;
44
44
canvas. clear ( ) ;
45
45
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
+
46
69
// `render_geometry_raw` supports any custom struct as long as it contains the needed data
47
70
// (or other layout compatible of the needed data).
48
71
// The struct does not need to be `repr(C)` or `Copy` for example.
@@ -59,26 +82,31 @@ fn main() {
59
82
pos : FPoint ,
60
83
}
61
84
62
- // Define the triangles
85
+ // Define the vertices of a square
63
86
let vertices = [
64
87
MyVertex {
65
- color : [ 0xff , 0 , 0 , 0xff ] ,
88
+ color : [ 0 , 0 , 0 , 0xff ] ,
66
89
foo : b"some" . to_vec ( ) ,
67
- pos : FPoint :: new ( 100 .0, 500 .0) ,
90
+ pos : FPoint :: new ( 300 .0, 100 .0) ,
68
91
} ,
69
92
MyVertex {
70
93
color : [ 0 , 0xff , 0 , 0xff ] ,
71
94
foo : b"unrelated" . to_vec ( ) ,
72
- pos : FPoint :: new ( 700 .0, 500 .0) ,
95
+ pos : FPoint :: new ( 400 .0, 100 .0) ,
73
96
} ,
74
97
MyVertex {
75
- color : [ 0 , 0 , 0xff , 0xff ] ,
98
+ color : [ 0xff , 0 , 0 , 0xff ] ,
76
99
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 ) ,
78
106
} ,
79
107
] ;
80
108
81
- // Actually render
109
+ // A square is rendered as two triangles (see indices)
82
110
// SAFETY: core::mem::offset_of makes sure the offsets are right.
83
111
unsafe {
84
112
canvas. render_geometry_raw (
@@ -87,10 +115,30 @@ fn main() {
87
115
& vertices,
88
116
offset_of ! ( MyVertex , color) ,
89
117
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 ] ] ,
91
139
)
92
140
}
93
- . expect ( "render_geometry failed (probably unsupported, see error message)" ) ;
141
+ . expect ( "render_geometry_raw failed (probably unsupported, see error message)" ) ;
94
142
95
143
canvas. present ( ) ;
96
144
thread:: sleep ( Duration :: from_millis ( 16 ) ) ;
0 commit comments