Skip to content

Commit

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

std::mutex m;

int staticWaterLevel = 0;
int staticRandomAdvancement = 0;
Expand All @@ -27,7 +26,6 @@ std::minstd_rand0 r;

Data::Chunk Data::Region::getChunk(int x, int z)
{
std::lock_guard<std::mutex> lock(m);
int realX = (x - startX) / CHUNK_SIZE;
int realZ = (z - startZ) / CHUNK_SIZE;

Expand All @@ -45,7 +43,6 @@ Data::Chunk Data::Region::getChunk(int x, int z)

Data::Chunk* Data::Region::getChunkPtr(int x, int z)
{
std::lock_guard<std::mutex> lock(m);
int realX = (x - startX) / CHUNK_SIZE;
int realZ = (z - startZ) / CHUNK_SIZE;

Expand All @@ -63,7 +60,6 @@ Data::Chunk* Data::Region::getChunkPtr(int x, int z)

void Data::Region::addChunk(Chunk c)
{
std::lock_guard<std::mutex> lock(m);

int realX = (c.x - startX) / CHUNK_SIZE;
int realZ = (c.z - startZ) / CHUNK_SIZE;
Expand Down Expand Up @@ -98,8 +94,6 @@ bool Data::Region::doesBlockExist(int x, int y, int z)

bool Data::Region::doesBlockExistInRange(int x, int y, int z, int type, int range)
{
std::lock_guard<std::mutex> lock(m);

for (int i = 0; i < REGION_SIZE; i++)
{
for (int j = 0; j < REGION_SIZE; j++)
Expand Down
1 change: 0 additions & 1 deletion src/Game/LightingManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,6 @@ void LightingManager::RefreshShadows()
auto gp = (Gameplay*)Game::instance->currentScene;
// Get all regions

std::lock_guard<std::mutex> lock(WorldManager::instance->generateMutex);

for (auto& r : WorldManager::instance->regions)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Game/Objects/Base/Chunk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1280,7 +1280,7 @@ void Chunk::UpdateChunk(int tick)
chunkMutex.lock();
for (int i = 0; i < subChunks.size(); i++)
{
subChunk sbc = subChunks[i];
subChunk& sbc = subChunks[i];

if (sbc.y == -1)
continue;
Expand Down
1 change: 0 additions & 1 deletion src/Game/Objects/Base/Chunk.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ class Chunk : public GameObject
int transparentSize = 0;
int size = 0;
public:
std::mutex chunkMutex;
bool inited = false;
bool isBeingLoaded = false;
bool isShadowLoaded = false;
Expand Down
21 changes: 17 additions & 4 deletions src/Game/Scenes/Gameplay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ void Gameplay::Draw()

celestialMoon->Draw();

gpMutex.lock();
// Draw chunks (regular)
for (Chunk* c : allChunks)
{
Expand Down Expand Up @@ -271,6 +272,8 @@ void Gameplay::Draw()
c->DrawTransparent();
}

gpMutex.unlock();

player->Draw();
Scene::Draw();

Expand Down Expand Up @@ -398,8 +401,9 @@ void Gameplay::UpdateChunks()
c->myData = {};
delete c;
}

gpMutex.lock();
allChunks.erase(std::remove(allChunks.begin(), allChunks.end(), c), allChunks.end());
gpMutex.unlock();
}

r.chunks.clear();
Expand All @@ -418,7 +422,9 @@ void Gameplay::UpdateChunks()

if (!r.loaded)
{
gpMutex.lock();
allChunks.insert(allChunks.end(), r.chunks.begin(), r.chunks.end());
gpMutex.unlock();
}

r.loaded = true;
Expand Down Expand Up @@ -529,7 +535,7 @@ void Gameplay::UpdateChunks()


// sort chunks by distance

gpMutex.lock();
std::sort(allChunks.begin(), allChunks.end(), [camera](Chunk* a, Chunk* b)
{
glm::vec3 fakePosA = glm::vec3(a->position.x, camera->position.y, a->position.z);
Expand All @@ -540,6 +546,7 @@ void Gameplay::UpdateChunks()

return distanceA < distanceB;
});
gpMutex.unlock();
}

chunksLoaded = 0;
Expand All @@ -551,7 +558,7 @@ void Gameplay::UpdateChunks()

if (Settings::instance->fogDistance >= 2.0)
fog = 10000;

gpMutex.lock();
for (Chunk* c : allChunks)
{
glm::vec3 fakePosC = glm::vec3(c->position.x + 8, 0, c->position.z + 8);
Expand All @@ -569,7 +576,8 @@ void Gameplay::UpdateChunks()
if (!c->isLoaded)
{
QueueLoad(c);
return;
continue;

}

float angle = glm::degrees(glm::acos(glm::dot(glm::normalize(fakePos - fakePosC), glm::normalize(camera->cameraFront))));
Expand Down Expand Up @@ -613,6 +621,7 @@ void Gameplay::UpdateChunks()
chunksRendered++;
}
}
gpMutex.unlock();

}

Expand Down Expand Up @@ -645,6 +654,10 @@ void Gameplay::KeyPress(int key)

player->playerData.GiveItem(item);

Data::InventoryItem item2(Data::ITEM_IRON_ORE, 32);

player->playerData.GiveItem(item2);

hud->UpdateHotbar();

hud->ShowHint("Gave you some coal!");
Expand Down
5 changes: 2 additions & 3 deletions src/Game/WorldManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,6 @@ void WorldManager::CreateChunks(Region& r)

void WorldManager::CheckGeneratedRegions()
{
const std::lock_guard<std::mutex> g(generateMutex);
if (_generatedRegions.size() != 0)
{
for (Region r : _generatedRegions)
Expand Down Expand Up @@ -184,7 +183,6 @@ void WorldManager::GenerateRegion(int x, int z)
CreateChunks(reg);

{
const std::lock_guard<std::mutex> g(generateMutex);
_generatedRegions.push_back(reg);
}

Expand Down Expand Up @@ -297,7 +295,6 @@ void WorldManager::LoadRegion(int x, int z)
CreateChunks(reg);

{
const std::lock_guard<std::mutex> g(generateMutex);
_generatedRegions.push_back(reg);
}

Expand Down Expand Up @@ -362,6 +359,7 @@ Chunk* WorldManager::GetChunk(float x, float z)
{
Gameplay* gp = (Gameplay*)Game::instance->currentScene;


for (auto&& c : gp->allChunks)
{
if (c->IsInChunk(x, z))
Expand All @@ -386,6 +384,7 @@ Data::Chunk WorldManager::GetChunkData(float x, float z)
}
}


return {};
}

Expand Down

0 comments on commit 0cd5d62

Please sign in to comment.