From e2043c3b2c587a76900b68b68c41bb0f7cb6b3d1 Mon Sep 17 00:00:00 2001 From: Ben Date: Fri, 18 Oct 2019 18:38:59 +0100 Subject: [PATCH] at least it works --- src/game.cpp | 15 ++++++++++----- src/renderer/chunk.cpp | 22 +++++++++------------- src/renderer/voxel.cpp | 11 ++--------- src/world/block.cpp | 20 ++++++++++++++++---- src/world/block.hpp | 10 +++++++--- 5 files changed, 44 insertions(+), 34 deletions(-) diff --git a/src/game.cpp b/src/game.cpp index 054fcde..9779104 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -82,16 +82,21 @@ void Game::Setup(int w, int h) { m_cameras["Default"] = std::make_shared(w, h); m_activeCamera = m_cameras["Default"]; - BlockDictionary.Build(); + std::shared_ptr BlockDictionary = CBlockDictionary::GetInstance(); + + BlockDictionary->Build(); m_world = std::make_unique(); Texture texture; - m_world->TextureID = texture.LoadTextures(BlockDictionary.Textures); + m_world->TextureID = texture.LoadTextures(BlockDictionary->Textures); - m_world->Chunks.push_back(std::make_shared(0, 0)); - m_world->Chunks.push_back(std::make_shared(1, 1)); - m_world->Chunks.push_back(std::make_shared(1, 3)); + for (int x = 0; x < 3; x++) + for (int y = 0; y < 3; y++) { + + m_world->Chunks.push_back(std::make_shared(x, y)); + + } m_world->Shaders["Basic"] = std::make_shared(); m_world->Shaders["Basic"]->Load(GameConfig.ResourceBase + "shaders/simple"); diff --git a/src/renderer/chunk.cpp b/src/renderer/chunk.cpp index 6f0e4ee..30eac84 100644 --- a/src/renderer/chunk.cpp +++ b/src/renderer/chunk.cpp @@ -16,21 +16,19 @@ Chunk::Chunk(int x, int z) { for (int y = 0; y < CHUNK_HEIGHT; y++) for (int z = 0; z < CHUNK_DEPTH; z++) { - // Grass on the top layer - // if (y == CHUNK_HEIGHT - 1) { + // Grass on the top layer + if (y == CHUNK_HEIGHT - 1) { Voxels.push_back((uint8_t)EBlockType::Grass); - // } else { + } else { - // Voxels.push_back((uint8_t)EBlockType::Dirt); + Voxels.push_back((uint8_t)EBlockType::Dirt); - // } + } } - std::cout << Voxels.size() << " voxels" << std::endl; - m_mesh(); } @@ -85,12 +83,8 @@ void Chunk::m_mesh() { std::vector tempVerts; std::vector tempUVs; - std::cout << x << " " << y << " " << z << std::endl; - uint8_t block = BlockAt(x, y, z); - std::cout << "Block ID " << (int)block << std::endl; - Voxel tmp({x, y, z}, BlockAt(x, y, z)); tmp.AddFace(EFaceType::Top); @@ -100,8 +94,10 @@ void Chunk::m_mesh() { tmp.AddFace(EFaceType::Front); tmp.AddFace(EFaceType::Back); - for (auto& uv : tempUVs) - m_uvs.push_back(uv); + tmp.GetMesh(tempVerts, tempUVs); + + m_vertices.insert(m_vertices.end(), tempVerts.begin(), tempVerts.end()); + m_uvs.insert(m_uvs.end(), tempUVs.begin(), tempUVs.end()); } diff --git a/src/renderer/voxel.cpp b/src/renderer/voxel.cpp index dba00b5..11c691b 100644 --- a/src/renderer/voxel.cpp +++ b/src/renderer/voxel.cpp @@ -47,7 +47,7 @@ void Voxel::AddFace(EFaceType::Face face) { case EFaceType::Left: { - verts = CubeTopFace; + verts = CubeLeftFace; uvs = CubeLeftFaceUVs; break; @@ -86,17 +86,10 @@ void Voxel::AddFace(EFaceType::Face face) { verts = m_translateIntoChunk(verts, m_coordsInChunk); m_vertices.insert(m_vertices.end(), verts.begin(), verts.end()); - std::cout << "Voxel ID " << (int)Block << std::endl; - std::shared_ptr block = BlockDictionary.BlockEntries[Block]; + std::shared_ptr block = CBlockDictionary::GetInstance()->BlockEntries[Block]; - std::cout << "Block Entries Size " << BlockDictionary.BlockEntries.size() << std::endl; - - std::cout << "Block ID " << (int)block->ID << std::endl; - std::cout << "Block Texture Count " << (int)block->FaceTextures.size() << std::endl; - uint16_t tex = block->FaceTextures[(uint16_t)face]; - std::cout << "Texture ID " << (int)tex << std::endl; std::vector uvws; diff --git a/src/world/block.cpp b/src/world/block.cpp index 814a5ba..b8271e3 100644 --- a/src/world/block.cpp +++ b/src/world/block.cpp @@ -4,6 +4,21 @@ #include +std::shared_ptr CBlockDictionary::Instance; + +std::shared_ptr CBlockDictionary::GetInstance() { + + if (!CBlockDictionary::Instance) { + + CBlockDictionary::Instance = std::make_shared(); + + } + + return CBlockDictionary::Instance; + +} + + void CBlockDictionary::Build() { RegisterTexture("dirt.png"); @@ -15,8 +30,6 @@ void CBlockDictionary::Build() { RegisterBlock(EBlockType::Dirt, { EFaceTexture::Dirt, EFaceTexture::Dirt, EFaceTexture::Dirt, EFaceTexture::Dirt, EFaceTexture::Dirt, EFaceTexture::Dirt }); RegisterBlock(EBlockType::Grass, { EFaceTexture::Grass, EFaceTexture::Dirt, EFaceTexture::GrassSide, EFaceTexture::GrassSide, EFaceTexture::GrassSide, EFaceTexture::GrassSide }); - std::cout << "Block Entries Size " << BlockEntries.size() << std::endl; - } void CBlockDictionary::RegisterTexture(std::string texture) { @@ -25,9 +38,8 @@ void CBlockDictionary::RegisterTexture(std::string texture) { } -void CBlockDictionary::RegisterBlock(uint8_t block, std::vector faceTextures) { +void CBlockDictionary::RegisterBlock(EBlockType::Block block, std::vector faceTextures) { - std::cout << "Block Entries Size " << BlockEntries.size() << std::endl; BlockEntries[block] = std::make_shared((uint8_t)block, faceTextures); } diff --git a/src/world/block.hpp b/src/world/block.hpp index f9e654b..d1cf586 100644 --- a/src/world/block.hpp +++ b/src/world/block.hpp @@ -44,6 +44,12 @@ public: // ie, import all the data used in the build from // files and that class CBlockDictionary { +public: + + static std::shared_ptr GetInstance(); + + static std::shared_ptr Instance; + public: void Build(); @@ -60,13 +66,11 @@ public: // Expects textures to be inserted in order, 0-... void RegisterTexture(std::string texture); - void RegisterBlock(uint8_t block, std::vector faceTextures); + void RegisterBlock(EBlockType::Block block, std::vector faceTextures); }; -static CBlockDictionary BlockDictionary; - // static std::vector> TextureIdsAndPaths { // {0, "dirt.png"}, // {1, "grass_side.png"},