chunks: make chunk state atomic

pull/1/head
EmaMaker 2023-04-23 16:28:36 +02:00
parent 3daf994ac3
commit 52537715ef
2 changed files with 10 additions and 8 deletions

View File

@ -5,6 +5,7 @@
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <atomic>
#include <array>
#include <bitset>
#include <mutex>
@ -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<Block> blocks{};
std::bitset<8> state{0};
std::atomic_uint8_t state{0};
};
};

View File

@ -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);
}
}