summaryrefslogtreecommitdiff
path: root/src/chunk.hpp
blob: 5fcec1703418bf7a8e2efdb1ecfd9c6a9120ee09 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#pragma once

#include "xe_model.hpp"
#include "xe_engine.hpp"
#include "xe_image.hpp"

#include "chunk_noise.hpp"

#include <glm/common.hpp>
#include <glm/fwd.hpp>
#include <vector>
#include <thread>
#include <memory>
#include <string>
#include <map>
#include <algorithm>
#include <cstdlib>

#define INVALID             -1
#define AIR                 0
#define DIRT                1
#define GRASS               2
#define FULL_GRASS          3
#define STONE               4
#define SNOW                5
#define SAND                6
#define WATER               7
#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"
#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"
#define SHRUB_TEXTURE       "res/image/shrub.png"
#define SHRUB_TOP_TEXTURE   "res/image/shrub_top.png"

namespace app {

struct Block {
  uint32_t textures[6];
};

class Chunk {

  public:

    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);
    static Chunk* getChunk(int32_t gridX, int32_t gridZ);
    static void deleteChunk(int32_t gridX, int32_t gridZ);

    static void createMesh(Chunk* c);
    static void createMeshAsync(Chunk* c);

    static void generate(Chunk* c);
    static void generateAsync(Chunk* c);

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

    static bool isGenerated(int32_t gridX, int32_t gridZ);
    static bool isMeshed(int32_t gridX, int32_t gridZ);

    const int32_t gridX, gridZ;
    const uint32_t world_seed, chunk_seed;

  private:

    Chunk(int32_t gridX, int32_t gridZ, uint32_t world_seed);
    ~Chunk();

    void resetThread();

    bool generated;
    bool reload;
    bool finished;

    xe::Model* chunkMesh;
    xe::Model::Data vertexData;
    std::vector<uint8_t> cubes{};
    std::thread* worker;
    
};

}