Prechádzať zdrojové kódy

count printable characters for font buffer

Kajetan Johannes Hammerle 4 rokov pred
rodič
commit
ea4e9b41b6

+ 17 - 1
src/me/hammerle/snuviengine/api/FontRenderer.java

@@ -48,7 +48,7 @@ public final class FontRenderer {
     }
 
     public float drawString(float x, float y, boolean shadow, String text) {
-        buffer = getNextBuffer(120 * 256 * 2);
+        buffer = getNextBuffer(120 * countPrintableCharacters(text) * (shadow ? 2 : 1));
         if(buffer == null) {
             return y;
         }
@@ -61,6 +61,22 @@ public final class FontRenderer {
         return y;
     }
 
+    private int countPrintableCharacters(String text) {
+        int count = 0;
+        int index = 0;
+        while(true) {
+            index += consumeColor(text, index, false);
+            if(index >= text.length()) {
+                break;
+            }
+            if(text.charAt(index) != '\n') {
+                count++;
+            }
+            index++;
+        }
+        return count;
+    }
+
     public float drawString(float x, float y, String text) {
         return drawString(x, y, true, text);
     }