Sloth loves Chunk!
This commit is contained in:
		@@ -1,25 +1,83 @@
 | 
			
		||||
#include "chunk.h"
 | 
			
		||||
 | 
			
		||||
Chunk::Chunk() : BlockArray3d(CHUNK_SIZE_X, CHUNK_SIZE_Y, CHUNK_SIZE_Z)
 | 
			
		||||
{
 | 
			
		||||
Chunk::Chunk() {}
 | 
			
		||||
 | 
			
		||||
Chunk::~Chunk() {}
 | 
			
		||||
 | 
			
		||||
void Chunk::RemoveBlock(int x, int y, int z) {
 | 
			
		||||
    m_blocks.Set(x, y, z, BTYPE_AIR);
 | 
			
		||||
    m_isDirty = true;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
Chunk::~Chunk()
 | 
			
		||||
{
 | 
			
		||||
void Chunk::SetBlock(int x, int y, int z, BlockType type) {
 | 
			
		||||
    m_blocks.Set(x, y, z, type);
 | 
			
		||||
    m_isDirty = true;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void Chunk::RemoveBlock(int x, int y, int z)
 | 
			
		||||
{
 | 
			
		||||
    Set(x, y, z, BTYPE_AIR);
 | 
			
		||||
BlockType Chunk::GetBlock(int x, int y, int z) { return m_blocks.Get(x, y, z); }
 | 
			
		||||
 | 
			
		||||
void Chunk::Update() {
 | 
			
		||||
    // Update mesh
 | 
			
		||||
    if (m_isDirty) {
 | 
			
		||||
        int maxVertexCount = (CHUNK_SIZE_X * CHUNK_SIZE_Y * CHUNK_SIZE_Z) * (6 * 4);
 | 
			
		||||
        VertexBuffer::VertexData* vd = new VertexBuffer::VertexData[maxVertexCount];
 | 
			
		||||
        int count = 0;
 | 
			
		||||
        for (int x = 0; x < CHUNK_SIZE_X; ++x) {
 | 
			
		||||
            for (int z = 0; z < CHUNK_SIZE_Z; ++z) {
 | 
			
		||||
                for (int y = 0; y < CHUNK_SIZE_Y; ++y) {
 | 
			
		||||
                    if (count > USHRT_MAX)
 | 
			
		||||
                        break;
 | 
			
		||||
                    BlockType bt = GetBlock(x, y, z);
 | 
			
		||||
                    if (bt != BTYPE_AIR) {
 | 
			
		||||
                        AddBlockToMesh(vd, count, bt, x, y, z);
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        if (count > USHRT_MAX) {
 | 
			
		||||
            count = USHRT_MAX;
 | 
			
		||||
            std::cout << "[ Chunk :: Update ] Chunk data truncaned , too much vertices to have a 16 bit index " << std::endl;
 | 
			
		||||
        }
 | 
			
		||||
        m_vertexBuffer.SetMeshData(vd, count);
 | 
			
		||||
        delete[] vd;
 | 
			
		||||
    }
 | 
			
		||||
    m_isDirty = false;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void Chunk::SetBlock(int x, int y, int z, BlockType type)
 | 
			
		||||
{
 | 
			
		||||
    Set(x, y, z, type);
 | 
			
		||||
void Chunk::AddBlockToMesh(VertexBuffer::VertexData* vd, int& count, BlockType bt, int x, int y, int z) {
 | 
			
		||||
    
 | 
			
		||||
    vd[count++] = VertexBuffer::VertexData(x - .5f, y - .5f, z + .5f, 1.f, 1.f, 1.f, 0.f, 0.f);
 | 
			
		||||
    vd[count++] = VertexBuffer::VertexData(x + .5f, y - .5f, z + .5f, 1.f, 1.f, 1.f, 1.f, 0.f);
 | 
			
		||||
    vd[count++] = VertexBuffer::VertexData(x + .5f, y + .5f, z + .5f, 1.f, 1.f, 1.f, 1.f, 1.f);
 | 
			
		||||
    vd[count++] = VertexBuffer::VertexData(x - .5f, y + .5f, z + .5f, 1.f, 1.f, 1.f, 0.f, 1.f);
 | 
			
		||||
 | 
			
		||||
	vd[count++] = VertexBuffer::VertexData(x - .5f, y + .5f, z - .5f, 1.f, 1.f, 1.f, 0.f, 1.f);
 | 
			
		||||
    vd[count++] = VertexBuffer::VertexData(x + .5f, y + .5f, z - .5f, 1.f, 1.f, 1.f, 1.f, 1.f);
 | 
			
		||||
    vd[count++] = VertexBuffer::VertexData(x + .5f, y - .5f, z - .5f, 1.f, 1.f, 1.f, 1.f, 0.f);
 | 
			
		||||
    vd[count++] = VertexBuffer::VertexData(x - .5f, y - .5f, z - .5f, 1.f, 1.f, 1.f, 0.f, 0.f);
 | 
			
		||||
 | 
			
		||||
	vd[count++] = VertexBuffer::VertexData(x - .5f, y + .5f, z + .5f, 1.f, 1.f, 1.f, 0.f, 1.f);
 | 
			
		||||
	vd[count++] = VertexBuffer::VertexData(x - .5f, y + .5f, z - .5f, 1.f, 1.f, 1.f, 1.f, 1.f);
 | 
			
		||||
	vd[count++] = VertexBuffer::VertexData(x - .5f, y - .5f, z - .5f, 1.f, 1.f, 1.f, 1.f, 0.f);
 | 
			
		||||
	vd[count++] = VertexBuffer::VertexData(x - .5f, y - .5f, z + .5f, 1.f, 1.f, 1.f, 0.f, 0.f);
 | 
			
		||||
 | 
			
		||||
    vd[count++] = VertexBuffer::VertexData(x + .5f, y - .5f, z - .5f, 1.f, 1.f, 1.f, 0.f, 0.f);
 | 
			
		||||
	vd[count++] = VertexBuffer::VertexData(x + .5f, y + .5f, z - .5f, 1.f, 1.f, 1.f, 0.f, 1.f);
 | 
			
		||||
	vd[count++] = VertexBuffer::VertexData(x + .5f, y + .5f, z + .5f, 1.f, 1.f, 1.f, 1.f, 1.f);
 | 
			
		||||
	vd[count++] = VertexBuffer::VertexData(x + .5f, y - .5f, z + .5f, 1.f, 1.f, 1.f, 1.f, 0.f);
 | 
			
		||||
 | 
			
		||||
    vd[count++] = VertexBuffer::VertexData(x - .5f, y + .5f, z - .5f, 1.f, 1.f, 1.f, 0.f, 0.f);
 | 
			
		||||
    vd[count++] = VertexBuffer::VertexData(x - .5f, y + .5f, z + .5f, 1.f, 1.f, 1.f, 0.f, 1.f);
 | 
			
		||||
    vd[count++] = VertexBuffer::VertexData(x + .5f, y + .5f, z + .5f, 1.f, 1.f, 1.f, 1.f, 1.f);
 | 
			
		||||
    vd[count++] = VertexBuffer::VertexData(x + .5f, y + .5f, z - .5f, 1.f, 1.f, 1.f, 1.f, 0.f);
 | 
			
		||||
 | 
			
		||||
    vd[count++] = VertexBuffer::VertexData(x - .5f, y - .5f, z + .5f, 1.f, 1.f, 1.f, 0.f, 0.f);
 | 
			
		||||
    vd[count++] = VertexBuffer::VertexData(x - .5f, y - .5f, z - .5f, 1.f, 1.f, 1.f, 0.f, 1.f);
 | 
			
		||||
    vd[count++] = VertexBuffer::VertexData(x + .5f, y - .5f, z - .5f, 1.f, 1.f, 1.f, 1.f, 1.f);
 | 
			
		||||
    vd[count++] = VertexBuffer::VertexData(x + .5f, y - .5f, z + .5f, 1.f, 1.f, 1.f, 1.f, 0.f);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
BlockType Chunk::GetBlock(int x, int y, int z)
 | 
			
		||||
{
 | 
			
		||||
    return Get(x, y, z);
 | 
			
		||||
}
 | 
			
		||||
void Chunk::Render() const { m_vertexBuffer.Render(); }
 | 
			
		||||
 | 
			
		||||
bool Chunk::IsDirty() const { return m_isDirty; }
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user