2012-03-31 15 views
3

Ich habe ziemlich viel nach Wegen gesucht, dies zu tun, und ich entschied mich schließlich, nur Text zu schreiben, indem Sie es aus einem Bitmap-Bild laden. Dies ist meine erste Aufgabe beim Laden einzelner Bereiche eines Bildes und der Verwendung einer Bitmap, also weiß ich, dass ich einige Fehler in meinem Algorithmus habe. (Einige wichtige würde ich wetten) aber ich tat es am besten, wie ich mir vorstellen konnte. WENN jemand mir sagen würde, was ich falsch mache, und mir in die richtige Richtung zeigen würde, dass das großartig wäre. Von dem, was ich sagen kann, wenn ich die Informationen in den Render richte, ist es richtig, ich bekomme das richtige Zeichen und die richtige x, y Position, um es auch zu übersetzen, und die Textur lädt auch gut. Ich kann einfach nicht herausfinden, warum ich nicht den richtigen Teil des Bildes für den Brief bekomme, nichts wird angezeigt.Verwenden von LWJGL zum Anzeigen von Text

Hier ist mein 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; 
    } 
} 

}

Antwort

1

dachte ich einen besseren Weg, um es als dies zu tun, einen ASCII-Bitmap unter Verwendung.

+1

... Wie? Irgendetwas Interessantes? – Jamey

+0

@Jamey Das ist, womit ich endete. https://github.com/InsanerGamer/Abandead/blob/master/Abandead/src/net/scylla/abandead/gui/StringText.java – cgasser

Verwandte Themen