Explorar el Código

scaled view port, font renderer improvements

Kajetan Johannes Hammerle hace 5 años
padre
commit
ed8d49bb43

+ 1 - 0
.gitignore

@@ -3,3 +3,4 @@
 /dist
 /images
 build.xml
+ascii.png

+ 14 - 5
src/me/hammerle/snuviengine/api/Engine.java

@@ -9,9 +9,7 @@ import static org.lwjgl.system.MemoryUtil.*;
 
 public abstract class Engine
 {
-    private static final String VERSION = "0.0.1";
-    public static final int WIDTH = 1024;
-    public static final int HEIGHT = 576;
+    private static final String VERSION = "0.0.1";   
     public static final int TILE_SIZE = 16;
     public static final float SCALE = 2.0f;
     
@@ -62,7 +60,7 @@ public abstract class Engine
         glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
         glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE);
 
-        window = glfwCreateWindow(WIDTH, HEIGHT, "SnuviEngine " + VERSION, NULL, NULL);
+        window = glfwCreateWindow(Shader.getViewWidth(), Shader.getViewHeight(), "SnuviEngine " + VERSION, NULL, NULL);
         if(window == NULL)
         {
             throw new RuntimeException("Failed to create the GLFW window");
@@ -87,9 +85,10 @@ public abstract class Engine
         glfwSetFramebufferSizeCallback(window, (w, fwidth, fheight) -> 
         {
             glViewport(0, 0, fwidth, fheight);
+            Shader.setViewPort(fwidth, fheight);
         });
         
-        glfwSetWindowAspectRatio(window, 16, 9);
+        //glfwSetWindowAspectRatio(window, 16, 9);
         
         glfwMakeContextCurrent(window);
         glfwSwapInterval(1);
@@ -151,6 +150,16 @@ public abstract class Engine
         nanosPerTick = nanos;
     }
     
+    public final double getTps()
+    {
+        return currentTps;
+    }
+    
+    public final double getFps()
+    {
+        return currentFps;
+    }
+    
     public abstract void init();
     public abstract void tick();
     public abstract void renderTick();

+ 72 - 47
src/me/hammerle/snuviengine/api/FontRenderer.java

@@ -1,6 +1,7 @@
 package me.hammerle.snuviengine.api;
 
 import java.nio.FloatBuffer;
+import me.hammerle.snuviengine.game.Rectangle;
 import me.hammerle.snuviengine.util.Color;
 import org.lwjgl.BufferUtils;
 import static org.lwjgl.opengl.GL11.*;
@@ -14,6 +15,10 @@ public class FontRenderer
     private static final float[] COLORS = new float[128];
     private static final float[] DARK_COLORS = new float[128];
     
+    private static final int FONT_SIZE = 8;
+    private static final int LINE_STEP = 1;
+    private static final float SHADOW_STEP = 1f;
+    
     static
     {
         COLORS['0'] = Float.intBitsToFloat(Color.get(0, 0, 0));
@@ -83,8 +88,15 @@ public class FontRenderer
         });
     }
 
-    private void addRectangle(float minX, float minY, float maxX, float maxY, float tMinX, float tMinY, float tMaxX, float tMaxY)
+    private void addRectangle(float minX, float minY, char c)
     {
+        float tMinX = (c & 0xF) / 16.0f;
+        float tMinY = (c >> 4) / 16.0f;
+        float tMaxX = tMinX + 0.0625f;
+        float tMaxY = tMinY + 0.0625f;
+        float maxX = minX + FONT_SIZE;
+        float maxY = minY + FONT_SIZE;
+        
         buffer.put(minX);
         buffer.put(maxY);        
         buffer.put(tMinX);
@@ -122,41 +134,32 @@ public class FontRenderer
         buffer.put(color);
     }
     
-    public void drawString(float x, float y, boolean shadow, String s)
+    public float drawString(float x, float y, boolean shadow, String s)
     {
-        int l = Math.min(s.length(), MAX_LENGTH);
-        
         if(shadow)
         {
-            color = DARK_COLORS['f'];
-            float oldX = x;
-            float oldY = y;
-            
-            for(int pos = 0; pos < l; pos++)
-            {
-                char c = s.charAt(pos);
-                if(c == '&')
-                {
-                    pos++;
-                    int index = s.charAt(pos);
-                    if(index >= 0 && index <= COLORS.length)
-                    {
-                        color = DARK_COLORS[s.charAt(pos)];
-                    }
-                    continue;
-                }
-
-                float tMinX = (c & 0xF) / 16.0f;
-                float tMinY = 0.9375f - ((c >> 4) / 16.0f);
-                addRectangle(x + 1, y - 1, x + 17, y + 15, tMinX, tMinY, tMinX + 0.0625f, tMinY + 0.0625f);
-                x += 16;
-            }
-            
-            x = oldX;
-            y = oldY;
+            addString(x + SHADOW_STEP, y + SHADOW_STEP, DARK_COLORS, s);
         }
+        y = addString(x, y, COLORS, s);
         
-        color = COLORS['f'];
+        buffer.flip();
+
+        glBindVertexArray(vao);
+        glBindBuffer(GL_ARRAY_BUFFER, vbo);
+        glBufferSubData(GL_ARRAY_BUFFER, 0, buffer);
+        glDrawArrays(GL_TRIANGLES, 0, buffer.limit() / 5);    
+        
+        buffer.limit(buffer.capacity());
+        return y;
+    }
+    
+    private float addString(float x, float y, float[] colors, String s)
+    {
+        int l = Math.min(s.length(), MAX_LENGTH);
+        
+        float oldX = x;
+        color = colors['f'];
+
         for(int pos = 0; pos < l; pos++)
         {
             char c = s.charAt(pos);
@@ -164,29 +167,51 @@ public class FontRenderer
             {
                 pos++;
                 int index = s.charAt(pos);
-                if(index >= 0 && index <= COLORS.length)
+                if(index >= 0 && index <= colors.length)
                 {
-                    color = COLORS[s.charAt(pos)];
+                    color = colors[s.charAt(pos)];
                 }
                 continue;
             }
-            
-            float tMinX = (c & 0xF) / 16.0f;
-            float tMinY = 0.9375f - ((c >> 4) / 16.0f);
-            addRectangle(x, y, x + 16, y + 16, tMinX, tMinY, tMinX + 0.0625f, tMinY + 0.0625f);
-            x += 16;
+            else if(c == '\n')
+            {
+                y += FONT_SIZE + LINE_STEP;
+                x = oldX;
+                continue;
+            }
+
+            addRectangle(x, y, c);
+            x += FONT_SIZE;
         }
-        
-        buffer.flip();
-        
-        glBindVertexArray(vao);
-        glBindBuffer(GL_ARRAY_BUFFER, vbo);
-        glBufferSubData(GL_ARRAY_BUFFER, 0, buffer);
-        glDrawArrays(GL_TRIANGLES, 0, buffer.limit() / 5);    
-        
-        buffer.limit(buffer.capacity());
+        return y + FONT_SIZE + LINE_STEP;
+    }
+    
+    public Rectangle getSize(String s)
+    {
+        int length = 0;
+        int counter = 0;
+        for(int i = 0; i < s.length(); i++)
+        {
+            switch(s.charAt(i))
+            {
+                case '\n':
+                    counter++;
+                    break;
+                case '&':
+                    i++;
+                    break;
+                default:
+                    length++;
+            }
+        }
+        return new Rectangle(FONT_SIZE * length, (FONT_SIZE + LINE_STEP) * counter);
     }
     
+    public float getHeight()
+    {
+        return FONT_SIZE + LINE_STEP;
+    }
+
     public void delete()
     {
         buffer = null;

+ 4 - 4
src/me/hammerle/snuviengine/api/Map.java

@@ -74,12 +74,12 @@ public class Map
     
     private float getViewX(float x) 
     {
-        x -= Engine.WIDTH >> 1;
+        x -= Shader.getViewWidth() >> 1;
         if(x < 0)
         {
             return 0;
         }
-        float max = width * Engine.TILE_SIZE * Engine.SCALE - Engine.WIDTH;
+        float max = width * Engine.TILE_SIZE * Engine.SCALE - Shader.getViewWidth();
         if(x > max)
         {
             return max;
@@ -89,12 +89,12 @@ public class Map
     
     private float getViewY(float y) 
     {
-        y -= Engine.HEIGHT >> 1;
+        y -= Shader.getViewHeight() >> 1;
         if(y < 0)
         {
             return 0;
         }
-        float max = height * Engine.TILE_SIZE * Engine.SCALE - Engine.HEIGHT;
+        float max = height * Engine.TILE_SIZE * Engine.SCALE - Shader.getViewHeight();
         if(y > max)
         {
             return max;

+ 37 - 3
src/me/hammerle/snuviengine/api/Shader.java

@@ -14,6 +14,10 @@ public final class Shader
 {
     private static int program = -1;
     
+    private static int width = 1024;
+    private static int height = 576;
+    private static int scale = 2;
+    
     private static final List<Runnable> TASKS = new LinkedList<>();
     
     protected static boolean initDone = false;
@@ -70,6 +74,7 @@ public final class Shader
         unifUseLight = glGetUniformLocation(program, "useLight");
         setLightUsing(false);
         
+        setViewPort(width, height);
         initDone = true;
     }
     
@@ -91,13 +96,13 @@ public final class Shader
     {
         FloatBuffer buffer = BufferUtils.createFloatBuffer(16);
         
-        buffer.put(2.0f / Engine.WIDTH);
+        buffer.put(2.0f / width);
         buffer.put(0.0f);
         buffer.put(0.0f);
         buffer.put(0.0f);
         
         buffer.put(0.0f);
-        buffer.put(2.0f / Engine.HEIGHT);
+        buffer.put(-2.0f / height);
         buffer.put(0.0f);
         buffer.put(0.0f);
         
@@ -107,7 +112,7 @@ public final class Shader
         buffer.put(0.0f);
         
         buffer.put(-1.0f);
-        buffer.put(-1.0f);
+        buffer.put(1.0f);
         buffer.put(0.0f);
         buffer.put(1.0f);
         
@@ -116,6 +121,35 @@ public final class Shader
         glUniformMatrix4fv(unifViewMatrix, false, buffer);
     }
     
+    protected static void setViewPort(int width, int height)
+    {
+        scale = 1;
+        while(width / (scale + 1) >= 400 && height / (scale + 1) >= 300)
+        {
+            scale++;
+        }
+
+        Shader.width = width / scale;
+        Shader.height = height / scale;
+        
+        updateViewMatrix();
+    }
+    
+    public static int getViewScale()
+    {
+        return scale;
+    }
+    
+    public static int getViewWidth()
+    {
+        return width;
+    }
+    
+    public static int getViewHeight()
+    {
+        return height;
+    }
+    
     public static void updateMatrix()
     {
         glUniformMatrix4fv(unifModelMatrix, false, modelMatrix.getData());

+ 4 - 2
src/me/hammerle/snuviengine/api/Texture.java

@@ -11,6 +11,7 @@ import org.lwjgl.BufferUtils;
 import static org.lwjgl.opengl.GL11.*;
 import static org.lwjgl.opengl.GL12.*;
 import static org.lwjgl.opengl.GL13.*;
+import static org.lwjgl.opengl.GL30.*;
 
 public class Texture
 {
@@ -58,7 +59,7 @@ public class Texture
                 {
                     for(int x = 0; x < wi[i]; x++)
                     {
-                        int c = image.getRGB(x, he[i] - y - 1);
+                        int c = image.getRGB(x, y);
                         buffer.put((c << 8) | ((c >> 24) & 0xFF));
                     }
                 }
@@ -117,7 +118,7 @@ public class Texture
         {
             for(int x = 0; x < w; x++)
             {
-                int c = image.getRGB(x, h - y - 1);
+                int c = image.getRGB(x, y);
                 buffer.put((c << 8) | ((c >> 24) & 0xFF));
             }
         }
@@ -127,6 +128,7 @@ public class Texture
         
         glBindTexture(GL_TEXTURE_2D, texture);
         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, buffer);
+        glGenerateMipmap(GL_TEXTURE_2D);
     }    
 
     public void bindTexture()

+ 22 - 37
src/me/hammerle/snuviengine/game/Game.java

@@ -3,7 +3,6 @@ package me.hammerle.snuviengine.game;
 import me.hammerle.snuviengine.api.ColorRenderer;
 import me.hammerle.snuviengine.api.Engine;
 import me.hammerle.snuviengine.api.FontRenderer;
-import me.hammerle.snuviengine.api.FontRenderer;
 import me.hammerle.snuviengine.api.KeyBinding;
 import me.hammerle.snuviengine.api.KeyHandler;
 import me.hammerle.snuviengine.api.Shader;
@@ -99,44 +98,16 @@ public class Game extends Engine
     {
         //Shader.drawMap();
         
-        /*Shader.setTextureUsing(false);
+        Shader.setTextureUsing(false);
 
-        Shader.translateTo(500, 250);
-        Shader.scale(5, 5);
-        Shader.rotate(x);
-        Shader.translate(-10, -10);
+        Shader.translateTo(0, 0);
         Shader.updateMatrix();
         
-        cr.setColor(0.75f, 0.75f, 0.75f, 1.0f);
-        cr.addRectangle(0, 0, 20, 20);
+        cr.setColor(0.15f, 0.15f, 0.15f, 1.0f);
+        cr.addRectangle(0, 0, 400, 300);
         cr.build();
         cr.draw();
         
-        Shader.pushMatrix();
-        Shader.translate(30, 30);
-        Shader.rotate(x);
-        Shader.translate(-5, -5);
-        Shader.updateMatrix();
-        Shader.popMatrix();
-        
-        cr.setColor(0.5f, 0.5f, 0.5f, 1.0f);
-        cr.addRectangle(0, 0, 5, 5);
-        cr.build();
-        cr.draw();
-        
-        Shader.pushMatrix();
-        Shader.translate(30, 10);
-        Shader.rotate(x);
-        Shader.translate(-5, -5);
-        Shader.updateMatrix();
-        Shader.popMatrix();
-        
-        cr.setColor(0.5f, 1.0f, 0.5f, 1.0f);
-        cr.addRectangle(0, 0, 5, 5);
-        cr.build();
-        cr.draw();*/
-        
-        clock.start();
         Shader.setColorUsing(false);
         Shader.setTextureUsing(true);
         t.bindTexture();
@@ -147,6 +118,7 @@ public class Game extends Engine
         tr.build();
         tr.draw();
         
+        clock.start();
         text.bindTexture();
         Shader.setColorUsing(true);
         
@@ -155,10 +127,23 @@ public class Game extends Engine
         //Shader.scale(3, 3);
         Shader.updateMatrix();
         
-        for(int i = 0; i < 30; i++)
-        {
-            fr.drawString(0.0f, i * 16.0f, true, "&üDas &0i&1s&2t &3e&4i&5n &6F&7a&8r&9b&ae&bn&ct&de&es&ft. Das &0i&1s&2t &3e&4i&5n &6F&7a&8r&9b&ae&bn&ct&de&es&ft.");
-        }
+        String s = "Das ist &6ein&f Test. ";
+        
+        float px = 0.0f;
+        float py = 0.0f;
+        
+        fr.drawString(px, py, true, s);
+        px += fr.getSize(s).getWidth();
+        py = fr.drawString(px, py, true, s);
+        px = 0.0f;
+        
+        py = fr.drawString(px, py, true, "FPS " + String.format("%.1f", super.getFps()));
+        py = fr.drawString(px, py, true, "TPS " + String.format("%.1f", super.getTps()));
+        py = fr.drawString(px, py, false, Shader.getViewWidth() + "\n" + Shader.getViewHeight() + "\n" + Shader.getViewScale());
+        py = fr.drawString(px, py, false, "Hmm g ji__dfsd\nBlaBlaBlaBla");
+        //fr.drawString(0, Shader.getViewHeight() - 60, true, Shader.getViewWidth() + " ");
+        //fr.drawString(0, Shader.getViewHeight() - 60, true, Shader.getViewHeight() + " ");
+        //fr.drawString(0, Shader.getViewHeight() - 60, true, Shader.getViewScale()+ " ");
         
         Shader.popMatrix();
         Shader.updateMatrix();

+ 23 - 0
src/me/hammerle/snuviengine/game/Rectangle.java

@@ -0,0 +1,23 @@
+package me.hammerle.snuviengine.game;
+
+public class Rectangle
+{
+    private final float w;
+    private final float h;
+    
+    public Rectangle(float w, float h)
+    {
+        this.w = w;
+        this.h = h;
+    }
+    
+    public float getWidth()
+    {
+        return w;
+    }
+    
+    public float getHeight()
+    {
+        return h;
+    }
+}