在 rust 中使用活塞2d 在单独的函数中渲染文本

问题描述

我正在尝试使用活塞2d/活塞窗口在单独的函数中呈现文本。我能够很好地绘制文本,但我不知道如何将适当的参数传递给单独的函数

我已经研究了 What is GlyphCache type in a function to render text in Piston2d 并相应地调整了我的代码,但我无法理解我得到的错误


use piston_window::*;

fn main() {
    let font = include_bytes!("IBMPlexSans-Regular.ttf");
    let opengl = OpenGL::V3_2;

    let settings = WindowSettings::new("test",[500,500])
        .graphics_api(opengl)
        .fullscreen(false)
        .vsync(true)
        .exit_on_esc(true);

    let mut window: PistonWindow = settings.build().unwrap();

    let mut glyphs = Glyphs::from_bytes(
        font,window.create_texture_context(),TextureSettings::new(),)
    .unwrap();

    while let Some(e) = window.next() {

        window.draw_2d(&e,|c,gfx,device| {
            clear([0.2; 4],gfx);

        text::Text::new_color([1.0,1.0,0.7],30)
            .draw(
                "Hi!",&mut glyphs,&c.draw_state,c.transform
                    .trans(100.,100.),)
            .unwrap();
            glyphs.factory.encoder.flush(device);
        });
    }
}

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,glyphs,c.transform.trans(x,y),g)
        .unwrap();
}

我收到以下错误

error[E0277]: the trait bound `Texture<gfx_device_gl::Resources>: UpdateTexture<gfx_device_gl::factory::Factory>` is not satisfied
  --> src/main.rs:54:10
   |
54 |         .draw(text,g)
   |          ^^^^ the trait `UpdateTexture<gfx_device_gl::factory::Factory>` is not implemented for `Texture<gfx_device_gl::Resources>`
   |
   = help: the following implementations were found:
             <Texture<R> as UpdateTexture<TextureContext<F,R,C>>>
   = note: required because of the requirements on the impl of `CharacterCache` for `GlyphCache<'_,gfx_device_gl::factory::Factory,Texture<gfx_device_gl::Resources>>`

我知道这可能是特定于活塞的,但我会很高兴收到任何指示。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)