Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (527)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (594)
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 608 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:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
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.
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:
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:
https://github.com/mattdesl/lwjgl-basics/wiki/Display
https://github.com/mattdesl/lwjgl-basics/wiki/Textures

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
1  
2  
3  
4  
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:
https://github.com/mattdesl/lwjgl-basics/wiki/Textures#texture-atlases

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:
https://github.com/mattdesl/lwjgl-basics/blob/master/src/mdesl/graphics/text/BitmapFont.java

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

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

PocketCrafter7 (12 views)
2014-11-28 16:25:35

PocketCrafter7 (7 views)
2014-11-28 16:25:09

PocketCrafter7 (8 views)
2014-11-28 16:24:29

toopeicgaming1999 (75 views)
2014-11-26 15:22:04

toopeicgaming1999 (65 views)
2014-11-26 15:20:36

toopeicgaming1999 (15 views)
2014-11-26 15:20:08

SHC (29 views)
2014-11-25 12:00:59

SHC (27 views)
2014-11-25 11:53:45

Norakomi (32 views)
2014-11-25 11:26:43

Gibbo3771 (28 views)
2014-11-24 19:59:16
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org 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‑gaming.org
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!