Expand description
§Easy Sdl3 Text
To get started, just look at one of the examples (or at the example code below)
Quick api overview:
TextCache- holds all the long-lived data that this crate needs for rendering text, excluding the canvas and texture creatorTextRenderingSettings- holds most of the arguments for the text rendering functionsrender_text_regular()- renders text without subpixel renderingrender_text_subpixel()- renders text with subpixel rendering
§Example Code:
use easy_sdl3_text::*;
pub fn example_draw_function<'a, F: ThreadSafeFont>(
app_data: &AppData,
canvas: &mut Canvas<Window>,
texture_creator: &'a TextureCreator<WindowContext>,
text_cache: &mut TextCache<'a, F>,
) -> anyhow::Result<()> {
canvas.set_draw_color(Color::RGB(255, 255, 255));
canvas.clear();
let size: u32 = 25;
let (mut x, mut y): (i32, i32) = (50, 50);
let foreground = Color::RGB(30, 30, 30);
let background = Color::RGB(255, 255, 255);
// most arguments to the rendering functions are the same across calls, so they're all put into a reusable struct
let mut text_rendering_settings = TextRenderingSettings::new_subpixel(
size,
HAlign::Left, VAlign::Center,
foreground, background,
canvas, texture_creator, text_cache
);
render_text_subpixel("Example text", x, y, &mut text_rendering_settings)?;
y += size as i32;
render_text_subpixel("More example text", x, y, &mut text_rendering_settings)?;
y += size as i32;
canvas.present();
Ok(())
}§Known Limitations:
- The subpixel rendering cannot blend into a background, and must have the background color supplied so that it can pre-blend the text onto a single color. This is because of subpixel rendering’s per-channel mixing, which cannot be done after initial rasterization without custom shaders, which sdl3’s renderer api doesn’t support.
- The subpixel rendering takes an integer size, which makes resizing look slightly strange. This is an intentional choice to cut down significantly on the number of textures to rasterize and cache.