Why doesn't my font render?

醉酒当歌 提交于 2020-01-06 07:57:10

问题


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

    }

}

回答1:


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?



来源:https://stackoverflow.com/questions/14510730/why-doesnt-my-font-render

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!