Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (536)
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  
  OpenGL text renderer  (Read 2646 times)
0 Members and 1 Guest are viewing this topic.
Offline jammas615

Junior Member


Projects: 1



« Posted 2012-01-30 22:07:29 »

I got bored yesterday and had an idea. So, i decided to write myself a small text renderer. It's far from finished or perfect, but it works.
You need to initialize it with a font file that is alpha based. I got the idea from the coke and code one after i tried using that and it didn't work. But it should be pretty straight forward. I would also appreciate feedback!
You can download the font's i used, be aware one is from the coke and code tutorial but only the top font and the other is and 8-bit i made:http://lonecoder.heliohost.org/download/

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  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
import static org.lwjgl.opengl.GL11.*;

import java.io.IOException;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;
import org.newdawn.slick.util.ResourceLoader;

import net.lonecoder.reachfortheskies.*;

public class TextRenderer {
   
   public static final int CENTER = 0; //Alignment (Optional but it overides the x value if either of these three a passed)
  public static final int LEFT = 1;
   public static final int RIGHT = 2;
   
   private static int charWidth;
   private static int charHeight;
   private static float charWidthInTex;
   private static float charHeightInTex;
   
   private static int charsAcross;
   private static int charsDown;
   private static int charSpacing;
   
   private static Texture texture;
   
   public TextRenderer(String texFileName, int charWidth, int charHeight) {
      TextRenderer.charWidth = charWidth;
      TextRenderer.charHeight = charHeight;
     
      try {
         texture = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream(texFileName));
      } catch (IOException e) {
         e.printStackTrace();
      }
     
      charsAcross = texture.getImageWidth() / charWidth;
      charsDown = texture.getImageHeight() / charHeight;
      System.out.println(texture.getImageWidth() + " " + texture.getWidth());
     
      charWidthInTex = texture.getWidth() / charsAcross;
      charHeightInTex = texture.getHeight() / charsDown;
      charSpacing = charWidth - 5;
     
   }
   
   public static void drawString(String text ,double x, double y, double size, float red, float green, float blue, double orientation) {
      double charSize;
      double charSpace;
     
      //If no size value passed, use default size
     if (size == 0) {
         charSize = charWidth;
         charSpace = charSpacing;
      }
      else {
         charSize = charSpacing * size;
         charSpace = charSize - 4;
      }
     
      if (orientation == CENTER) {
         x = (Game.WIDTH - (text.length() * charSpace))/2;
      }
      if (orientation == LEFT) {
         x = 0;
      }
      if (orientation == RIGHT) {
         x = (Game.WIDTH - (text.length() * charSpace + 10));
      }
      glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
      glEnable(GL_BLEND);
      glEnable(GL_TEXTURE_2D);
      glPushMatrix();
      texture.bind();
     
      glBegin(GL_QUADS);
     
      for (int i = 0; i < text.length(); i++) {
         int c = (text.charAt(i)) - 32;
         
         float u = (c % charsAcross) * charWidthInTex;
         float v = (c / charsAcross) * charHeightInTex;

         glColor3f(red, green, blue); //Color can be (must be) set when function is called.
        glTexCoord2f(u, v);
         glVertex2d(x + (i * charSpace), y);
         glTexCoord2f(u + charWidthInTex, v);
         glVertex2d(x + (i * charSpace) + charSize, y);
         glTexCoord2f(u + charWidthInTex, v + charHeightInTex);
         glVertex2d(x + (i * charSpace) + charSize, y + charSize);
         glTexCoord2f(u, v + charHeightInTex);
         glVertex2d(x + (i * charSpace), y + charSize);
      }
      glEnd();
      glPopMatrix();
      glDisable(GL_TEXTURE_2D);
      glDisable(GL_BLEND);
   }
}


This is just how i use it:
1  
2  
TextRenderer textRenderer;
textRenderer = new TextRenderer("res/8-bit_font1.png", 32, 32);


As you can access it statically, i make calls like this without creating any objects:
1  
TextRenderer.drawString("HELLO", 0, 0, 1, 1f, 1f, 1f, TextRenderer.LEFT);

Offline lhkbob

JGO Knight


Medals: 32



« Reply #1 - Posted 2012-01-31 02:25:41 »

Isn't it a little strange that a constructor initializes the static texture object?  Calling drawString() without ever calling the constructor will break, and calling the constructor multiple times will also cause strange behavior.

I think in this case, better design would be to just use an instance method to draw a string.  If you're worried about creating too many objects, just make sure to pass the TextRenderer in as a dependency to your code that needs to render text.

Online ra4king

JGO Kernel


Medals: 341
Projects: 2
Exp: 5 years


I'm the King!


« Reply #2 - Posted 2012-01-31 06:02:50 »

It's best if you make a public static Hashmap of <String,TextRenderer> inside that TextRenderer class 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.

CogWheelz (11 views)
2014-07-30 21:08:39

Riven (21 views)
2014-07-29 18:09:19

Riven (14 views)
2014-07-29 18:08:52

Dwinin (12 views)
2014-07-29 10:59:34

E.R. Fleming (32 views)
2014-07-29 03:07:13

E.R. Fleming (12 views)
2014-07-29 03:06:25

pw (42 views)
2014-07-24 01:59:36

Riven (42 views)
2014-07-23 21:16:32

Riven (29 views)
2014-07-23 21:07:15

Riven (30 views)
2014-07-23 20:56:16
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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!