From 76475dd1b1b60ea09c3c4b2052a94530bcb9644d Mon Sep 17 00:00:00 2001 From: A-lex-Ra Date: Wed, 20 Dec 2023 22:23:55 +0600 Subject: [PATCH] refactoring btw --- src/assets/AssetsLoader.cpp | 2 +- ...{asset_loaders.cpp => assetload_funcs.cpp} | 2 +- .../{asset_loaders.h => assetload_funcs.h} | 0 src/files/WorldFiles.cpp | 26 +++++++++---------- src/files/WorldFiles.h | 6 ++--- src/maths/rays.h | 11 +++----- 6 files changed, 21 insertions(+), 26 deletions(-) rename src/assets/{asset_loaders.cpp => assetload_funcs.cpp} (99%) rename src/assets/{asset_loaders.h => assetload_funcs.h} (100%) diff --git a/src/assets/AssetsLoader.cpp b/src/assets/AssetsLoader.cpp index dd255dac0..e8ec09963 100644 --- a/src/assets/AssetsLoader.cpp +++ b/src/assets/AssetsLoader.cpp @@ -1,7 +1,7 @@ #include "AssetsLoader.h" #include "Assets.h" -#include "asset_loaders.h" +#include "assetload_funcs.h" #include #include diff --git a/src/assets/asset_loaders.cpp b/src/assets/assetload_funcs.cpp similarity index 99% rename from src/assets/asset_loaders.cpp rename to src/assets/assetload_funcs.cpp index 7b50fd826..60131aff1 100644 --- a/src/assets/asset_loaders.cpp +++ b/src/assets/assetload_funcs.cpp @@ -1,4 +1,4 @@ -#include "asset_loaders.h" +#include "assetload_funcs.h" #include #include diff --git a/src/assets/asset_loaders.h b/src/assets/assetload_funcs.h similarity index 100% rename from src/assets/asset_loaders.h rename to src/assets/assetload_funcs.h diff --git a/src/files/WorldFiles.cpp b/src/files/WorldFiles.cpp index 5264313a1..66d0b8f24 100644 --- a/src/files/WorldFiles.cpp +++ b/src/files/WorldFiles.cpp @@ -46,12 +46,12 @@ using std::filesystem::path; namespace fs = std::filesystem; WorldRegion::WorldRegion() { - chunksData = new ubyte*[REGION_VOL]{}; - sizes = new uint32_t[REGION_VOL]{}; + chunksData = new ubyte*[REGION_CHUNKS_COUNT]{}; + sizes = new uint32_t[REGION_CHUNKS_COUNT]{}; } WorldRegion::~WorldRegion() { - for (uint i = 0; i < REGION_VOL; i++) { + for (uint i = 0; i < REGION_CHUNKS_COUNT; i++) { delete[] chunksData[i]; } delete[] sizes; @@ -80,11 +80,11 @@ void WorldRegion::put(uint x, uint z, ubyte* data, uint32_t size) { sizes[chunk_index] = size; } -ubyte* WorldRegion::get(uint x, uint z) { +ubyte* WorldRegion::getChunkData(uint x, uint z) { return chunksData[z * REGION_SIZE + x]; } -uint WorldRegion::getSize(uint x, uint z) { +uint WorldRegion::getChunkDataSize(uint x, uint z) { return sizes[z * REGION_SIZE + x]; } @@ -240,7 +240,7 @@ ubyte* WorldFiles::getData(unordered_map& regions, WorldRegion* region = getOrCreateRegion(regions, regionX, regionZ); - ubyte* data = region->get(localX, localZ); + ubyte* data = region->getChunkData(localX, localZ); if (data == nullptr) { uint32_t size; data = readChunkData(x, z, size, @@ -250,7 +250,7 @@ ubyte* WorldFiles::getData(unordered_map& regions, } } if (data != nullptr) { - return decompress(data, region->getSize(localX, localZ), CHUNK_DATA_LEN); + return decompress(data, region->getChunkDataSize(localX, localZ), CHUNK_DATA_LEN); } return nullptr; } @@ -265,13 +265,13 @@ ubyte* WorldFiles::readChunkData(int x, int z, uint32_t& length, path filename){ int localZ = z - (regionZ * REGION_SIZE); int chunkIndex = localZ * REGION_SIZE + localX; - std::ifstream input(filename, std::ios::binary); + std::ifstream input(filename, std::ios::binary); // BAD: open/close a file for every single chunk may be ineffective if (!input.is_open()){ return nullptr; } input.seekg(0, ios::end); size_t file_size = input.tellg(); - size_t table_offset = file_size - REGION_VOL * 4; + size_t table_offset = file_size - REGION_CHUNKS_COUNT * 4; uint32_t offset; input.seekg(table_offset + chunkIndex * 4); @@ -296,7 +296,7 @@ ubyte* WorldFiles::readChunkData(int x, int z, uint32_t& length, path filename){ void WorldFiles::writeRegion(int x, int y, WorldRegion* entry, path filename){ ubyte** region = entry->getChunks(); uint32_t* sizes = entry->getSizes(); - for (size_t i = 0; i < REGION_VOL; i++) { + for (size_t i = 0; i < REGION_CHUNKS_COUNT; i++) { int chunk_x = (i % REGION_SIZE) + x * REGION_SIZE; int chunk_z = (i / REGION_SIZE) + y * REGION_SIZE; if (region[i] == nullptr) { @@ -312,9 +312,9 @@ void WorldFiles::writeRegion(int x, int y, WorldRegion* entry, path filename){ size_t offset = 10; char intbuf[4]{}; - uint offsets[REGION_VOL]{}; + uint offsets[REGION_CHUNKS_COUNT]{}; - for (size_t i = 0; i < REGION_VOL; i++) { + for (size_t i = 0; i < REGION_CHUNKS_COUNT; i++) { ubyte* chunk = region[i]; if (chunk == nullptr){ offsets[i] = 0; @@ -329,7 +329,7 @@ void WorldFiles::writeRegion(int x, int y, WorldRegion* entry, path filename){ file.write((const char*)chunk, compressedSize); } } - for (size_t i = 0; i < REGION_VOL; i++) { + for (size_t i = 0; i < REGION_CHUNKS_COUNT; i++) { dataio::write_int32_big(offsets[i], (ubyte*)intbuf, 0); file.write(intbuf, 4); } diff --git a/src/files/WorldFiles.h b/src/files/WorldFiles.h index 547534dfa..d46b9327d 100644 --- a/src/files/WorldFiles.h +++ b/src/files/WorldFiles.h @@ -16,7 +16,7 @@ const uint REGION_SIZE_BIT = 5; const uint REGION_SIZE = (1 << (REGION_SIZE_BIT)); -const uint REGION_VOL = ((REGION_SIZE) * (REGION_SIZE)); +const uint REGION_CHUNKS_COUNT = ((REGION_SIZE) * (REGION_SIZE)); const uint REGION_FORMAT_VERSION = 1; const uint WORLD_FORMAT_VERSION = 1; #define REGION_FORMAT_MAGIC ".VOXREG" @@ -37,8 +37,8 @@ class WorldRegion { ~WorldRegion(); void put(uint x, uint z, ubyte* data, uint32_t size); - ubyte* get(uint x, uint z); - uint getSize(uint x, uint z); + ubyte* getChunkData(uint x, uint z); + uint getChunkDataSize(uint x, uint z); void setUnsaved(bool unsaved); bool isUnsaved() const; diff --git a/src/maths/rays.h b/src/maths/rays.h index 17badfdff..47e8d5d7f 100644 --- a/src/maths/rays.h +++ b/src/maths/rays.h @@ -4,6 +4,8 @@ // #include "../typedefs.h" #include "aabb.h" #include "glm/glm.hpp" +#define GLM_ENABLE_EXPERIMENTAL +#include "glm/gtx/hash.hpp" #include #include @@ -35,16 +37,9 @@ class AABBFaces{ }; -template<> -struct std::hash{ - std::size_t operator()(const rayvec3& r) const noexcept{ - return std::hash{}(r.x) ^ (std::hash{}(r.y) << 1) ^ (std::hash{}(r.z) << 2); - } -}; - class Ray{ protected: - static const bool IS_RAYS_BOX_CACHE_ON = false; + static const bool IS_RAYS_BOX_CACHE_ON = false; // Now not working properly because not observe updates static std::unordered_map raysBoxCache_; //[boxPos]: faces array public: