summary refs log tree commit diff
path: root/src/MapRenderer.h
blob: d6361c732ad762e81eb6ddbb7aa51ad9eab0d226 (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
#ifndef CC_MAPRENDERER_H
#define CC_MAPRENDERER_H
#include "Core.h"
#include "Constants.h"
/* Renders the blocks of the world by subdividing it into chunks.
   Also manages the process of building/deleting chunk meshes.
   Also sorts chunks so nearest chunks are rendered first, and calculates chunk visibility.
   Copyright 2014-2023 ClassiCube | Licensed under BSD-3
*/
struct IGameComponent;
extern struct IGameComponent MapRenderer_Component;

/* Max used 1D atlases. (i.e. Atlas1D_Index(maxTextureLoc) + 1) */
extern int MapRenderer_1DUsedCount;

/* Buffer for all chunk parts. There are (MapRenderer_ChunksCount * Atlas1D_Count) parts in the buffer,
with parts for 'normal' buffer being in lower half. */
extern struct ChunkPartInfo* MapRenderer_PartsNormal; /* TODO: THAT DESC SUCKS */
extern struct ChunkPartInfo* MapRenderer_PartsTranslucent;

/* Describes a portion of the data needed for rendering a chunk. */
struct ChunkPartInfo {
#ifdef CC_BUILD_GL11
	/* 1 VB per face, another VB for sprites */
	#define CHUNKPART_MAX_VBS (FACE_COUNT + 1)
	GfxResourceID vbs[CHUNKPART_MAX_VBS];
#endif
	int offset;      /* -1 if no vertices at all */
	int spriteCount; /* Sprite vertices count */
	cc_uint16 counts[FACE_COUNT]; /* Counts per face */
};

/* Describes data necessary for rendering a chunk. */
struct ChunkInfo {	
	cc_uint16 centreX, centreY, centreZ; /* Centre coordinates of the chunk */

	cc_uint8 visible : 1; /* Whether chunk is visible to the player */
	cc_uint8 empty : 1;   /* Whether the chunk is empty of data and is known to have no data */
	cc_uint8 dirty : 1;   /* Whether chunk is pending being rebuilt */
	cc_uint8 allAir : 1;  /* Whether chunk is completely air */
	cc_uint8 noData : 1;  /* Whether the chunk is currently empty of data, but may have data if built */
	cc_uint8 : 0;         /* pad to next byte*/

	cc_uint8 drawXMin : 1;
	cc_uint8 drawXMax : 1;
	cc_uint8 drawZMin : 1;
	cc_uint8 drawZMax : 1;
	cc_uint8 drawYMin : 1;
	cc_uint8 drawYMax : 1;
	cc_uint8 : 0;          /* pad to next byte */
#ifdef OCCLUSION
	public cc_bool Visited = false, Occluded = false;
	public byte OcclusionFlags, OccludedFlags, DistanceFlags;
#endif
#ifndef CC_BUILD_GL11
	GfxResourceID vb;
#endif
	struct ChunkPartInfo* normalParts;
	struct ChunkPartInfo* translucentParts;
};

/* Renders the meshes of non-translucent blocks in visible chunks. */
void MapRenderer_RenderNormal(float delta);
/* Renders the meshes of translucent blocks in visible chunks. */
void MapRenderer_RenderTranslucent(float delta);
/* Potentially updates sort order of rendered chunks. */
/* Potentially builds meshes for several nearby chunks. */
/* NOTE: This should be called once per frame. */
void MapRenderer_Update(float delta);

/* Marks the given chunk as needing to be rebuilt/redrawn. */
/* NOTE: Coordinates outside the map are simply ignored. */
void MapRenderer_RefreshChunk(int cx, int cy, int cz);
/* Called when a block is changed, to update internal state. */
void MapRenderer_OnBlockChanged(int x, int y, int z, BlockID block);
/* Deletes all chunks and resets internal state. */
void MapRenderer_Refresh(void);
#endif