Browse Source

smaller font width

Kajetan Johannes Hammerle 3 years ago
parent
commit
fd5fe652cc
3 changed files with 34 additions and 34 deletions
  1. 2 2
      client/Game.cpp
  2. 26 26
      client/rendering/FontRenderer.cpp
  3. 6 6
      client/rendering/FontRenderer.h

+ 2 - 2
client/Game.cpp

@@ -121,7 +121,7 @@ void Game::tick() {
 
     ray.store();
     ray.set(pos, lengthAngle, widthAngle);
-    
+
     if(control.keys.test5.getDownTime() == 1) {
         renderSettings.ortho = !renderSettings.ortho;
     }
@@ -165,4 +165,4 @@ void Game::renderTextOverlay(float lag, MatrixStack& stack, Shader& sh, FontRend
 
 bool Game::isRunning() const {
     return true;
-}
+}

+ 26 - 26
client/rendering/FontRenderer.cpp

@@ -1,34 +1,34 @@
 #include "client/rendering/FontRenderer.h"
 
-const size_t FontRenderer::BUFFER_LENGTH = 8 * 1024 * 1024;
+const u64 FontRenderer::BUFFER_LENGTH = 8 * 1024 * 1024;
 
-FontRenderer::FontRenderer() : tex("resources/font8x8.png"), offset(BUFFER_LENGTH), vba(0), vbo(0) {
-    glGenVertexArrays(1, &vba);
-    glBindVertexArray(vba);
-
-    glGenBuffers(1, &vbo);
-    glBindBuffer(GL_ARRAY_BUFFER, vbo);
+static void setVertexAttribute(uint index, uint length, uint offset) {
+    glVertexAttribPointer(index, length, GL_FLOAT, false, sizeof (float) * 7, static_cast<float*> (0) + offset);
+    glEnableVertexAttribArray(index);
+}
 
-    glVertexAttribPointer(0, 2, GL_FLOAT, false, sizeof (float) * 7, static_cast<float*> (0));
-    glEnableVertexAttribArray(0);
+FontRenderer::FontRenderer() : tex("resources/font8x8.png"), offset(BUFFER_LENGTH), vertexArray(0), vertexBuffer(0) {
+    glGenVertexArrays(1, &vertexArray);
+    glBindVertexArray(vertexArray);
 
-    glVertexAttribPointer(1, 2, GL_FLOAT, false, sizeof (float) * 7, static_cast<float*> (0) + 2);
-    glEnableVertexAttribArray(1);
+    glGenBuffers(1, &vertexBuffer);
+    glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
 
-    glVertexAttribPointer(2, 3, GL_FLOAT, false, sizeof (float) * 7, static_cast<float*> (0) + 4);
-    glEnableVertexAttribArray(2);
+    setVertexAttribute(0, 2, 0);
+    setVertexAttribute(1, 2, 2);
+    setVertexAttribute(2, 3, 4);
 }
 
 FontRenderer::~FontRenderer() {
-    glDeleteVertexArrays(1, &vba);
-    glDeleteBuffers(1, &vbo);
+    glDeleteBuffers(1, &vertexBuffer);
+    glDeleteVertexArrays(1, &vertexArray);
 }
 
 void FontRenderer::drawString(float x, float y, const char* text) {
-    glBindBuffer(GL_ARRAY_BUFFER, vbo);
+    glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
 
-    const size_t maxIndex = 256;
-    const size_t maxLength = maxIndex * 4 * sizeof (float) * 7;
+    const u64 maxIndex = 256;
+    const u64 maxLength = maxIndex * 4 * sizeof (float) * 7;
 
     if(offset + maxLength >= BUFFER_LENGTH) {
         offset = 0;
@@ -40,8 +40,8 @@ void FontRenderer::drawString(float x, float y, const char* text) {
         return;
     }
 
-    size_t index = 0;
-    size_t i = 0;
+    u64 index = 0;
+    u64 i = 0;
     float r = 1.0f;
     float g = 1.0f;
     float b = 1.0f;
@@ -59,9 +59,9 @@ void FontRenderer::drawString(float x, float y, const char* text) {
             continue;
         }
 
-        float minX = (c & 0xF) * (1.0f / 16.0f);
+        float minX = (c & 0xF) * (1.0f / 16.0f) + 1.0f / 128.0f;
         float minY = (c >> 4) * (1.0f / 16.0f);
-        float maxX = minX + (1.0f / 16.0f);
+        float maxX = minX + (1.0f / 16.0f) - 2.0f / 128.0f;
         float maxY = minY + (1.0f / 16.0f);
 
         buffer[i++] = x;
@@ -80,7 +80,7 @@ void FontRenderer::drawString(float x, float y, const char* text) {
         buffer[i++] = g;
         buffer[i++] = b;
 
-        buffer[i++] = x + 8;
+        buffer[i++] = x + 6;
         buffer[i++] = y;
         buffer[i++] = maxX;
         buffer[i++] = minY;
@@ -88,7 +88,7 @@ void FontRenderer::drawString(float x, float y, const char* text) {
         buffer[i++] = g;
         buffer[i++] = b;
 
-        buffer[i++] = x + 8;
+        buffer[i++] = x + 6;
         buffer[i++] = y + 8;
         buffer[i++] = maxX;
         buffer[i++] = maxY;
@@ -96,12 +96,12 @@ void FontRenderer::drawString(float x, float y, const char* text) {
         buffer[i++] = g;
         buffer[i++] = b;
 
-        x += 8;
+        x += 6;
         index++;
     }
 
     glUnmapBuffer(GL_ARRAY_BUFFER);
-    glBindVertexArray(vba);
+    glBindVertexArray(vertexArray);
     tex.bind(0);
     glDrawArrays(GL_TRIANGLE_STRIP, offset / (sizeof (float) * 7), i / 7);
     offset += maxLength;

+ 6 - 6
client/rendering/FontRenderer.h

@@ -2,15 +2,14 @@
 #define FONTRENDERER_H
 
 #include <GL/glew.h>
-#include <array>
 
+#include "common/utils/Types.h"
 #include "client/rendering/Texture.h"
 
 class FontRenderer final {
 public:
     FontRenderer();
     ~FontRenderer();
-
     FontRenderer(const FontRenderer& other) = delete;
     FontRenderer(FontRenderer&& other) = delete;
     FontRenderer& operator=(const FontRenderer& other) = delete;
@@ -19,12 +18,13 @@ public:
     void drawString(float x, float y, const char* text);
 
 private:
+    static const u64 BUFFER_LENGTH;
+    
     Texture tex;
-    static const size_t BUFFER_LENGTH;
-    size_t offset;
+    u64 offset;
 
-    GLuint vba;
-    GLuint vbo;
+    GLuint vertexArray;
+    GLuint vertexBuffer;
 };
 
 #endif