|
| 1 | +extern crate sdl2; |
| 2 | + |
| 3 | +use sdl2::event::Event; |
| 4 | +use sdl2::keyboard::Keycode; |
| 5 | +use sdl2::pixels::Color; |
| 6 | +use sdl2::rect::FPoint; |
| 7 | +use sdl2::render::{RenderGeometryTextureParams, Vertex, VertexIndices}; |
| 8 | +use std::mem::offset_of; |
| 9 | +use std::thread; |
| 10 | +use std::time::Duration; |
| 11 | + |
| 12 | +fn main() { |
| 13 | + let sdl_context = sdl2::init().unwrap(); |
| 14 | + let video_subsystem = sdl_context.video().unwrap(); |
| 15 | + |
| 16 | + let window = video_subsystem |
| 17 | + .window("Rust SDL2 render_geometry custom struct example", 800, 600) |
| 18 | + .position_centered() |
| 19 | + .opengl() |
| 20 | + .build() |
| 21 | + .unwrap(); |
| 22 | + |
| 23 | + let mut canvas = window.into_canvas().build().unwrap(); |
| 24 | + |
| 25 | + let mut event_pump = sdl_context.event_pump().unwrap(); |
| 26 | + let mut running = true; |
| 27 | + |
| 28 | + while running { |
| 29 | + for event in event_pump.poll_iter() { |
| 30 | + match event { |
| 31 | + Event::Quit { .. } |
| 32 | + | Event::KeyDown { |
| 33 | + keycode: Some(Keycode::Escape), |
| 34 | + .. |
| 35 | + } => { |
| 36 | + running = false; |
| 37 | + } |
| 38 | + _ => {} |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + // black background |
| 43 | + canvas.set_draw_color(Color::BLACK); |
| 44 | + canvas.clear(); |
| 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 | + |
| 69 | + // `render_geometry_raw` supports any custom struct as long as it contains the needed data |
| 70 | + // (or other layout compatible of the needed data). |
| 71 | + // The struct does not need to be `repr(C)` or `Copy` for example. |
| 72 | + struct MyVertex { |
| 73 | + // For demonstration purposes color is `[u8; 4]` here. `[u8; 4]` is layout-compatible |
| 74 | + // with `sdl2::pixels::Color` |
| 75 | + color: [u8; 4], |
| 76 | + // The struct may contain data not needed by SDL. |
| 77 | + #[expect(dead_code)] |
| 78 | + foo: Vec<u8>, |
| 79 | + // When defining your own vertex struct, using `FPoint` for position and tex_coord |
| 80 | + // (and `Color` for color) is the easiest way. These are obviously layout-compatible |
| 81 | + // with `FPoint` and `Color`, respectively. |
| 82 | + pos: FPoint, |
| 83 | + } |
| 84 | + |
| 85 | + // Define the vertices of a square |
| 86 | + let vertices = [ |
| 87 | + MyVertex { |
| 88 | + color: [0, 0, 0, 0xff], |
| 89 | + foo: b"some".to_vec(), |
| 90 | + pos: FPoint::new(300.0, 100.0), |
| 91 | + }, |
| 92 | + MyVertex { |
| 93 | + color: [0, 0xff, 0, 0xff], |
| 94 | + foo: b"unrelated".to_vec(), |
| 95 | + pos: FPoint::new(400.0, 100.0), |
| 96 | + }, |
| 97 | + MyVertex { |
| 98 | + color: [0xff, 0, 0, 0xff], |
| 99 | + foo: b"data".to_vec(), |
| 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), |
| 106 | + }, |
| 107 | + ]; |
| 108 | + |
| 109 | + // A square is rendered as two triangles (see indices) |
| 110 | + // SAFETY: core::mem::offset_of makes sure the offsets are right and alignment is respected. |
| 111 | + unsafe { |
| 112 | + canvas.render_geometry_raw( |
| 113 | + &vertices, |
| 114 | + offset_of!(MyVertex, pos), |
| 115 | + &vertices, |
| 116 | + offset_of!(MyVertex, color), |
| 117 | + None::<RenderGeometryTextureParams<()>>, |
| 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 and alignment is respected. |
| 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]], |
| 139 | + ) |
| 140 | + } |
| 141 | + .expect("render_geometry_raw failed (probably unsupported, see error message)"); |
| 142 | + |
| 143 | + canvas.present(); |
| 144 | + thread::sleep(Duration::from_millis(16)); |
| 145 | + } |
| 146 | +} |
0 commit comments