Line data Source code
1 : /* Copyright (C) 2026 Wildfire Games. 2 : * This file is part of 0 A.D. 3 : * 4 : * 0 A.D. is free software: you can redistribute it and/or modify 5 : * it under the terms of the GNU General Public License as published by 6 : * the Free Software Foundation, either version 2 of the License, or 7 : * (at your option) any later version. 8 : * 9 : * 0 A.D. is distributed in the hope that it will be useful, 10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 : * GNU General Public License for more details. 13 : * 14 : * You should have received a copy of the GNU General Public License 15 : * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. 16 : */ 17 : 18 : #include "precompiled.h" 19 : 20 : #include "ICmpOverlayRenderer.h" 21 : 22 : #include "graphics/Color.h" 23 : #include "graphics/HeightMipmap.h" 24 : #include "graphics/Overlay.h" 25 : #include "graphics/TextureManager.h" 26 : #include "maths/FixedVector2D.h" 27 : #include "maths/Matrix3D.h" 28 : #include "maths/Vector3D.h" 29 : #include "ps/CLogger.h" 30 : #include "ps/CStr.h" 31 : #include "ps/Profile.h" 32 : #include "renderer/Renderer.h" 33 : #include "renderer/Scene.h" 34 : #include "simulation2/MessageTypes.h" 35 : #include "simulation2/components/ICmpPosition.h" 36 : #include "simulation2/components/ICmpRangeManager.h" 37 : #include "simulation2/helpers/Player.h" 38 : #include "simulation2/system/Component.h" 39 : #include "simulation2/system/Message.h" 40 : 41 : #include <cstddef> 42 : #include <string> 43 : #include <vector> 44 : 45 186 : class CCmpOverlayRenderer final : public ICmpOverlayRenderer 46 : { 47 : public: 48 137 : static void ClassInit(CComponentManager &componentManager) 49 : { 50 137 : componentManager.SubscribeToMessageType(MT_VisibilityChanged); 51 : } 52 : 53 372 : DEFAULT_COMPONENT_ALLOCATOR(OverlayRenderer) 54 : 55 : // Currently-enabled set of sprites 56 : std::vector<SOverlaySprite> m_Sprites; 57 : 58 : // For each entry in m_Sprites, store the offset of the sprite from the unit's position 59 : // (so we can recompute the sprite position after the unit moves) 60 : std::vector<CVector3D> m_SpriteOffsets; 61 : 62 : // Whether the sprites should be drawn (only valid between Interpolate and RenderSubmit) 63 : bool m_Enabled; 64 : 65 : // NOT SAFE TO SERIALIZE - used to hide status bars/auras of entities invisible to the player. 66 : // We also can't just deserialize it because annoyingly the range manager computes LOS data on deserialize... 67 : LosVisibility m_VisibilityToCurrentPlayer = LosVisibility::VISIBLE; 68 : // So to work around that, just store which player we saved this for last. 69 : player_id_t m_LastStoredPlayerVisibility = INVALID_PLAYER; 70 : 71 137 : static std::string GetSchema() 72 : { 73 274 : return "<empty/>"; 74 : } 75 : 76 186 : void Init(const CParamNode&) override 77 : { 78 186 : } 79 : 80 186 : void Deinit() override 81 : { 82 186 : } 83 : 84 1032 : void Serialize(ISerializer&) override 85 : { 86 : // TODO: should we do anything here? 87 : // or should we expect other components to reinitialise us 88 : // after deserialization? 89 1032 : } 90 : 91 0 : void Deserialize(const CParamNode ¶mNode, IDeserializer&) override 92 : { 93 0 : Init(paramNode); 94 0 : } 95 : 96 5418 : void HandleMessage(const CMessage &msg, bool /*global*/) override 97 : { 98 5418 : switch (msg.GetType()) 99 : { 100 0 : case MT_Interpolate: 101 0 : { 102 0 : PROFILE("OverlayRenderer::Interpolate"); 103 0 : const CMessageInterpolate &msgData = static_cast<const CMessageInterpolate &>(msg); 104 0 : Interpolate(msgData.deltaSimTime, msgData.offset); 105 0 : break; 106 0 : } 107 0 : case MT_RenderSubmit: 108 0 : { 109 0 : PROFILE("OverlayRenderer::RenderSubmit"); 110 0 : const CMessageRenderSubmit &msgData = static_cast<const CMessageRenderSubmit &>(msg); 111 0 : RenderSubmit(msgData.collector); 112 0 : break; 113 0 : } 114 5418 : case MT_VisibilityChanged: 115 5418 : { 116 5418 : const CMessageVisibilityChanged &msgData = static_cast<const CMessageVisibilityChanged &>(msg); 117 5418 : if (msgData.player == GetSimContext().GetCurrentDisplayedPlayer()) 118 0 : m_VisibilityToCurrentPlayer = static_cast<LosVisibility>(msgData.newVisibility); 119 : break; 120 : } 121 : } 122 5418 : } 123 : 124 : /* 125 : * Must be called whenever the size of m_Sprites changes, 126 : * to determine whether we need to respond to rendering messages. 127 : */ 128 0 : void UpdateMessageSubscriptions() 129 : { 130 0 : bool needRender = !m_Sprites.empty(); 131 : 132 0 : GetSimContext().GetComponentManager().DynamicSubscriptionNonsync(MT_Interpolate, this, needRender); 133 0 : GetSimContext().GetComponentManager().DynamicSubscriptionNonsync(MT_RenderSubmit, this, needRender); 134 0 : } 135 : 136 0 : void Reset() override 137 : { 138 0 : m_Sprites.clear(); 139 0 : m_SpriteOffsets.clear(); 140 : 141 0 : UpdateMessageSubscriptions(); 142 0 : } 143 : 144 0 : void AddSprite(const VfsPath &textureName, const CFixedVector2D &corner0, const CFixedVector2D &corner1, const CFixedVector3D &position, const std::string &color) override 145 : { 146 0 : CColor colorObj(1.0f, 1.0f, 1.0f, 1.0f); 147 0 : if (!colorObj.ParseString(color, 1)) 148 0 : LOGERROR("OverlayRenderer: Error parsing '{}'", color); 149 : 150 0 : CTextureProperties textureProps(textureName); 151 : 152 0 : SOverlaySprite sprite; 153 0 : sprite.m_Texture = g_Renderer.GetTextureManager().CreateTexture(textureProps); 154 0 : sprite.m_X0 = corner0.X.ToFloat(); 155 0 : sprite.m_Y0 = corner0.Y.ToFloat(); 156 0 : sprite.m_X1 = corner1.X.ToFloat(); 157 0 : sprite.m_Y1 = corner1.Y.ToFloat(); 158 0 : sprite.m_Color = colorObj; 159 : 160 0 : m_Sprites.push_back(sprite); 161 0 : m_SpriteOffsets.push_back(CVector3D(position)); 162 : 163 0 : UpdateMessageSubscriptions(); 164 0 : } 165 : 166 0 : void Interpolate(float /*frameTime*/, float frameOffset) 167 : { 168 : // Recompute our visibility if needed. 169 0 : if (m_LastStoredPlayerVisibility != GetSimContext().GetCurrentDisplayedPlayer()) 170 : { 171 0 : CmpPtr<ICmpRangeManager> cmpRangeManager(GetSystemEntity()); 172 0 : m_VisibilityToCurrentPlayer = static_cast<LosVisibility>(cmpRangeManager->GetLosVisibility(GetEntityHandle(), GetSimContext().GetCurrentDisplayedPlayer())); 173 0 : m_LastStoredPlayerVisibility = GetSimContext().GetCurrentDisplayedPlayer(); 174 : } 175 : 176 : // Skip all the following computations if we have no sprites 177 0 : if (m_Sprites.empty() || m_VisibilityToCurrentPlayer == LosVisibility::HIDDEN) 178 : { 179 0 : m_Enabled = false; 180 0 : return; 181 : } 182 : 183 : // Disable rendering of the unit if it has no position 184 0 : CmpPtr<ICmpPosition> cmpPosition(GetEntityHandle()); 185 0 : if (!cmpPosition || !cmpPosition->IsInWorld()) 186 : { 187 0 : m_Enabled = false; 188 0 : return; 189 : } 190 : 191 : // Find the precise position of the unit 192 0 : CMatrix3D transform(cmpPosition->GetInterpolatedTransform(frameOffset)); 193 0 : CVector3D position(transform.GetTranslation()); 194 : 195 : // Move all the sprites to the desired offset relative to the unit 196 0 : for (size_t i = 0; i < m_Sprites.size(); ++i) 197 0 : m_Sprites[i].m_Position = position + m_SpriteOffsets[i]; 198 : 199 0 : m_Enabled = true; 200 : } 201 : 202 0 : void RenderSubmit(SceneCollector &collector) 203 : { 204 0 : if (!m_Enabled) 205 : return; 206 : 207 0 : for (size_t i = 0; i < m_Sprites.size(); ++i) 208 0 : collector.Submit(&m_Sprites[i]); 209 : } 210 : }; 211 : 212 274 : REGISTER_COMPONENT_TYPE(OverlayRenderer)