Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing LWJGL to display text
    text
    copied!<p>I've searched around quite a bit for ways to do this and I finally decided to just write text by loading it from a bitmap image. This is my first go at loading separate regions of an image and using a bitmap, so I know I have some mistakes in my algorithm. (Some major ones I would bet) but I did it the best way I could think of how. IF someone would tell me what I'm doing wrong and point me in the right direction to figuring this out that would be great. From what I can tell when I send the information into the render it is correct, I get the right character and the right x,y position to translate it too, and the texture is loading just fine as well. I just can't seem to figure out why I'm not getting the right part of the image for the letter, nothing is showing up. </p> <p>Here is my code. </p> <pre><code>public class StringText { private ArrayList&lt;TextChar&gt; 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&lt;TextChar&gt;(); readInFont("font"); populateCheck(); } public void drawText(String ts, int x, int y, float red, float green, float blue){ for(int a = 0; a &lt; ts.length(); a++){ for(int b = 0; b &lt; 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 &lt; 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 &lt; 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 &lt; 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; } } </code></pre> <p>}</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload