Skip to content

Commit

Permalink
the lockining v2
Browse files Browse the repository at this point in the history
  • Loading branch information
Kade-github committed May 24, 2024
1 parent 0cd5d62 commit 96b27ac
Show file tree
Hide file tree
Showing 9 changed files with 111 additions and 103 deletions.
17 changes: 16 additions & 1 deletion src/Game/Data/World.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ siv::PerlinNoise ironPerlin;
siv::PerlinNoise diamondPerlin;
siv::PerlinNoise goldPerlin;

std::mutex Data::World::worldMutex = {};

int staticWaterLevel = 0;
int staticRandomAdvancement = 0;
Expand Down Expand Up @@ -74,26 +75,37 @@ void Data::Region::addChunk(Chunk c)

void Data::Region::freePlace(int x, int y, int z, int type)
{
Data::World::worldMutex.lock();
Chunk* c = getChunkPtr(x, z);

if (c == nullptr)
{
Data::World::worldMutex.unlock();
return;
}

c->placeBlock(x - c->x, y, z - c->z, type);
Data::World::worldMutex.unlock();
}

bool Data::Region::doesBlockExist(int x, int y, int z)
{
Data::World::worldMutex.lock();
Chunk* c = getChunkPtr(x, z);

if (c == nullptr)
{
Data::World::worldMutex.unlock();
return false;
}
Data::World::worldMutex.unlock();

return c->bChunk.blocks[x - c->x][z - c->z][y] > 0;
}

bool Data::Region::doesBlockExistInRange(int x, int y, int z, int type, int range)
{
Data::World::worldMutex.lock();
for (int i = 0; i < REGION_SIZE; i++)
{
for (int j = 0; j < REGION_SIZE; j++)
Expand All @@ -109,14 +121,17 @@ bool Data::Region::doesBlockExistInRange(int x, int y, int z, int type, int rang
if (c.bChunk.blocks[_x][_z][_y] == type)
{
if (abs(c.x + _x - x) < range && abs(c.z + _z - z) < range && abs(_y - y) < range)
{
Data::World::worldMutex.unlock();
return true;
}
}
}
}
}
}
}

Data::World::worldMutex.unlock();
return false;
}

Expand Down
2 changes: 2 additions & 0 deletions src/Game/Data/World.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

namespace Data
{

struct DataTag
{
char name[32] = "tag";
Expand Down Expand Up @@ -274,6 +275,7 @@ namespace Data
class World
{
public:
static std::mutex worldMutex;
std::string _path = "";
std::vector<std::string> storedRegions;
std::string name;
Expand Down
7 changes: 4 additions & 3 deletions src/Game/LightingManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ bool IsLightBlocked(glm::vec3 start, glm::vec3 end)
if (abs(start.y - end.y) < 2)
return false;


float progress = 0;
glm::vec3 direction = glm::normalize(end - start);

Expand Down Expand Up @@ -182,6 +183,8 @@ void LightingManager::RefreshAround(glm::vec3 pos)
{
// around a 2x2 area

std::lock_guard<std::mutex> lock(Data::World::worldMutex);

Chunk* current = WorldManager::instance->GetChunk(pos.x, pos.z);

if (current == nullptr)
Expand All @@ -207,6 +210,7 @@ void LightingManager::RefreshShadows()
auto gp = (Gameplay*)Game::instance->currentScene;
// Get all regions

std::lock_guard<std::mutex> lock(Data::World::worldMutex);

for (auto& r : WorldManager::instance->regions)
{
Expand Down Expand Up @@ -246,14 +250,12 @@ void LightingManager::RemoveLight(glm::vec3 pos)
}
int LightingManager::GetLightLevel(glm::vec3 pos)
{
lightMutex.lock();
int level = sun.strength;

Chunk* c = WorldManager::instance->GetChunk(pos.x, pos.z);

if (c == nullptr)
{
lightMutex.unlock();
return 0;
}

Expand Down Expand Up @@ -344,7 +346,6 @@ int LightingManager::GetLightLevel(glm::vec3 pos)
}
}

lightMutex.unlock();

return level;
}
1 change: 0 additions & 1 deletion src/Game/LightingManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ class LightingManager
{
static LightingManager* instance;
LightingManager() {}
std::mutex lightMutex;
public:
bool nextFrameRefresh = false;
std::vector<Light> lights;
Expand Down
55 changes: 27 additions & 28 deletions src/Game/Objects/Base/Chunk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,7 @@ void Chunk::RenderSubChunks()

models.clear();

chunkMutex.lock();
std::lock_guard<std::mutex> lock(Data::World::worldMutex);
for (int i = 0; i < subChunks.size(); i++)
{
subChunk& sbc = subChunks[i];
Expand All @@ -569,7 +569,7 @@ void Chunk::RenderSubChunks()

RenderSubChunk(sbc);
}
chunkMutex.unlock();

}

glm::vec3 Chunk::WorldToChunk(glm::vec3 pos)
Expand Down Expand Up @@ -712,9 +712,9 @@ void Chunk::RenderSubChunkShadow(subChunk& sbc)

void Chunk::RenderSubChunksShadow()
{
std::lock_guard<std::mutex> lock(Data::World::worldMutex);
shadowVertices.clear();
shadowIndices.clear();
chunkMutex.lock();
for (int i = 0; i < subChunks.size(); i++)
{
subChunk& sbc = subChunks[i];
Expand All @@ -723,7 +723,6 @@ void Chunk::RenderSubChunksShadow()

RenderSubChunkShadow(sbc);
}
chunkMutex.unlock();
}

subChunk Chunk::CreateSubChunk(int y)
Expand Down Expand Up @@ -934,29 +933,27 @@ void Chunk::DestroySubChunk(subChunk& c)

void Chunk::DestroySubChunks()
{
chunkMutex.lock();
std::lock_guard<std::mutex> lock(Data::World::worldMutex);
for (int i = CHUNK_HEIGHT; i > -1; i--)
DestroySubChunk(i);

models.clear();

subChunks.clear();
chunkMutex.unlock();
}

void Chunk::CreateSubChunks()
{
DestroySubChunks();
std::lock_guard<std::mutex> lock(Data::World::worldMutex);

chunkMutex.lock();
for (int y = CHUNK_HEIGHT - 1; y > -1; y--)
{
subChunk sbc = CreateSubChunk(y);
if (sbc.y != -1)
subChunks.push_back(sbc);
}

chunkMutex.unlock();
}

void Chunk::SetBuffer()
Expand Down Expand Up @@ -1277,34 +1274,36 @@ void Chunk::UpdateChunk(int tick)

if (subChunks.size() == 0)
return;
chunkMutex.lock();
for (int i = 0; i < subChunks.size(); i++)

{
subChunk& sbc = subChunks[i];
std::lock_guard<std::mutex> lock(Data::World::worldMutex);
for (int i = 0; i < subChunks.size(); i++)
{
subChunk& sbc = subChunks[i];

if (sbc.y == -1)
continue;
if (sbc.y == -1)
continue;

for (int x = 0; x < CHUNK_SIZE; x++)
{
for (int z = 0; z < CHUNK_SIZE; z++)
for (int x = 0; x < CHUNK_SIZE; x++)
{
Block* b = sbc.blocks[x][z];

if (b == nullptr)
for (int z = 0; z < CHUNK_SIZE; z++)
{
continue;
}
Block* b = sbc.blocks[x][z];

if (!b->updateable)
{
continue;
}
if (b == nullptr)
{
continue;
}

if (!b->updateable)
{
continue;
}

b->Update(tick);
b->Update(tick);
}
}
}

}
}
chunkMutex.unlock();
}
Loading

0 comments on commit 96b27ac

Please sign in to comment.