0

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

    }

}
zergylord
  • 4,368
  • 5
  • 38
  • 60
  • What is the path to the font inside your jar? Is it at the root level, or is it somewhere like `resources/fonts/16-Jackash.fnt`? – CassOnMars Jan 24 '13 at 21:11
  • The `Font` is probably not registered. Register it in the `main(String[])`. See [this answer](http://stackoverflow.com/questions/10417907/importing-font-to-gui/10418150#10418150) for e.g. As a general tip, try to get something (e.g. 'use custom font') working in a J2SE app., before loading all this other crap on top of the problem (i.e. LWJGL). – Andrew Thompson Jan 24 '13 at 21:25
  • @AndrewThompson I don't think that answer applies; Nifty only accepts bitmap fonts (.fnt) and I don't believe AWT works with those. – zergylord Jan 24 '13 at 21:56
  • @d_r_w it's not at the root level, its more of your latter example. – zergylord Jan 24 '13 at 21:57
  • @zergylord I believe you have to specify the full path relative to the root of your JAR. If this solves your problem, I'll repost this as the answer. – CassOnMars Jan 25 '13 at 00:30
  • @d_r_w I've tried that and it doesn't seem to make a difference. I don't think I should have to since I added the absolute path to the class path. Also, my .fnt files aren't compressed into jars; do they need to be? – zergylord Jan 25 '13 at 02:17
  • @d_r_w Tried putting all of the fonts into a jar and adding that to my libraries, to no avail :( – zergylord Jan 25 '13 at 02:30
  • @zergylord I've tried it on my system. You need a relative path to the .fnt file. So if you've set up the Eclipse project as follows: /src/ <- java source files and set as a source folder /assets/ <-- asset files and set as source folder containing the package gui.fonts with 16-Jackash.fnt in it. Then set the font paramater to gui/fonts/16-Jackash.fnt. If this works I'll create an answer. – JeroenWarmerdam Jan 25 '13 at 16:19
  • @JeroenWarmerdam This sounds like the correct solution, but I still can't quite get it working. I added /fonts as a src folder and tried setting the font parameter to both fonts/16-Jackash.fnt and 16-Jackash.fnt, but it still isn't working. – zergylord Jan 25 '13 at 17:58
  • @JeroenWarmerdam Confused by what this means:"set as a source folder /assets/ <-- asset files and set as source folder containing the package gui.fonts with 16-Jackash.fnt in it." Is a folder named "assets" special requirement of nifty or something? Likewise with packaging it inside gui.fonts? – zergylord Jan 25 '13 at 18:00

1 Answers1

2

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?

void256
  • 1,286
  • 10
  • 11
  • Wow, sadly this was the problem -- I only had the .fnt files and no pngs. I was hoping my problem wasn't just me being a font newb, but alas. Thanks so much for bearing with a notice's ignorance! – zergylord Jan 26 '13 at 21:18