default load-speed set to 4 + minor refactor

This commit is contained in:
MihailRis 2024-01-24 20:43:52 +03:00
parent 58fbb96f2c
commit 5687e55094
5 changed files with 17 additions and 15 deletions

View File

@ -9,7 +9,6 @@
#include <glm/ext.hpp> #include <glm/ext.hpp>
using glm::ivec2; using glm::ivec2;
using std::shared_ptr;
ChunksRenderer::ChunksRenderer(Level* level, const ContentGfxCache* cache, const EngineSettings& settings) : level(level) { ChunksRenderer::ChunksRenderer(Level* level, const ContentGfxCache* cache, const EngineSettings& settings) : level(level) {
const int MAX_FULL_CUBES = 3000; const int MAX_FULL_CUBES = 3000;
@ -20,10 +19,10 @@ ChunksRenderer::~ChunksRenderer() {
delete renderer; delete renderer;
} }
shared_ptr<Mesh> ChunksRenderer::render(Chunk* chunk) { std::shared_ptr<Mesh> ChunksRenderer::render(Chunk* chunk) {
chunk->setModified(false); chunk->setModified(false);
Mesh* mesh = renderer->render(chunk, level->chunksStorage); Mesh* mesh = renderer->render(chunk, level->chunksStorage);
auto sptr = shared_ptr<Mesh>(mesh); auto sptr = std::shared_ptr<Mesh>(mesh);
meshes[ivec2(chunk->x, chunk->z)] = sptr; meshes[ivec2(chunk->x, chunk->z)] = sptr;
return sptr; return sptr;
} }
@ -35,7 +34,7 @@ void ChunksRenderer::unload(Chunk* chunk) {
} }
} }
shared_ptr<Mesh> ChunksRenderer::getOrRender(Chunk* chunk) { std::shared_ptr<Mesh> ChunksRenderer::getOrRender(Chunk* chunk) {
auto found = meshes.find(ivec2(chunk->x, chunk->z)); auto found = meshes.find(ivec2(chunk->x, chunk->z));
if (found != meshes.end() && !chunk->isModified()){ if (found != meshes.end() && !chunk->isModified()){
return found->second; return found->second;
@ -43,7 +42,7 @@ shared_ptr<Mesh> ChunksRenderer::getOrRender(Chunk* chunk) {
return render(chunk); return render(chunk);
} }
shared_ptr<Mesh> ChunksRenderer::get(Chunk* chunk) { std::shared_ptr<Mesh> ChunksRenderer::get(Chunk* chunk) {
auto found = meshes.find(ivec2(chunk->x, chunk->z)); auto found = meshes.find(ivec2(chunk->x, chunk->z));
if (found != meshes.end()) { if (found != meshes.end()) {
return found->second; return found->second;

View File

@ -40,18 +40,17 @@ void Lighting::clear(){
} }
} }
void Lighting::prebuildSkyLight(int cx, int cz){ void Lighting::prebuildSkyLight(Chunk* chunk, const ContentIndices* indices){
const Block* const* blockDefs = content->getIndices()->getBlockDefs(); auto* blockDefs = indices->getBlockDefs();
Chunk* chunk = chunks->getChunk(cx, cz);
int highestPoint = 0; int highestPoint = 0;
for (int z = 0; z < CHUNK_D; z++){ for (int z = 0; z < CHUNK_D; z++){
for (int x = 0; x < CHUNK_W; x++){ for (int x = 0; x < CHUNK_W; x++){
for (int y = CHUNK_H-1;;y--){ for (int y = CHUNK_H-1;;y--){
if (y < 0) if (y < 0)
break; break;
voxel* vox = &(chunk->voxels[(y * CHUNK_D + z) * CHUNK_W + x]); voxel& vox = chunk->voxels[(y * CHUNK_D + z) * CHUNK_W + x];
const Block* block = blockDefs[vox->id]; const Block* block = blockDefs[vox.id];
if (!block->skyLightPassing) { if (!block->skyLightPassing) {
if (highestPoint < y) if (highestPoint < y)
highestPoint = y; highestPoint = y;

View File

@ -2,6 +2,8 @@
#define LIGHTING_LIGHTING_H_ #define LIGHTING_LIGHTING_H_
class Content; class Content;
class ContentIndices;
class Chunk;
class Chunks; class Chunks;
class LightSolver; class LightSolver;
@ -17,10 +19,11 @@ public:
~Lighting(); ~Lighting();
void clear(); void clear();
void prebuildSkyLight(int cx, int cz);
void buildSkyLight(int cx, int cz); void buildSkyLight(int cx, int cz);
void onChunkLoaded(int cx, int cz, bool expand); void onChunkLoaded(int cx, int cz, bool expand);
void onBlockSet(int x, int y, int z, int id); void onBlockSet(int x, int y, int z, int id);
static void prebuildSkyLight(Chunk* chunk, const ContentIndices* indices);
}; };
#endif /* LIGHTING_LIGHTING_H_ */ #endif /* LIGHTING_LIGHTING_H_ */

View File

@ -4,6 +4,7 @@
#include <memory> #include <memory>
#include <iostream> #include <iostream>
#include "../content/Content.h"
#include "../voxels/Block.h" #include "../voxels/Block.h"
#include "../voxels/Chunk.h" #include "../voxels/Chunk.h"
#include "../voxels/Chunks.h" #include "../voxels/Chunks.h"
@ -63,7 +64,7 @@ bool ChunksController::loadVisible(){
int index = z * w + x; int index = z * w + x;
auto chunk = chunks->chunks[index]; auto chunk = chunks->chunks[index];
if (chunk != nullptr){ if (chunk != nullptr){
if (!chunk->isLighted()) { if (chunk->isLoaded() && !chunk->isLighted()) {
int surrounding = 0; int surrounding = 0;
for (int oz = -1; oz <= 1; oz++){ for (int oz = -1; oz <= 1; oz++){
for (int ox = -1; ox <= 1; ox++){ for (int ox = -1; ox <= 1; ox++){
@ -112,11 +113,11 @@ bool ChunksController::loadVisible(){
); );
chunk->setUnsaved(true); chunk->setUnsaved(true);
} }
chunk->updateHeights(); chunk->updateHeights();
if (!chunk->isLoadedLights()) { if (!chunk->isLoadedLights()) {
lighting->prebuildSkyLight(chunk->x, chunk->z); Lighting::prebuildSkyLight(chunk.get(), level->content->getIndices());
} }
chunk->setLoaded(true);
return true; return true;
} }

View File

@ -25,7 +25,7 @@ struct DisplaySettings {
struct ChunksSettings { struct ChunksSettings {
/* Max milliseconds that engine uses for chunks loading only */ /* Max milliseconds that engine uses for chunks loading only */
uint loadSpeed = 10; uint loadSpeed = 4;
/* Radius of chunks loading zone (chunk is unit) */ /* Radius of chunks loading zone (chunk is unit) */
uint loadDistance = 22; uint loadDistance = 22;
/* Buffer zone where chunks are not unloading (chunk is unit)*/ /* Buffer zone where chunks are not unloading (chunk is unit)*/