LCOV - code coverage report
Current view: top level - source/gui - GUIRenderer.cpp (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 0 149 0.0 %
Date: 2026-12-03 01:19:39 Functions: 0 6 0.0 %

          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 "GUIRenderer.h"
      21             : 
      22             : #include "graphics/Canvas2D.h"
      23             : #include "graphics/TextureManager.h"
      24             : #include "gui/CGUI.h"
      25             : #include "gui/CGUISprite.h"
      26             : #include "gui/SettingTypes/CGUIColor.h"
      27             : #include "gui/SettingTypes/CGUISize.h"
      28             : #include "i18n/L10n.h"
      29             : #include "lib/file/vfs/vfs_path.h"
      30             : #include "lib/utf8.h"
      31             : #include "maths/Vector2D.h"
      32             : #include "ps/CLogger.h"
      33             : #include "ps/CStr.h"
      34             : #include "renderer/Renderer.h"
      35             : 
      36             : #include <string>
      37             : #include <utility>
      38             : 
      39             : using namespace GUIRenderer;
      40             : 
      41           0 : DrawCalls::DrawCalls()
      42             : {
      43           0 : }
      44             : 
      45             : // DrawCalls needs to be copyable, so it can be used in other copyable types.
      46             : // But actually copying data is hard, since we'd need to avoid losing track of
      47             : // who owns various pointers, so instead we just return an empty list.
      48             : // The list should get filled in again (by GUIRenderer::UpdateDrawCallCache)
      49             : // before it's used for rendering. (TODO: Is this class actually used safely
      50             : // in practice?)
      51             : 
      52           0 : DrawCalls::DrawCalls(const DrawCalls&)
      53           0 :     : std::vector<SDrawCall>()
      54             : {
      55           0 : }
      56             : 
      57           0 : DrawCalls& DrawCalls::operator=(const DrawCalls&)
      58             : {
      59           0 :     return *this;
      60             : }
      61             : 
      62             : 
      63           0 : void GUIRenderer::UpdateDrawCallCache(const CGUI& pGUI, DrawCalls& Calls, const CStr& SpriteName, const CRect& Size, const bool RoundCoordinates, std::map<CStr, std::unique_ptr<const CGUISprite>>& Sprites)
      64             : {
      65             :     // This is called only when something has changed (like the size of the
      66             :     // sprite), so it doesn't need to be particularly efficient.
      67             : 
      68             :     // Clean up the old data
      69           0 :     Calls.clear();
      70             : 
      71             :     // If this object has zero size, there's nothing to render. (This happens
      72             :     // with e.g. tooltips that have zero size before they're first drawn, so
      73             :     // it isn't necessarily an error.)
      74           0 :     if (Size.left == Size.right && Size.top == Size.bottom)
      75             :         return;
      76             : 
      77           0 :     std::map<CStr, std::unique_ptr<const CGUISprite>>::iterator it(Sprites.find(SpriteName));
      78           0 :     if (it == Sprites.end())
      79             :     {
      80             :         /*
      81             :          * Sprite not found. Check whether this a special sprite,
      82             :          * and if so create a new sprite:
      83             :          * "stretched:filename.ext" - stretched image
      84             :          * "stretched:grayscale:filename.ext" - stretched grayscale image.
      85             :          * "cropped:0.5, 0.25"    - stretch this ratio (x,y) of the top left of the image
      86             :          * "color:r g b a"        - solid color
      87             :          *     > "textureAsMask"  - when using color, use the (optional) texture alpha channel as mask.
      88             :          * These can be combined, but they must be separated by a ":"
      89             :          * so you can have a white overlay over an stretched grayscale image with:
      90             :          * "grayscale:color:255 255 255 100:stretched:filename.ext"
      91             :          */
      92             :         // Check that this can be a special sprite.
      93           0 :         if (SpriteName.find(":") == std::string::npos && SpriteName.find("color(") == std::string::npos)
      94             :         {
      95           0 :             LOGERROR("Trying to use a sprite that doesn't exist (\"{}\").", SpriteName);
      96           0 :             return;
      97             :         }
      98             : 
      99           0 :         auto sprite = std::make_unique<CGUISprite>();
     100           0 :         VfsPath TextureName = VfsPath("art/textures/ui") / wstring_from_utf8(SpriteName.AfterLast(":"));
     101           0 :         if (SpriteName.find("stretched:") != std::string::npos)
     102             :         {
     103             :             // TODO: Should check (nicely) that this is a valid file?
     104           0 :             auto image = std::make_unique<SGUIImage>();
     105             : 
     106           0 :             image->m_TextureName = TextureName;
     107           0 :             if (SpriteName.find("grayscale:") != std::string::npos)
     108             :             {
     109           0 :                 image->m_Effects = std::make_shared<SGUIImageEffects>();
     110           0 :                 image->m_Effects->m_Greyscale = true;
     111             :             }
     112             : 
     113           0 :             sprite->AddImage(std::move(image));
     114           0 :         }
     115           0 :         else if (SpriteName.find("cropped:") != std::string::npos)
     116             :         {
     117             :             // TODO: Should check (nicely) that this is a valid file?
     118           0 :             auto image = std::make_unique<SGUIImage>();
     119             : 
     120           0 :             const bool centered = SpriteName.find("center:") != std::string::npos;
     121             : 
     122           0 :             CStr info = SpriteName.AfterLast("cropped:").BeforeFirst(":");
     123           0 :             double xRatio = info.BeforeFirst(",").ToDouble();
     124           0 :             double yRatio = info.AfterLast(",").ToDouble();
     125           0 :             const CRect percentSize = centered
     126           0 :                 ? CRect(50 - 50 / xRatio, 50 - 50 / yRatio, 50 + 50 / xRatio, 50 + 50 / yRatio)
     127           0 :                 : CRect(0, 0, 100 / xRatio, 100 / yRatio);
     128           0 :             image->m_TextureSize = CGUISize(CRect(0, 0, 0, 0), percentSize);
     129           0 :             image->m_TextureName = TextureName;
     130             : 
     131           0 :             if (SpriteName.find("grayscale:") != std::string::npos)
     132             :             {
     133           0 :                 image->m_Effects = std::make_shared<SGUIImageEffects>();
     134           0 :                 image->m_Effects->m_Greyscale = true;
     135             :             }
     136             : 
     137           0 :             sprite->AddImage(std::move(image));
     138           0 :         }
     139           0 :         if (SpriteName.find("color:") != std::string::npos)
     140             :         {
     141           0 :             CStrW value = wstring_from_utf8(SpriteName.AfterLast("color:").BeforeFirst(":"));
     142             : 
     143           0 :             auto image = std::make_unique<SGUIImage>();
     144           0 :             CGUIColor* color;
     145             : 
     146             :             // If we are using a mask, this is an effect.
     147             :             // Otherwise we can fallback to the "back color" attribute
     148             :             // TODO: we are assuming there is a filename here.
     149           0 :             if (SpriteName.find("textureAsMask:") != std::string::npos)
     150             :             {
     151           0 :                 image->m_TextureName = TextureName;
     152           0 :                 image->m_Effects = std::make_shared<SGUIImageEffects>();
     153           0 :                 color = &image->m_Effects->m_SolidColor;
     154             :             }
     155             :             else
     156           0 :                 color = &image->m_BackColor;
     157             : 
     158             :             // Check color is valid
     159           0 :             if (!CGUI::ParseString<CGUIColor>(&pGUI, value, *color))
     160             :             {
     161           0 :                 LOGERROR("GUI: Error parsing sprite 'color' (\"{}\")", utf8_from_wstring(value));
     162           0 :                 return;
     163             :             }
     164             : 
     165           0 :             sprite->AddImage(std::move(image));
     166           0 :         }
     167             : 
     168           0 :         if (sprite->m_Images.empty())
     169             :         {
     170           0 :             LOGERROR("Trying to use a sprite that doesn't exist (\"{}\").", SpriteName);
     171           0 :             return;
     172             :         }
     173             : 
     174           0 :         it = Sprites.emplace(SpriteName, std::move(sprite)).first;
     175           0 :     }
     176             : 
     177           0 :     Calls.reserve(it->second->m_Images.size());
     178             : 
     179             :     // Iterate through all the sprite's images, loading the texture and
     180             :     // calculating the texture coordinates
     181           0 :     std::vector<std::unique_ptr<SGUIImage>>::const_iterator cit;
     182           0 :     for (cit = it->second->m_Images.begin(); cit != it->second->m_Images.end(); ++cit)
     183             :     {
     184           0 :         SDrawCall Call(cit->get()); // pointers are safe since we never modify sprites/images after startup
     185             : 
     186           0 :         CRect ObjectSize = (*cit)->m_Size.GetSize(Size);
     187             : 
     188           0 :         if (ObjectSize.GetWidth() == 0.0 || ObjectSize.GetHeight() == 0.0)
     189             :         {
     190             :             // Zero sized object. Don't report as an error, since it's common for e.g. hitpoint bars.
     191           0 :             continue; // i.e. don't continue with this image
     192             :         }
     193             : 
     194           0 :         Call.m_Vertices = ObjectSize;
     195           0 :         if ((*cit)->m_RoundCoordinates && RoundCoordinates)
     196             :         {
     197             :             // Round the vertex coordinates to integers, to avoid ugly filtering artifacts
     198           0 :             Call.m_Vertices.left = (int)(Call.m_Vertices.left + 0.5f);
     199           0 :             Call.m_Vertices.right = (int)(Call.m_Vertices.right + 0.5f);
     200           0 :             Call.m_Vertices.top = (int)(Call.m_Vertices.top + 0.5f);
     201           0 :             Call.m_Vertices.bottom = (int)(Call.m_Vertices.bottom + 0.5f);
     202             :         }
     203             : 
     204           0 :         bool hasTexture = false;
     205           0 :         if (!(*cit)->m_TextureName.empty())
     206             :         {
     207           0 :             CTextureProperties textureProps(g_L10n.LocalizePath((*cit)->m_TextureName));
     208           0 :             textureProps.SetAddressMode((*cit)->m_AddressMode);
     209           0 :             textureProps.SetIgnoreQuality(true);
     210           0 :             CTexturePtr texture = g_Renderer.GetTextureManager().CreateTexture(textureProps);
     211           0 :             texture->Prefetch();
     212           0 :             hasTexture = true;
     213           0 :             Call.m_Texture = texture;
     214           0 :             Call.m_ObjectSize = ObjectSize;
     215           0 :         }
     216             : 
     217           0 :         Call.m_BackColor = &(*cit)->m_BackColor;
     218           0 :         Call.m_GrayscaleFactor = 0.0f;
     219           0 :         if (!hasTexture)
     220             :         {
     221           0 :             Call.m_ColorAdd = *Call.m_BackColor;
     222           0 :             Call.m_ColorMultiply = CColor(0.0f, 0.0f, 0.0f, 0.0f);
     223           0 :             Call.m_Texture = g_Renderer.GetTextureManager().GetTransparentTexture();
     224             :         }
     225           0 :         else if ((*cit)->m_Effects)
     226             :         {
     227           0 :             if ((*cit)->m_Effects->m_AddColor != CGUIColor())
     228             :             {
     229           0 :                 const CColor color = (*cit)->m_Effects->m_AddColor;
     230           0 :                 Call.m_ColorAdd = CColor(color.r, color.g, color.b, 0.0f);
     231           0 :                 Call.m_ColorMultiply = CColor(1.0f, 1.0f, 1.0f, 1.0f);
     232             :             }
     233           0 :             else if ((*cit)->m_Effects->m_Greyscale)
     234             :             {
     235           0 :                 Call.m_ColorAdd = CColor(0.0f, 0.0f, 0.0f, 0.0f);
     236           0 :                 Call.m_ColorMultiply = CColor(1.0f, 1.0f, 1.0f, 1.0f);
     237           0 :                 Call.m_GrayscaleFactor = 1.0f;
     238             :             }
     239           0 :             else if ((*cit)->m_Effects->m_SolidColor != CGUIColor())
     240             :             {
     241           0 :                 const CColor color = (*cit)->m_Effects->m_SolidColor;
     242           0 :                 Call.m_ColorAdd = CColor(color.r, color.g, color.b, 0.0f);
     243           0 :                 Call.m_ColorMultiply = CColor(0.0f, 0.0f, 0.0f, color.a);
     244             :             }
     245             :             else /* Slight confusion - why no effects? */
     246             :             {
     247           0 :                 Call.m_ColorAdd = CColor(0.0f, 0.0f, 0.0f, 0.0f);
     248           0 :                 Call.m_ColorMultiply = CColor(1.0f, 1.0f, 1.0f, 1.0f);
     249             :             }
     250             :         }
     251             :         else
     252             :         {
     253           0 :             Call.m_ColorAdd = CColor(0.0f, 0.0f, 0.0f, 0.0f);
     254           0 :             Call.m_ColorMultiply = CColor(1.0f, 1.0f, 1.0f, 1.0f);
     255             :         }
     256             : 
     257           0 :         Calls.push_back(Call);
     258           0 :     }
     259             : }
     260             : 
     261           0 : CRect SDrawCall::ComputeTexCoords() const
     262             : {
     263           0 :     float TexWidth = m_Texture->GetWidth();
     264           0 :     float TexHeight = m_Texture->GetHeight();
     265             : 
     266           0 :     if (!TexWidth || !TexHeight)
     267           0 :         return CRect(0, 0, 1, 1);
     268             : 
     269             :     // Textures are positioned by defining a rectangular block of the
     270             :     // texture (usually the whole texture), and a rectangular block on
     271             :     // the screen. The texture is positioned to make those blocks line up.
     272             : 
     273             :     // Get the screen's position/size for the block
     274           0 :     CRect BlockScreen = m_Image->m_TextureSize.GetSize(m_ObjectSize);
     275             : 
     276           0 :     if (m_Image->m_FixedHAspectRatio)
     277           0 :         BlockScreen.right = BlockScreen.left + BlockScreen.GetHeight() * m_Image->m_FixedHAspectRatio;
     278             : 
     279             :     // Get the texture's position/size for the block:
     280           0 :     CRect BlockTex;
     281             : 
     282             :     // "real_texture_placement" overrides everything
     283           0 :     if (m_Image->m_TexturePlacementInFile != CRect())
     284           0 :         BlockTex = m_Image->m_TexturePlacementInFile;
     285             :     // Use the whole texture
     286             :     else
     287           0 :         BlockTex = CRect(0, 0, TexWidth, TexHeight);
     288             : 
     289             :     // When rendering, BlockTex will be transformed onto BlockScreen.
     290             :     // Also, TexCoords will be transformed onto ObjectSize (giving the
     291             :     // UV coords at each vertex of the object). We know everything
     292             :     // except for TexCoords, so calculate it:
     293             : 
     294           0 :     CVector2D translation(BlockTex.TopLeft()-BlockScreen.TopLeft());
     295           0 :     float ScaleW = BlockTex.GetWidth()/BlockScreen.GetWidth();
     296           0 :     float ScaleH = BlockTex.GetHeight()/BlockScreen.GetHeight();
     297             : 
     298           0 :     CRect TexCoords (
     299             :                 // Resize (translating to/from the origin, so the
     300             :                 // topleft corner stays in the same place)
     301           0 :                 (m_ObjectSize-m_ObjectSize.TopLeft())
     302           0 :                 .Scale(ScaleW, ScaleH)
     303           0 :                 + m_ObjectSize.TopLeft()
     304             :                 // Translate from BlockTex to BlockScreen
     305             :                 + translation
     306           0 :     );
     307             : 
     308             :     // The tex coords need to be scaled so that (texwidth,texheight) is
     309             :     // mapped onto (1,1)
     310           0 :     TexCoords.left   /= TexWidth;
     311           0 :     TexCoords.right  /= TexWidth;
     312           0 :     TexCoords.top    /= TexHeight;
     313           0 :     TexCoords.bottom /= TexHeight;
     314             : 
     315           0 :     return TexCoords;
     316             : }
     317             : 
     318           0 : void GUIRenderer::Draw(DrawCalls& Calls, CCanvas2D& canvas)
     319             : {
     320           0 :     if (Calls.empty())
     321             :         return;
     322             : 
     323             :     // Called every frame, to draw the object (based on cached calculations)
     324             : 
     325             :     // Iterate through each DrawCall, and execute whatever drawing code is being called
     326           0 :     for (DrawCalls::const_iterator cit = Calls.begin(); cit != Calls.end(); ++cit)
     327             :     {
     328             :         // A hack to get a correct backend texture size.
     329           0 :         cit->m_Texture->UploadBackendTextureIfNeeded(g_Renderer.GetDeviceCommandContext());
     330             : 
     331           0 :         CRect texCoords = cit->ComputeTexCoords().Scale(
     332           0 :             cit->m_Texture->GetWidth(), cit->m_Texture->GetHeight());
     333             : 
     334             :         // Ensure the quad has the correct winding order
     335           0 :         CRect rect = cit->m_Vertices;
     336           0 :         if (rect.right < rect.left)
     337             :         {
     338           0 :             std::swap(rect.right, rect.left);
     339           0 :             std::swap(texCoords.right, texCoords.left);
     340             :         }
     341           0 :         if (rect.bottom < rect.top)
     342             :         {
     343           0 :             std::swap(rect.bottom, rect.top);
     344           0 :             std::swap(texCoords.bottom, texCoords.top);
     345             :         }
     346             : 
     347           0 :         canvas.DrawTexture(cit->m_Texture,
     348           0 :             rect, texCoords, cit->m_ColorMultiply, cit->m_ColorAdd, cit->m_GrayscaleFactor);
     349             :     }
     350             : }

Generated by: LCOV version 1.16