Hi !
Featured games (85)
games approved by the League of Dukes
Games in Showcase (616)
Games in Android Showcase (173)
games submitted by our members
Games in WIP (659)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  LWJGL Textureloader not working?  (Read 928 times)
0 Members and 1 Guest are viewing this topic.
Offline riun23

Junior Newbie

« Posted 2013-08-18 20:40:43 »

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];
      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
         } catch (IOException e) {
            // TODO Auto-generated catch block
      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.
Offline riun23

Junior Newbie

« Reply #1 - Posted 2013-08-19 16:41:22 »

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

Junior Devvie

Medals: 4

« Reply #2 - Posted 2013-08-19 16:49:17 »

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.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline davedes
« Reply #3 - Posted 2013-08-19 16:51:29 »

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:

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.

Offline riun23

Junior Newbie

« Reply #4 - Posted 2013-08-19 16:54:00 »

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)
Offline riun23

Junior Newbie

« Reply #5 - Posted 2013-08-19 16:59:20 »

@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.
Offline Zhon

Junior Devvie

Medals: 4

« Reply #6 - Posted 2013-08-19 17:10:04 »

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.
Offline riun23

Junior Newbie

« Reply #7 - Posted 2013-08-19 17:21:23 »

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!
Offline davedes
« Reply #8 - Posted 2013-08-19 18:04:46 »

You need to learn about texture coordinates:

For bitmap fonts usually you would use a tool like this:

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

Pages: [1]
  ignore  |  Print  
You cannot reply to this message, because it is very, very old.

Coldstream24 (16 views)
2015-09-03 00:41:28

Andrew_3ds (26 views)
2015-09-01 19:08:10

afikri (17 views)
2015-08-31 09:30:22

afikri (25 views)
2015-08-31 09:30:07

afikri (13 views)
2015-08-31 09:27:24

afikri (16 views)
2015-08-31 09:26:40

Roquen (30 views)
2015-08-29 11:30:54

GamerC4 (36 views)
2015-08-22 20:38:50

GamerC4 (33 views)
2015-08-22 20:37:18

GamerC4 (40 views)
2015-08-22 20:37:01
HotSpot Options
by Roquen
2015-08-29 11:33:11

Rendering resources
by Roquen
2015-08-17 12:42:29

Rendering resources
by Roquen
2015-08-17 09:36:56

Rendering resources
by Roquen
2015-08-13 07:40:51

Networking Resources
by Roquen
2015-08-13 07:40:43

List of Learning Resources
by gouessej
2015-07-09 11:29:36

How Do I Expand My Game?
by bashfrog
2015-06-14 11:34:43

List of Learning Resources
by PocketCrafter7
2015-05-31 05:37:30 is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!