在 Rust 中使用 piston2d 在单独的函数中渲染文本

Rendering text in a separate function using piston2d in rust

我正在尝试使用 piston2d / piston_window 在单独的函数中呈现文本。我可以很好地绘制文本,但我不知道如何将适当的参数传递给单独的函数。

我研究了 并相应地调整了我的代码,但我无法理解我遇到的错误。


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, 1.0, 0.7], 30)
            .draw(
                "Hi!",
                &mut glyphs,
                &c.draw_state,
                c.transform
                    .trans(100., 100.),
                gfx,
            )
            .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.draw_state, 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, glyphs, &c.draw_state, c.transform.trans(x, y), 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>>`

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

刚遇到同样的问题。已经快一年了,但是 piston_window 的文档不是最好的,所以也许其他人会需要它。

这对我有用

use piston_window::types::Color;
use piston_window::{text, Context, G2d, Glyphs, Transformed};

pub const text_color: Color = [1.0, 1.0, 1.0, 1.0];

pub fn draw_text(
    ctx: &Context,
    graphics: &mut G2d,
    glyphs: &mut Glyphs,
    color: Color,
    pos: Position,
    text: &str,
) {
    text::Text::new_color(color, 20)
        .draw(
            text,
            glyphs,
            &ctx.draw_state,
            ctx.transform.trans(pos.x as f64, pos.y as f64),
            graphics,
        )
        .unwrap();
}

pub struct Pos {
    pub x: f64,
    pub y: f64,
}

pub fn main() {
    let assets = find_folder::Search::ParentsThenKids(3, 3)
        .for_folder("assets")
        .unwrap();
    let ref font = assets.join("retro-gaming.ttf");
    let mut glyphs = window.load_font(font).unwrap();
    
    let size = [500., 500.];

    let mut window: PistonWindow = WindowSettings::new("Test", size)
        .resizable(false)
        .exit_on_esc(true)
        .build()
        .unwrap();

    while let Some(event) = window.next() {
        window.draw_2d(&event, |ctx, g, _| {
            draw_text(&ctx, g, &mut glyphs, text_color, Pos { x: 0, y: 10 }, "20")
        }
    }
}

注意我使用的“字形”与您不同。 “Cargo.toml”中还有一个依赖项,即 find_folder = "0.3.0".

我不完全确定上面的代码片段是否可以编译和工作。这是此提交 https://github.com/laszukdawid/rsnake/commit/e6e23563ebdd9c7b972ee17b5d0299e2202358cf.

的快速重构