Loading font

Before going any further, we actually need a font. I chose Lucida console, but pick the one you prefer, it doesn't really matter. Once downloaded, put it in the assets folder as well. Now, time to actually load the font:

     let font = ttf_context.load_font("assets/lucida.ttf", 128).expect("       Couldn't load the font");

Note that if you want to apply a style to your font (such as bold, italic, strikethrough, or underline), that's the object on which you need to apply it. Here is an example:

    font.set_style(sdl2::ttf::STYLE_BOLD);

Now, two steps are remaining to be able to actually display text:

  1. Render the text.
  2. Create a texture from it.

Let's write a function in order to do so:

    fn create_texture_from_text<'a>(texture_creator: ...

Get Rust Programming By Example now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.