微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决在 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 举报,一经查实,本站将立刻删除。