Kaynağa Gözat

8x8 and 24x24 font

Kajetan Johannes Hammerle 5 yıl önce
ebeveyn
işleme
d8d4508fc6

+ 18 - 2
src/me/hammerle/snuviengine/api/FontRenderer.java

@@ -11,7 +11,14 @@ import static org.lwjgl.opengl.GL30.*;
 
 public class FontRenderer
 {
-    private final static Texture FONT_TEXTURE = new Texture("font.png", true);
+    private final static float ERROR = 0.0f;
+    
+    private final static Texture[] FONT_TEXTURE = new Texture[]
+    {
+        new Texture("font8x8.png", true), 
+        new Texture("font16x16.png", true), 
+        new Texture("font24x24.png", true)
+    };
     
     public static final char COLOR_CHAR = '&';
     
@@ -95,6 +102,10 @@ public class FontRenderer
 
     private void addRectangle(float minX, float minY, char c)
     {
+        float scale = Shader.getViewScale();
+        minY = Math.round(minY * scale) / scale;
+        minX = Math.round(minX * scale) / scale;
+        
         float tMinX = (c & 0xF) / 16.0f;
         float tMinY = (c >> 4) / 16.0f;
         float tMaxX = tMinX + 0.0625f;
@@ -165,7 +176,7 @@ public class FontRenderer
         glUnmapBuffer(GL_ARRAY_BUFFER);
         
         GLHelper.glBindVertexArray(vao);
-        FONT_TEXTURE.bind();
+        FONT_TEXTURE[Math.min(Shader.getViewScale() - 1, FONT_TEXTURE.length - 1)].bind();
         
         glDrawArrays(GL_TRIANGLES, offset / 20, buffer.limit() / 20);
 
@@ -244,4 +255,9 @@ public class FontRenderer
     {
         return FONT_SIZE + LINE_STEP;
     }
+    
+    public float getWidth()
+    {
+        return FONT_SIZE;
+    }
 }

+ 23 - 1
src/me/hammerle/snuviengine/api/TextureRenderer.java

@@ -12,6 +12,7 @@ public class TextureRenderer
     private int vao;
     private int vbo;
     
+    private int triangles = 0;
     private FloatBuffer buffer;
     
     private boolean built = false;
@@ -59,6 +60,8 @@ public class TextureRenderer
         buffer.put(y3);        
         buffer.put(tx3);
         buffer.put(ty3);
+        
+        triangles++;
     }
     
     public void addRectangle(float minX, float minY, float maxX, float maxY, float tMinX, float tMinY, float tMaxX, float tMaxY)
@@ -67,12 +70,21 @@ public class TextureRenderer
         addTriangle(maxX, maxY, minX, minY, maxX, minY, tMaxX, tMaxY, tMinX, tMinY, tMaxX, tMinY);
     }
     
+    public boolean isBuilt()
+    {
+        return built;
+    }
+    
     public void build()
     {
-        if(!Shader.initDone || buffer.position() == 0)
+        if(!Shader.initDone)
         {
             throw new ShaderException("build called too early");
         }
+        if(triangles == 0 || built)
+        {
+            return;
+        }
         buffer.flip();
         GLHelper.glBindVertexArray(vao);
         GLHelper.glBindBuffer(vbo);
@@ -82,8 +94,18 @@ public class TextureRenderer
         built = true;
     }
     
+    public void clear()
+    {
+        triangles = 0;
+        built = false;
+    }
+    
     public void draw()
     {
+        if(triangles == 0)
+        {
+            return;
+        }
         if(!built)
         {
             throw new ShaderException("build must be called before drawing");

+ 2 - 2
src/me/hammerle/snuviengine/game/Game.java

@@ -104,8 +104,8 @@ public class Game extends Engine
         Shader.setColorEnabled(true);
         Shader.translateTo(0.0f, 0.0f);
         Shader.updateMatrix();
-        float y = 5;
-        y = Shader.getFontRenderer().drawString(5, y, true, "FPS " + String.format("%.1f", getFps()));
+        float y = 5.5f;
+        y = Shader.getFontRenderer().drawString(5.5f, y, true, "FPS " + String.format("%.1f", getFps()));
         y = Shader.getFontRenderer().drawString(5, y, true, "TPS " + String.format("%.1f", getTps()));
         y = Shader.getFontRenderer().drawString(5, y, true, String.valueOf(CLOCK.getTime()));
         y = Shader.getFontRenderer().drawString(5, y, true, String.valueOf(CLOCK.getMinTime()));

BIN
src/me/hammerle/snuviengine/resources/font.png


BIN
src/me/hammerle/snuviengine/resources/font16x16.png


BIN
src/me/hammerle/snuviengine/resources/font24x24.png


BIN
src/me/hammerle/snuviengine/resources/font24x24.xcf


BIN
src/me/hammerle/snuviengine/resources/font8x8.png


+ 1 - 1
src/me/hammerle/snuviengine/shader/fragment.fs

@@ -43,7 +43,7 @@ void main(void)
     if(useTexture)
     {
         color = texture(samp, tc);
-        if(color.a != 1.0)
+        if(color.a == 0.0)
         {
             discard;
         }