-
Notifications
You must be signed in to change notification settings - Fork 14
/
blockrenderer.h
151 lines (116 loc) · 9.27 KB
/
blockrenderer.h
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#ifndef BLOCKRENDERER_H
#define BLOCKRENDERER_H
#include <memory>
#include "terrain.h"
#include "chunk.h"
class BlockRenderer
{
public:
virtual ~BlockRenderer() {}
virtual void renderSpecialBlock(const BLOCK_WDATA block, GLFix x, GLFix y, GLFix z, Chunk &c) = 0;
virtual void geometryNormalBlock(const BLOCK_WDATA block, const int x, const int y, const int z, const BLOCK_SIDE side, Chunk &c) = 0;
virtual bool isOpaque(const BLOCK_WDATA block) = 0; //If true, adjacent sides aren't rendered
virtual bool isObstacle(const BLOCK_WDATA block) = 0; //Whether the player can pass through
virtual bool isOriented(const BLOCK_WDATA block) = 0; //Whether its data is a BLOCK_SIDE
virtual bool isFullyOriented(const BLOCK_WDATA block) = 0; //Whether its data can be BLOCK_TOP or BLOCK_BOTTOM
virtual bool isBlockShaped(const BLOCK_WDATA block) = 0; // Whether the AABB is the full block
virtual AABB getAABB(const BLOCK_WDATA block, GLFix x, GLFix y, GLFix z) = 0;
virtual void drawPreview(const BLOCK_WDATA block, TEXTURE &dest, const int x, const int y) = 0;
static void renderNormalBlockSide(int local_x, int local_y, int local_z, const BLOCK_SIDE side, const TextureAtlasEntry &tex, Chunk &c, const COLOR color = 0);
static void renderNormalBlockSideQuad(int local_x, int local_y, int local_z, const BLOCK_SIDE side, const TextureAtlasEntry &tex, Chunk &c, const COLOR color = 0);
static void renderNormalBlockSideForceColor(int local_x, int local_y, int local_z, const BLOCK_SIDE side, const COLOR color, Chunk &c);
static void renderNormalBlockSideQuadForceColor(int local_x, int local_y, int local_z, const BLOCK_SIDE side, const COLOR color, Chunk &c);
//Doesn't render sides adjacent to other blocks of the same type, used by glass, water, leaves etc.
static void renderNormalConnectedBlockSide(const BLOCK_WDATA block, int local_x, int local_y, int local_z, const BLOCK_SIDE side, const TextureAtlasEntry &tex, const COLOR col, Chunk &c);
static void renderBillboard(int local_x, int local_y, int local_z, const TextureAtlasEntry &tex, Chunk &c);
static void drawTextureAtlasEntry(TEXTURE &src, const TextureAtlasEntry &tex, TEXTURE &dest, const int dest_x, const int dest_y);
virtual bool action(const BLOCK_WDATA block, const int local_x, const int local_y, const int local_z, Chunk &c) = 0; //Invoked by e.g. a right click
virtual void tick(const BLOCK_WDATA block, int local_x, int local_y, int local_z, Chunk &c) = 0; //Invoked every now-and-then
virtual void addedBlock(const BLOCK_WDATA block, int local_x, int local_y, int local_z, Chunk &c) = 0; //Invoked if this block has been placed
virtual void removedBlock(const BLOCK_WDATA block, int local_x, int local_y, int local_z, Chunk &c) = 0; //Invoked if this block has been removed
/* Redstone */
virtual PowerState powersSide(const BLOCK_WDATA block, BLOCK_SIDE side) = 0; //Return the power state to the block adjacent to side.
virtual const char* getName(const BLOCK_WDATA block) = 0;
};
//A proxy class which redirects calls to the appropriate BlockRenderer
class UniversalBlockRenderer : public BlockRenderer
{
public:
UniversalBlockRenderer();
virtual ~UniversalBlockRenderer();
virtual void renderSpecialBlock(const BLOCK_WDATA block, GLFix x, GLFix y, GLFix z, Chunk &c) override;
virtual void geometryNormalBlock(const BLOCK_WDATA block, const int local_x, const int local_y, const int local_z, const BLOCK_SIDE side, Chunk &c) override;
virtual bool isOpaque(const BLOCK_WDATA block) override;
virtual bool isObstacle(const BLOCK_WDATA block) override;
virtual bool isOriented(const BLOCK_WDATA block) override;
virtual bool isFullyOriented(const BLOCK_WDATA block) override;
virtual bool isBlockShaped(const BLOCK_WDATA block) override;
virtual AABB getAABB(const BLOCK_WDATA block, GLFix x, GLFix y, GLFix z) override;
virtual void drawPreview(const BLOCK_WDATA block, TEXTURE &dest, const int x, const int y) override;
// Used for particles spawned on destruction
const TerrainAtlasEntry &materialTexture(const BLOCK_WDATA block);
virtual bool action(const BLOCK_WDATA block, const int local_x, const int local_y, const int local_z, Chunk &c) override;
virtual void tick(const BLOCK_WDATA block, int local_x, int local_y, int local_z, Chunk &c) override;
virtual void addedBlock(const BLOCK_WDATA block, int local_x, int local_y, int local_z, Chunk &c) override;
virtual void removedBlock(const BLOCK_WDATA block, int local_x, int local_y, int local_z, Chunk &c) override;
virtual PowerState powersSide(const BLOCK_WDATA block, BLOCK_SIDE side) override;
virtual const char* getName(const BLOCK_WDATA block) override;
private:
std::shared_ptr<BlockRenderer> map[256];
};
class DumbBlockRenderer : public BlockRenderer
{
public:
virtual void renderSpecialBlock(const BLOCK_WDATA /*block*/, GLFix /*x*/, GLFix /*y*/, GLFix /*z*/, Chunk &/*c*/) override {}
virtual void geometryNormalBlock(const BLOCK_WDATA /*block*/, const int /*x*/, const int /*y*/, const int /*z*/, const BLOCK_SIDE /*side*/, Chunk &/*c*/) override {}
virtual bool isOpaque(const BLOCK_WDATA /*block*/) override { return true; }
virtual bool isObstacle(const BLOCK_WDATA /*block*/) override { return false; }
virtual bool isOriented(const BLOCK_WDATA /*block*/) override { return false; }
virtual bool isFullyOriented(const BLOCK_WDATA /*block*/) override { return false; }
virtual bool isBlockShaped(const BLOCK_WDATA /*block*/) override { return true; }
virtual AABB getAABB(const BLOCK_WDATA block, GLFix x, GLFix y, GLFix z) override;
virtual void drawPreview(const BLOCK_WDATA /*block*/, TEXTURE &/*dest*/, const int /*x*/, const int /*y*/) override { return; }
virtual bool action(const BLOCK_WDATA /*block*/, const int /*local_x*/, const int /*local_y*/, const int /*local_z*/, Chunk &/*c*/) override { return false; }
virtual void tick(const BLOCK_WDATA /*block*/, int /*local_x*/, int /*local_y*/, int /*local_z*/, Chunk &/*c*/) override {}
virtual void addedBlock(const BLOCK_WDATA /*block*/, int /*local_x*/, int /*local_y*/, int /*local_z*/, Chunk &/*c*/) override {}
virtual void removedBlock(const BLOCK_WDATA /*block*/, int /*local_x*/, int /*local_y*/, int /*local_z*/, Chunk &/*c*/) override {}
virtual PowerState powersSide(const BLOCK_WDATA /*block*/, BLOCK_SIDE /*side*/) override { return PowerState::NotPowered; }
};
class NormalBlockRenderer : public DumbBlockRenderer
{
public:
virtual void renderSpecialBlock(const BLOCK_WDATA /*block*/, GLFix /*x*/, GLFix /*y*/, GLFix /*z*/, Chunk &/*c*/) override {};
virtual void geometryNormalBlock(const BLOCK_WDATA block, const int local_x, const int local_y, const int local_z, const BLOCK_SIDE side, Chunk &c) override;
virtual bool isOpaque(const BLOCK_WDATA /*block*/) override { return true; }
virtual bool isObstacle(const BLOCK_WDATA /*block*/) override { return true; }
virtual bool isOriented(const BLOCK_WDATA /*block*/) override { return false; }
virtual bool isFullyOriented(const BLOCK_WDATA /*block*/) override { return false; }
virtual bool isBlockShaped(const BLOCK_WDATA /*block*/) override { return true; }
virtual void drawPreview(const BLOCK_WDATA block, TEXTURE &dest, const int dest_x, const int dest_y) override;
virtual const char* getName(const BLOCK_WDATA block) override { return block_names[getBLOCK(block)]; }
//For face-combining (see terrain.h, bottom) we need to know whether we can combine it
bool shouldRenderFaceAndItsTheSameAs(const int local_x, const int local_y, const int local_z, const BLOCK_SIDE side, Chunk &c, const BLOCK_WDATA block);
};
class OrientedBlockRenderer : public NormalBlockRenderer
{
virtual void geometryNormalBlock(const BLOCK_WDATA block, const int local_x, const int local_y, const int local_z, const BLOCK_SIDE side, Chunk &c) override;
virtual bool isOriented(const BLOCK_WDATA /*block*/) override { return true; }
virtual bool isFullyOriented(const BLOCK_WDATA /*block*/) override { return false; }
};
//As placeholder, so unknown blocks won't crash the game. Also used for air
class NullBlockRenderer : public DumbBlockRenderer
{
public:
virtual void renderSpecialBlock(const BLOCK_WDATA /*block*/, GLFix /*x*/, GLFix /*y*/, GLFix /*z*/, Chunk &/*c*/) override {};
virtual void geometryNormalBlock(const BLOCK_WDATA /*block*/, const int /*local_x*/, const int /*local_y*/, const int /*local_z*/, const BLOCK_SIDE /*side*/, Chunk &/*c*/) override {}
virtual bool isOpaque(const BLOCK_WDATA /*block*/) override { return false; }
virtual bool isObstacle(const BLOCK_WDATA /*block*/) override { return false; }
virtual bool isOriented(const BLOCK_WDATA /*block*/) override { return false; }
virtual bool isFullyOriented(const BLOCK_WDATA /*block*/) override { return false; }
virtual bool isBlockShaped(const BLOCK_WDATA /*block*/) override { return true; }
virtual AABB getAABB(const BLOCK_WDATA /*block*/, GLFix /*x*/, GLFix /*y*/, GLFix /*z*/) override { return {}; }
virtual void drawPreview(const BLOCK_WDATA /*block*/, TEXTURE &/*dest*/, int /*dest_x*/, int /*dest_y*/) override {}
virtual const char* getName(const BLOCK_WDATA /*block*/) override { return "NULL"; }
};
extern UniversalBlockRenderer global_block_renderer;
#endif // BLOCKRENDERER_H