pub fn create_text_texture<'a>(
font: &Font<'_, '_>,
text: &str,
color: &Color,
texture_creator: &'a TextureCreator<WindowContext>,
) -> Result<Texture<'a>, String>Expand description
Creates a texture from a given font and a given text.
font - The font to use.
text - The text to render.
color - The color of the text.
texture_creator - The texture creator to use.
Examples found in repository?
examples/text.rs (line 27)
3fn main() {
4 let mut frug_instance = Instance::new("Spritesheet Example", 800, 600);
5 let background_color = Color::RGB(70, 70, 100);
6
7 // load the font and create a texture creator
8 let texture_creator = frug_instance.new_texture_creator();
9 let ttf_context = frug_instance.new_ttf_context().unwrap();
10
11 // text settings
12 let font_scale = 7.0;
13 let font_size = 8.0 * font_scale;
14 let text = "FRUG!";
15 let text_color = Color::RGB(100, 255, 100);
16
17 // create the font
18 let font = match ttf_context.load_font("examples/PressStart2P-Regular.ttf", font_size) {
19 Ok(font) => font,
20 Err(e) => {
21 eprintln!("Failed to load font: {}", e);
22 return;
23 }
24 };
25
26 // create the text texture
27 let text_texture = match create_text_texture(&font, text, &text_color, &texture_creator) {
28 Ok(texture) => texture,
29 Err(e) => {
30 eprintln!("Failed to create text texture: {}", e);
31 return;
32 }
33 };
34
35 // get the dimensions of the text texture
36 let TextureQuery { width, height, .. } = text_texture.query();
37
38 'running: loop {
39 // Input
40 for event in frug_instance.get_events() {
41 match event {
42 // Quit the application
43 Event::Quit { .. }
44 | Event::KeyDown {
45 keycode: Some(Keycode::Escape),
46 ..
47 } => break 'running,
48 _ => {}
49 }
50 }
51
52 // Render
53 frug_instance.clear(background_color);
54 frug_instance.draw_full_texture(
55 &text_texture,
56 &Vec2d { x: 50, y: 200 },
57 &Vec2d {
58 x: width,
59 y: height,
60 },
61 );
62 frug_instance.present();
63 }
64}