refactor: change pointer parameters to references for Level and Content in various classes

This commit is contained in:
MihailRis
2024-12-25 18:53:53 +03:00
parent ef2c0b2370
commit c1b311f3c4
41 changed files with 258 additions and 251 deletions
+8 -8
View File
@@ -9,8 +9,8 @@
#include "voxels/voxel.hpp"
#include "voxels/Block.hpp"
LightSolver::LightSolver(const ContentIndices* contentIds, Chunks* chunks, int channel)
: blockDefs(contentIds->blocks.getDefs()),
LightSolver::LightSolver(const ContentIndices& contentIds, Chunks& chunks, int channel)
: blockDefs(contentIds.blocks.getDefs()),
chunks(chunks),
channel(channel) {
}
@@ -18,7 +18,7 @@ LightSolver::LightSolver(const ContentIndices* contentIds, Chunks* chunks, int c
void LightSolver::add(int x, int y, int z, int emission) {
if (emission <= 1)
return;
Chunk* chunk = chunks->getChunkByVoxel(x, y, z);
Chunk* chunk = chunks.getChunkByVoxel(x, y, z);
if (chunk == nullptr)
return;
ubyte light = chunk->lightmap.get(x-chunk->x*CHUNK_W, y, z-chunk->z*CHUNK_D, channel);
@@ -32,11 +32,11 @@ void LightSolver::add(int x, int y, int z, int emission) {
void LightSolver::add(int x, int y, int z) {
assert (chunks != nullptr);
add(x,y,z, chunks->getLight(x,y,z, channel));
add(x,y,z, chunks.getLight(x,y,z, channel));
}
void LightSolver::remove(int x, int y, int z) {
Chunk* chunk = chunks->getChunkByVoxel(x, y, z);
Chunk* chunk = chunks.getChunkByVoxel(x, y, z);
if (chunk == nullptr)
return;
@@ -68,7 +68,7 @@ void LightSolver::solve(){
int y = entry.y+coords[imul3+1];
int z = entry.z+coords[imul3+2];
Chunk* chunk = chunks->getChunkByVoxel(x,y,z);
Chunk* chunk = chunks.getChunkByVoxel(x,y,z);
if (chunk) {
int lx = x - chunk->x * CHUNK_W;
int lz = z - chunk->z * CHUNK_D;
@@ -76,7 +76,7 @@ void LightSolver::solve(){
ubyte light = chunk->lightmap.get(lx,y,lz, channel);
if (light != 0 && light == entry.light-1){
voxel* vox = chunks->get(x, y, z);
voxel* vox = chunks.get(x, y, z);
if (vox && vox->id != 0) {
const Block* block = blockDefs[vox->id];
if (uint8_t emission = block->emission[channel]) {
@@ -105,7 +105,7 @@ void LightSolver::solve(){
int y = entry.y+coords[imul3+1];
int z = entry.z+coords[imul3+2];
Chunk* chunk = chunks->getChunkByVoxel(x,y,z);
Chunk* chunk = chunks.getChunkByVoxel(x,y,z);
if (chunk) {
int lx = x - chunk->x * CHUNK_W;
int lz = z - chunk->z * CHUNK_D;
+2 -2
View File
@@ -17,10 +17,10 @@ class LightSolver {
std::queue<lightentry> addqueue;
std::queue<lightentry> remqueue;
const Block* const* blockDefs;
Chunks* chunks;
Chunks& chunks;
int channel;
public:
LightSolver(const ContentIndices* contentIds, Chunks* chunks, int channel);
LightSolver(const ContentIndices& contentIds, Chunks& chunks, int channel);
void add(int x, int y, int z);
void add(int x, int y, int z, int emission);
+16 -16
View File
@@ -11,9 +11,9 @@
#include <memory>
Lighting::Lighting(const Content* content, Chunks* chunks)
Lighting::Lighting(const Content& content, Chunks& chunks)
: content(content), chunks(chunks) {
auto indices = content->getIndices();
auto& indices = *content.getIndices();
solverR = std::make_unique<LightSolver>(indices, chunks, 0);
solverG = std::make_unique<LightSolver>(indices, chunks, 1);
solverB = std::make_unique<LightSolver>(indices, chunks, 2);
@@ -23,7 +23,7 @@ Lighting::Lighting(const Content* content, Chunks* chunks)
Lighting::~Lighting() = default;
void Lighting::clear(){
const auto& chunks = this->chunks->getChunks();
const auto& chunks = this->chunks.getChunks();
for (size_t index = 0; index < chunks.size(); index++){
auto chunk = chunks[index];
if (chunk == nullptr)
@@ -35,34 +35,34 @@ void Lighting::clear(){
}
}
void Lighting::prebuildSkyLight(Chunk* chunk, const ContentIndices* indices){
const auto* blockDefs = indices->blocks.getDefs();
void Lighting::prebuildSkyLight(Chunk& chunk, const ContentIndices& indices){
const auto* blockDefs = indices.blocks.getDefs();
int highestPoint = 0;
for (int z = 0; z < CHUNK_D; z++){
for (int x = 0; x < CHUNK_W; x++){
for (int y = CHUNK_H-1; y >= 0; y--){
int index = (y * CHUNK_D + z) * CHUNK_W + x;
voxel& vox = chunk->voxels[index];
voxel& vox = chunk.voxels[index];
const Block* block = blockDefs[vox.id];
if (!block->skyLightPassing) {
if (highestPoint < y)
highestPoint = y;
break;
}
chunk->lightmap.setS(x,y,z, 15);
chunk.lightmap.setS(x,y,z, 15);
}
}
}
if (highestPoint < CHUNK_H-1)
highestPoint++;
chunk->lightmap.highestPoint = highestPoint;
chunk.lightmap.highestPoint = highestPoint;
}
void Lighting::buildSkyLight(int cx, int cz){
const auto blockDefs = content->getIndices()->blocks.getDefs();
const auto blockDefs = content.getIndices()->blocks.getDefs();
Chunk* chunk = chunks->getChunk(cx, cz);
Chunk* chunk = chunks.getChunk(cx, cz);
for (int z = 0; z < CHUNK_D; z++){
for (int x = 0; x < CHUNK_W; x++){
int gx = x + cx * CHUNK_W;
@@ -93,8 +93,8 @@ void Lighting::onChunkLoaded(int cx, int cz, bool expand) {
auto& solverB = *this->solverB;
auto& solverS = *this->solverS;
auto blockDefs = content->getIndices()->blocks.getDefs();
auto chunk = chunks->getChunk(cx, cz);
auto blockDefs = content.getIndices()->blocks.getDefs();
auto chunk = chunks.getChunk(cx, cz);
for (uint y = 0; y < CHUNK_H; y++){
for (uint z = 0; z < CHUNK_D; z++){
@@ -151,7 +151,7 @@ void Lighting::onChunkLoaded(int cx, int cz, bool expand) {
}
void Lighting::onBlockSet(int x, int y, int z, blockid_t id){
const auto& block = content->getIndices()->blocks.require(id);
const auto& block = content.getIndices()->blocks.require(id);
solverR->remove(x,y,z);
solverG->remove(x,y,z);
solverB->remove(x,y,z);
@@ -160,9 +160,9 @@ void Lighting::onBlockSet(int x, int y, int z, blockid_t id){
solverR->solve();
solverG->solve();
solverB->solve();
if (chunks->getLight(x,y+1,z, 3) == 0xF){
if (chunks.getLight(x,y+1,z, 3) == 0xF){
for (int i = y; i >= 0; i--){
voxel* vox = chunks->get(x,i,z);
voxel* vox = chunks.get(x,i,z);
if ((vox == nullptr || vox->id != 0) && block.skyLightPassing)
break;
solverS->add(x,i,z, 0xF);
@@ -183,7 +183,7 @@ void Lighting::onBlockSet(int x, int y, int z, blockid_t id){
solverS->remove(x,y,z);
for (int i = y-1; i >= 0; i--){
solverS->remove(x,i,z);
if (i == 0 || chunks->get(x,i-1,z)->id != 0){
if (i == 0 || chunks.get(x,i-1,z)->id != 0){
break;
}
}
+4 -4
View File
@@ -9,14 +9,14 @@ class Chunks;
class LightSolver;
class Lighting {
const Content* const content;
Chunks* chunks;
const Content& content;
Chunks& chunks;
std::unique_ptr<LightSolver> solverR;
std::unique_ptr<LightSolver> solverG;
std::unique_ptr<LightSolver> solverB;
std::unique_ptr<LightSolver> solverS;
public:
Lighting(const Content* content, Chunks* chunks);
Lighting(const Content& content, Chunks& chunks);
~Lighting();
void clear();
@@ -24,5 +24,5 @@ public:
void onChunkLoaded(int cx, int cz, bool expand);
void onBlockSet(int x, int y, int z, blockid_t id);
static void prebuildSkyLight(Chunk* chunk, const ContentIndices* indices);
static void prebuildSkyLight(Chunk& chunk, const ContentIndices& indices);
};