1#ifndef GAME_MAP_RENDER_LAYER_H
2#define GAME_MAP_RENDER_LAYER_H
3
4#include <cstdint>
5
6using offset_ptr_size = char *;
7using offset_ptr = uintptr_t;
8using offset_ptr32 = unsigned int;
9
10#include <base/color.h>
11
12#include <engine/graphics.h>
13
14#include <game/map/envelope_manager.h>
15#include <game/map/render_component.h>
16#include <game/map/render_map.h>
17#include <game/mapitems.h>
18#include <game/mapitems_ex.h>
19
20#include <memory>
21#include <optional>
22#include <vector>
23
24class CMapLayers;
25class CMapItemLayerTilemap;
26class CMapItemLayerQuads;
27class IMap;
28class CMapImages;
29
30typedef std::function<void(int GroupId, int LayerId)> FCallbackLayerInit;
31
32constexpr int BorderRenderDistance = 201;
33
34class CClipRegion
35{
36public:
37 CClipRegion() = default;
38 CClipRegion(float X, float Y, float Width, float Height) :
39 m_X(X), m_Y(Y), m_Width(Width), m_Height(Height) {}
40
41 float m_X;
42 float m_Y;
43 float m_Width;
44 float m_Height;
45};
46
47class CRenderLayerParams
48{
49public:
50 int m_RenderType;
51 int m_EntityOverlayVal;
52 vec2 m_Center;
53 float m_Zoom;
54 bool m_RenderText;
55 bool m_RenderInvalidTiles;
56 bool m_RenderTileBorder;
57 bool m_DebugRenderGroupClips;
58 bool m_DebugRenderQuadClips;
59 bool m_DebugRenderClusterClips;
60 bool m_DebugRenderTileClips;
61};
62
63class CRenderLayer : public CRenderComponent
64{
65public:
66 CRenderLayer(int GroupId, int LayerId, int Flags);
67 virtual void OnInit(IGraphics *pGraphics, ITextRender *pTextRender, CRenderMap *pRenderMap, std::shared_ptr<CEnvelopeManager> &pEnvelopeManager, IMap *pMap, IMapImages *pMapImages, std::optional<FCallbackLayerInit> &CallbackLayerInitOptional, bool TileAndQuadBuffering);
68
69 virtual void Init() = 0;
70 virtual void Render(const CRenderLayerParams &Params) = 0;
71 virtual bool DoRender(const CRenderLayerParams &Params) = 0;
72 virtual bool IsValid() const { return true; }
73 virtual bool IsGroup() const { return false; }
74 virtual void Unload() = 0;
75
76 bool IsVisibleInClipRegion(const std::optional<CClipRegion> &ClipRegion) const;
77 int GetGroup() const { return m_GroupId; }
78
79protected:
80 int m_GroupId;
81 int m_LayerId;
82 int m_Flags;
83
84 void UseTexture(IGraphics::CTextureHandle TextureHandle);
85 virtual IGraphics::CTextureHandle GetTexture() const = 0;
86 virtual void InitCallback() const;
87
88 bool m_TileAndQuadBuffering = true;
89 class IMap *m_pMap = nullptr;
90 IMapImages *m_pMapImages = nullptr;
91 std::shared_ptr<CEnvelopeManager> m_pEnvelopeManager;
92 std::optional<FCallbackLayerInit> m_InitCallback;
93 std::optional<CClipRegion> m_LayerClip;
94};
95
96class CRenderLayerGroup : public CRenderLayer
97{
98public:
99 CRenderLayerGroup(int GroupId, const CMapItemGroup *pGroup);
100 ~CRenderLayerGroup() override = default;
101 void Init() override;
102 void Render(const CRenderLayerParams &Params) override;
103 bool DoRender(const CRenderLayerParams &Params) override;
104 bool IsValid() const override { return m_pGroup != nullptr; }
105 bool IsGroup() const override { return true; }
106 void Unload() override {}
107
108protected:
109 void InitCallback() const override;
110 IGraphics::CTextureHandle GetTexture() const override
111 {
112 return IGraphics::CTextureHandle();
113 }
114
115 const CMapItemGroup *m_pGroup;
116};
117
118class CRenderLayerTile : public CRenderLayer
119{
120public:
121 CRenderLayerTile(int GroupId, int LayerId, int Flags, const CMapItemLayerTilemap *pLayerTilemap);
122 ~CRenderLayerTile() override = default;
123 void Render(const CRenderLayerParams &Params) override;
124 bool DoRender(const CRenderLayerParams &Params) override;
125 void Init() override;
126 void OnInit(IGraphics *pGraphics, ITextRender *pTextRender, CRenderMap *pRenderMap, std::shared_ptr<CEnvelopeManager> &pEnvelopeManager, IMap *pMap, IMapImages *pMapImages, std::optional<FCallbackLayerInit> &CallbackLayerInitOptional, bool TileAndQuadBuffering) override;
127
128 virtual int GetDataIndex() const;
129 bool IsValid() const override;
130 void Unload() override;
131
132protected:
133 virtual void *GetRawData() const;
134 template<class T>
135 T *GetData() const;
136
137 virtual ColorRGBA GetRenderColor(const CRenderLayerParams &Params) const;
138 virtual void InitTileData();
139 virtual void GetTileData(unsigned char *pIndex, unsigned char *pFlags, int *pAngleRotate, unsigned int x, unsigned int y, int CurOverlay) const;
140 IGraphics::CTextureHandle GetTexture() const override { return m_TextureHandle; }
141 virtual bool IsDesignLayer() const { return true; }
142 const CTile *m_pTiles = nullptr;
143
144private:
145 IGraphics::CTextureHandle m_TextureHandle;
146
147protected:
148 class CTileLayerVisuals : public CRenderComponent
149 {
150 public:
151 CTileLayerVisuals()
152 {
153 m_Width = 0;
154 m_Height = 0;
155 m_BufferContainerIndex = -1;
156 m_IsTextured = false;
157 }
158
159 bool Init(unsigned int Width, unsigned int Height);
160 void Unload();
161
162 class CTileVisual
163 {
164 public:
165 CTileVisual() :
166 m_IndexBufferByteOffset(0) {}
167
168 private:
169 offset_ptr32 m_IndexBufferByteOffset;
170
171 public:
172 bool DoDraw() const
173 {
174 return (m_IndexBufferByteOffset & 0x10000000) != 0;
175 }
176
177 void Draw(bool SetDraw)
178 {
179 m_IndexBufferByteOffset = (SetDraw ? 0x10000000 : (offset_ptr32)0) | (m_IndexBufferByteOffset & 0xEFFFFFFF);
180 }
181
182 offset_ptr IndexBufferByteOffset() const
183 {
184 return ((offset_ptr)(m_IndexBufferByteOffset & 0xEFFFFFFF) * 6 * sizeof(uint32_t));
185 }
186
187 void SetIndexBufferByteOffset(offset_ptr32 IndexBufferByteOff)
188 {
189 m_IndexBufferByteOffset = IndexBufferByteOff | (m_IndexBufferByteOffset & 0x10000000);
190 }
191
192 void AddIndexBufferByteOffset(offset_ptr32 IndexBufferByteOff)
193 {
194 m_IndexBufferByteOffset = ((m_IndexBufferByteOffset & 0xEFFFFFFF) + IndexBufferByteOff) | (m_IndexBufferByteOffset & 0x10000000);
195 }
196 };
197
198 std::vector<CTileVisual> m_vTilesOfLayer;
199
200 CTileVisual m_BorderTopLeft;
201 CTileVisual m_BorderTopRight;
202 CTileVisual m_BorderBottomRight;
203 CTileVisual m_BorderBottomLeft;
204
205 CTileVisual m_BorderKillTile; // end of map kill tile -- game layer only
206
207 std::vector<CTileVisual> m_vBorderTop;
208 std::vector<CTileVisual> m_vBorderLeft;
209 std::vector<CTileVisual> m_vBorderRight;
210 std::vector<CTileVisual> m_vBorderBottom;
211
212 unsigned int m_Width;
213 unsigned int m_Height;
214 int m_BufferContainerIndex;
215 bool m_IsTextured;
216 };
217
218 void UploadTileData(std::optional<CTileLayerVisuals> &VisualsOptional, int CurOverlay, bool AddAsSpeedup, bool IsGameLayer = false);
219
220 virtual void RenderTileLayerWithTileBuffer(const ColorRGBA &Color, const CRenderLayerParams &Params);
221 virtual void RenderTileLayerNoTileBuffer(const ColorRGBA &Color, const CRenderLayerParams &Params);
222
223 void RenderTileLayer(const ColorRGBA &Color, const CRenderLayerParams &Params, CTileLayerVisuals *pTileLayerVisuals = nullptr);
224 void RenderTileBorder(const ColorRGBA &Color, int BorderX0, int BorderY0, int BorderX1, int BorderY1, CTileLayerVisuals *pTileLayerVisuals);
225 void RenderKillTileBorder(const ColorRGBA &Color);
226
227 std::optional<CRenderLayerTile::CTileLayerVisuals> m_VisualTiles;
228 const CMapItemLayerTilemap *m_pLayerTilemap;
229 ColorRGBA m_Color;
230};
231
232class CRenderLayerQuads : public CRenderLayer
233{
234public:
235 CRenderLayerQuads(int GroupId, int LayerId, int Flags, const CMapItemLayerQuads *pLayerQuads);
236 void OnInit(IGraphics *pGraphics, ITextRender *pTextRender, CRenderMap *pRenderMap, std::shared_ptr<CEnvelopeManager> &pEnvelopeManager, IMap *pMap, IMapImages *pMapImages, std::optional<FCallbackLayerInit> &CallbackLayerInitOptional, bool TileAndQuadBuffering) override;
237 void Init() override;
238 bool IsValid() const override { return m_pLayerQuads->m_NumQuads > 0 && m_pQuads; }
239 void Render(const CRenderLayerParams &Params) override;
240 bool DoRender(const CRenderLayerParams &Params) override;
241 void Unload() override;
242
243protected:
244 IGraphics::CTextureHandle GetTexture() const override { return m_TextureHandle; }
245
246 class CQuadLayerVisuals : public CRenderComponent
247 {
248 public:
249 CQuadLayerVisuals() :
250 m_QuadNum(0), m_BufferContainerIndex(-1), m_IsTextured(false) {}
251 void Unload();
252
253 int m_QuadNum;
254 int m_BufferContainerIndex;
255 bool m_IsTextured;
256 };
257 void RenderQuadLayer(float Alpha, const CRenderLayerParams &Params);
258
259 std::optional<CRenderLayerQuads::CQuadLayerVisuals> m_VisualQuad;
260 const CMapItemLayerQuads *m_pLayerQuads;
261
262 class CQuadCluster
263 {
264 public:
265 bool m_Grouped;
266 int m_StartIndex;
267 int m_NumQuads;
268
269 int m_PosEnv;
270 float m_PosEnvOffset;
271 int m_ColorEnv;
272 float m_ColorEnvOffset;
273
274 std::vector<SQuadRenderInfo> m_vQuadRenderInfo;
275 std::optional<CClipRegion> m_ClipRegion;
276 };
277 void CalculateClipping(CQuadCluster &QuadCluster);
278 bool CalculateQuadClipping(const CQuadCluster &QuadCluster, float aQuadOffsetMin[2], float aQuadOffsetMax[2]) const;
279
280 std::vector<CQuadCluster> m_vQuadClusters;
281 const CQuad *m_pQuads = nullptr;
282
283private:
284 IGraphics::CTextureHandle m_TextureHandle;
285};
286
287class CRenderLayerEntityBase : public CRenderLayerTile
288{
289public:
290 CRenderLayerEntityBase(int GroupId, int LayerId, int Flags, const CMapItemLayerTilemap *pLayerTilemap);
291 ~CRenderLayerEntityBase() override = default;
292 bool DoRender(const CRenderLayerParams &Params) override;
293
294protected:
295 ColorRGBA GetRenderColor(const CRenderLayerParams &Params) const override { return ColorRGBA(1.0f, 1.0f, 1.0f, Params.m_EntityOverlayVal / 100.0f); }
296 IGraphics::CTextureHandle GetTexture() const override;
297 bool IsDesignLayer() const override { return false; }
298};
299
300class CRenderLayerEntityGame final : public CRenderLayerEntityBase
301{
302public:
303 CRenderLayerEntityGame(int GroupId, int LayerId, int Flags, const CMapItemLayerTilemap *pLayerTilemap);
304 void Init() override;
305
306protected:
307 void RenderTileLayerWithTileBuffer(const ColorRGBA &Color, const CRenderLayerParams &Params) override;
308 void RenderTileLayerNoTileBuffer(const ColorRGBA &Color, const CRenderLayerParams &Params) override;
309
310private:
311 ColorRGBA GetDeathBorderColor() const;
312};
313
314class CRenderLayerEntityFront final : public CRenderLayerEntityBase
315{
316public:
317 CRenderLayerEntityFront(int GroupId, int LayerId, int Flags, const CMapItemLayerTilemap *pLayerTilemap);
318 int GetDataIndex() const override;
319 void Init() override;
320
321protected:
322 void RenderTileLayerNoTileBuffer(const ColorRGBA &Color, const CRenderLayerParams &Params) override;
323};
324
325class CRenderLayerEntityTele final : public CRenderLayerEntityBase
326{
327public:
328 CRenderLayerEntityTele(int GroupId, int LayerId, int Flags, const CMapItemLayerTilemap *pLayerTilemap);
329 int GetDataIndex() const override;
330 bool IsValid() const override;
331 void Init() override;
332 void Unload() override;
333
334protected:
335 void InitTileData() override;
336 void RenderTileLayerWithTileBuffer(const ColorRGBA &Color, const CRenderLayerParams &Params) override;
337 void RenderTileLayerNoTileBuffer(const ColorRGBA &Color, const CRenderLayerParams &Params) override;
338 void GetTileData(unsigned char *pIndex, unsigned char *pFlags, int *pAngleRotate, unsigned int x, unsigned int y, int CurOverlay) const override;
339
340private:
341 std::optional<CRenderLayerTile::CTileLayerVisuals> m_VisualTeleNumbers;
342 const CTeleTile *m_pTeleTiles = nullptr;
343};
344
345class CRenderLayerEntitySpeedup final : public CRenderLayerEntityBase
346{
347public:
348 CRenderLayerEntitySpeedup(int GroupId, int LayerId, int Flags, const CMapItemLayerTilemap *pLayerTilemap);
349 int GetDataIndex() const override;
350 bool IsValid() const override;
351 void Init() override;
352 void Unload() override;
353
354protected:
355 void InitTileData() override;
356 void RenderTileLayerWithTileBuffer(const ColorRGBA &Color, const CRenderLayerParams &Params) override;
357 void RenderTileLayerNoTileBuffer(const ColorRGBA &Color, const CRenderLayerParams &Params) override;
358 void GetTileData(unsigned char *pIndex, unsigned char *pFlags, int *pAngleRotate, unsigned int x, unsigned int y, int CurOverlay) const override;
359 IGraphics::CTextureHandle GetTexture() const override;
360
361private:
362 std::optional<CRenderLayerTile::CTileLayerVisuals> m_VisualForce;
363 std::optional<CRenderLayerTile::CTileLayerVisuals> m_VisualMaxSpeed;
364 const CSpeedupTile *m_pSpeedupTiles = nullptr;
365};
366
367class CRenderLayerEntitySwitch final : public CRenderLayerEntityBase
368{
369public:
370 CRenderLayerEntitySwitch(int GroupId, int LayerId, int Flags, const CMapItemLayerTilemap *pLayerTilemap);
371 int GetDataIndex() const override;
372 bool IsValid() const override;
373 void Init() override;
374 void Unload() override;
375
376protected:
377 void InitTileData() override;
378 void RenderTileLayerWithTileBuffer(const ColorRGBA &Color, const CRenderLayerParams &Params) override;
379 void RenderTileLayerNoTileBuffer(const ColorRGBA &Color, const CRenderLayerParams &Params) override;
380 void GetTileData(unsigned char *pIndex, unsigned char *pFlags, int *pAngleRotate, unsigned int x, unsigned int y, int CurOverlay) const override;
381 IGraphics::CTextureHandle GetTexture() const override;
382
383private:
384 std::optional<CRenderLayerTile::CTileLayerVisuals> m_VisualSwitchNumberTop;
385 std::optional<CRenderLayerTile::CTileLayerVisuals> m_VisualSwitchNumberBottom;
386 const CSwitchTile *m_pSwitchTiles = nullptr;
387};
388
389class CRenderLayerEntityTune final : public CRenderLayerEntityBase
390{
391public:
392 CRenderLayerEntityTune(int GroupId, int LayerId, int Flags, const CMapItemLayerTilemap *pLayerTilemap);
393 int GetDataIndex() const override;
394 bool IsValid() const override;
395 void Init() override;
396
397protected:
398 void InitTileData() override;
399 void RenderTileLayerNoTileBuffer(const ColorRGBA &Color, const CRenderLayerParams &Params) override;
400 void GetTileData(unsigned char *pIndex, unsigned char *pFlags, int *pAngleRotate, unsigned int x, unsigned int y, int CurOverlay) const override;
401 IGraphics::CTextureHandle GetTexture() const override;
402
403private:
404 const CTuneTile *m_pTuneTiles = nullptr;
405 mutable CTuneColorMapper m_TuneColorMapper;
406};
407#endif
408