Skip to content

Commit

Permalink
Smooth Chunk Building until I manage to thread it
Browse files Browse the repository at this point in the history
  • Loading branch information
Jukitsu committed Dec 16, 2023
1 parent ad45d9e commit 9264c73
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 17 deletions.
5 changes: 2 additions & 3 deletions Jukcraft/src/world/chunk/Chunk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,10 @@ namespace Jukcraft {
if (!block)
continue;

Block type = blockTypes[block];
const Block& type = blockTypes[block];
if (true) {
for (uint8_t i = 0; i < 6; i++) {
if (canRenderFacing(localPos + IDIRECTIONS[i])) {
uint8_t light = 15 << 4;
uint8_t blocklight = getBlockLightSafe(localPos + IDIRECTIONS[i]);
uint8_t skylight = getSkyLightSafe(localPos + IDIRECTIONS[i]);

Expand Down Expand Up @@ -173,7 +172,7 @@ namespace Jukcraft {
}

void Chunk::uploadMesh() {

// For future use
}
void Chunk::drawCubeLayer() {
if (!drawable)
Expand Down
22 changes: 10 additions & 12 deletions Jukcraft/src/world/chunk/ChunkManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace Jukcraft {
blocks,
*this
);
chunksToUpdates.insert(chunk);
chunksToUpdates.push(chunk);
chunksToLight.insert(chunk);
if (z > 0) {
std::shared_ptr<Chunk>& northernChunk = chunks[x][z - 1];
Expand Down Expand Up @@ -43,34 +43,32 @@ namespace Jukcraft {
blocks,
*this
);
chunksToUpdates.insert(chunk);
chunksToUpdates.push(chunk);
chunksToLight.insert(chunk);
return chunk;
}

void ChunkManager::tick() {
std::vector<std::future<void>> results;
for (std::shared_ptr<Chunk> chunk : chunksToUpdates) {
if (!chunksToUpdates.empty()) {
auto& chunk = chunksToUpdates.front();
chunksToUpdates.pop();
chunk->updateLayers();
// results.push_back(std::async(std::launch::async, std::bind(&Chunk::updateLayers, chunk.get())));
}
for (std::shared_ptr<Chunk> chunk : chunksToUpdates) {
chunk->uploadMesh();
}
chunksToUpdates.clear();
}

void ChunkManager::updateChunkAtPosition(std::shared_ptr<Chunk>& chunk, const glm::ivec3& localPos) {
chunksToUpdates.insert(chunk);
chunksToUpdates.push(chunk);

if (Chunk::IsOutside(localPos + IEAST) && !chunk->neighbourChunks.east.expired())
chunksToUpdates.insert(chunk->neighbourChunks.east.lock());
chunksToUpdates.push(chunk->neighbourChunks.east.lock());
if (Chunk::IsOutside(localPos + INORTH) && !chunk->neighbourChunks.north.expired())
chunksToUpdates.insert(chunk->neighbourChunks.north.lock());
chunksToUpdates.push(chunk->neighbourChunks.north.lock());
if (Chunk::IsOutside(localPos + IWEST) && !chunk->neighbourChunks.west.expired())
chunksToUpdates.insert(chunk->neighbourChunks.west.lock());
chunksToUpdates.push(chunk->neighbourChunks.west.lock());
if (Chunk::IsOutside(localPos + ISOUTH) && !chunk->neighbourChunks.south.expired())
chunksToUpdates.insert(chunk->neighbourChunks.south.lock());
chunksToUpdates.push(chunk->neighbourChunks.south.lock());

}

Expand Down
2 changes: 1 addition & 1 deletion Jukcraft/src/world/chunk/ChunkManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace Jukcraft {
private:

std::shared_ptr<Chunk> chunks[WORLD_SIZE][WORLD_SIZE];
std::unordered_set<std::shared_ptr<Chunk>> chunksToUpdates;
std::queue<std::shared_ptr<Chunk>> chunksToUpdates;
std::unordered_set<std::shared_ptr<Chunk>> chunksToLight;
const std::vector<Block>& blocks;
gfx::Buffer chunkUbo;
Expand Down
2 changes: 1 addition & 1 deletion Jukcraft/src/world/chunk/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace Jukcraft {
constexpr uint8_t CHUNK_DIM = 16;
constexpr uint8_t CHUNK_HEIGHT = 128;

constexpr uint8_t WORLD_SIZE = 2;
constexpr uint8_t WORLD_SIZE = 16;


struct PerChunkData {
Expand Down

0 comments on commit 9264c73

Please sign in to comment.