How to set font to Text?
Need to show simple text on screen with ggez engine. But getting these errors:
error[E0433]: failed to resolve: could not find `Font` in `graphics`
--> src/main.rs:11:30
|
11 | let font = graphics::Font::default();
| ^^^^ could not find `Font` in `graphics`
Code:
use ggez::{event, graphics, input, Context, GameResult};
use std::env;
use std::path;
struct MainState {
font: graphics::Font,
}
impl MainState {
fn new(ctx: &mut Context) -> GameResult<MainState> {
let font = graphics::Font::new(ctx, "/path/to/font.ttf")?; // lets say, Font.Default();
Ok(MainState { font })
}
}
impl event::EventHandler for MainState {
fn update(&mut self, _ctx: &mut Context) -> GameResult {
Ok(())
}
fn draw(&mut self, ctx: &mut Context) -> GameResult {
graphics::clear(ctx, [1.0, 1.0, 1.0, 1.0].into());
let text = "Hello World!";
let text_fragment = graphics::Text::new((text, self.font, 24.0));
let text_dimensions = graphics::dimensions(ctx, &text_fragment)?;
let text_position = graphics::Point2::new(
(graphics::screen_width(ctx) - text_dimensions.w) / 2.0,
(graphics::screen_height(ctx) - text_dimensions.h) / 2.0,
);
graphics::draw(ctx, &text_fragment, (text_position,))?;
graphics::present(ctx)?;
Ok(())
}
}
fn main() -> GameResult {
let resource_dir = if let Ok(manifest_dir) = env::var("CARGO_MANIFEST_DIR") {
let mut path = path::PathBuf::from(manifest_dir);
path.push("resources");
path
} else {
path::PathBuf::from("./resources")
};
let (mut ctx, mut event_loop) = ggez::ContextBuilder::new("hello_world", "ggez")
.add_resource_path(resource_dir)
.build()
.expect("aieee, could not create ggez context!");
let state = MainState::new(&mut ctx)?;
event::run(ctx, event_loop, state)
}
There is a ggez::graphics::Font in versions up to 0.7.1, but later versions do not have it anymore. There don't seem to be any release notes mentioning this, since 0.8.0 does not seem to have release notes.
Anyway, if you look at the TextFragment struct in 0.7.1:
pub struct TextFragment {
pub text: String,
pub color: Option<Color>,
pub font: Option<Font>,
pub scale: Option<PxScale>,
}
and in 0.9.3:
pub struct TextFragment {
pub text: String,
pub font: Option<String>,
pub scale: Option<PxScale>,
pub color: Option<Color>,
}
then it seems clear that Font
got replaced with a plain String
.