Write text on the screen with LWJGL
Asked Answered
B

2

7

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

Ballance answered 4/10, 2013 at 13:44 Comment(3)
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.Aery
are you saying that its better move to a game engine if i want to write text?Ballance
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).Aery
D
5

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: https://jvm-gaming.org/t/lwjgl-stb-bindings/54537


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.

Disenthrone answered 4/10, 2013 at 16:42 Comment(3)
Could you please update the links? They have already expired. Thank you in advance.Radiocommunication
@IchHabsDrauf Thanks for letting me know; I'll get to it when I can.Disenthrone
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?Radiocommunication
S
2

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.

Stabler answered 17/10, 2014 at 17:3 Comment(2)
could you please explain how we can use the glTexImage2D() in this case?Radiocommunication
could you please explain how we can use the glTexImage2D() in this case?Radiocommunication

© 2022 - 2024 — McMap. All rights reserved.