You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ggez引擎Font未找到问题及Text字体设置方法问询

问题:ggez中Font未找到,无法显示文本

尝试用ggez引擎在屏幕显示简单文本时,遇到以下错误:

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`

以下是我的代码:

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)
}

解决方法

1. 修正Font的引用路径

ggez版本更新后,Font不再直接放在graphics模块下,而是移到了graphics::font子模块中。你需要:

  • 直接引用graphics::font::Font,或者在顶部添加导入:use ggez::graphics::font::Font;
  • 获取默认字体可以直接用Font::default(),无需传入Context;加载自定义字体时,确保字体文件放在你设置的resources目录下,路径写相对路径即可(比如"font.ttf")。

2. 更新Text的创建与尺寸获取API

新版本ggez中,Text的创建方式和尺寸获取方法有变化:

  • 不再支持Text::new((text, font, size))的元组写法,需要用TextFragment配置样式后构建Text
  • 获取文本尺寸改用text.dimensions(ctx)方法,替代旧的graphics::dimensions

修正后的完整代码

use ggez::{event, graphics, graphics::font::Font, Context, GameResult};
use std::env;
use std::path;

struct MainState {
    font: Font,
}

impl MainState {
    fn new(_ctx: &mut Context) -> GameResult<MainState> {
        // 使用默认字体,或者加载自定义字体:Font::new(ctx, "font.ttf")?
        let font = 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_content = "Hello World!";
        // 创建文本片段,指定内容、字体和大小
        let fragment = graphics::TextFragment::new(text_content)
            .font(self.font.clone())
            .scale(graphics::PxScale::from(24.0));
        // 构建Text对象
        let text = graphics::Text::new(fragment);
        
        // 获取文本尺寸
        let text_dimensions = text.dimensions(ctx);
        let text_position = graphics::Vec2::new(
            (graphics::screen_width(ctx) - text_dimensions.w as f32) / 2.0,
            (graphics::screen_height(ctx) - text_dimensions.h as f32) / 2.0,
        );
        
        // 绘制文本
        graphics::draw(ctx, &text, (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)
}

内容的提问来源于stack exchange,提问作者Rifat

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.17 22:18:16