Merge pull request #68 from A-lex-Ra/main

Небольшой рефакторинг (+ комментарии на будущее)
This commit is contained in:
MihailRis
2023-12-20 23:27:41 +03:00
committed by GitHub
6 changed files with 21 additions and 26 deletions
+1 -1
View File
@@ -1,7 +1,7 @@
#include "AssetsLoader.h" #include "AssetsLoader.h"
#include "Assets.h" #include "Assets.h"
#include "asset_loaders.h" #include "assetload_funcs.h"
#include <iostream> #include <iostream>
#include <memory> #include <memory>
@@ -1,4 +1,4 @@
#include "asset_loaders.h" #include "assetload_funcs.h"
#include <iostream> #include <iostream>
#include <filesystem> #include <filesystem>
+13 -13
View File
@@ -45,12 +45,12 @@ using std::unordered_map;
namespace fs = std::filesystem; namespace fs = std::filesystem;
WorldRegion::WorldRegion() { WorldRegion::WorldRegion() {
chunksData = new ubyte*[REGION_VOL]{}; chunksData = new ubyte*[REGION_CHUNKS_COUNT]{};
sizes = new uint32_t[REGION_VOL]{}; sizes = new uint32_t[REGION_CHUNKS_COUNT]{};
} }
WorldRegion::~WorldRegion() { WorldRegion::~WorldRegion() {
for (uint i = 0; i < REGION_VOL; i++) { for (uint i = 0; i < REGION_CHUNKS_COUNT; i++) {
delete[] chunksData[i]; delete[] chunksData[i];
} }
delete[] sizes; delete[] sizes;
@@ -79,11 +79,11 @@ void WorldRegion::put(uint x, uint z, ubyte* data, uint32_t size) {
sizes[chunk_index] = 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]; 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]; return sizes[z * REGION_SIZE + x];
} }
@@ -243,7 +243,7 @@ ubyte* WorldFiles::getData(unordered_map<ivec2, WorldRegion*>& regions,
WorldRegion* region = getOrCreateRegion(regions, regionX, regionZ); WorldRegion* region = getOrCreateRegion(regions, regionX, regionZ);
ubyte* data = region->get(localX, localZ); ubyte* data = region->getChunkData(localX, localZ);
if (data == nullptr) { if (data == nullptr) {
uint32_t size; uint32_t size;
data = readChunkData(x, z, size, data = readChunkData(x, z, size,
@@ -253,7 +253,7 @@ ubyte* WorldFiles::getData(unordered_map<ivec2, WorldRegion*>& regions,
} }
} }
if (data != nullptr) { 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; return nullptr;
} }
@@ -268,13 +268,13 @@ ubyte* WorldFiles::readChunkData(int x, int z, uint32_t& length, fs::path filena
int localZ = z - (regionZ * REGION_SIZE); int localZ = z - (regionZ * REGION_SIZE);
int chunkIndex = localZ * REGION_SIZE + localX; 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()){ if (!input.is_open()){
return nullptr; return nullptr;
} }
input.seekg(0, ios::end); input.seekg(0, ios::end);
size_t file_size = input.tellg(); 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; uint32_t offset;
input.seekg(table_offset + chunkIndex * 4); input.seekg(table_offset + chunkIndex * 4);
@@ -299,7 +299,7 @@ ubyte* WorldFiles::readChunkData(int x, int z, uint32_t& length, fs::path filena
void WorldFiles::writeRegion(int x, int y, WorldRegion* entry, fs::path filename){ void WorldFiles::writeRegion(int x, int y, WorldRegion* entry, fs::path filename){
ubyte** region = entry->getChunks(); ubyte** region = entry->getChunks();
uint32_t* sizes = entry->getSizes(); 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_x = (i % REGION_SIZE) + x * REGION_SIZE;
int chunk_z = (i / REGION_SIZE) + y * REGION_SIZE; int chunk_z = (i / REGION_SIZE) + y * REGION_SIZE;
if (region[i] == nullptr) { if (region[i] == nullptr) {
@@ -315,9 +315,9 @@ void WorldFiles::writeRegion(int x, int y, WorldRegion* entry, fs::path filename
size_t offset = 10; size_t offset = 10;
char intbuf[4]{}; 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]; ubyte* chunk = region[i];
if (chunk == nullptr){ if (chunk == nullptr){
offsets[i] = 0; offsets[i] = 0;
@@ -332,7 +332,7 @@ void WorldFiles::writeRegion(int x, int y, WorldRegion* entry, fs::path filename
file.write((const char*)chunk, compressedSize); 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); dataio::write_int32_big(offsets[i], (ubyte*)intbuf, 0);
file.write(intbuf, 4); file.write(intbuf, 4);
} }
+3 -3
View File
@@ -16,7 +16,7 @@
const uint REGION_SIZE_BIT = 5; const uint REGION_SIZE_BIT = 5;
const uint REGION_SIZE = (1 << (REGION_SIZE_BIT)); 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 REGION_FORMAT_VERSION = 1;
const uint WORLD_FORMAT_VERSION = 1; const uint WORLD_FORMAT_VERSION = 1;
#define REGION_FORMAT_MAGIC ".VOXREG" #define REGION_FORMAT_MAGIC ".VOXREG"
@@ -37,8 +37,8 @@ public:
~WorldRegion(); ~WorldRegion();
void put(uint x, uint z, ubyte* data, uint32_t size); void put(uint x, uint z, ubyte* data, uint32_t size);
ubyte* get(uint x, uint z); ubyte* getChunkData(uint x, uint z);
uint getSize(uint x, uint z); uint getChunkDataSize(uint x, uint z);
void setUnsaved(bool unsaved); void setUnsaved(bool unsaved);
bool isUnsaved() const; bool isUnsaved() const;
+3 -8
View File
@@ -4,6 +4,8 @@
// #include "../typedefs.h" // #include "../typedefs.h"
#include "aabb.h" #include "aabb.h"
#include "glm/glm.hpp" #include "glm/glm.hpp"
#define GLM_ENABLE_EXPERIMENTAL
#include "glm/gtx/hash.hpp"
#include <array> #include <array>
#include <unordered_map> #include <unordered_map>
@@ -35,16 +37,9 @@ public:
}; };
template<>
struct std::hash<rayvec3>{
std::size_t operator()(const rayvec3& r) const noexcept{
return std::hash<scalar_t>{}(r.x) ^ (std::hash<scalar_t>{}(r.y) << 1) ^ (std::hash<scalar_t>{}(r.z) << 2);
}
};
class Ray{ class Ray{
protected: 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<rayvec3, AABBFaces> raysBoxCache_; //[boxPos]: faces array static std::unordered_map<rayvec3, AABBFaces> raysBoxCache_; //[boxPos]: faces array
public: public: