What is GlyphCache type in a function to render text in Piston2d

1029 views rust
8

I am trying to write a separate function to render text using piston2d. Taking the hello_world.rs example, I am trying to extend that to allow me to render text from within a function.

Here is the code that I wrote:

extern crate piston_window;
extern crate find_folder;

use piston_window::*;

fn main() {
    let mut window: PistonWindow = WindowSettings::new(
            "piston: try to render text",
            [200, 200]
        )
        .exit_on_esc(true)
        .build()
        .unwrap();

    let assets = find_folder::Search::ParentsThenKids(3, 3)
        .for_folder("assets").unwrap();
    println!("{:?}", assets);
    let ref font = assets.join("FiraSans-Regular.ttf");
    let factory = window.factory.clone();
    let mut glyphs = Glyphs::new(font, factory, TextureSettings::new()).unwrap();

    window.set_lazy(true);
    while let Some(e) = window.next() {
        window.draw_2d(&e, |c, mut g| {
            clear([0.0, 0.0, 0.0, 1.0], g);
            render_text(10.0, 100.0, "Hello World", 32, c, &mut g, &mut glyphs);
        });
    }
}

fn render_text(x: f64, y: f64,
               text: &str, size: u32,
               c: Context, g: &mut G2d, 
               glyphs: &mut glyph_cache::rusttype::GlyphCache<GfxFactory, G2dTexture>) {
    text::Text::new(size).draw(
           text,
           &mut glyphs,
           &c.draw_state,
           c.transform.trans(x, y),
           g
        ).unwrap();
} 

When I try to run this code, I get the following error:

error[E0277]: the trait bound `&mut piston_window::glyph_cache::rusttype::GlyphCache<'_, gfx_device_gl::factory::Factory, piston_window::Texture<gfx_device_gl::Resources>>: piston_window::character::CharacterCache` is not satisfied

the trait `piston_window::character::CharacterCache` is not implemented for `&mut piston_window::glyph_cache::rusttype::GlyphCache<'_, gfx_device_gl::factory::Factory, piston_window::Texture<gfx_device_gl::Resources>>`

I have tried many different types for glyphs, and this was the farthest I could get.

What should the type be? Any guidance is appreciated.

answered question

Yup. That was it. Wow. Thanks.

1 Answer

9

The problem is that you are passing a mutable reference to a mutable reference to the GlyphCache to draw() (render_text receives a mutable reference, and you then create a mutable reference to that). Simply change &mut glyphs to glyphs in the call to draw().

draw() expects a mutable reference to a type that implements Graphics<Texture = <C as CharacterCache>::Texture>, and GlyphCache<GfxFactory, G2dTexture> does implement that trait, but &mut GlyphCache<GfxFactory, G2dTexture> doesn't.

When the type of a function parameter is a concrete type, the compiler will automatically dereference a reference to match the expected type (Clippy has a lint to identify the places where unnecessary references are created). However, when the type of the function paramter is a generic type (as is the case here), the compiler will not try to do that.

posted this

Have an answer?

JD

Please login first before posting an answer.