Browse Source

new gaming core version

Kajetan Johannes Hammerle 3 years ago
parent
commit
6b65dd0b03

+ 11 - 6
client/Game.cpp

@@ -1,9 +1,10 @@
 #include "client/Game.h"
 #include "gaming-core/utils/Utils.h"
 
-Game::Game(const Controller& controller, const Clock& fps, const Clock& tps, RenderSettings& settings, const Size& size)
-    : controller(controller), fps(fps), tps(tps), renderSettings(settings), size(size), world(blockRegistry),
-      worldRenderer(world) {
+Game::Game(const Controller& controller, const Clock& fps, const Clock& tps,
+           RenderSettings& settings, const Size& size)
+    : controller(controller), fps(fps), tps(tps), renderSettings(settings),
+      size(size), world(blockRegistry), worldRenderer(world) {
     pos = Vector3(16.0f, 30.0f, -10.0f);
     rotation = Quaternion(Vector3(1.0f, 0.0f, 0.0f), 30) * rotation;
     rotation = Quaternion(Vector3(0.0f, 1.0f, 0.0f), 30) * rotation;
@@ -17,7 +18,7 @@ void Game::tick() {
     Vector3 up = rotation * Vector3(0.0f, 1.0f, 0.0f);
     Vector3 back = rotation * Vector3(0.0f, 0.0f, -1.0f);
 
-    const float speed = 8.0f;
+    const float speed = 2.0f;
     if(controller.down.isDown()) {
         pos += back * speed;
     }
@@ -53,7 +54,8 @@ void Game::tick() {
 }
 
 void Game::renderWorld(float lag, Renderer& renderer) {
-    renderer.update(Utils::interpolate(lastPos, pos, lag), lastRotation.lerp(lag, rotation));
+    renderer.update(Utils::interpolate(lastPos, pos, lag),
+                    lastRotation.lerp(lag, rotation));
     worldRenderer.render(lag, renderer);
 }
 
@@ -61,7 +63,10 @@ void Game::renderTextOverlay(float lag, Renderer& renderer, FontRenderer& fr) {
     (void)lag;
     renderer.scale(2.0f).update();
     StringBuffer<100> s;
-    s.append("FPS: &074").append(fps.getUpdatesPerSecond()).append(" &999TPS: &722").append(tps.getUpdatesPerSecond());
+    s.append("FPS: &074")
+        .append(fps.getUpdatesPerSecond())
+        .append(" &999TPS: &722")
+        .append(tps.getUpdatesPerSecond());
     fr.drawString(10, 10, s);
 }
 

+ 13 - 10
client/Main.cpp

@@ -1,17 +1,20 @@
 #include <iostream>
 
-#include "gaming-core/wrapper/GLFW.h"
-#include "gaming-core/wrapper/GL.h"
-#include "gaming-core/wrapper/GLEW.h"
-#include "gaming-core/wrapper/Window.h"
-#include "client/rendering/Shaders.h"
-#include "client/rendering/Framebuffers.h"
 #include "client/input/Controller.h"
 #include "client/rendering/Engine.h"
-#include "gaming-core/utils/Clock.h"
+#include "client/rendering/Framebuffers.h"
 #include "client/rendering/RenderSettings.h"
+#include "client/rendering/Shaders.h"
+#include "gaming-core/utils/Clock.h"
+#include "gaming-core/wrapper/GL.h"
+#include "gaming-core/wrapper/GLEW.h"
+#include "gaming-core/wrapper/GLFW.h"
+#include "gaming-core/wrapper/Window.h"
 #include "gaming-core/wrapper/WindowOptions.h"
 
+#include <memory>
+#include <vector>
+
 void updateSize(const Window& window, Size& size, Framebuffers& framebuffers) {
     Size newSize = window.getSize();
     if(newSize.width != size.width || newSize.height != size.height) {
@@ -54,14 +57,14 @@ int main() {
 
     window.show();
 
-    GL::checkAndPrintError("setup error");
+    GL::printError("setup error");
 
     const Clock::Nanos nanosPerTick = 50000000;
     Clock::Nanos lag = 0;
     while(!window.shouldClose() && game.isRunning()) {
-        GL::checkAndPrintError("loop error");
+        GL::printError("loop error");
         updateSize(window, size, framebuffers);
-        engine.renderTick(static_cast<float> (lag) / nanosPerTick, game);
+        engine.renderTick(static_cast<float>(lag) / nanosPerTick, game);
         window.swapBuffers();
         lag += fps.update();
         while(lag >= nanosPerTick) {

+ 19 - 13
client/rendering/Engine.cpp

@@ -2,16 +2,19 @@
 #include "client/rendering/Renderer.h"
 #include "gaming-core/wrapper/GL.h"
 
-Engine::Engine(Shaders& shaders, Framebuffers& fb, const Size& size, RenderSettings& renderSettings)
-    : shaders(shaders), framebuffers(fb), size(size), renderSettings(renderSettings),
-      frustum(60.0f, 0.1f, 1000.0f, size) {
+Engine::Engine(Shaders& shaders, Framebuffers& fb, const Size& size,
+               RenderSettings& renderSettings)
+    : shaders(shaders), framebuffers(fb), size(size),
+      renderSettings(renderSettings), frustum(60.0f, 0.1f, 1000.0f, size) {
     TypedBuffer<Triangle> buffer(2);
-    buffer.add(Triangle(Vertex(Vector3(-1.0f, -1.0f, 0.0f), Vector2(0, 0.0f)),
-                        Vertex(Vector3(1.0f, 1.0f, 0.0f), Vector2(1.0f, 1.0f)),
-                        Vertex(Vector3(-1.0f, 1.0f, 0.0f), Vector2(0.0f, 1.0f))));
-    buffer.add(Triangle(Vertex(Vector3(-1.0f, -1.0f, 0.0f), Vector2(0, 0.0f)),
-                        Vertex(Vector3(1.0f, -1.0f, 0.0f), Vector2(1.0f, 0.0f)),
-                        Vertex(Vector3(1.0f, 1.0f, 0.0f), Vector2(1.0f, 1.0f))));
+    buffer.add(
+        Triangle(Vertex(Vector3(-1.0f, -1.0f, 0.0f), Vector2(0, 0.0f)),
+                 Vertex(Vector3(1.0f, 1.0f, 0.0f), Vector2(1.0f, 1.0f)),
+                 Vertex(Vector3(-1.0f, 1.0f, 0.0f), Vector2(0.0f, 1.0f))));
+    buffer.add(
+        Triangle(Vertex(Vector3(-1.0f, -1.0f, 0.0f), Vector2(0, 0.0f)),
+                 Vertex(Vector3(1.0f, -1.0f, 0.0f), Vector2(1.0f, 0.0f)),
+                 Vertex(Vector3(1.0f, 1.0f, 0.0f), Vector2(1.0f, 1.0f))));
     rectangle.build(buffer);
 }
 
@@ -90,7 +93,7 @@ void Engine::renderSSAO() {
 
 void Engine::renderPostWorld() {
     GL::bindMainFramebuffer();
-    GL::clearFramebuffer();
+    GL::clear();
     shaders.postWorld.use();
     framebuffers.world.bindTextureTo(2, 0);
     framebuffers.ssaoBlur.bindTextureTo(0, 1);
@@ -107,7 +110,8 @@ void Engine::renderTextOverlay(float lag, Game& game) {
 
     Matrix m;
     shaders.text.setMatrix("proj", m.getValues());
-    m.scale(Vector3(2.0f / size.width, -2.0f / size.height, 1.0f)).translate(Vector3(-1.0f, 1.0f, 0.0f));
+    m.scale(Vector3(2.0f / size.width, -2.0f / size.height, 1.0f))
+        .translate(Vector3(-1.0f, 1.0f, 0.0f));
     shaders.text.setMatrix("view", m.getValues());
     model.clear();
     shaders.text.setMatrix("model", model.peek().getValues());
@@ -141,8 +145,10 @@ void Engine::updateWorldView() {
     Vector3 up(-0.196175f, 0.819152f, -0.538986f);
     Vector3 center(16.0f, 24.0f, 24.0f);
 
-    worldShadowView.set(0, Vector4(right[0], right[1], right[2], right.dot(-center)));
+    worldShadowView.set(
+        0, Vector4(right[0], right[1], right[2], right.dot(-center)));
     worldShadowView.set(1, Vector4(up[0], up[1], up[2], up.dot(-center)));
-    worldShadowView.set(2, Vector4(back[0], back[1], back[2], back.dot(-center)));
+    worldShadowView.set(2,
+                        Vector4(back[0], back[1], back[2], back.dot(-center)));
     worldShadowView.set(3, Vector4(0.0f, 0.0f, 0.0f, 1.0f));
 }

+ 5 - 3
client/rendering/FontRenderer.cpp

@@ -1,9 +1,10 @@
 #include "client/rendering/FontRenderer.h"
-#include "gaming-core/wrapper/Attributes.h"
 #include "gaming-core/utils/Buffer.h"
+#include "gaming-core/wrapper/Attributes.h"
 
 FontRenderer::FontRenderer() : tex("resources/font8x8.png") {
-    vertexBuffer.setAttributes(Attributes().addFloat(2).addFloat(2).addColor4());
+    vertexBuffer.setAttributes(
+        Attributes().addFloat(2).addFloat(2).addColor4());
     vertexBuffer.setStreamData(MAX_CHARS * VERTEX_SIZE * 4);
 }
 
@@ -20,7 +21,8 @@ void FontRenderer::drawString(float x, float y, const char* text) {
             c = (text[index] & 0x3F) | ((c & 0x1F) << 6);
         }
         if(c == '&') {
-            if(text[index + 1] == '\0' || text[index + 2] == '\0' || text[index + 3] == '\0') {
+            if(text[index + 1] == '\0' || text[index + 2] == '\0' ||
+               text[index + 3] == '\0') {
                 break;
             }
             color[0] = ((text[index + 1] - '0') * 255) / 9;

+ 4 - 4
client/rendering/NoiseTexture.cpp

@@ -1,14 +1,14 @@
 #include "client/rendering/NoiseTexture.h"
+#include "gaming-core/utils/Array.h"
 #include "gaming-core/utils/Random.h"
-#include "gaming-core/utils/List.h"
 
 NoiseTexture::NoiseTexture() : texture(TextureFormat::float32(3)) {
     Random r(1);
-    List<float, 48> data;
+    Array<float, 48> data;
     for(int i = 0; i < 48; i++) {
-        data.add(r.nextFloat() * 2.0f - 1.0f);
+        data[i] = r.nextFloat() * 2.0f - 1.0f;
     }
-    texture.setData(4, 4, &(data[0]));
+    texture.setData(4, 4, data.begin());
 }
 
 void NoiseTexture::bindTo(int index) const {

+ 30 - 15
client/rendering/renderer/WorldRenderer.cpp

@@ -1,6 +1,7 @@
 #include "client/rendering/renderer/WorldRenderer.h"
 
-WorldRenderer::WorldRenderer(const World& world) : world(world), texture("resources/textures.png", 4) {
+WorldRenderer::WorldRenderer(const World& world)
+    : world(world), texture("resources/textures.png", 4) {
     TypedBuffer<Triangle> buffer(100);
     for(int x = 0; x < world.getSize(); x++) {
         for(int y = 0; y < world.getHeight(); y++) {
@@ -20,7 +21,8 @@ void WorldRenderer::render(float lag, Renderer& renderer) {
     texture.bindTo(0);
     for(int x = -1; x <= 1; x++) {
         for(int z = -1; z <= 1; z++) {
-            renderer.translateTo(world.getSize() * x, 0.0f, world.getSize() * z).update();
+            renderer.translateTo(world.getSize() * x, 0.0f, world.getSize() * z)
+                .update();
             mesh.draw();
         }
     }
@@ -30,7 +32,8 @@ bool WorldRenderer::isAir(int x, int y, int z) const {
     return world.getBlock(x, y, z).getId() == 0;
 }
 
-void WorldRenderer::addCube(TypedBuffer<Triangle>& buffer, float x, float y, float z) {
+void WorldRenderer::addCube(TypedBuffer<Triangle>& buffer, float x, float y,
+                            float z) {
     Vector3 v000(x, y, z);
     Vector3 v001(x, y, z + 1);
     Vector3 v010(x, y + 1, z);
@@ -48,28 +51,40 @@ void WorldRenderer::addCube(TypedBuffer<Triangle>& buffer, float x, float y, flo
 
     if(isAir(x, y - 1, z)) {
         Vector2 tb(0.125f, 0.0625f);
-        buffer.add(Triangle(Vertex(v000, Vector2(0.125f, 0.0f)), Vertex(v100, t1), Vertex(v001, tb)));
-        buffer.add(Triangle(Vertex(v100, t1), Vertex(v101, t4), Vertex(v001, tb)));
+        buffer.add(Triangle(Vertex(v000, Vector2(0.125f, 0.0f)),
+                            Vertex(v100, t1), Vertex(v001, tb)));
+        buffer.add(
+            Triangle(Vertex(v100, t1), Vertex(v101, t4), Vertex(v001, tb)));
     }
     if(isAir(x, y + 1, z)) {
         Vector2 tt(0.3125f, 0.0f);
-        buffer.add(Triangle(Vertex(v010, t2), Vertex(v011, t3), Vertex(v110, tt)));
-        buffer.add(Triangle(Vertex(v110, tt), Vertex(v011, t3), Vertex(v111, Vector2(0.3125f, 0.0625f))));
+        buffer.add(
+            Triangle(Vertex(v010, t2), Vertex(v011, t3), Vertex(v110, tt)));
+        buffer.add(Triangle(Vertex(v110, tt), Vertex(v011, t3),
+                            Vertex(v111, Vector2(0.3125f, 0.0625f))));
     }
     if(isAir(x - 1, y, z)) {
-        buffer.add(Triangle(Vertex(v000, t4), Vertex(v001, t3), Vertex(v010, t1)));
-        buffer.add(Triangle(Vertex(v001, t3), Vertex(v011, t2), Vertex(v010, t1)));
+        buffer.add(
+            Triangle(Vertex(v000, t4), Vertex(v001, t3), Vertex(v010, t1)));
+        buffer.add(
+            Triangle(Vertex(v001, t3), Vertex(v011, t2), Vertex(v010, t1)));
     }
     if(isAir(x + 1, y, z)) {
-        buffer.add(Triangle(Vertex(v100, t3), Vertex(v110, t2), Vertex(v101, t4)));
-        buffer.add(Triangle(Vertex(v101, t4), Vertex(v110, t2), Vertex(v111, t1)));
+        buffer.add(
+            Triangle(Vertex(v100, t3), Vertex(v110, t2), Vertex(v101, t4)));
+        buffer.add(
+            Triangle(Vertex(v101, t4), Vertex(v110, t2), Vertex(v111, t1)));
     }
     if(isAir(x, y, z + 1)) {
-        buffer.add(Triangle(Vertex(v001, t4), Vertex(v101, t3), Vertex(v011, t1)));
-        buffer.add(Triangle(Vertex(v111, t2), Vertex(v011, t1), Vertex(v101, t3)));
+        buffer.add(
+            Triangle(Vertex(v001, t4), Vertex(v101, t3), Vertex(v011, t1)));
+        buffer.add(
+            Triangle(Vertex(v111, t2), Vertex(v011, t1), Vertex(v101, t3)));
     }
     if(isAir(x, y, z - 1)) {
-        buffer.add(Triangle(Vertex(v000, t3), Vertex(v010, t2), Vertex(v100, t4)));
-        buffer.add(Triangle(Vertex(v110, t1), Vertex(v100, t4), Vertex(v010, t2)));
+        buffer.add(
+            Triangle(Vertex(v000, t3), Vertex(v010, t2), Vertex(v100, t4)));
+        buffer.add(
+            Triangle(Vertex(v110, t1), Vertex(v100, t4), Vertex(v010, t2)));
     }
 }

+ 4 - 4
common/block/BlockRegistry.h

@@ -1,15 +1,15 @@
 #ifndef BLOCKREGISTRY_H
 #define BLOCKREGISTRY_H
 
+#include "common/block/Block.h"
 #include "common/block/BlockTypes.h"
-#include "gaming-core/utils/List.h"
 #include "gaming-core/utils/HashMap.h"
-#include "common/block/Block.h"
+#include "gaming-core/utils/List.h"
 
 class BlockRegistry final {
 public:
     BlockRegistry();
-    void forEach(void (*f) (const Block&)) const;
+    void forEach(void (*f)(const Block&)) const;
     const Block& getBlock(const BlockName& registry) const;
     const Block& getBlock(BlockId id) const;
 
@@ -18,7 +18,7 @@ private:
 
     static constexpr int MAX_BLOCKS = 4096;
 
-    List<Block, MAX_BLOCKS> blocks;
+    List<Block> blocks;
     HashMap<BlockName, BlockId, MAX_BLOCKS> registryToId;
 };
 

+ 11 - 6
common/world/BlockMap.cpp

@@ -2,8 +2,11 @@
 
 static constexpr BlockId EMPTY_BLOCK = 65535;
 
-BlockMap::BlockMap(int length, BlockId id) : blocks(length, 1), map(2, EMPTY_BLOCK) {
-    map[0] = id;
+BlockMap::BlockMap(int length, BlockId id) : blocks(length, 1) {
+    blocks.fill(0);
+    map.reserve(2);
+    map.add(id);
+    map.add(EMPTY_BLOCK);
 }
 
 BlockId BlockMap::get(int index) const {
@@ -22,8 +25,10 @@ void BlockMap::set(int index, BlockId id) {
         }
     }
     blocks.resize(blocks.getBits() + 1);
-    int i = map.getLength();
-    map.grow(map.getLength(), EMPTY_BLOCK);
-    map[i] = id;
-    blocks.set(index, i);
+    int mapId = map.getLength();
+    for(int i = 0; i < mapId; i++) {
+        map.add(EMPTY_BLOCK);
+    }
+    map[mapId] = id;
+    blocks.set(index, mapId);
 }

+ 2 - 2
common/world/BlockMap.h

@@ -3,11 +3,11 @@
 
 #include "common/block/BlockTypes.h"
 #include "gaming-core/utils/BitArray.h"
-#include "gaming-core/utils/HeapArray.h"
+#include "gaming-core/utils/List.h"
 
 class BlockMap {
     BitArray blocks;
-    HeapArray<BlockId> map;
+    List<BlockId> map;
 
 public:
     BlockMap(int length, BlockId id);

+ 6 - 1
common/world/BlockStorage.cpp

@@ -5,7 +5,12 @@ static constexpr int SEGMENT = 1 << SEGMENT_BITS;
 static constexpr int SEGMENT_MASK = (1 << SEGMENT_BITS) - 1;
 
 BlockStorage::BlockStorage(int sizeBits, int heightBits)
-    : size(1 << sizeBits), height(1 << heightBits), sizeMask(size - 1), maps((size * size * height) / SEGMENT) {
+    : size(1 << sizeBits), height(1 << heightBits), sizeMask(size - 1) {
+    // int pointers = (size * size * height) / SEGMENT;
+    maps.resize((size * size * height) / SEGMENT);
+    // for(int i = 0; i < pointers; i++) {
+    //    maps.add();
+    //}
 }
 
 BlockId BlockStorage::get(int x, int y, int z) const {

+ 4 - 2
common/world/BlockStorage.h

@@ -3,14 +3,16 @@
 
 #include "common/world/BlockMap.h"
 #include "gaming-core/memory/UniquePointer.h"
-#include "gaming-core/utils/HeapArray.h"
+#include "gaming-core/utils/List.h"
+
+#include <memory>
 
 class BlockStorage final {
     int size;
     int height;
     int sizeMask;
 
-    HeapArray<UniquePointer<BlockMap>> maps;
+    List<UniquePointer<BlockMap>> maps;
 
 public:
     BlockStorage(int sizeBits, int heightBits);

+ 3 - 2
common/world/World.cpp

@@ -2,8 +2,9 @@
 #include "common/world/HighMap.h"
 #include "gaming-core/utils/Random.h"
 
-World::World(const BlockRegistry& blockRegistry) : blockRegistry(blockRegistry), blocks(7, 7) {
-    Block stone = blockRegistry.getBlock("stone");
+World::World(const BlockRegistry& blockRegistry)
+    : blockRegistry(blockRegistry), blocks(7, 7) {
+    const Block& stone = blockRegistry.getBlock("stone");
     HighMap map(blocks.getSize(), blocks.getHeight());
     for(int x = 0; x < blocks.getSize(); x++) {
         for(int z = 0; z < blocks.getSize(); z++) {

+ 1 - 1
gaming-core

@@ -1 +1 @@
-Subproject commit 26bdee18ce5b5de78c9ffa3707c6646c9b6742fc
+Subproject commit 6c37eecba810767b8e6ce5644d1bca27c14b8ba7