Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,9 @@ required-features = ["unsafe_textures"]
name = "renderer_09_scaling_textures"
path = "examples/renderer/a09_scaling_textures.rs"

[[example]]
name = "render-geometry"

[[example]]
name = "renderer-texture"

Expand Down
141 changes: 141 additions & 0 deletions examples/render-geometry.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
use sdl3::event::Event;
use sdl3::keyboard::Keycode;
use sdl3::pixels::{Color, FColor};
use sdl3::render::{FPoint, RenderGeometryTextureParams, Vertex, VertexIndices};
use std::mem::offset_of;
use std::thread;
use std::time::Duration;

fn main() {
let sdl_context = sdl3::init().unwrap();
let video_subsystem = sdl_context.video().unwrap();

let window = video_subsystem
.window("Rust SDL3 render_geometry custom struct example", 800, 600)
.position_centered()
.opengl()
.build()
.unwrap();

let mut canvas = window.into_canvas();

let mut event_pump = sdl_context.event_pump().unwrap();
let mut running = true;

while running {
for event in event_pump.poll_iter() {
match event {
Event::Quit { .. }
| Event::KeyDown {
keycode: Some(Keycode::Escape),
..
} => {
running = false;
}
_ => {}
}
}

// black background
canvas.set_draw_color(Color::BLACK);
canvas.clear();

// First, draw a triangle using `render_geometry`. The `tex_coord` fields are unused but
// must be provided, `render_geometry` only supports `sdl3::render::Vertex`.
let vertices = [
Vertex {
position: FPoint::new(100.0, 200.0),
color: FColor::RED,
tex_coord: FPoint::new(0.0, 0.0),
},
Vertex {
position: FPoint::new(200.0, 200.0),
color: FColor::GREEN,
tex_coord: FPoint::new(0.0, 0.0),
},
Vertex {
position: FPoint::new(150.0, 100.0),
color: FColor::BLUE,
tex_coord: FPoint::new(0.0, 0.0),
},
];
canvas
.render_geometry(&vertices, None, VertexIndices::Sequential)
.expect("render_geometry failed (probably unsupported, see error message)");

// `render_geometry_raw` supports any custom struct as long as it contains the needed data
// (or other layout compatible of the needed data).
// The struct does not need to be `repr(C)` or `Copy` for example.
struct MyVertex {
color: [f32; 4],
// The struct may contain data not needed by SDL.
#[expect(dead_code)]
foo: Vec<u8>,
// When defining your own vertex struct, using `FPoint` for position and tex_coord
// (and `FColor` for color) is the easiest way. These are obviously layout-compatible
// with `FPoint` and `Color`, respectively.
pos: FPoint,
}

// Define the vertices of a square
let vertices = [
MyVertex {
color: [0., 0., 0., 1.],
foo: b"some".to_vec(),
pos: FPoint::new(300.0, 100.0),
},
MyVertex {
color: [0., 1., 0., 1.],
foo: b"unrelated".to_vec(),
pos: FPoint::new(400.0, 100.0),
},
MyVertex {
color: [1., 0., 0., 1.],
foo: b"data".to_vec(),
pos: FPoint::new(300.0, 200.0),
},
MyVertex {
color: [1., 1., 0., 1.],
foo: b"!".to_vec(),
pos: FPoint::new(400.0, 200.0),
},
];

// A square is rendered as two triangles (see indices)
// SAFETY: core::mem::offset_of makes sure the offsets are right and alignment is respected.
unsafe {
canvas.render_geometry_raw(
&vertices,
offset_of!(MyVertex, pos),
&vertices,
offset_of!(MyVertex, color),
None::<RenderGeometryTextureParams<()>>,
&[[0, 1, 2], [1, 2, 3]],
)
}
.expect("render_geometry_raw failed (probably unsupported, see error message)");

// Parameters can be reused, here only the positions are swapped out for new ones.
// SAFETY: core::mem::offset_of makes sure the offsets are right and alignment is respected.
// The offset 0 is correct because the element type of positions is `[f32; 2]`.
unsafe {
canvas.render_geometry_raw(
&[
[500.0f32, 100.0],
[600.0, 100.0],
[500.0, 200.0],
[600.0, 200.0],
],
0,
&vertices,
offset_of!(MyVertex, color),
None::<RenderGeometryTextureParams<()>>,
&[[0, 1, 2], [1, 2, 3]],
)
}
.expect("render_geometry_raw failed (probably unsupported, see error message)");

canvas.present();
thread::sleep(Duration::from_millis(16));
}
}
104 changes: 104 additions & 0 deletions src/sdl3/pixels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,110 @@ impl From<(u8, u8, u8, u8)> for Color {
}
}

#[derive(Copy, Clone, PartialEq, Debug)]
pub struct FColor {
pub r: f32,
pub g: f32,
pub b: f32,
pub a: f32,
}

impl FColor {
#[inline]
#[allow(non_snake_case)]
pub const fn RGB(r: f32, g: f32, b: f32) -> FColor {
FColor { r, g, b, a: 1. }
}

#[inline]
#[allow(non_snake_case)]
pub const fn RGBA(r: f32, g: f32, b: f32, a: f32) -> FColor {
FColor { r, g, b, a }
}

pub fn invert(self) -> FColor {
FColor::RGBA(1. - self.r, 1. - self.g, 1. - self.b, 1. - self.a)
}

#[inline]
pub const fn rgb(self) -> (f32, f32, f32) {
(self.r, self.g, self.b)
}

#[inline]
pub const fn rgba(self) -> (f32, f32, f32, f32) {
(self.r, self.g, self.b, self.a)
}

// Implemented manually and kept private, because reasons
#[inline]
const fn raw(self) -> sys::pixels::SDL_FColor {
sys::pixels::SDL_FColor {
r: self.r,
g: self.g,
b: self.b,
a: self.a,
}
}

pub const WHITE: FColor = FColor::RGBA(1., 1., 1., 1.);
pub const BLACK: FColor = FColor::RGBA(0., 0., 0., 1.);
pub const GRAY: FColor = FColor::RGBA(0.5, 0.5, 0.5, 1.);
pub const GREY: FColor = FColor::GRAY;
pub const RED: FColor = FColor::RGBA(1., 0., 0., 1.);
pub const GREEN: FColor = FColor::RGBA(0., 1., 0., 1.);
pub const BLUE: FColor = FColor::RGBA(0., 0., 1., 1.);
pub const MAGENTA: FColor = FColor::RGBA(1., 0., 1., 1.);
pub const YELLOW: FColor = FColor::RGBA(1., 1., 0., 1.);
pub const CYAN: FColor = FColor::RGBA(0., 1., 1., 1.);
}

impl From<FColor> for sys::pixels::SDL_FColor {
fn from(val: FColor) -> Self {
val.raw()
}
}

impl From<sys::pixels::SDL_FColor> for FColor {
fn from(raw: sys::pixels::SDL_FColor) -> FColor {
FColor::RGBA(raw.r, raw.g, raw.b, raw.a)
}
}

impl From<(f32, f32, f32)> for FColor {
fn from((r, g, b): (f32, f32, f32)) -> FColor {
FColor::RGB(r, g, b)
}
}

impl From<(f32, f32, f32, f32)> for FColor {
fn from((r, g, b, a): (f32, f32, f32, f32)) -> FColor {
FColor::RGBA(r, g, b, a)
}
}

impl From<Color> for FColor {
fn from(val: Color) -> FColor {
FColor {
r: val.r as f32 / 255.,
g: val.g as f32 / 255.,
b: val.b as f32 / 255.,
a: val.a as f32 / 255.,
}
}
}

impl From<FColor> for Color {
fn from(val: FColor) -> Color {
Color {
r: (val.r * 255.).round().clamp(0., 255.) as u8,
g: (val.g * 255.).round().clamp(0., 255.) as u8,
b: (val.b * 255.).round().clamp(0., 255.) as u8,
a: (val.a * 255.).round().clamp(0., 255.) as u8,
}
}
}

pub struct PixelMasks {
/// Bits per pixel; usually 15, 16, or 32
pub bpp: u8,
Expand Down
Loading