我正在尝试编写一个单独的函数来使用活塞2d 呈现文本。以hello_world.rs为例,我正在尝试对其进行扩展,以允许我从函数中呈现文本。
这是我写的代码:
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();
}
当我尝试运行此代码时,出现以下错误:
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>>`
我尝试了许多不同的类型glyphs
,这是我能得到的最远的。
类型应该是什么?任何指导表示赞赏。