master uint/magnolia / include / world / chunk.h
 1#ifndef CHUNK_H
 2#define CHUNK_H
 3
 4#include "types.h"
 5#include "world/block.h"
 6#include "renderer/renderer.h"
 7
 8#define CHUNK_WIDTH 16
 9#define CHUNK_DEPTH 16
10#define CHUNK_HEIGHT 256
11
12#define CHUNK_SECTION_WIDTH 16
13#define CHUNK_SECTION_HEIGHT 16
14#define CHUNK_SECTION_DEPTH 16
15#define CHUNK_SECTION_COUNT 16
16
17typedef struct {
18	BlockType blocks[CHUNK_SECTION_WIDTH][CHUNK_SECTION_HEIGHT][CHUNK_SECTION_DEPTH];
19	MGMesh    meshes[BLOCK_TEXTURE_COUNT];
20} ChunkSection;
21
22typedef struct {
23	i32 x;
24	i32 z;
25
26	ChunkSection* sections[CHUNK_SECTION_COUNT];
27	u16           section_mask;
28	i8            used;
29} Chunk;
30
31/* clear chunk contents and mark unused */
32void chunk_clear(Chunk* chunk);
33
34/* clear chunk contents and keep unloaded */
35void chunk_remove(Chunk* chunk);
36
37/* create a new chunk section
38   TODO: make it general not only for test world */
39void chunk_section_create(ChunkSection* section);
40
41/* decode packed block states into a chunk section
42   returns 1 on success, 0 on failure */
43i8 chunk_section_decode(ChunkSection* section, const u8* data, size_t size);
44
45/* get the block at specified coordinate in chunk section
46   returns block type, or BLOCK_AIR on invalid input */
47BlockType chunk_section_get(const ChunkSection* section, i32 x, i32 y, i32 z);
48
49/* set the block at specified coordinate in chunk section */
50void chunk_section_set(ChunkSection* section, i32 x, i32 y, i32 z, BlockType block);
51
52/* create a mesh for a chunk section
53   returns 1 on success, 0 on failure */
54i8 chunk_section_mesh_create(ChunkSection* section, const ChunkSection* neighbours[6]);
55
56/* destroy a chunk sections mesh */
57void chunk_section_mesh_destroy(ChunkSection* section);
58
59#endif /* CHUNK_H */