3

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

Taylor Golden
  • 41
  • 1
  • 2
  • 10
  • I didn't touch your post, I retagged it with `Slick2D` so more people would see it. I've never worked with Slick before. – David B Jun 21 '12 at 19:00
  • Sorry! I just hate it when people do that though. Not what you done, that's great. – Taylor Golden Jun 21 '12 at 19:01
  • You mentions ninjacave's tutorials but have you looked at the ones on the slick2d wiki? http://slick.cokeandcode.com/wiki/doku.php?id=tutorials One of them in there taught me how to write text but I don't remember which ones. Unfortunately, I can't answer you cause I don't have the code handy. Ill try to dig it up and give you an example. – Jesse Webb Jun 21 '12 at 19:03
  • Thanks Jesse. It seems that the one that @David B provided was quite hard to follow. – Taylor Golden Jun 21 '12 at 19:06
  • Are you sure you are even using/learnign Slick2D? Nothing in the code you posted is using Slick2D, only lwjgl. You will want to check out the tutorials i linked to in my other comment and try actually using Slick2D, it will do most of what your code is doing automatically for you. – Jesse Webb Jun 21 '12 at 19:18
  • Jesse is correct, you don't have Slick2D in that code at all. – David B Jun 21 '12 at 19:32

3 Answers3

3

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.

Jesse Webb
  • 43,135
  • 27
  • 106
  • 143
  • That comes up with a green streak across the screen. Are there any "gl" things I need to add? – Taylor Golden Jun 21 '12 at 19:22
  • I'll post a pic of what I'm getting! – Taylor Golden Jun 21 '12 at 19:37
  • Can you pls explain where you got that graphics variable from –  Jan 08 '17 at 21:21
  • @IchHabsDrauf This code is meant to be executed inside a `render` method which has a signature of: `public void render(GameContainer gc, Graphics g)`. It is the Slick library which calls this function for you and provides you with the `Graphics` instance. – Jesse Webb Jan 10 '17 at 20:59
3

Download the Slick2D library and import the jar.

Inside your class to cover the scope of the whole class:

Font font;
TrueTypeFont ttf;

Your init method

public void init(GameContainer gc){

    font = new Font("Verdana", Font.BOLD, 20);
    ttf = new TrueTypeFont(font, true);

}

Your render method:

public void render(GameContainer gc, Graphics g){

    //render code here
    ttf.drawString("Hello, World!")

}
Michael Curry
  • 991
  • 8
  • 20
2

According to this tutorial, you can call drawString from your graphics object in the render method.

g.drawString("Hello World!",200,200);

David B
  • 2,688
  • 18
  • 25
  • Thanks, I'll give it a try now. Sorry for my comment earlier. :'( – Taylor Golden Jun 21 '12 at 19:04
  • No problem. If it works for you, mark the answer *accepted* by clicking on the green checkmark underneath the vote number. – David B Jun 21 '12 at 19:06
  • Sorry, the tutorial I find really hard to follow. But thanks anyway. – Taylor Golden Jun 21 '12 at 19:06
  • @TaylorGolden How proficient are you with Java, sans Slick? Learning this library may be difficult if you don't possess some core Java skills. – David B Jun 21 '12 at 19:08
  • That tutorial is for if I am extending by the BasicGame class. I'm not though. Thanks but it wasn't the right one for me. Mostly my fault for not posting the code in the question. I will do that now. – Taylor Golden Jun 21 '12 at 19:13
  • What *Game class you use doesn't change how you `drawString()`. What does matter is that it appears as though you aren't using Slick2D at all, only lwjgl. You may want to search for lwjgl tutorials specifically, or try going through the 'Spiegel' tutorials here: http://slick.cokeandcode.com/wiki/doku.php?id=tutorials – Jesse Webb Jun 21 '12 at 22:35
  • where do you get that g variable from –  Jan 08 '17 at 21:25