5

I want to write text on the screen for my game, for things like fps, random text for items and stuff. How can I write that text?

Is it possible without the Basic Game class? Isn't there a command like this g.drawString("Hello World", 100, 100);?

julian
  • 4,188
  • 9
  • 38
  • 58
  • LWJGL is really quite a thin wrapper around OpenGL - if you want to do things like this more simply I'd suggest something like JMonkey which provides a scenegraph API. – Michael Berry Oct 04 '13 at 13:55
  • 2
    are you saying that its better move to a game engine if i want to write text? – julian Oct 04 '13 at 14:00
  • You say you're looking for a simple `drawString()` type method, which implies you want simplicity - JMonkey will make things simpler in this respect (and many others). – Michael Berry Oct 04 '13 at 17:56

2 Answers2

4

Update: this answer is now outdated, and does not work at all with the latest versions of LWJGL. Until I update this answer fully, I recommend that you look here: http://www.java-gaming.org/topics/lwjgl-stb-bindings/36153/view.html


You could use the TrueType fonts feature in the Slick-util library.

Using a common font is easy, just create the font like this:

TrueTypeFont font;
Font awtFont = new Font("Times New Roman", Font.BOLD, 24); //name, style (PLAIN, BOLD, or ITALIC), size
font = new TrueTypeFont(awtFont, false); //base Font, anti-aliasing true/false

If you want to load the font from a .ttf file, it's a little more tricky:

try {
    InputStream inputStream = ResourceLoader.getResourceAsStream("myfont.ttf");

    Font awtFont = Font.createFont(Font.TRUETYPE_FONT, inputStream);
    awtFont = awtFont.deriveFont(24f); // set font size
    font = new TrueTypeFont(awtFont, false);

} catch (Exception e) {
    e.printStackTrace();
}

After you have successfully created a TrueTypeFont, you can draw it like this:

font.drawString(100, 50, "ABC123", Color.yellow); //x, y, string to draw, color

For more information, you can look at the documentation for TrueTypeFont and java.awt.Font, and the Slick-Util tutorial I got most of this code from.

The Guy with The Hat
  • 9,689
  • 7
  • 51
  • 69
  • Could you please update the links? They have already expired. Thank you in advance. – Andi Jan 08 '17 at 20:37
  • @IchHabsDrauf Thanks for letting me know; I'll get to it when I can. – The Guy with The Hat Jan 08 '17 at 21:44
  • I tried both of your suggestions and none of them worked for me. The first one was printing just a yellow Rect at the specified location in the window. I tried the second option, by loading Consolas.ttf from my Control Panel into a directory in my project folder and used the inputStream just like in your example but it did not work. It still drew just a yellow Reck. What should I do? – Andi Jan 09 '17 at 12:16
1

Try Making a BufferedImage of required size. Then get its Graphics and draw a String. Then Convert it to a ByteBuffer and render it in OpenGL.

String text = "ABCD";
int s = 256; //Take whatever size suits you.
BufferedImage b = new BufferedImage(s, s, BufferedImage.TYPE_4BYTE_ABGR);
Graphics2D g = b.createGraphics();
g.drawString(text, 0, 0);

int co = b.getColorModel().getNumComponents();

byte[] data = new byte[co * s * s];
b.getRaster().getDataElements(0, 0, s, s, data);

ByteBuffer pixels = BufferUtils.createByteBuffer(data.length);
pixels.put(data);
pixels.rewind();

Now pixels contains the required Image data you need to draw. Use GL11.glTexImage2D() function to draw the byte buffer.