Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (538)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (600)
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 Font metrics not working  (Read 244 times)
0 Members and 1 Guest are viewing this topic.
Offline CopyableCougar4
« Posted 2014-08-21 22:45:39 »

Hi

So I am fixing up some font rendering code (and improving upon it). The font sheet is created/shown perfectly, but when the regions for each character are checked when rendering individual characters, everything is offset. Just to prevent this, I know I'm "rolling out my own" but it almost works perfectly like it should.

Code:
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  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
package game.util;

import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.util.HashMap;

import static org.lwjgl.opengl.GL11.*;

public class GLFont {
   
   public static HashMap<Float, Texture> textures = new HashMap<Float, Texture>();
   
   private Font font;
   private float fontsize;
   private IntObject[] objects = new IntObject[256];
   private FontMetrics metrics;
   private Texture fontTexture;
   private BufferedImage image;
   private int fontHeight = 0;
   
   public static class IntObject {
      public int x, y, width, height;
      public void debug(){
         System.out.println("IntObject[x=" + x + ",y=" + y + ",width=" + width + ",height=" + height + "]");
      }
   }
   
   public GLFont(Font _font, float fontsize){
      this.fontsize = fontsize;
      font = _font.deriveFont(fontsize);
      if(textures.get(fontsize) != null){
         fontTexture = textures.get(fontsize);
      } else {
         createTexture();
         textures.put(fontsize, fontTexture);
      }
   }
   
   private float textureWidth = 512, textureHeight = 512;
   public void drawText(float x, float y, String text, Color color){
      glColor3f(color.getRed() / 255.0f, color.getGreen() / 255.0f, color.getBlue() / 255.0f);
      fontTexture.bind();
      float drawX = x;
      float drawY = y;
      glBegin(GL_QUADS);
      for(char letter : text.toCharArray()){
         IntObject intObject = objects[(int)letter];
         intObject.debug();
         float drawX2 = drawX + intObject.width;
         float drawY2 = drawY + intObject.height;
         float srcX = intObject.x; //
         float srcY = intObject.y; //
         float srcX2 = srcX + intObject.width;
         float srcY2 = srcY + intObject.height;
         float DrawWidth = drawX2 - drawX;
         float DrawHeight = drawY2 - drawY;
         float TextureSrcX = srcX / textureWidth; //
         float TextureSrcY = srcY / textureHeight; //
         float SrcWidth = srcX2 - srcX;
         float SrcHeight = srcY2 - srcY;
         float RenderWidth = (SrcWidth / textureWidth);
         float RenderHeight = (SrcHeight / textureHeight);
         // Shift the render row.
         TextureSrcY -= RenderHeight;
         TextureSrcY += (5.0f / 512.0f);
         // Draw the letter
         glTexCoord2f(TextureSrcX, TextureSrcY);
         glVertex2f(drawX, drawY);
         glTexCoord2f(TextureSrcX, TextureSrcY + RenderHeight);
         glVertex2f(drawX, drawY + DrawHeight);
         glTexCoord2f(TextureSrcX + RenderWidth, TextureSrcY + RenderHeight);
         glVertex2f(drawX + DrawWidth, drawY + DrawHeight);
         glTexCoord2f(TextureSrcX + RenderWidth, TextureSrcY);
         glVertex2f(drawX + DrawWidth, drawY);
         drawX += intObject.width;
      }
      glEnd();
      glColor3f(1.0f, 1.0f, 1.0f);
   }
   
   public void debug(){
      fontTexture.bind();
      glBegin(GL_QUADS);
      glTexCoord2f(0, 0);
      glVertex2f(100, 100);
      glTexCoord2f(1, 0);
      glVertex2f(612, 100);
      glTexCoord2f(1, 1);
      glVertex2f(612, 612);
      glTexCoord2f(0, 1);
      glVertex2f(100, 612);
      glEnd();
   }
   
   public void createTexture(){
      image = new BufferedImage(512, 512, BufferedImage.TYPE_INT_ARGB);
      BufferedImage scratch = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
      Graphics2D graphics = scratch.createGraphics();
      // get the dimensions
      graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
      graphics.setFont(font);
      metrics = graphics.getFontMetrics();
      // prepare the graphics
      Graphics2D realGraphics = image.createGraphics();
      realGraphics.setColor(new Color(255, 255, 255, 0));
      realGraphics.drawRect(0, 0, 512, 512);
      realGraphics.setColor(new Color(255, 255, 255));
      realGraphics.setFont(font);
      // real work
      int rowHeight = 0;
      int positionX = 0;
      int positionY = 0;
      for(int index = 0; index < 256; index++){
         char letter = (char) index;
         int charwidth = metrics.charWidth(letter);
         if (charwidth <= 0) {
            charwidth = 1;
         }
         int charheight = metrics.getHeight();
         if (charheight <= 0) {
            charheight = Math.round(fontsize);
         }
         IntObject intObject = new IntObject();
         intObject.width = charwidth;
         intObject.height = charheight;
         if (positionX + intObject.width >= 512) {
            positionX = 0;
            positionY += rowHeight;
            rowHeight = 0;
         }
         intObject.x = positionX;
         intObject.y = positionY;
         if(intObject.height > fontHeight){
            fontHeight = intObject.height;
         }
         if(intObject.height > rowHeight){
            rowHeight = intObject.height;
         }
         realGraphics.drawString(String.valueOf(letter), positionX, positionY);
         positionX += intObject.width;
         objects[index] = intObject;
      }
      fontTexture = new Texture(image);
   }

}


Any assistance in fixing the character regions would be appreciated Smiley

CopyableCougar4

Either wandering the forum or programming. Most likely the latter Smiley

Github: http://github.com/CopyableCougar4
Offline CopyableCougar4
« Reply #1 - Posted 2014-08-21 23:05:24 »

As an example of this, the following image should say Example:



CopyableCougar4

Either wandering the forum or programming. Most likely the latter Smiley

Github: http://github.com/CopyableCougar4
Offline CopyableCougar4
« Reply #2 - Posted 2014-08-22 00:43:29 »

I fixed my silly issue. The row below each character was rendered. Updated code for all who want it is in first post.

CopyableCougar4

Either wandering the forum or programming. Most likely the latter Smiley

Github: http://github.com/CopyableCougar4
Pages: [1]
  ignore  |  Print  
 
 

 

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

The first screenshot will be displayed as a thumbnail.

rwatson462 (29 views)
2014-12-15 09:26:44

Mr.CodeIt (20 views)
2014-12-14 19:50:38

BurntPizza (40 views)
2014-12-09 22:41:13

BurntPizza (75 views)
2014-12-08 04:46:31

JscottyBieshaar (37 views)
2014-12-05 12:39:02

SHC (50 views)
2014-12-03 16:27:13

CopyableCougar4 (46 views)
2014-11-29 21:32:03

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

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

toopeicgaming1999 (30 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

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
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!