【问题标题】:Why doesn't my font render?为什么我的字体不渲染?
【发布时间】:2013-01-26 06:27:47
【问题描述】:

我一直在使用带有 lwjgl 的漂亮 GUI,它运行良好,但我不知道如何让它使用我提供的字体;它似乎只能呈现默认字体。我已将包含 .fnt 文件的文件夹添加到运行时类路径(通过 eclipse),这样就不会发生“找不到资源”异常,但是当我运行程序时,任何使用我的字体的文本字段都不会' t 渲染。

这是源代码,它在左上角呈现两个文本字段,在中心呈现两个白色形状。除非我使用默认字体,否则右侧的文本字段不会呈现。

package game;
import static org.lwjgl.opengl.GL11.*;

import org.lwjgl.opengl.*;

import de.lessvoid.nifty.Nifty;
import de.lessvoid.nifty.builder.LayerBuilder;
import de.lessvoid.nifty.builder.ScreenBuilder;
import de.lessvoid.nifty.builder.TextBuilder;
import de.lessvoid.nifty.nulldevice.NullSoundDevice;
import de.lessvoid.nifty.renderer.lwjgl.input.LwjglInputSystem;
import de.lessvoid.nifty.renderer.lwjgl.render.LwjglRenderDevice;
import de.lessvoid.nifty.screen.Screen;
import de.lessvoid.nifty.tools.TimeProvider;

public class NiftyWontUseMyFont {
    private Nifty nifty;
    private Screen screen;
    private float[] WINDOW_DIMENSIONS = {640,480};
    public NiftyWontUseMyFont() throws Exception{
            //init display
            Display.setDisplayMode(new DisplayMode(640,480));
            Display.create();

            //init nifty gui
              LwjglInputSystem inputSystem = new LwjglInputSystem();
              inputSystem.startup();
              nifty = new Nifty(
                new LwjglRenderDevice(),
              new NullSoundDevice(),
                inputSystem,
              new TimeProvider());
            // load default styles
            nifty.loadStyleFile("nifty-default-styles.xml");
            // load standard controls
            nifty.loadControlFile("nifty-default-controls.xml");
            screen = new ScreenBuilder("start") {{
                  layer(new LayerBuilder("baseLayer") {{
                    childLayoutHorizontal();
                    text(new TextBuilder("test"){{
                        font("aurulent-sans-16.fnt");
                        color("#f00f");
                        backgroundColor("#33af");
                        text("l33t");
                    }});
                    text(new TextBuilder("test2"){{
                        font("16-Jackash.fnt");//doesn't work with this external font
//                      font("aurulent-sans-16.fnt");//works with the default font
                        color("#f00f");
                        backgroundColor("#33af");
                        text("l33t");
                    }});
                  }});
                }}.build(nifty);
                nifty.gotoScreen("start");

        //init opengl
                glDisable(GL_DEPTH_TEST);
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glOrtho(0,640,480,0,1,-1);
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
        glViewport(0,0,640,480);
        glPushMatrix();

        glPopMatrix();
        while(!Display.isCloseRequested())
        {
            //render

            glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
            glBegin(GL_QUADS);
            glTexCoord2f(0,0);glVertex2i(400,400); //Upper left
            glTexCoord2f(1,0);glVertex2i(450,400);//upper right
            glTexCoord2f(1,1);glVertex2i(450,450);//bottom right
            glTexCoord2f(0,1);glVertex2i(400,450);//bottom left 
            glEnd();

            glBegin(GL_LINES);
                glVertex2i(100,100);
                glVertex2i(200,200);
            glEnd();

            nifty.update();

            glPushMatrix();
            glPushAttrib(GL_ALL_ATTRIB_BITS);
            nifty.render(false);
            glPopAttrib();
            glPopMatrix();
            Display.update();
            Display.sync(60);
        }
        Display.destroy();
    }
    public static void main(String[] args) throws Exception {
        new NiftyWontUseMyFont();

    }

}

【问题讨论】:

  • jar 中字体的路径是什么?它是在根级别,还是像resources/fonts/16-Jackash.fnt 这样的地方?
  • Font 可能未注册。在main(String[]) 中注册。例如,请参阅this answer作为一般提示,尝试在 J2SE 应用程序中运行一些东西(例如“使用自定义字体”),然后再加载所有其他废话以解决问题(即 LWJGL)。
  • @AndrewThompson 我认为这个答案不适用; Nifty 只接受位图字体 (.fnt),我认为 AWT 不支持这些字体。
  • @d_r_w 它不在根级别,更多的是您的后一个示例。
  • @zergylord 我相信您必须指定相对于 JAR 根目录的完整路径。如果这解决了你的问题,我会重新发布这个作为答案。

标签: java fonts lwjgl nifty-gui


【解决方案1】:

当我将具有该名称的 fnt 添加到我的类路径时,您的示例对我有用。我正在使用 maven,我刚刚将 .fnt 放入 src/main/resources。当您指定字体名称时,Nifty 期望它可以在相对于当前目录的文件系统中或在类路径的根目录中作为类路径资源访问。

没有什么特别的事情发生。您可以检查加载资源的类here

但是,您的 .fnt 文件可能存在问题。您是如何创建该文件的?通常,您需要将相应的 .png 或 .tga 放在与 .fnt 相同的位置,并且 .fnt 内有对 Nifty 读取的实际图像文件的引用。也许 Nifty 可以找到 .fnt 但找不到实际的图像资源?

【讨论】:

  • 哇,很遗憾这就是问题所在——我只有 .fnt 文件,没有 png。我希望我的问题不仅仅是我是一个字体新手,而是唉。非常感谢您容忍通知的无知!
猜你喜欢
  • 1970-01-01
  • 2014-12-29
  • 2015-11-23
  • 2013-11-09
  • 2017-02-02
  • 2014-01-20
  • 1970-01-01
  • 2014-09-02
  • 2015-07-24
相关资源
最近更新 更多