Browse Source

upgraded strings

Kajetan Johannes Hammerle 3 years ago
parent
commit
5a4fa643a1

+ 13 - 12
client/Game.cpp

@@ -1,6 +1,7 @@
 #include "client/Game.h"
 #include "client/utils/Utils.h"
 #include "rendering/Renderer.h"
+#include "common/utils/String.h"
 
 Game::Game(const Control& control, const Camera& camera, Ray& ray, const Clock& fps, const Clock& tps,
         RenderSettings& renderSettings) :
@@ -48,7 +49,7 @@ void Game::tick() {
     ray.set(pos, lengthAngle, widthAngle);
 
     if(control.keys.test5.getDownTime() == 1) {
-        renderSettings.shadows = !renderSettings.shadows;
+        renderSettings.ssao = !renderSettings.ssao;
     }
     /*if(control.keys.test.isDown()) {
         renderSettings.testRadius /= 0.95f;
@@ -72,17 +73,17 @@ void Game::renderTextOverlay(float lag, Renderer& renderer, FontRenderer& fr) co
     (void) lag;
     renderer.scale(1.0f).update();
 
-    char buffer[50];
-    snprintf(buffer, 50, "FPS: %.2f", fps.getUpdatesPerSecond());
-    fr.drawString(10, 10, buffer);
-    snprintf(buffer, 50, "TPS: %.2f", tps.getUpdatesPerSecond());
-    fr.drawString(10, 20, buffer);
-    snprintf(buffer, 50, "Bias: %.6f", renderSettings.testBias);
-    fr.drawString(10, 30, buffer);
-    snprintf(buffer, 50, "Radius: %.6f", renderSettings.testRadius);
-    fr.drawString(10, 40, buffer);
-    snprintf(buffer, 50, "Shadows: %d", renderSettings.shadows);
-    fr.drawString(10, 50, buffer);
+    String s;
+    s.append("FPS: ").append(fps.getUpdatesPerSecond());
+    fr.drawString(10, 10, s);
+    s.clear().append("TPS: ").append(tps.getUpdatesPerSecond());
+    fr.drawString(10, 20, s);
+    s.clear().append("Bias: ").append("%.6f", renderSettings.testBias);
+    fr.drawString(10, 30, s);
+    s.clear().append("Radius: ").append("%.6f", renderSettings.testRadius);
+    fr.drawString(10, 40, s);
+    s.clear().append("Shadows: ").append(renderSettings.shadows);
+    fr.drawString(10, 50, s);
 }
 
 bool Game::isRunning() const {

+ 1 - 1
client/rendering/Shaders.cpp

@@ -1,4 +1,4 @@
-#include "Shaders.h"
+#include "client/rendering/Shaders.h"
 
 Shaders::Shaders() :
 world("resources/shader/worldVertex.vs", "resources/shader/worldFragment.fs"),

+ 1 - 1
client/rendering/WindowSize.cpp

@@ -1,4 +1,4 @@
-#include "WindowSize.h"
+#include "client/rendering/WindowSize.h"
 
 WindowSize::WindowSize(int width, int height) : width(width), height(height) {
 }

+ 2 - 2
client/rendering/wrapper/Texture.cpp

@@ -1,4 +1,4 @@
-#include "Texture.h"
+#include "client/rendering/wrapper/Texture.h"
 
 Texture::Texture() : texture(0) {
     glGenTextures(1, &texture);
@@ -24,4 +24,4 @@ void Texture::setRGBFloatData(int width, int height, const float* data) {
 void Texture::bind(uint index) const {
     glActiveTexture(GL_TEXTURE0 + index);
     glBindTexture(GL_TEXTURE_2D, texture);
-}
+}

+ 1 - 1
client/rendering/wrapper/VertexBuffer.cpp

@@ -1,4 +1,4 @@
-#include "VertexBuffer.h"
+#include "client/rendering/wrapper/VertexBuffer.h"
 
 VertexBuffer::VertexBuffer() : vertexArray(0), vertexBuffer(0) {
     glGenVertexArrays(1, &vertexArray);

+ 4 - 4
common/utils/SplitString.cpp

@@ -6,21 +6,21 @@ SplitString::SplitString(const char* str) : entries(0) {
             if(i >= 1 && str[i - 1] != ' ') {
                 return;
             }
-            data += '"';
+            data.append('"');
             i++;
             while(str[i] != '"') {
                 if(str[i] == '\0') {
                     return;
                 }
-                data += str[i++];
+                data.append(str[i++]);
             }
             if(str[i + 1] != '\0' && str[i + 1] != ' ') {
                 return;
             }
-            data += '\0';
+            data.append('\0');
             continue;
         }
-        data += (str[i] == ' ' ? '\0' : str[i]);
+        data.append(str[i] == ' ' ? '\0' : str[i]);
     }
     uint last = 0;
     for(uint i = 0; i < data.getLength() + 1; i++) {

+ 35 - 5
common/utils/String.cpp

@@ -1,4 +1,5 @@
 #include <cstring>
+#include <cstdio>
 
 #include "common/utils/String.h"
 
@@ -25,7 +26,15 @@ String::operator const char*() const {
     return data;
 }
 
-String& String::operator+=(char c) {
+char String::operator[](uint index) const {
+    return data[index];
+}
+
+uint String::getLength() const {
+    return length;
+}
+
+String& String::append(char c) {
     if(length < MAX_LENGTH - 1) {
         data[length++] = c;
         data[length] = '\0';
@@ -33,11 +42,32 @@ String& String::operator+=(char c) {
     return *this;
 }
 
-char String::operator[](uint index) const {
-    return data[index];
+String& String::append(const char* str) {
+    for(uint i = 0; length < MAX_LENGTH - 1 && str[i] != '\0'; length++, i++) {
+        data[length] = str[i];
+    }
+    data[length] = '\0';
+    return *this;
 }
 
-uint String::getLength() const {
-    return length;
+String& String::append(uint i) {
+    return append("%u", i);
 }
 
+String& String::append(int i) {
+    return append("%d", i);
+}
+
+String& String::append(float f) {
+    return append("%.2f", f);
+}
+
+String& String::append(bool b) {
+    return append(b ? "true" : "false");
+}
+
+String& String::clear() {
+    data[0] = '\0';
+    length = 0;
+    return *this;
+}

+ 22 - 3
common/utils/String.h

@@ -7,15 +7,34 @@ class String final {
 public:
     String();
     String(const char* str);
-    
+
     bool operator==(const String& other) const;
     bool operator!=(const String& other) const;
     operator const char*() const;
-    String& operator+=(char c);
     char operator[](uint index) const;
-    
     uint getLength() const;
 
+    template<typename T>
+    String& append(const char* format, const T& t) {
+        uint left = MAX_LENGTH - length;
+        uint written = snprintf(data + length, left, format, t);
+        if(written < left) {
+            length += written;
+        } else {
+            length = MAX_LENGTH;
+        }
+        return *this;
+    }
+
+    String& append(char c);
+    String& append(const char* str);
+    String& append(uint i);
+    String& append(int i);
+    String& append(float f);
+    String& append(bool b);
+    
+    String& clear();
+    
     static constexpr uint MAX_LENGTH = 255;
 private:
     char data[MAX_LENGTH];

+ 1 - 1
server/GameServer.cpp

@@ -35,7 +35,7 @@ void GameServer::onClientPackage(int socket, Stream& in) {
     while(in.hasData()) {
         char c;
         in.read(&c, 1);
-        s += c;
+        s.append(c);
     }
 
     Stream answer;