calculateIndex belongs to Chunk namespace

fix-multithread
EmaMaker 2023-10-03 15:42:01 +02:00
parent b95ea49c93
commit bc3c5587f8
4 changed files with 14 additions and 22 deletions

View File

@ -33,6 +33,8 @@ namespace Chunk
constexpr uint16_t CHUNK_STATE_IN_DELETING_QUEUE = 128; constexpr uint16_t CHUNK_STATE_IN_DELETING_QUEUE = 128;
int coord3DTo1D(int x, int y, int z); int coord3DTo1D(int x, int y, int z);
int32_t calculateIndex(int16_t x, int16_t y, int16_t z);
int32_t calculateIndex(glm::vec3 pos);
class Chunk class Chunk
{ {
@ -59,8 +61,9 @@ namespace Chunk
public: public:
GLuint VAO{0}, VBO{0}, extentsBuffer{0}, texinfoBuffer{0}, numVertices{0}; GLuint VAO{0}, VBO{0}, extentsBuffer{0}, texinfoBuffer{0}, numVertices{0};
int32_t getIndex(){ return index; }
std::atomic<float> unload_timer{0}; std::atomic<float> unload_timer{0};
// uint32_t is fine, since i'm limiting the coordinate to only use up to ten bits (1023). There's actually two spare bits
int32_t getIndex(){ return index; }
private: private:
glm::vec3 position{}; glm::vec3 position{};

View File

@ -36,9 +36,6 @@ namespace chunkmanager
void init(); void init();
void blockpick(bool place); void blockpick(bool place);
int32_t calculateIndex(int16_t i, int16_t j, int16_t k);
int32_t calculateIndex(Chunk::Chunk* c);
int32_t calculateIndex(glm::vec3 position);
void stop(); void stop();
void destroy(); void destroy();

View File

@ -15,16 +15,20 @@ namespace Chunk
return utils::coord3DTo1D(x, y, z, CHUNK_SIZE, CHUNK_SIZE, CHUNK_SIZE); return utils::coord3DTo1D(x, y, z, CHUNK_SIZE, CHUNK_SIZE, CHUNK_SIZE);
} }
int32_t calculateIndex(glm::vec3 pos){
return calculateIndex(static_cast<int16_t>(pos.x), static_cast<int16_t>(pos.y),
static_cast<int16_t>(pos.z));
}
int32_t calculateIndex(int16_t x, int16_t y, int16_t z){
return x | (y << 10) | (z << 20);
}
Chunk::Chunk(glm::vec3 pos) Chunk::Chunk(glm::vec3 pos)
{ {
this->position = pos; this->position = pos;
this->setState(CHUNK_STATE_EMPTY, true); this->setState(CHUNK_STATE_EMPTY, true);
this->setBlocks(0, CHUNK_MAX_INDEX, Block::AIR); this->setBlocks(0, CHUNK_MAX_INDEX, Block::AIR);
this->index = calculateIndex(pos);
int16_t i = static_cast<int16_t>(pos.x);
int16_t j = static_cast<int16_t>(pos.y);
int16_t k = static_cast<int16_t>(pos.z);
index = i | (j << 10) | (k << 20);
} }
Block Chunk::getBlock(int x, int y, int z) Block Chunk::getBlock(int x, int y, int z)

View File

@ -122,7 +122,7 @@ namespace chunkmanager
if(x < 0 || y < 0 || z < 0 || x > 1023 || y > 1023 || z > 1023) continue; if(x < 0 || y < 0 || z < 0 || x > 1023 || y > 1023 || z > 1023) continue;
nExplored++; nExplored++;
const int32_t index = calculateIndex(x, y, z); const int32_t index = Chunk::calculateIndex(x, y, z);
ChunkTable::accessor a; ChunkTable::accessor a;
if(!chunks.find(a, index)) chunks.emplace(a, std::make_pair(index, new if(!chunks.find(a, index)) chunks.emplace(a, std::make_pair(index, new
Chunk::Chunk(glm::vec3(x,y,z)))); Chunk::Chunk(glm::vec3(x,y,z))));
@ -214,18 +214,6 @@ namespace chunkmanager
} }
} }
// uint32_t is fine, since i'm limiting the coordinate to only use up to ten bits (1023). There's actually two spare bits
int32_t calculateIndex(Chunk::Chunk* c){
return calculateIndex(c->getPosition());
}
int32_t calculateIndex(glm::vec3 position){
return calculateIndex(static_cast<int16_t>(position.x), static_cast<int16_t>(position.y),
static_cast<int16_t>(position.z));
}
int32_t calculateIndex(int16_t i, int16_t j, int16_t k){
return i | (j << 10) | (k << 20);
}
std::array<std::array<int16_t, 3>, chunks_volume>& getChunksIndices(){ return chunks_indices; } std::array<std::array<int16_t, 3>, chunks_volume>& getChunksIndices(){ return chunks_indices; }
void stop() { void stop() {