Génération de Chunks en multithreads
This commit is contained in:
parent
f4ec4816af
commit
7eb9f44d41
1
.gitignore
vendored
1
.gitignore
vendored
@ -367,3 +367,4 @@ FodyWeavers.xsd
|
|||||||
/SQCSim2021/Release
|
/SQCSim2021/Release
|
||||||
/SQCSim2021/media/chunks
|
/SQCSim2021/media/chunks
|
||||||
/x64/Release/SQCSim2021.exe
|
/x64/Release/SQCSim2021.exe
|
||||||
|
/SQCSim2021/x64/Release/SQCSim2021.tlog
|
||||||
|
@ -1,13 +1,73 @@
|
|||||||
#include "chunk.h"
|
#include "chunk.h"
|
||||||
#include "world.h"
|
#include "world.h"
|
||||||
|
|
||||||
Chunk::Chunk(int x, int y) : m_posX(x), m_posY(y) { m_blocks.Reset(BTYPE_AIR); }
|
Chunk::Chunk(int x, int y) : m_posX(x), m_posY(y) {
|
||||||
|
std::ostringstream pos; // Vérifie l'existence d'un fichier .chunk avec sa position.
|
||||||
|
pos << CHUNK_PATH << x << '_' << y << ".chunk";
|
||||||
|
std::ifstream input(pos.str(), std::fstream::binary);
|
||||||
|
|
||||||
Chunk::Chunk(int x, int y, char data[CHUNK_SIZE_X * CHUNK_SIZE_Y * CHUNK_SIZE_Z]) : m_posX(x), m_posY(y) {
|
if (input.fail()) {
|
||||||
for (int x = 0; x < CHUNK_SIZE_X; ++x)
|
Perlin perlin = Perlin(8, 45.f, 7.f, PERLIN_SEED);
|
||||||
for (int z = 0; z < CHUNK_SIZE_Z; ++z)
|
m_blocks.Reset(BTYPE_AIR);
|
||||||
for (int y = 0; y < CHUNK_SIZE_Y; ++y)
|
|
||||||
m_blocks.Set(x, y, z, data[x + (z * CHUNK_SIZE_X) + (y * CHUNK_SIZE_Z * CHUNK_SIZE_X)]);
|
for (int ix = 0; ix < CHUNK_SIZE_X; ++ix) // Montagnes
|
||||||
|
for (int iz = 0; iz < CHUNK_SIZE_Z; ++iz) {
|
||||||
|
float xnoiz, ynoiz;
|
||||||
|
xnoiz = (double)(ix + x * CHUNK_SIZE_X) / (double)INT16_MAX;
|
||||||
|
ynoiz = (double)(iz + y * CHUNK_SIZE_Z) / (double)INT16_MAX;
|
||||||
|
float height = (perlin.Get(xnoiz, ynoiz)) * 20.f + 5.f;
|
||||||
|
for (int iy = 0; iy <= (int)height % CHUNK_SIZE_Y; ++iy)
|
||||||
|
SetBlock(ix, iy, iz, BTYPE_METAL, nullptr);
|
||||||
|
}
|
||||||
|
for (int ix = 0; ix < CHUNK_SIZE_X; ++ix) // Collines
|
||||||
|
for (int iz = 0; iz < CHUNK_SIZE_Z; ++iz) {
|
||||||
|
float xnoiz, ynoiz;
|
||||||
|
xnoiz = (double)(ix + x * CHUNK_SIZE_X) / (double)INT16_MAX;
|
||||||
|
ynoiz = (double)(iz + y * CHUNK_SIZE_Z) / (double)INT16_MAX;
|
||||||
|
float height = perlin.Get(xnoiz, ynoiz) * 5.f + 24.f;
|
||||||
|
for (int iy = 0; iy <= (int)height % CHUNK_SIZE_Y; ++iy) {
|
||||||
|
if (GetBlock(ix, iy, iz) == BTYPE_AIR)
|
||||||
|
SetBlock(ix, iy, iz, BTYPE_GRASS, nullptr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (int ix = 0; ix < CHUNK_SIZE_X; ++ix) // "Lacs"
|
||||||
|
for (int iz = 0; iz < CHUNK_SIZE_Z; ++iz) {
|
||||||
|
for (int iy = 0; iy < 13; ++iy) {
|
||||||
|
if (GetBlock(ix, iy, iz) == BTYPE_AIR)
|
||||||
|
SetBlock(ix, iy, iz, BTYPE_ICE, nullptr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (int ix = 0; ix < CHUNK_SIZE_X; ++ix) // "Arbres"
|
||||||
|
for (int iz = 0; iz < CHUNK_SIZE_Z; ++iz) {
|
||||||
|
float xnoiz, ynoiz;
|
||||||
|
xnoiz = (double)(iz * CHUNK_SIZE_Y + x * CHUNK_SIZE_X) / (double)INT16_MAX;
|
||||||
|
ynoiz = (double)(ix * CHUNK_SIZE_Y + y * CHUNK_SIZE_Z) / (double)INT16_MAX;
|
||||||
|
bool tree = (int)(abs(perlin.Get(xnoiz, ynoiz)) * 17933.f) % CHUNK_SIZE_Y > 126 ? true : false;
|
||||||
|
|
||||||
|
for (int iy = 0; iy < CHUNK_SIZE_Y - 10; ++iy)
|
||||||
|
if (GetBlock(ix, iy, iz) == BTYPE_AIR)
|
||||||
|
if (GetBlock(ix, iy - 1, iz) == BTYPE_GRASS)
|
||||||
|
if (tree) {
|
||||||
|
for (int i = 0; i < (int)(abs(perlin.Get(xnoiz, ynoiz))) % 3 + 1; ++i)
|
||||||
|
SetBlock(ix, iy + i, iz, BTYPE_DIRT, nullptr);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
input.seekg(0, std::ios_base::end);
|
||||||
|
int size = input.tellg();
|
||||||
|
input.seekg(0, std::ios_base::beg);
|
||||||
|
|
||||||
|
char data[CHUNK_SIZE_X * CHUNK_SIZE_Y * CHUNK_SIZE_Z];
|
||||||
|
input.read(data, size);
|
||||||
|
input.close();
|
||||||
|
|
||||||
|
for (int ix = 0; ix < CHUNK_SIZE_X; ++ix)
|
||||||
|
for (int iz = 0; iz < CHUNK_SIZE_Z; ++iz)
|
||||||
|
for (int iy = 0; iy < CHUNK_SIZE_Y; ++iy)
|
||||||
|
m_blocks.Set(ix, iy, iz, data[ix + (iz * CHUNK_SIZE_X) + (iy * CHUNK_SIZE_Z * CHUNK_SIZE_X)]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Chunk::~Chunk() {
|
Chunk::~Chunk() {
|
||||||
@ -36,7 +96,7 @@ void Chunk::RemoveBlock(int x, int y, int z, World* world) {
|
|||||||
|
|
||||||
void Chunk::SetBlock(int x, int y, int z, BlockType type, World* world) {
|
void Chunk::SetBlock(int x, int y, int z, BlockType type, World* world) {
|
||||||
m_blocks.Set(x, y, z, type);
|
m_blocks.Set(x, y, z, type);
|
||||||
CheckNeighbors(x, z, world);
|
if (world) CheckNeighbors(x, z, world); // Si nullptr, ne pas vérifier les chunks voisines.
|
||||||
m_isDirty = true;
|
m_isDirty = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -99,7 +159,6 @@ void Chunk::Update(BlockInfo* blockinfo[BTYPE_LAST], World* world) {
|
|||||||
void Chunk::AddBlockToMesh(VertexBuffer::VertexData* vd, int& count, BlockType bt,
|
void Chunk::AddBlockToMesh(VertexBuffer::VertexData* vd, int& count, BlockType bt,
|
||||||
int x, int y, int z, float u, float v, float s, World* world) {
|
int x, int y, int z, float u, float v, float s, World* world) {
|
||||||
|
|
||||||
static Perlin perlin = Perlin(2, 4.f, 1.f, 362436);
|
|
||||||
int cex, cey;
|
int cex, cey;
|
||||||
|
|
||||||
world->GetScope(cex, cey);
|
world->GetScope(cex, cey);
|
||||||
|
@ -22,7 +22,6 @@ class Chunk {
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
Chunk(int x, int y);
|
Chunk(int x, int y);
|
||||||
Chunk(int x, int y, char data[CHUNK_SIZE_X * CHUNK_SIZE_Y * CHUNK_SIZE_Z]);
|
|
||||||
~Chunk();
|
~Chunk();
|
||||||
|
|
||||||
void RemoveBlock(int x, int y, int z, World* world);
|
void RemoveBlock(int x, int y, int z, World* world);
|
||||||
|
@ -16,6 +16,7 @@
|
|||||||
#define CHUNK_SIZE_Y 128
|
#define CHUNK_SIZE_Y 128
|
||||||
#define CHUNK_SIZE_Z 16
|
#define CHUNK_SIZE_Z 16
|
||||||
#define MAX_SELECTION_DISTANCE 5
|
#define MAX_SELECTION_DISTANCE 5
|
||||||
|
#define PERLIN_SEED 12345
|
||||||
|
|
||||||
#ifdef _DEBUG
|
#ifdef _DEBUG
|
||||||
#define WORLD_SIZE_X 64
|
#define WORLD_SIZE_X 64
|
||||||
@ -25,6 +26,9 @@
|
|||||||
#define FRAMES_UPDATE_CHUNKS 4
|
#define FRAMES_UPDATE_CHUNKS 4
|
||||||
#define FRAMES_DELETE_CHUNKS 4
|
#define FRAMES_DELETE_CHUNKS 4
|
||||||
|
|
||||||
|
#define THREADS_GENERATE_CHUNKS 1
|
||||||
|
#define THREADS_UPDATE_CHUNKS 1
|
||||||
|
|
||||||
#define VIEW_DISTANCE 256
|
#define VIEW_DISTANCE 256
|
||||||
#define TEXTURE_SIZE 128
|
#define TEXTURE_SIZE 128
|
||||||
#define MAX_BULLETS 64
|
#define MAX_BULLETS 64
|
||||||
@ -38,6 +42,9 @@
|
|||||||
#define FRAMES_UPDATE_CHUNKS 1
|
#define FRAMES_UPDATE_CHUNKS 1
|
||||||
#define FRAMES_DELETE_CHUNKS 1
|
#define FRAMES_DELETE_CHUNKS 1
|
||||||
|
|
||||||
|
#define THREADS_GENERATE_CHUNKS 10
|
||||||
|
#define THREADS_UPDATE_CHUNKS 10
|
||||||
|
|
||||||
#define VIEW_DISTANCE 1024
|
#define VIEW_DISTANCE 1024
|
||||||
#define TEXTURE_SIZE 512
|
#define TEXTURE_SIZE 512
|
||||||
#define MAX_BULLETS 512
|
#define MAX_BULLETS 512
|
||||||
|
@ -16,11 +16,14 @@ bool VertexBuffer::IsValid() const {
|
|||||||
return m_isValid;
|
return m_isValid;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::mutex VertexBuffer::m_opgl;
|
||||||
|
|
||||||
void VertexBuffer::SetMeshData(VertexData* vd, int vertexCount) {
|
void VertexBuffer::SetMeshData(VertexData* vd, int vertexCount) {
|
||||||
|
const std::lock_guard<std::mutex> prout(VertexBuffer::m_opgl);
|
||||||
assert(vertexCount <= USHRT_MAX);
|
assert(vertexCount <= USHRT_MAX);
|
||||||
if(vertexCount == 0)
|
if(vertexCount == 0)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if(!m_isValid) {
|
if(!m_isValid) {
|
||||||
glGenBuffers(1, &m_vertexVboId);
|
glGenBuffers(1, &m_vertexVboId);
|
||||||
glGenBuffers(1, &m_indexVboId);
|
glGenBuffers(1, &m_indexVboId);
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
#define VERTEXBUFFER_H__
|
#define VERTEXBUFFER_H__
|
||||||
|
|
||||||
#include "define.h"
|
#include "define.h"
|
||||||
|
#include <mutex>
|
||||||
|
|
||||||
class VertexBuffer
|
class VertexBuffer
|
||||||
{
|
{
|
||||||
@ -33,6 +34,7 @@ class VertexBuffer
|
|||||||
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
static std::mutex m_opgl;
|
||||||
bool m_isValid;
|
bool m_isValid;
|
||||||
int m_vertexCount;
|
int m_vertexCount;
|
||||||
GLuint m_vertexVboId;
|
GLuint m_vertexVboId;
|
||||||
|
@ -113,109 +113,19 @@ void World::Update(int& rendercount, Bullet* bullets[MAX_BULLETS], Player& playe
|
|||||||
glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
|
glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
|
||||||
atlas.Bind();
|
atlas.Bind();
|
||||||
RenderWorld(rendercount, player, world, shader);
|
RenderWorld(rendercount, player, world, shader);
|
||||||
TransposeWorld(player, bullets);
|
|
||||||
UpdateWorld(player, perlin, blockinfo);
|
UpdateWorld(player, perlin, blockinfo);
|
||||||
|
TransposeWorld(player, bullets);
|
||||||
shader.Disable();
|
shader.Disable();
|
||||||
glStencilFunc(GL_GREATER, 1, 0xFF);
|
glStencilFunc(GL_GREATER, 1, 0xFF);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool World::GenerateChunk(int chx, int chy, Perlin& perlin) {
|
void World::UpdateChunk(int& updates, int chx, int chy, BlockInfo* blockinfo[BTYPE_LAST]) {
|
||||||
if (chx < WORLD_SIZE_X * CHUNK_SIZE_X && chy < WORLD_SIZE_Y * CHUNK_SIZE_Z &&
|
|
||||||
chx >= 0 && chy >= 0)
|
|
||||||
if (!ChunkAt(chx, 1, chy)) {
|
|
||||||
|
|
||||||
for (int index = 0; index < m_tbDeleted.size(); ++index) { // Vérifie l'existence d'un chunk dans le buffer de suppression avec sa position.
|
|
||||||
int x, y;
|
|
||||||
|
|
||||||
if (m_tbDeleted.at(index)) {
|
|
||||||
m_tbDeleted.at(index)->GetPosition(x, y);
|
|
||||||
if (chx / CHUNK_SIZE_X + m_center[0] == x &&
|
|
||||||
chy / CHUNK_SIZE_Z + m_center[1] == y) {
|
|
||||||
GetChunks().Set(chx / CHUNK_SIZE_X, chy / CHUNK_SIZE_Z, std::move(m_tbDeleted.at(index)));
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
std::ostringstream pos; // Vérifie l'existence d'un fichier .chunk avec sa position.
|
|
||||||
pos << CHUNK_PATH << chx / CHUNK_SIZE_X + m_center[0] << '_' << chy / CHUNK_SIZE_Z + m_center[1] << ".chunk";
|
|
||||||
std::ifstream input(pos.str(), std::fstream::binary);
|
|
||||||
|
|
||||||
if (input.fail()) {
|
|
||||||
GetChunks().Set(chx / CHUNK_SIZE_X, chy / CHUNK_SIZE_Z, new Chunk(chx / CHUNK_SIZE_X + m_center[0], chy / CHUNK_SIZE_Z + m_center[1]));
|
|
||||||
Chunk* chunk = GetChunks().Get(chx / CHUNK_SIZE_X, chy / CHUNK_SIZE_Z);
|
|
||||||
|
|
||||||
for (int x = 0; x < CHUNK_SIZE_X; ++x) // Montagnes
|
|
||||||
for (int z = 0; z < CHUNK_SIZE_Z; ++z) {
|
|
||||||
float xnoiz, ynoiz;
|
|
||||||
xnoiz = (double)(x + (chx / CHUNK_SIZE_X + m_center[0]) * CHUNK_SIZE_X) / (double)INT16_MAX;
|
|
||||||
ynoiz = (double)(z + (chy / CHUNK_SIZE_Z + m_center[1]) * CHUNK_SIZE_Z) / (double)INT16_MAX;
|
|
||||||
float height = (perlin.Get(xnoiz, ynoiz)) * 20.f + 5.f;
|
|
||||||
for (int y = 0; y <= (int)height % CHUNK_SIZE_Y; ++y)
|
|
||||||
chunk->SetBlock(x, y, z, BTYPE_METAL, this);
|
|
||||||
}
|
|
||||||
|
|
||||||
for (int x = 0; x < CHUNK_SIZE_X; ++x) // Collines
|
|
||||||
for (int z = 0; z < CHUNK_SIZE_Z; ++z) {
|
|
||||||
float xnoiz, ynoiz;
|
|
||||||
xnoiz = (double)(x + (chx / CHUNK_SIZE_X + m_center[0]) * CHUNK_SIZE_X) / (double)INT16_MAX;
|
|
||||||
ynoiz = (double)(z + (chy / CHUNK_SIZE_Z + m_center[1]) * CHUNK_SIZE_Z) / (double)INT16_MAX;
|
|
||||||
float height = perlin.Get(xnoiz, ynoiz) * 5.f + 24.f;
|
|
||||||
for (int y = 0; y <= (int)height % CHUNK_SIZE_Y; ++y) {
|
|
||||||
if (chunk->GetBlock(x, y, z) == BTYPE_AIR)
|
|
||||||
chunk->SetBlock(x, y, z, BTYPE_GRASS, this);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for (int x = 0; x < CHUNK_SIZE_X; ++x) // "Lacs"
|
|
||||||
for (int z = 0; z < CHUNK_SIZE_Z; ++z) {
|
|
||||||
for (int y = 0; y < 13; ++y) {
|
|
||||||
if (chunk->GetBlock(x, y, z) == BTYPE_AIR)
|
|
||||||
chunk->SetBlock(x, y, z, BTYPE_ICE, this);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for (int x = 0; x < CHUNK_SIZE_X; ++x) // "Arbres"
|
|
||||||
for (int z = 0; z < CHUNK_SIZE_Z; ++z) {
|
|
||||||
float xnoiz, ynoiz;
|
|
||||||
xnoiz = (double)(z * CHUNK_SIZE_Y + (chx / CHUNK_SIZE_X + m_center[0]) * CHUNK_SIZE_X) / (double)INT16_MAX;
|
|
||||||
ynoiz = (double)(x * CHUNK_SIZE_Y + (chy / CHUNK_SIZE_Z + m_center[1]) * CHUNK_SIZE_Z) / (double)INT16_MAX;
|
|
||||||
bool tree = (int)(abs(perlin.Get(xnoiz, ynoiz)) * 17933.f) % CHUNK_SIZE_Y > 126 ? true: false;
|
|
||||||
|
|
||||||
for (int y = 0; y < CHUNK_SIZE_Y - 10; ++y)
|
|
||||||
if (chunk->GetBlock(x, y, z) == BTYPE_AIR)
|
|
||||||
if (chunk->GetBlock(x, y - 1, z) == BTYPE_GRASS)
|
|
||||||
if (tree) {
|
|
||||||
for (int i = 0; i < (int)(abs(perlin.Get(xnoiz, ynoiz))) % 3 + 1; ++i)
|
|
||||||
chunk->SetBlock(x, y + i, z, BTYPE_DIRT, this);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
input.seekg(0, std::ios_base::end);
|
|
||||||
int size = input.tellg();
|
|
||||||
input.seekg(0, std::ios_base::beg);
|
|
||||||
|
|
||||||
char data[CHUNK_SIZE_X * CHUNK_SIZE_Y * CHUNK_SIZE_Z];
|
|
||||||
input.read(data, size);
|
|
||||||
input.close();
|
|
||||||
|
|
||||||
GetChunks().Set(chx / CHUNK_SIZE_X, chy / CHUNK_SIZE_Z, new Chunk(chx / CHUNK_SIZE_X + m_center[0], chy / CHUNK_SIZE_Z + m_center[1], data));
|
|
||||||
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
void World::UpdateChunk(int& generates, int& updates, int chx, int chy, Perlin& perlin, BlockInfo* blockinfo[BTYPE_LAST]) {
|
|
||||||
if (updates == 0 && ChunkAt(chx, 1, chy) &&
|
if (updates == 0 && ChunkAt(chx, 1, chy) &&
|
||||||
ChunkAt(chx, 1, chy)->IsDirty()) {
|
ChunkAt(chx, 1, chy)->IsDirty()) {
|
||||||
ChunkAt(chx, 1, chy)->Update(blockinfo, this);
|
ChunkAt(chx, 1, chy)->Update(blockinfo, this);
|
||||||
updates = FRAMES_UPDATE_CHUNKS;
|
updates = FRAMES_UPDATE_CHUNKS;
|
||||||
}
|
}
|
||||||
if (generates == 0 && GenerateChunk(chx, chy, perlin)) generates = FRAMES_RENDER_CHUNKS;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void World::ChangeBlockAtCursor(BlockType blockType, Player& player, bool& block) {
|
void World::ChangeBlockAtCursor(BlockType blockType, Player& player, bool& block) {
|
||||||
@ -377,26 +287,152 @@ void World::UpdateWorld(Player& player, Perlin& perlin, BlockInfo* blockinfo[BTY
|
|||||||
static int frameUpdate = 0;
|
static int frameUpdate = 0;
|
||||||
static int frameDelete = 0;
|
static int frameDelete = 0;
|
||||||
int side = 0;
|
int side = 0;
|
||||||
|
int threads = 0;
|
||||||
|
std::future<Chunk*> genThList[THREADS_GENERATE_CHUNKS];
|
||||||
|
//std::future<Chunk*> updateThList[THREADS_UPDATE_CHUNKS];
|
||||||
|
|
||||||
if (frameGenerate > 0) --frameGenerate;
|
if (frameGenerate > 0) --frameGenerate;
|
||||||
if (frameUpdate > 0) --frameUpdate;
|
if (frameUpdate > 0) --frameUpdate;
|
||||||
if (frameDelete > 0) --frameDelete;
|
if (frameDelete > 0) --frameDelete;
|
||||||
|
|
||||||
if (!frameGenerate || !frameUpdate)
|
if (!frameGenerate)
|
||||||
while (side * CHUNK_SIZE_X <= VIEW_DISTANCE * 2) {
|
while (side * CHUNK_SIZE_X <= VIEW_DISTANCE * 2) {
|
||||||
int tx = -side, ty = -side;
|
int tx = -side, ty = -side;
|
||||||
|
int chx = 0;
|
||||||
|
int chy = 0;
|
||||||
|
|
||||||
for (; tx <= side; ++tx)
|
for (; tx <= side; ++tx) {
|
||||||
UpdateChunk(frameGenerate, frameUpdate, cx + tx * CHUNK_SIZE_X, cy + ty * CHUNK_SIZE_Z, perlin, blockinfo);
|
if (frameGenerate)
|
||||||
for (; ty <= side; ++ty)
|
break;
|
||||||
UpdateChunk(frameGenerate, frameUpdate, cx + tx * CHUNK_SIZE_X, cy + ty * CHUNK_SIZE_Z, perlin, blockinfo);
|
chx = cx + tx * CHUNK_SIZE_X;
|
||||||
for (; tx >= -side; --tx)
|
chy = cy + ty * CHUNK_SIZE_Z;
|
||||||
UpdateChunk(frameGenerate, frameUpdate, cx + tx * CHUNK_SIZE_X, cy + ty * CHUNK_SIZE_Z, perlin, blockinfo);
|
if (chx < WORLD_SIZE_X * CHUNK_SIZE_X && chy < WORLD_SIZE_Y * CHUNK_SIZE_Z &&
|
||||||
for (; ty >= -side; --ty)
|
chx >= 0 && chy >= 0 && !ChunkAt(chx, 1, chy))
|
||||||
UpdateChunk(frameGenerate, frameUpdate, cx + tx * CHUNK_SIZE_X, cy + ty * CHUNK_SIZE_Z, perlin, blockinfo);
|
genThList[threads++] = std::async(std::launch::async, [](int x, int y) { return new Chunk(x, y); }, chx / CHUNK_SIZE_X + m_center[0], chy / CHUNK_SIZE_Z + m_center[1]);
|
||||||
|
if (threads == THREADS_GENERATE_CHUNKS) frameGenerate = FRAMES_RENDER_CHUNKS;
|
||||||
|
}
|
||||||
|
for (; ty <= side; ++ty) {
|
||||||
|
if (frameGenerate)
|
||||||
|
break;
|
||||||
|
chx = cx + tx * CHUNK_SIZE_X;
|
||||||
|
chy = cy + ty * CHUNK_SIZE_Z;
|
||||||
|
if (chx < WORLD_SIZE_X * CHUNK_SIZE_X && chy < WORLD_SIZE_Y * CHUNK_SIZE_Z &&
|
||||||
|
chx >= 0 && chy >= 0 && !ChunkAt(chx, 1, chy))
|
||||||
|
genThList[threads++] = std::async(std::launch::async, [](int x, int y) { return new Chunk(x, y); }, chx / CHUNK_SIZE_X + m_center[0], chy / CHUNK_SIZE_Z + m_center[1]);
|
||||||
|
if (threads == THREADS_GENERATE_CHUNKS) frameGenerate = FRAMES_RENDER_CHUNKS;
|
||||||
|
}
|
||||||
|
for (; tx >= -side; --tx) {
|
||||||
|
if (frameGenerate)
|
||||||
|
break;
|
||||||
|
chx = cx + tx * CHUNK_SIZE_X;
|
||||||
|
chy = cy + ty * CHUNK_SIZE_Z;
|
||||||
|
if (chx < WORLD_SIZE_X * CHUNK_SIZE_X && chy < WORLD_SIZE_Y * CHUNK_SIZE_Z &&
|
||||||
|
chx >= 0 && chy >= 0 && !ChunkAt(chx, 1, chy))
|
||||||
|
genThList[threads++] = std::async(std::launch::async, [](int x, int y) { return new Chunk(x, y); }, chx / CHUNK_SIZE_X + m_center[0], chy / CHUNK_SIZE_Z + m_center[1]);
|
||||||
|
if (threads == THREADS_GENERATE_CHUNKS) frameGenerate = FRAMES_RENDER_CHUNKS;
|
||||||
|
}
|
||||||
|
for (; ty >= -side; --ty) {
|
||||||
|
if (frameGenerate)
|
||||||
|
break;
|
||||||
|
chx = cx + tx * CHUNK_SIZE_X;
|
||||||
|
chy = cy + ty * CHUNK_SIZE_Z;
|
||||||
|
if (chx < WORLD_SIZE_X * CHUNK_SIZE_X && chy < WORLD_SIZE_Y * CHUNK_SIZE_Z &&
|
||||||
|
chx >= 0 && chy >= 0 && !ChunkAt(chx, 1, chy))
|
||||||
|
genThList[threads++] = std::async(std::launch::async, [](int x, int y) { return new Chunk(x, y); }, chx / CHUNK_SIZE_X + m_center[0], chy / CHUNK_SIZE_Z + m_center[1]);
|
||||||
|
if (threads == THREADS_GENERATE_CHUNKS) frameGenerate = FRAMES_RENDER_CHUNKS;
|
||||||
|
}
|
||||||
|
if (frameGenerate)
|
||||||
|
break;
|
||||||
++side;
|
++side;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (threads > 0) {
|
||||||
|
for (int i = 0; i < threads; ++i)
|
||||||
|
genThList[i].wait();
|
||||||
|
|
||||||
|
for (int i = 0; i < threads; ++i) {
|
||||||
|
int x, y;
|
||||||
|
Chunk* chunk = genThList[i].get();
|
||||||
|
chunk->GetPosition(x,y);
|
||||||
|
m_chunks.Set(x - m_center[0], y - m_center[1], chunk);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
side = 0;
|
||||||
|
threads = 0;
|
||||||
|
|
||||||
|
if (!frameUpdate)
|
||||||
|
while (side * CHUNK_SIZE_X <= VIEW_DISTANCE * 2) {
|
||||||
|
int tx = -side, ty = -side;
|
||||||
|
|
||||||
|
for (; tx <= side; ++tx) {
|
||||||
|
if (frameUpdate)
|
||||||
|
break;
|
||||||
|
int chx = cx + tx * CHUNK_SIZE_X;
|
||||||
|
int chy = cy + ty * CHUNK_SIZE_Z;
|
||||||
|
if (ChunkAt(chx, 1, chy) &&
|
||||||
|
ChunkAt(chx, 1, chy)->IsDirty()) {
|
||||||
|
ChunkAt(chx, 1, chy)->Update(blockinfo, this);
|
||||||
|
if (threads++ == THREADS_UPDATE_CHUNKS) frameUpdate = FRAMES_UPDATE_CHUNKS;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (; ty <= side; ++ty) {
|
||||||
|
if (frameUpdate)
|
||||||
|
break;
|
||||||
|
int chx = cx + tx * CHUNK_SIZE_X;
|
||||||
|
int chy = cy + ty * CHUNK_SIZE_Z;
|
||||||
|
if (ChunkAt(chx, 1, chy) &&
|
||||||
|
ChunkAt(chx, 1, chy)->IsDirty()) {
|
||||||
|
ChunkAt(chx, 1, chy)->Update(blockinfo, this);
|
||||||
|
if (threads++ == THREADS_UPDATE_CHUNKS) frameUpdate = FRAMES_UPDATE_CHUNKS;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (; tx >= -side; --tx) {
|
||||||
|
if (frameUpdate)
|
||||||
|
break;
|
||||||
|
int chx = cx + tx * CHUNK_SIZE_X;
|
||||||
|
int chy = cy + ty * CHUNK_SIZE_Z;
|
||||||
|
if (ChunkAt(chx, 1, chy) &&
|
||||||
|
ChunkAt(chx, 1, chy)->IsDirty()) {
|
||||||
|
ChunkAt(chx, 1, chy)->Update(blockinfo, this);
|
||||||
|
if (threads++ == THREADS_UPDATE_CHUNKS) frameUpdate = FRAMES_UPDATE_CHUNKS;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (; ty >= -side; --ty) {
|
||||||
|
if (frameUpdate)
|
||||||
|
break;
|
||||||
|
int chx = cx + tx * CHUNK_SIZE_X;
|
||||||
|
int chy = cy + ty * CHUNK_SIZE_Z;
|
||||||
|
if (ChunkAt(chx, 1, chy) &&
|
||||||
|
ChunkAt(chx, 1, chy)->IsDirty()) {
|
||||||
|
ChunkAt(chx, 1, chy)->Update(blockinfo, this);
|
||||||
|
if (threads++ == THREADS_UPDATE_CHUNKS) frameUpdate = FRAMES_UPDATE_CHUNKS;
|
||||||
|
}
|
||||||
|
/*if (ChunkAt(chx, 1, chy) && // Version multithread d'UpdateChunks qui ne fonctionne pas.
|
||||||
|
ChunkAt(chx, 1, chy)->IsDirty()) {
|
||||||
|
updateThList[threads++] =
|
||||||
|
std::async(std::launch::async,
|
||||||
|
[](Chunk* chunk, BlockInfo* blockinfo[BTYPE_LAST], World* world) {
|
||||||
|
chunk->Update(blockinfo, world); return chunk; }, ChunkAt(chx, 1, chy), blockinfo, this);
|
||||||
|
if (threads == THREADS_UPDATE_CHUNKS) frameUpdate = FRAMES_UPDATE_CHUNKS;
|
||||||
|
}*/
|
||||||
|
}
|
||||||
|
if (frameUpdate)
|
||||||
|
break;
|
||||||
|
++side;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*if (threads > 0) {
|
||||||
|
for (int i = 0; i < threads; ++i)
|
||||||
|
updateThList[i].wait();
|
||||||
|
|
||||||
|
for (int i = 0; i < threads; ++i) {
|
||||||
|
int x, y;
|
||||||
|
Chunk* chunk = updateThList[i].get();
|
||||||
|
chunk->GetPosition(x, y);
|
||||||
|
m_chunks.Set(x - m_center[0], y - m_center[1], chunk);
|
||||||
|
}
|
||||||
|
}*/
|
||||||
|
|
||||||
CleanUpWorld(frameDelete);
|
CleanUpWorld(frameDelete);
|
||||||
}
|
}
|
@ -13,6 +13,8 @@
|
|||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include <future>
|
||||||
|
#include <thread>
|
||||||
|
|
||||||
class Chunk;
|
class Chunk;
|
||||||
class Player;
|
class Player;
|
||||||
@ -44,8 +46,7 @@ private:
|
|||||||
|
|
||||||
int m_center[2] = { INT16_MAX / 2 - WORLD_SIZE_X / 2, INT16_MAX / 2 - WORLD_SIZE_Y / 2 };
|
int m_center[2] = { INT16_MAX / 2 - WORLD_SIZE_X / 2, INT16_MAX / 2 - WORLD_SIZE_Y / 2 };
|
||||||
|
|
||||||
bool GenerateChunk(int x, int y, Perlin& perlin);
|
void UpdateChunk(int& updates, int chx, int chy, BlockInfo* blockinfo[BTYPE_LAST]);
|
||||||
void UpdateChunk(int& generates, int& updates, int chx, int chy, Perlin& perlin, BlockInfo* blockinfo[BTYPE_LAST]);
|
|
||||||
void RenderWorld(int& rendercount, Player& player, Transformation& world, Shader& shader);
|
void RenderWorld(int& rendercount, Player& player, Transformation& world, Shader& shader);
|
||||||
void UpdateWorld(Player& player, Perlin& perlin, BlockInfo* blockinfo[BTYPE_LAST]);
|
void UpdateWorld(Player& player, Perlin& perlin, BlockInfo* blockinfo[BTYPE_LAST]);
|
||||||
void TransposeWorld(Player& player, Bullet* bullets[MAX_BULLETS]);
|
void TransposeWorld(Player& player, Bullet* bullets[MAX_BULLETS]);
|
||||||
|
Loading…
Reference in New Issue
Block a user