2012-03-31 57 views
3

我已經搜索了很多方法來做到這一點,我終於決定從位圖圖像加載它來寫文本。這是我第一次加載圖像的不同區域並使用位圖,所以我知道我的算法中有一些錯誤。 (我敢打賭一些主要的),但我做了我能想到的最好方式。如果有人會告訴我我做錯了什麼,並指出我朝着正確的方向去弄清楚這很好。從我能告訴我什麼時候將信息發送到渲染中是正確的,我得到正確的字符和正確的x,y位置來翻譯它,並且紋理加載也很好。我似乎無法弄清楚爲什麼我沒有得到這封信的圖像的正確部分,沒有任何顯示。使用LWJGL來顯示文本

這是我的代碼。

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; 
    } 
} 

}

回答

1

我想出了一個更好的方式通過使用ASCII位做到這一點莫過於此。

+1

...怎麼樣?有什麼有趣的? – Jamey 2012-09-14 03:58:22

+0

@Jamey這是我結束了。 https://github.com/InsanerGamer/Abandead/blob/master/Abandead/src/net/scylla/abandead/gui/StringText.java – cgasser 2012-09-24 14:02:39