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