minecraftvulkan/src/chunk.hpp

98 lines
2.5 KiB
C++
Raw Normal View History

2022-09-25 23:05:56 +00:00
#pragma once
#include "xe_model.hpp"
#include "xe_engine.hpp"
#include "xe_image.hpp"
2022-10-01 18:35:16 +00:00
#include "chunk_noise.hpp"
2022-09-25 23:05:56 +00:00
#include <glm/common.hpp>
#include <glm/fwd.hpp>
#include <vector>
#include <thread>
#include <memory>
#include <string>
#include <map>
2022-09-29 02:55:07 +00:00
#include <algorithm>
2022-09-30 19:40:42 +00:00
#include <cstdlib>
2022-09-25 23:05:56 +00:00
#define INVALID -1
#define AIR 0
#define DIRT 1
#define GRASS 2
2022-09-29 03:20:37 +00:00
#define FULL_GRASS 3
2022-09-29 02:55:07 +00:00
#define STONE 4
#define SNOW 5
#define SAND 6
#define WATER 7
2022-09-29 03:20:37 +00:00
#define SHRUB 8
#define FULL_SHRUB 9
#define DIRT_TEXTURE "res/image/dirt.png"
#define GRASS_TEXTURE "res/image/grass.png"
#define GRASS_TOP_TEXTURE "res/image/grass_top.png"
2022-09-29 02:55:07 +00:00
#define STONE_TEXTURE "res/image/stone.png"
#define SNOW_TEXTURE "res/image/snow.png"
#define SAND_TEXTURE "res/image/sand.png"
#define WATER_TEXTURE "res/image/water.png"
2022-09-29 03:20:37 +00:00
#define SHRUB_TEXTURE "res/image/shrub.png"
#define SHRUB_TOP_TEXTURE "res/image/shrub_top.png"
2022-09-29 02:55:07 +00:00
2022-09-25 23:05:56 +00:00
namespace app {
struct Block {
uint32_t textures[6];
};
2022-09-25 23:05:56 +00:00
class Chunk {
public:
2022-10-01 18:12:54 +00:00
static constexpr int WATER_LEVEL = 20;
static constexpr glm::ivec3 CHUNK_SIZE{32, 256, 32};
static void load();
static void unload();
static std::vector<xe::Image*>& getTextures();
static Chunk* newChunk(int32_t gridX, int32_t gridZ, uint32_t world_seed);
2022-09-27 17:35:49 +00:00
static Chunk* getChunk(int32_t gridX, int32_t gridZ);
2022-09-27 21:03:43 +00:00
static void deleteChunk(int32_t gridX, int32_t gridZ);
2022-09-25 23:05:56 +00:00
2022-09-27 17:35:49 +00:00
static void createMesh(Chunk* c);
static void createMeshAsync(Chunk* c);
2022-09-25 23:05:56 +00:00
2022-09-28 23:36:35 +00:00
static void generate(Chunk* c);
static void generateAsync(Chunk* c);
2022-09-27 17:35:49 +00:00
xe::Model* getMesh();
uint8_t getBlock(int32_t x, int32_t y, int32_t z);
void setBlock(int32_t x, int32_t y, int32_t z, uint8_t block);
2022-10-03 10:50:49 +00:00
static uint8_t getGlobalBlock(int32_t x, int32_t y, int32_t z);
static void setGlobalBlock(int32_t x, int32_t y, int32_t z, uint8_t block);
2022-09-25 23:05:56 +00:00
2022-09-28 23:36:35 +00:00
static bool isGenerated(int32_t gridX, int32_t gridZ);
2022-09-30 19:40:42 +00:00
static bool isMeshed(int32_t gridX, int32_t gridZ);
2022-09-28 23:36:35 +00:00
2022-09-27 17:35:49 +00:00
const int32_t gridX, gridZ;
const uint32_t world_seed, chunk_seed;
2022-09-25 23:05:56 +00:00
private:
2022-09-27 17:35:49 +00:00
Chunk(int32_t gridX, int32_t gridZ, uint32_t world_seed);
~Chunk();
2022-09-30 19:40:42 +00:00
void resetThread();
bool generated;
bool reload;
bool finished;
2022-09-25 23:05:56 +00:00
2022-09-27 17:35:49 +00:00
xe::Model* chunkMesh;
xe::Model::Data vertexData;
2022-09-30 04:25:12 +00:00
std::vector<uint8_t> cubes{};
2022-09-30 19:40:42 +00:00
std::thread* worker;
2022-09-25 23:05:56 +00:00
};
}