Using LWJGL to Display Text
I did a bit of searching for ways to do this and I finally decided to just write the text by loading it from a bitmap. This is my first move to loading individual image regions and using a bitmap, so I know that I have some bugs in my algorithm. (Some of the big ones I would bet) but I did it in the best way I could think of how. If someone tells me what I am doing wrong and point me in the right direction to figure it out, that would be great. From what I can tell, when I send the information to render, it is correct, I get the correct character and the correct x, y position to translate it too, and the texture is loaded too. I just can't figure out why I am not getting the correct part of the image for the letter, nothing appears.
Here is my code.
public class StringText {
private ArrayList<TextChar> fontChar;
private final int CHAR_SIZE = 8;
private final int MARGINE = 1;
private Texture fontTexture;
private String lower;
private String upper;
private String symb;
private final int IMAGE_SIZE = 256;
public StringText(){
lower = "abcdefghijklmnopqrstuvwxyz";
upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
symb = ".,?:*!+-() ";
fontChar = new ArrayList<TextChar>();
readInFont("font");
populateCheck();
}
public void drawText(String ts, int x, int y, float red, float green, float blue){
for(int a = 0; a < ts.length(); a++){
for(int b = 0; b < fontChar.size(); b++)
if(fontChar.get(b).getChar == ts.charAt(a))
render(fontChar.get(b),a, x, y, red, green, blue);
}
}
private void populateCheck(){
int charX = 0;
int charY = 0;
for(int x = 0; x < lower.length(); x++){
TextChar t = new TextChar(lower.charAt(x), charX, -8);
fontChar.add(t);
charX += CHAR_SIZE;
}
charX = 0;
for(int x = 0; x < upper.length(); x++){
TextChar t = new TextChar (upper.charAt(x), charX, -16);
fontChar.add(t);
charX += CHAR_SIZE;
}
charX = 0;
for(int x = 0; x < symb.length(); x++){
TextChar t = new TextChar(symb.charAt(x), charX, -24);
fontChar.add(t);
charX += CHAR_SIZE;
}
}
private void render(TextChar textChar, int current, int x, int y, float red, float green, float blue){
int inc = current * (CHAR_SIZE + MARGINE);
glLoadIdentity();
glTranslatef(x + inc,y,0f);
glColor3f(red, green, blue);
glScalef(1.0f, -1.0f, 1.0f);
fontTexture.bind();
System.out.println("Letter Position: " + (x+inc) + ", " + y + " Character: " + textChar.getChar);
float x0 = textChar.x/IMAGE_SIZE;
float y0 = textChar.y/IMAGE_SIZE;
float x1 = (textChar.x + CHAR_SIZE)/IMAGE_SIZE;
float y1 = (textChar.y - CHAR_SIZE)/IMAGE_SIZE;
glBegin(GL_QUADS);
glTexCoord2f( x0, -y0); glVertex2f(0,0);
glTexCoord2f(x1,-y0); glVertex2f(IMAGE_SIZE,0);
glTexCoord2f(x1,-y1); glVertex2f(IMAGE_SIZE, IMAGE_SIZE);
glTexCoord2f(x0,-y1); glVertex2f(0,IMAGE_SIZE);
glEnd();
}
private void readInFont(String s){
try {
fontTexture = TextureLoader.getTexture("PNG", new FileInputStream(new File("res/font/"+ s +".png")));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private class TextChar{
int x,y;
char getChar;
public TextChar(char s, int xa, int ya){
x = xa;
y = ya;
getChar = s;
}
}
}
source to share