LWJGL Textureloader not working?

Okay, so I am new to LWJGL. I know Java pretty well, though. Anyway, I decided to create a clone of Pong, just to try it out, and had it working right up until I wanted to display text. Well, I just made a bunch of font files and was going to make a drawString() method. It would look like this:

public void drawString(String draw, float x, float y) {
		StringTokenizer t = new StringTokenizer(draw);
		String[] chars = new String[2048];
		for(int i = 0;i<draw.length();i++) {
			chars[i] = t.nextToken("");
		}
		Texture[] tex = new Texture[2048];
		glEnable(GL_TEXTURE_2D);
		for(int j = 0;j<draw.length();j++) {
			try {
				tex[j] = TextureLoader.getTexture("PNG", new FileInputStream("res/font/" + chars[j] + ".png"));
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		int o = 0;
		for(int a = 0;a<tex.length;a++) {
			drawRect(x+o, y, FONT_CONST, FONT_CONST, tex[a]);
		}
	
	}

But, when I run, since the score is 0 it tries to load a Texture. It doesn’t report a FileNotFound or IOException, but is passing the Texture as null, since I keep getting NullPointerExceptions from the drawRect() call. This is really confusing me; I would be very grateful for help.

20 hours, 63 views, but not even a single person commenting they don’t even have a clue what’s going on? -____-

First, afaik there is no “TextureLoader” class in LWJGL, are you using some external library?
Second, not related to the problem but I don’t think it’s a good idea to create textures in a loop, textures should be created only once then used multiple times after creation. Except that your drawString method is called only once, which I don’t think is the case.

Your code has a lot of errors.

  • Don’t create strings with new String[]
  • Don’t create a new Texture object every frame… that will be slow. Generally you should try to never allocate new objects within your game loop.
  • Use a proper bitmap font renderer instead of rendering each glyph as its own texture. For example:
    https://github.com/mattdesl/lwjgl-basics

Generally your method shows you don’t understand LWJGL enough to be working with text yet. Learn to walk before you run… Check out the basics here:


If you’re still having trouble with OpenGL, maybe a solution like LibGDX would be easier to get started with.

Yeah, noticed I forgot to mention that I used slick-util for the TextureLoader. I don’t know how to edit posts though. Anyway, i’ve been trying to figure this out, and if I load a texture in the constructor of my MainComponent they display just fine. So should I just load ALL the textures at once on startup into the Texture[] array and then use them as needed? Btw thanks SO MUCH just for replying this has been bugging me SO BAD
(no pun intended)

@davedes I saw many posts on the web about making one 256x256 texture with the whole font on it, each character in a 16x16 square, and I tried to use that. My idea was when using the glTexCoord(x,y) to use a floating point to specify which character I wanted. I figured since the chars were 16px long and the whole thing was 256px, in order to map the second(to the right) char of the very top row, the coords would be(0.0625, 0), being that the length of one char was 0.0625(1/16). But, i was getting stretched images of several chars at once, so I switched to the individual characters.

This is what I do to get the texture “slice” of a character in a 256x256 image, where each character is 16x16

glCoord2f(x/256f, 		y/256f);
		glCoord2f((x+16)/256f, 	y/256f);
		glCoord2f((x+16)/256f, 	(y+16)/256f);
		glCoord2f(x/256f, 		(y+16)/256f);

Where x and y are the positions of the character.

I’m wayyy confused; What is glCoord() doing? Is it like glVertex()? Because if i divided the actual plotting point by 256, i’d be way off of the desired drawing point.

EDIT: Okay, now I get it, the x and y refer to the individual characters. Like the top right would be (16,0). Okay. So now, i’ll use a HashMap to connect the characters to their corresponding (x,y) coords and then use that to pick and draw the character. Sweet!

You need to learn about texture coordinates:

For bitmap fonts usually you would use a tool like this:
http://www.angelcode.com/products/bmfont/

And parse/render the fonts in a sprite sheet for performance: