From 52537715ef0905627edd2e5769dd6f26b43ee9ee Mon Sep 17 00:00:00 2001 From: EmaMaker Date: Sun, 23 Apr 2023 16:28:36 +0200 Subject: [PATCH] chunks: make chunk state atomic --- include/chunk.hpp | 13 +++++++------ src/chunk.cpp | 5 +++-- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/include/chunk.hpp b/include/chunk.hpp index 2fdc6e7..180b903 100644 --- a/include/chunk.hpp +++ b/include/chunk.hpp @@ -5,6 +5,7 @@ #include #include +#include #include #include #include @@ -24,9 +25,9 @@ namespace Chunk constexpr uint8_t CHUNK_STATE_GENERATED = 1; constexpr uint8_t CHUNK_STATE_MESHED = 2; - constexpr uint8_t CHUNK_STATE_MESH_LOADED = 3; - constexpr uint8_t CHUNK_STATE_LOADED = 4; - constexpr uint8_t CHUNK_STATE_EMPTY = 7; + constexpr uint8_t CHUNK_STATE_MESH_LOADED = 4; + constexpr uint8_t CHUNK_STATE_LOADED = 8; + constexpr uint8_t CHUNK_STATE_EMPTY = 64; int coord3DTo1D(int x, int y, int z); @@ -39,8 +40,8 @@ namespace Chunk public: glm::vec3 getPosition() { return this->position; } - std::bitset<8> getTotalState() { return this->state; } - bool getState(uint8_t n) { return this->state.test(n); } + uint8_t getTotalState() { return this->state; } + bool getState(uint8_t n) { return (this->state & n) == n; } void setState(uint8_t nstate, bool value); void setBlock(Block b, int x, int y, int z); @@ -62,7 +63,7 @@ namespace Chunk glm::vec3 position{}; IntervalMap blocks{}; - std::bitset<8> state{0}; + std::atomic_uint8_t state{0}; }; }; diff --git a/src/chunk.cpp b/src/chunk.cpp index c96f7ef..d621a44 100644 --- a/src/chunk.cpp +++ b/src/chunk.cpp @@ -25,6 +25,7 @@ namespace Chunk glGenBuffers(1, &(this->VBO)); glGenBuffers(1, &(this->EBO)); + mutex_state.unlock(); } Chunk ::~Chunk() @@ -60,8 +61,8 @@ namespace Chunk void Chunk::setState(uint8_t nstate, bool value) { if (value) - this->state.set((size_t)nstate); + this->state.fetch_or(nstate); else - this->state.reset((size_t)nstate); + this->state.fetch_and(~nstate); } }