Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (497)
Games in Android Showcase (114)
games submitted by our members
Games in WIP (563)
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  
  How can one draw text?  (Read 1258 times)
0 Members and 1 Guest are viewing this topic.
Offline nhmllr

Senior Member


Medals: 1
Projects: 3


slow and steady...


« Posted 2013-11-11 02:36:56 »

I'm really sad. I'm trying to use LWJGL to draw some text on the screen, nothing fancy, I just need to be able to read some info. I find that I'll need Slick. I get slick. I use this ( http://www.lwjgl.org/wiki/index.php?title=Slick-Util_Library_-_Part_3_-_TrueType_Fonts_for_LWJGL ) to try and draw something with TrueTypeFont. Doesn't work, also deprecated. I find that I need to use "UnicodeFont". I use that, but I get null pointer errors which no one online seems to get (specifically this:)

Exception in thread "Thread-0" java.lang.IllegalStateException: The UnicodeFont must have at least one effect before any glyphs can be loaded.
   at org.newdawn.slick.UnicodeFont.loadGlyphs(UnicodeFont.java:340)
   at org.newdawn.slick.UnicodeFont.loadGlyphs(UnicodeFont.java:325)
Any API's I find are dead links.

I have a very simple question. How do I draw text on my screen?
Offline kramin42
« Reply #1 - Posted 2013-11-11 03:21:27 »

Try this:

1  
2  
3  
4  
5  
6  
7  
8  
Font awtFont = new Font("Default", Font.PLAIN, 14);
font = new UnicodeFont(awtFont);
font.getEffects().add(new ColorEffect(java.awt.Color.white));
font.addAsciiGlyphs();
try { font.loadGlyphs(); }
catch (SlickException e) {
    e.printStackTrace();
}


I'm guessing you don't have a "font.getEffects().add(...)".


"All science is either physics or stamp collecting." - Ernest Rutherford.
CodeGolf4k
M4nkala
Offline nhmllr

Senior Member


Medals: 1
Projects: 3


slow and steady...


« Reply #2 - Posted 2013-11-11 03:32:50 »

No dice

Exception in thread "Thread-0" java.lang.NoSuchMethodError: org.newdawn.slick.opengl.renderer.SGL.glTexSubImage2D(IIIIIIIILjava/nio/ByteBuffer;)V
   at org.newdawn.slick.font.GlyphPage.renderGlyph(GlyphPage.java:212)
   at org.newdawn.slick.font.GlyphPage.loadGlyphs(GlyphPage.java:166)
   at org.newdawn.slick.UnicodeFont.loadGlyphs(UnicodeFont.java:376)
   at org.newdawn.slick.UnicodeFont.loadGlyphs(UnicodeFont.java:325)

It doesn't like "try { font.loadGlyphs(); }"
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline kramin42
« Reply #3 - Posted 2013-11-11 03:42:20 »

This looks really weird: Huh
org.newdawn.slick.opengl.renderer.SGL.glTexSubImage2D(IIIIIIIILjava/nio/ByteBuffer;)V

Are you using the latest version of slick?

EDIT:
also: https://bitbucket.org/kevglass/slick/issue/29/unicodefontloadglyphs-throws

"All science is either physics or stamp collecting." - Ernest Rutherford.
CodeGolf4k
M4nkala
Offline nhmllr

Senior Member


Medals: 1
Projects: 3


slow and steady...


« Reply #4 - Posted 2013-11-11 03:43:41 »

I used the one at ninjacave, that's the only one i could find
http://slick.cokeandcode.com/
Offline nhmllr

Senior Member


Medals: 1
Projects: 3


slow and steady...


« Reply #5 - Posted 2013-11-11 03:48:13 »

Where can I even get the latest version of Slick?
Offline kramin42
« Reply #6 - Posted 2013-11-11 03:54:00 »

See my edit above and check your build path, what does it have in it? e.g. for one of my projects which uses font rendering I have lwjgl_util.jar, lwjgl.jar, and slick.jar.

You probably have the latest version.

"All science is either physics or stamp collecting." - Ernest Rutherford.
CodeGolf4k
M4nkala
Offline nhmllr

Senior Member


Medals: 1
Projects: 3


slow and steady...


« Reply #7 - Posted 2013-11-11 04:00:27 »

Here is a picture what my buildpath looks like

http://tinypic.com/r/2j0ioap/5
Offline kramin42
« Reply #8 - Posted 2013-11-11 04:06:37 »

From what this said it's probably an incompatability between you slick.jar and slick_util.jar versions. The latest slick download doesn't even include slick_util so try removing it, maybe the slick_util stuff was combined with slick in the latest version.

"All science is either physics or stamp collecting." - Ernest Rutherford.
CodeGolf4k
M4nkala
Offline nhmllr

Senior Member


Medals: 1
Projects: 3


slow and steady...


« Reply #9 - Posted 2013-11-11 04:23:30 »

That was a good idea. I no longer have any errors. However, I still cannot see my text.
I load everything like this:


1  
2  
3  
4  
5  
6  
7  
8  
      Font awtFont = new Font("Times New Roman", Font.PLAIN, 14);
      font = new UnicodeFont(awtFont);
      font.getEffects().add(new ColorEffect(java.awt.Color.white));
      font.addAsciiGlyphs();
      try { font.loadGlyphs(); }
      catch (SlickException e) {
          e.printStackTrace();
      }


And draw it like this:

1  
         font.drawString(0.2f, 0.2f, "efer");


Is there anything else I need to include to get it to draw properly?

Thank you
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline opiop65

JGO Kernel


Medals: 154
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #10 - Posted 2013-11-11 04:30:27 »

Do you have other textures drawing before the text? If so, call
1  
TextureImpl.unbind();

Or it could be
1  
TextureImpl.bindNone();

I can't remember which one Sad

Offline kramin42
« Reply #11 - Posted 2013-11-11 04:45:28 »

How are your matrices set up? e.g. I'm doing:
1  
2  
3  
4  
5  
6  
7  
8  
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glOrtho(0, w, h, 0, 1, -1);// w is the screen width and h is the height
       glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
        glEnable(GL_BLEND);
        glDisable(GL_DEPTH_TEST);
        font.drawString(10, 10, "Text");//draws text 10 pixels down and to the right of the top left corner

Maybe your text is getting drawn off-screen? I don't really know tbh.

"All science is either physics or stamp collecting." - Ernest Rutherford.
CodeGolf4k
M4nkala
Offline nhmllr

Senior Member


Medals: 1
Projects: 3


slow and steady...


« Reply #12 - Posted 2013-11-11 05:02:39 »

That works! The only problem is that is removes everything else drawn from the screen. How can I use different GL settings at different times to overlay them?
Offline kramin42
« Reply #13 - Posted 2013-11-11 05:22:43 »

Oh yeah, you need to set the matrices back to what they were again afterwards. I think this should work:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
        glMatrixMode(GL_PROJECTION);
        glPushMatrix();
        glLoadIdentity();
        glOrtho(0, w, h, 0, 1, -1);// w is the screen width and h is the height
       glMatrixMode(GL_MODELVIEW);
        glPushMatrix();
        glLoadIdentity();
        glEnable(GL_BLEND);
        glDisable(GL_DEPTH_TEST);
        font.drawString(10, 10, "Text");//draws text 10 pixels down and to the right of the top left corner
       // do rest of text drawing here + any other overlay stuff
       glMatrixMode(GL_PROJECTION);
        glPopMatrix();
        glMatrixMode(GL_MODELVIEW);
        glPopMatrix();
        glEnable(GL_DEPTH_TEST);


Also make sure any overlay type stuff is rendered last because if depth test is disabled it draws in the order of the commands.

"All science is either physics or stamp collecting." - Ernest Rutherford.
CodeGolf4k
M4nkala
Offline nhmllr

Senior Member


Medals: 1
Projects: 3


slow and steady...


« Reply #14 - Posted 2013-11-11 06:17:46 »

It works! Thank you guys so much!

Just one thing, is there a way to make the text look smoother?
I tried to render Courier, but it looks like this
http://tinypic.com/r/154ammx/5

(I have only copied the code found in this thread)
Offline kramin42
« Reply #15 - Posted 2013-11-11 06:35:09 »

Add glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); somewhere before rendering the text. I thought it got set to that by default.

"All science is either physics or stamp collecting." - Ernest Rutherford.
CodeGolf4k
M4nkala
Offline nhmllr

Senior Member


Medals: 1
Projects: 3


slow and steady...


« Reply #16 - Posted 2013-11-11 07:10:36 »

Excellent, thank you! This was a very confusing block of code.
Offline trollwarrior1
« Reply #17 - Posted 2013-11-11 08:43:34 »

You can do this the "Notch" way. The almighty Notch's way. Check out his Ludum Dare entries source code.
Offline davedes
« Reply #18 - Posted 2013-11-11 15:02:25 »

My API here also has some basic bitmap text support:
https://github.com/mattdesl/lwjgl-basics

Of course if you want something more robust and cross-platform, you should be using LibGDX. Smiley

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.

BurntPizza (25 views)
2014-09-19 03:14:18

Dwinin (39 views)
2014-09-12 09:08:26

Norakomi (68 views)
2014-09-10 13:57:51

TehJavaDev (93 views)
2014-09-10 06:39:09

Tekkerue (47 views)
2014-09-09 02:24:56

mitcheeb (68 views)
2014-09-08 06:06:29

BurntPizza (51 views)
2014-09-07 01:13:42

Longarmx (38 views)
2014-09-07 01:12:14

Longarmx (44 views)
2014-09-07 01:11:22

Longarmx (40 views)
2014-09-07 01:10:19
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

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!