When I tried to print text using the piston_window library, I found that the "10" was printed and only the "1" was displayed.Here is the code.
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 main() {
let assets = find_folder::Search::ParentsThenKids(3, 3)
.for_folder("assets")
.unwrap();
let ref font = assets.join("retro-gaming.ttf");
let factory = window.factory.clone();
let mut glyphs = Glyphs::new(
font,
TextureContext {
factory,
encoder: window.factory.create_command_buffer().into(),
},
TextureSettings::new(),
)
.unwrap();
let mut window: PistonWindow = WindowSettings::new("test", [30, 30])
.resizable(false)
.exit_on_esc(true)
.build()
.unwrap_or_else(|e| panic!("Failed to build Window: {}", e));
while let Some(event) = window.next() {
window.draw_2d(&event, |ctx, g, _| {
text::Text::new_color(TEXT_COLOR, 20)
.draw(
"10",
&mut glyphs,
&ctx.draw_state,
ctx.transform.trans(0.0, 20.0),
g,
)
.unwrap();
});
}
}
I can't figure out what's wrong.I hope someone who knows can help me.Thanks.
Reason is explained here
Ah! Yes, the flush is needed when using gfx_graphics. This is because of the caching.
and solution is
glyphs.factory.encoder.flush(device);
Below is the code which renders as expected
use std::env;
use piston_window::{clear, Glyphs, PistonWindow, rectangle, text, TextureContext, TextureSettings, Transformed, WindowSettings};
use piston_window::types::Color;
pub const TEXT_COLOR: Color = [1.0, 1.0, 1.0, 1.0];
pub fn main() {
let _args: Vec<_> = env::args().collect();
let assets = find_folder::Search::ParentsThenKids(3, 3)
.for_folder("assets")
.unwrap();
let ref font = assets.join("retro-gaming.ttf");
let mut window: PistonWindow = WindowSettings::new("test", [800, 600])
.resizable(false)
.exit_on_esc(true)
.build()
.unwrap_or_else(|e| panic!("Failed to build Window: {}", e));
let factory = window.factory.clone();
let mut glyphs = Glyphs::new(
font,
TextureContext {
factory,
encoder: window.factory.create_command_buffer().into(),
},
TextureSettings::new(),
).unwrap();
while let Some(event) = window.next() {
window.draw_2d(&event, |ctx, g, device| {
clear([0.5, 0.5, 0.5, 1.0], g);
rectangle([1.0, 0.0, 0.0, 1.0], // red
[0.0, 0.0, 100.0, 100.0], // rectangle
ctx.transform, g);
text::Text::new_color(TEXT_COLOR, 20)
.draw(
_args.get(1).unwrap_or(&String::from("10iubuoiygbiuyg87tr85riuygiu1011123456789111")),
&mut glyphs,
&ctx.draw_state,
ctx.transform.trans(0.0, 20.0),
g,
)
.unwrap();
glyphs.factory.encoder.flush(device);
});
}
}
Output
Also added to get the text from parameter in case you want to test with different string. here is the full working project.