How can I display text with Slick?

Taylor Golden picture Taylor Golden · Jun 21, 2012 · Viewed 10.8k times · Source

I have been trying to display text with ninjacave's tutorials and lot's of others but I can't get it to work. Could someone give me a link to a tutorial that works? Thanks.

The code is below:

Main Class:

package oregon.client;

import oregon.src.*;

import org.lwjgl.*;
import org.lwjgl.opengl.*;

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

public class Oregon {
    public static Settings settings = new Settings();

    public GameController controls = new GameController();

    public FPSCounter fps = new FPSCounter();

    public void init() {
        try {
            if (settings.fullscreen) {
                Display.setDisplayModeAndFullscreen(Display
                        .getDesktopDisplayMode());
            } else if (!settings.fullscreen) {
                Display.setDisplayModeAndFullscreen(new DisplayMode(
                        settings.defaultWidth, settings.defaultHeight));
            }

            Display.setVSyncEnabled(true);
            Display.create();
        } catch (LWJGLException e) {
            stop(e);
        }

        initOpenGL();
        start();
    }

    public void initOpenGL() {
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glOrtho(0, settings.defaultWidth, settings.defaultHeight, 0, 1, -1);
        glMatrixMode(GL_MODELVIEW);
    }

    public void debug() {     

    }

    public void openGL() {

    }

    public void start() {
        while (!Display.isCloseRequested()) {
            controls.keyboard();
            fps.tick();

            debug();
            openGL();

            Display.update();
            Display.sync(100);
        }

        Display.destroy();
    }

    public static void stop() {
        System.exit(0);
        Display.destroy();
    }

    public static void stop(Exception e) {
        e.printStackTrace();
        System.exit(0);
        Display.destroy();
    }

    public static void setFullscreen() {
        try {
            Display.setDisplayModeAndFullscreen(Display.getDesktopDisplayMode());
        } catch (LWJGLException e) {
            stop(e);
        }
    }

    public static void removeFullscreen() {
        try {
            Display.setDisplayModeAndFullscreen(new DisplayMode(
                    settings.defaultWidth, settings.defaultHeight));
        } catch (LWJGLException e) {
            stop(e);
        }
    }

    public static void main(String args[]) {
        Oregon oregon = new Oregon();
        oregon.init();
    }
}

That was the code for the main-class.

EDIT:- This is a picture of what I'm getting with Jesse B's code. enter image description here

Answer

Jesse Webb picture Jesse Webb · Jun 21, 2012

DavidB mentions in his answer using the graphics instance to drawString(). This will use your current font. This means you should call graphics.setFont() first, otherwise it will use the default system font. This also makes it hard to remember (in various methods of your game) which is the 'current' font.

Alternatively, you can initialize a font instance and use it directly to drawString. From Slick2D Wiki...

// initialise the font
Font font = new Font("Verdana", Font.BOLD, 20);
TrueTypeFont trueTypeFont = new TrueTypeFont(font, true);

// render some text to the screen
trueTypeFont.drawString(20.0f, 20.0f, "Slick displaying True Type Fonts", Color.green);

This requires that all supported platforms can resolve the font name. A better option is to embed the font in your JAR by placing it it your resources directory.

EDIT
After seeing the picture you posted, my guess is that you don't have the font loading correctly. Try this instead...

graphics.drawString("Test drawing string.",20.0f,20.0f);

If this works, it confirms the font isn't working. If you want to use custom fonts, you will have to add your font file into your JAR and reference it as a resource.