2

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

我已经研究了什么是函数中的 GlyphCache 类型以在 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, 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>>`

我知道这可能是非常特定于活塞的,但我会对任何指针感到非常高兴。

4

1 回答 1

1

刚遇到同样的问题。差不多一年后,但文档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的快速重构。

于 2022-01-02T21:28:19.753 回答