Java-Gaming.org Java4K winners: [ by our judges | by the community ]         
Featured games (67)
games approved by the League of Dukes
Games in Showcase (∞)
games submitted by our members



News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  Print  
  OpenGL text renderer  (Read 991 times)
0 Members and 2 Guests are viewing this topic.
Offline jammas615

Jr. Member
**

Posts: 70



« on: 2012-01-30 16: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 Neuromancer
****

Posts: 1174
Medals: 35



« Reply #1 on: 2012-01-30 20: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
*****

Posts: 3159
Medals: 196


I'm the King!


« Reply #2 on: 2012-01-31 00:02:50 »

It's best if you make a public static Hashmap of <String,TextRenderer> inside that TextRenderer class Smiley

Pages: [1]
  Print  
 
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.16 | SMF © 2011, Simple Machines Valid XHTML 1.0! Valid CSS!
Page created in 0.053 seconds with 19 queries.