rename ChunksStorage to GlobalChunks

This commit is contained in:
MihailRis
2024-12-14 18:58:51 +03:00
parent 323acb2573
commit e713412a6d
11 changed files with 27 additions and 27 deletions
@@ -1,4 +1,4 @@
#include "ChunksStorage.hpp"
#include "GlobalChunks.hpp"
#include <algorithm>
@@ -28,10 +28,10 @@ inline long long keyfrom(int x, int z) {
static debug::Logger logger("chunks-storage");
ChunksStorage::ChunksStorage(Level* level) : level(level) {
GlobalChunks::GlobalChunks(Level* level) : level(level) {
}
std::shared_ptr<Chunk> ChunksStorage::fetch(int x, int z) {
std::shared_ptr<Chunk> GlobalChunks::fetch(int x, int z) {
const auto& found = chunksMap.find(keyfrom(x, z));
if (found == chunksMap.end()) {
return nullptr;
@@ -63,11 +63,11 @@ static void check_voxels(const ContentIndices& indices, Chunk& chunk) {
}
}
void ChunksStorage::erase(int x, int z) {
void GlobalChunks::erase(int x, int z) {
chunksMap.erase(keyfrom(x, z));
}
std::shared_ptr<Chunk> ChunksStorage::create(int x, int z) {
std::shared_ptr<Chunk> GlobalChunks::create(int x, int z) {
const auto& found = chunksMap.find(keyfrom(x, z));
if (found != chunksMap.end()) {
return found->second;
@@ -120,19 +120,19 @@ std::shared_ptr<Chunk> ChunksStorage::create(int x, int z) {
return chunk;
}
void ChunksStorage::pinChunk(std::shared_ptr<Chunk> chunk) {
void GlobalChunks::pinChunk(std::shared_ptr<Chunk> chunk) {
pinnedChunks[{chunk->x, chunk->z}] = std::move(chunk);
}
void ChunksStorage::unpinChunk(int x, int z) {
void GlobalChunks::unpinChunk(int x, int z) {
pinnedChunks.erase({x, z});
}
size_t ChunksStorage::size() const {
size_t GlobalChunks::size() const {
return chunksMap.size();
}
voxel* ChunksStorage::get(int x, int y, int z) const {
voxel* GlobalChunks::get(int x, int y, int z) const {
if (y < 0 || y >= CHUNK_H) {
return nullptr;
}
@@ -150,7 +150,7 @@ voxel* ChunksStorage::get(int x, int y, int z) const {
return &chunk->voxels[(y * CHUNK_D + lz) * CHUNK_W + lx];
}
void ChunksStorage::incref(Chunk* chunk) {
void GlobalChunks::incref(Chunk* chunk) {
auto key = reinterpret_cast<ptrdiff_t>(chunk);
const auto& found = refCounters.find(key);
if (found == refCounters.end()) {
@@ -160,7 +160,7 @@ void ChunksStorage::incref(Chunk* chunk) {
found->second++;
}
void ChunksStorage::decref(Chunk* chunk) {
void GlobalChunks::decref(Chunk* chunk) {
auto key = reinterpret_cast<ptrdiff_t>(chunk);
const auto& found = refCounters.find(key);
if (found == refCounters.end()) {
@@ -180,7 +180,7 @@ void ChunksStorage::decref(Chunk* chunk) {
}
}
void ChunksStorage::save(Chunk* chunk) {
void GlobalChunks::save(Chunk* chunk) {
if (chunk == nullptr) {
return;
}
@@ -204,7 +204,7 @@ void ChunksStorage::save(Chunk* chunk) {
);
}
void ChunksStorage::saveAll() {
void GlobalChunks::saveAll() {
for (const auto& [_, chunk] : chunksMap) {
save(chunk.get());
}
@@ -12,14 +12,14 @@
class Chunk;
class Level;
class ChunksStorage {
class GlobalChunks {
Level* level;
std::unordered_map<long long, std::shared_ptr<Chunk>> chunksMap;
std::unordered_map<glm::ivec2, std::shared_ptr<Chunk>> pinnedChunks;
std::unordered_map<ptrdiff_t, int> refCounters;
public:
ChunksStorage(Level* level);
~ChunksStorage() = default;
GlobalChunks(Level* level);
~GlobalChunks() = default;
std::shared_ptr<Chunk> fetch(int x, int z);
std::shared_ptr<Chunk> create(int x, int z);