为什么我的字体不能渲染? [英] Why doesn't my font render?

查看:202
本文介绍了为什么我的字体不能渲染?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在用lwjgl使用漂亮的图形用户界面,它的工作很好,但是我不知道如何让它使用我提供的字体。它似乎只能呈现默认的字体。我已经添加了我的.fnt文件的文件夹到运行时类路径(通过eclipse),这使得没有资源未找到异常发生,但是当我运行该程序,任何文本字段使用我的字体don' t渲染。

这里是源代码,它在左上角呈现两个文本字段,在中间呈现两个白色的形状。

 包装游戏;如果我使用默认的字体, 
import static org.lwjgl.opengl.GL11。*;

import org.lwjgl.opengl。*;

导入de.lessvoid.nifty.Nifty;
import de.lessvoid.nifty.builder.LayerBuilder;
import de.lessvoid.nifty.builder.ScreenBuilder;
导入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;
导入de.lessvoid.nifty.screen.Screen;
导入de.lessvoid.nifty.tools.TimeProvider;

public class NiftyWontUseMyFont {
private Nifty漂亮;
私人屏幕;
私人浮动[] WINDOW_DIMENSIONS = {640,480};
public NiftyWontUseMyFont()抛出异常{
// 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,$ b $ new New TimeProvider());
//载入默认样式
nifty.loadStyleFile(nifty-default-styles.xml);
//加载标准控件
nifty.loadControlFile(nifty-default-controls.xml);
screen = new ScreenBuilder(start){{
layer(new LayerBuilder(baseLayer){{
childLayoutHorizo​​ntal();
text(new TextBuilder(test ){{
font(aurulent-sans-16.fnt);
color(#f00f);
backgroundColor(#33af);
text l33t);
}});
text(new TextBuilder(test2){{
font(16-Jackash.fnt); // does not这个外部字体
//字体(aurulent-sans-16.fnt); //使用默认字体
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(); (!Display.isCloseRequested())
{
//渲染

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glBegin(GL_QUADS);
glTexCoord2f(0,0); glVertex2i(400,400); //左上角
glTexCoord2f(1,0); glVertex2i(450,400); //右上角
glTexCoord2f(1,1); glVertex2i(450,450); //右下角
glTexCoord2f 0,1); glVertex2i(400,450); //左下角
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();




$ b

解决方案

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



没什么特别的。您可以检查加载资源的类 here

但是,.fnt文件可能存在问题。你是如何创建该文件的?通常你需要相应的.png或.tga与.fnt处于相同的位置,而.fnt里面有一个对Nifty读取的实际图像文件的引用。也许Nifty可以找到.fnt,但不是实际的图像资源?

I've been using nifty GUI with lwjgl, and its been working great, but I can't figure out how to get it to use fonts I've provided it; it only seems to be able to render the default fonts. I've added the folder with my .fnt files to the runtime classpath (via eclipse), and this makes it so that no "resource not found" exception occurs, but when I run the program, any text fields using my font don't render.

Here's the source code, it renders two text fields in the upper left corner and 2 white shapes in the center. the textfield on the right won't render unless I use the default font.

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();

    }

}

解决方案

Your example works for me when I add a fnt with that name to my classpath. I'm using maven and I've just put the .fnt into src/main/resources. When you specify the font name Nifty expects it to be accessible either in the filesystem relative to the current directory OR at the root at the classpath as a classpath resource.

There is nothing special going on. You can inspect the class that loads the resources here

However, there might be a problem with your .fnt file. How did you create that file? And usually you need the corresponding .png or .tga in the same position as your .fnt AND there is a reference inside the .fnt to the actual image file which Nifty reads. Maybe Nifty can find the .fnt but not the actual image resource?

这篇关于为什么我的字体不能渲染?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆