2021-01-13 00:18:29 +01:00
|
|
|
/* Copyright (C) 2021 Wildfire Games.
|
2009-04-18 19:00:33 +02:00
|
|
|
* This file is part of 0 A.D.
|
|
|
|
*
|
|
|
|
* 0 A.D. is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* 0 A.D. is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2006-04-24 01:14:18 +02:00
|
|
|
#include "precompiled.h"
|
|
|
|
|
|
|
|
#include "GUIRenderer.h"
|
|
|
|
|
2012-02-08 21:43:38 +01:00
|
|
|
#include "graphics/ShaderManager.h"
|
2010-09-10 23:02:10 +02:00
|
|
|
#include "graphics/TextureManager.h"
|
2019-08-29 11:07:29 +02:00
|
|
|
#include "gui/CGUI.h"
|
2019-08-25 10:57:36 +02:00
|
|
|
#include "gui/CGUISprite.h"
|
2019-08-04 05:52:00 +02:00
|
|
|
#include "gui/GUIMatrix.h"
|
2019-10-02 11:44:00 +02:00
|
|
|
#include "gui/SettingTypes/CGUIColor.h"
|
2014-04-20 22:03:57 +02:00
|
|
|
#include "i18n/L10n.h"
|
2006-04-24 01:14:18 +02:00
|
|
|
#include "lib/ogl.h"
|
|
|
|
#include "lib/res/h_mgr.h"
|
2007-12-20 21:21:45 +01:00
|
|
|
#include "lib/tex/tex.h"
|
2019-09-23 01:28:25 +02:00
|
|
|
#include "lib/utf8.h"
|
2006-04-24 01:14:18 +02:00
|
|
|
#include "ps/CLogger.h"
|
2010-09-10 23:02:10 +02:00
|
|
|
#include "ps/Filesystem.h"
|
|
|
|
#include "renderer/Renderer.h"
|
|
|
|
|
2006-04-24 01:14:18 +02:00
|
|
|
using namespace GUIRenderer;
|
|
|
|
|
|
|
|
DrawCalls::DrawCalls()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2009-07-28 11:27:56 +02:00
|
|
|
// DrawCalls needs to be copyable, so it can be used in other copyable types.
|
|
|
|
// But actually copying data is hard, since we'd need to avoid losing track of
|
|
|
|
// who owns various pointers, so instead we just return an empty list.
|
|
|
|
// The list should get filled in again (by GUIRenderer::UpdateDrawCallCache)
|
|
|
|
// before it's used for rendering. (TODO: Is this class actually used safely
|
|
|
|
// in practice?)
|
2006-04-24 01:14:18 +02:00
|
|
|
|
|
|
|
DrawCalls::DrawCalls(const DrawCalls&)
|
2009-07-28 11:27:56 +02:00
|
|
|
: std::vector<SDrawCall>()
|
2006-04-24 01:14:18 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-05-23 00:04:58 +02:00
|
|
|
DrawCalls& DrawCalls::operator=(const DrawCalls&)
|
2006-04-24 01:14:18 +02:00
|
|
|
{
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-08-21 12:12:33 +02:00
|
|
|
void GUIRenderer::UpdateDrawCallCache(const CGUI& pGUI, DrawCalls& Calls, const CStr& SpriteName, const CRect& Size, int CellID, std::map<CStr, const CGUISprite*>& Sprites)
|
2006-04-24 01:14:18 +02:00
|
|
|
{
|
|
|
|
// This is called only when something has changed (like the size of the
|
|
|
|
// sprite), so it doesn't need to be particularly efficient.
|
|
|
|
|
|
|
|
// Clean up the old data
|
|
|
|
Calls.clear();
|
|
|
|
|
|
|
|
// If this object has zero size, there's nothing to render. (This happens
|
|
|
|
// with e.g. tooltips that have zero size before they're first drawn, so
|
|
|
|
// it isn't necessarily an error.)
|
2012-01-29 21:04:21 +01:00
|
|
|
if (Size.left == Size.right && Size.top == Size.bottom)
|
2006-04-24 01:14:18 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
|
MOVABLE idiom, const CGUI struct maps, in place move construction instead of copying temporaries during CGUI XML loading and GenerateText.
Introduce MOVABLE idiom indicating that a class can use move semantics.
Make values of CGUI struct maps holding XML data const to ensure at the
root that the data is not modified.
Use NONCOPYABLE and MOVABLE for SGUIIcon and SGUIStyle to enforce the
non-copy policy on the compiler level (until someone changes the GUI
design to make modifications needed).
As indicated by that:
Replace copy operations by in place move operations for these CGUI
struct maps in the CGUI Xeromyces XML loading functions.
Replace copy operations by const references for CSize and SGUIIcon in
CGUIString::GenerateTextCall and CGUI::GenerateText.
This avoids copying of non primitive members, such as the strings and
containers of strings.
Further related cleanup to be kept separated for auditability.
Differential Revision: https://code.wildfiregames.com/D2163
Few comments on IRC by: wraitii, Itms
Tested on: gcc 9, Jenkins, partially VS2015
Refs #1984,
NONCOPYABLE CGUISpriteInstances: 0a7d0ecdde, 8f4f8e240f, c19f3608a5
NONCOPYABLE Image, Sprite: fb8032043b
NONCOPYABLE GUI page: 94c57085e9
NONCOPYABLE GUIManager: 7c2e9027c2
NONCOPYABLE macro: 16ccae10cd
This was SVN commit r22637.
2019-08-09 19:25:55 +02:00
|
|
|
std::map<CStr, const CGUISprite*>::iterator it(Sprites.find(SpriteName));
|
2006-04-24 01:14:18 +02:00
|
|
|
if (it == Sprites.end())
|
|
|
|
{
|
2015-12-13 17:03:17 +01:00
|
|
|
/*
|
|
|
|
* Sprite not found. Check whether this a special sprite,
|
|
|
|
* and if so create a new sprite:
|
|
|
|
* "stretched:filename.ext" - stretched image
|
|
|
|
* "stretched:grayscale:filename.ext" - stretched grayscale image.
|
|
|
|
* "cropped:0.5, 0.25" - stretch this ratio (x,y) of the top left of the image
|
|
|
|
* "color:r g b a" - solid color
|
|
|
|
* > "textureAsMask" - when using color, use the (optional) texture alpha channel as mask.
|
|
|
|
* These can be combined, but they must be separated by a ":"
|
|
|
|
* so you can have a white overlay over an stretched grayscale image with:
|
|
|
|
* "grayscale:color:255 255 255 100:stretched:filename.ext"
|
|
|
|
*/
|
|
|
|
// Check that this can be a special sprite.
|
|
|
|
if (SpriteName.ReverseFind(":") == -1 && SpriteName.Find("color(") == -1)
|
|
|
|
{
|
|
|
|
LOGERROR("Trying to use a sprite that doesn't exist (\"%s\").", SpriteName.c_str());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
CGUISprite* Sprite = new CGUISprite;
|
|
|
|
VfsPath TextureName = VfsPath("art/textures/ui") / wstring_from_utf8(SpriteName.AfterLast(":"));
|
|
|
|
if (SpriteName.Find("stretched:") != -1)
|
2006-04-24 01:14:18 +02:00
|
|
|
{
|
2010-10-21 23:22:40 +02:00
|
|
|
// TODO: Should check (nicely) that this is a valid file?
|
2019-10-01 17:06:13 +02:00
|
|
|
SGUIImage* Image = new SGUIImage();
|
2015-08-21 19:08:41 +02:00
|
|
|
|
2015-12-13 17:03:17 +01:00
|
|
|
Image->m_TextureName = TextureName;
|
2012-03-18 20:43:03 +01:00
|
|
|
// Allow grayscale images for disabled portraits
|
2015-12-13 17:03:17 +01:00
|
|
|
if (SpriteName.Find("grayscale:") != -1)
|
2012-03-18 20:43:03 +01:00
|
|
|
{
|
2019-08-17 13:52:57 +02:00
|
|
|
Image->m_Effects = std::make_shared<SGUIImageEffects>();
|
2014-01-03 21:19:43 +01:00
|
|
|
Image->m_Effects->m_Greyscale = true;
|
2012-03-18 20:43:03 +01:00
|
|
|
}
|
2010-10-21 23:22:40 +02:00
|
|
|
|
2014-01-03 21:19:43 +01:00
|
|
|
Sprite->AddImage(Image);
|
2006-04-24 01:14:18 +02:00
|
|
|
|
|
|
|
Sprites[SpriteName] = Sprite;
|
|
|
|
}
|
2015-12-13 17:03:17 +01:00
|
|
|
else if (SpriteName.Find("cropped:") != -1)
|
2012-08-10 21:59:20 +02:00
|
|
|
{
|
|
|
|
// TODO: Should check (nicely) that this is a valid file?
|
2019-10-01 17:06:13 +02:00
|
|
|
SGUIImage* Image = new SGUIImage();
|
2012-08-10 21:59:20 +02:00
|
|
|
|
2015-12-13 17:03:17 +01:00
|
|
|
CStr info = SpriteName.AfterLast("cropped:").BeforeFirst(":");
|
|
|
|
double xRatio = info.BeforeFirst(",").ToDouble();
|
|
|
|
double yRatio = info.AfterLast(",").ToDouble();
|
2019-10-01 17:06:13 +02:00
|
|
|
Image->m_TextureSize = CGUISize(CRect(0, 0, 0, 0), CRect(0, 0, 100/xRatio, 100/yRatio));
|
2015-08-21 19:08:41 +02:00
|
|
|
|
2015-12-13 17:03:17 +01:00
|
|
|
Image->m_TextureName = TextureName;
|
2012-08-10 21:59:20 +02:00
|
|
|
|
2014-01-03 21:19:43 +01:00
|
|
|
Sprite->AddImage(Image);
|
2012-08-10 21:59:20 +02:00
|
|
|
|
|
|
|
Sprites[SpriteName] = Sprite;
|
|
|
|
}
|
2015-12-13 17:03:17 +01:00
|
|
|
if (SpriteName.Find("color:") != -1)
|
2010-10-21 23:22:40 +02:00
|
|
|
{
|
2015-12-13 17:03:17 +01:00
|
|
|
CStrW value = wstring_from_utf8(SpriteName.AfterLast("color:").BeforeFirst(":"));
|
2010-10-21 23:22:40 +02:00
|
|
|
|
2019-10-01 17:06:13 +02:00
|
|
|
SGUIImage* Image = new SGUIImage();
|
Stop copying color every draw call for every GUI object using colors.
Avoid color copies for rendering Draw calls in
GUIRenderer::UpdateDrawCallCache, CButton::Draw, CChart::DrawAxes,
CDropDown::Draw, CList::DrawList, COList::DrawList, refs #1984,
8f4f8e240f, 3028551b91, a905fbbc98.
Avoid color copies during XML loading in CGUI::Xeromyces_ReadImage,
CGUI::Xeromyces_ReadEffects, COList::HandleAdditionalChildren.
Add CGUI::HasPreDefinedColor and mark m_PreDefinedColors,
CGUI::GetPreDefinedColor, IGUIButtonBehavior::ChooseColor() as const for
consistency with the other "databases", refs 3028551b91.
Mark CGUIColor as NONCOPYABLE to add compiler errors if there is an
unexplicit copy, refs 3028551b91.
Explicit ugly copy in CGUI::Xeromyces_ReadColor and
CGUIColor::ParseString.
Deregister copying <CGUIColor>GetSetting functions, refs 8f4f8e240f.
Uses the const ref GetSetting from 3dfa23cd25.
This was SVN commit r22694.
2019-08-19 14:53:58 +02:00
|
|
|
CGUIColor* color;
|
2010-10-21 23:22:40 +02:00
|
|
|
|
2015-12-13 17:03:17 +01:00
|
|
|
// If we are using a mask, this is an effect.
|
|
|
|
// Otherwise we can fallback to the "back color" attribute
|
|
|
|
// TODO: we are assuming there is a filename here.
|
|
|
|
if (SpriteName.Find("textureAsMask:") != -1)
|
|
|
|
{
|
|
|
|
Image->m_TextureName = TextureName;
|
2019-08-17 13:52:57 +02:00
|
|
|
Image->m_Effects = std::make_shared<SGUIImageEffects>();
|
Stop copying color every draw call for every GUI object using colors.
Avoid color copies for rendering Draw calls in
GUIRenderer::UpdateDrawCallCache, CButton::Draw, CChart::DrawAxes,
CDropDown::Draw, CList::DrawList, COList::DrawList, refs #1984,
8f4f8e240f, 3028551b91, a905fbbc98.
Avoid color copies during XML loading in CGUI::Xeromyces_ReadImage,
CGUI::Xeromyces_ReadEffects, COList::HandleAdditionalChildren.
Add CGUI::HasPreDefinedColor and mark m_PreDefinedColors,
CGUI::GetPreDefinedColor, IGUIButtonBehavior::ChooseColor() as const for
consistency with the other "databases", refs 3028551b91.
Mark CGUIColor as NONCOPYABLE to add compiler errors if there is an
unexplicit copy, refs 3028551b91.
Explicit ugly copy in CGUI::Xeromyces_ReadColor and
CGUIColor::ParseString.
Deregister copying <CGUIColor>GetSetting functions, refs 8f4f8e240f.
Uses the const ref GetSetting from 3dfa23cd25.
This was SVN commit r22694.
2019-08-19 14:53:58 +02:00
|
|
|
color = &Image->m_Effects->m_SolidColor;
|
2015-12-13 17:03:17 +01:00
|
|
|
}
|
|
|
|
else
|
Stop copying color every draw call for every GUI object using colors.
Avoid color copies for rendering Draw calls in
GUIRenderer::UpdateDrawCallCache, CButton::Draw, CChart::DrawAxes,
CDropDown::Draw, CList::DrawList, COList::DrawList, refs #1984,
8f4f8e240f, 3028551b91, a905fbbc98.
Avoid color copies during XML loading in CGUI::Xeromyces_ReadImage,
CGUI::Xeromyces_ReadEffects, COList::HandleAdditionalChildren.
Add CGUI::HasPreDefinedColor and mark m_PreDefinedColors,
CGUI::GetPreDefinedColor, IGUIButtonBehavior::ChooseColor() as const for
consistency with the other "databases", refs 3028551b91.
Mark CGUIColor as NONCOPYABLE to add compiler errors if there is an
unexplicit copy, refs 3028551b91.
Explicit ugly copy in CGUI::Xeromyces_ReadColor and
CGUIColor::ParseString.
Deregister copying <CGUIColor>GetSetting functions, refs 8f4f8e240f.
Uses the const ref GetSetting from 3dfa23cd25.
This was SVN commit r22694.
2019-08-19 14:53:58 +02:00
|
|
|
color = &Image->m_BackColor;
|
|
|
|
|
|
|
|
// Check color is valid
|
2019-08-29 11:07:29 +02:00
|
|
|
if (!CGUI::ParseString<CGUIColor>(&pGUI, value, *color))
|
Stop copying color every draw call for every GUI object using colors.
Avoid color copies for rendering Draw calls in
GUIRenderer::UpdateDrawCallCache, CButton::Draw, CChart::DrawAxes,
CDropDown::Draw, CList::DrawList, COList::DrawList, refs #1984,
8f4f8e240f, 3028551b91, a905fbbc98.
Avoid color copies during XML loading in CGUI::Xeromyces_ReadImage,
CGUI::Xeromyces_ReadEffects, COList::HandleAdditionalChildren.
Add CGUI::HasPreDefinedColor and mark m_PreDefinedColors,
CGUI::GetPreDefinedColor, IGUIButtonBehavior::ChooseColor() as const for
consistency with the other "databases", refs 3028551b91.
Mark CGUIColor as NONCOPYABLE to add compiler errors if there is an
unexplicit copy, refs 3028551b91.
Explicit ugly copy in CGUI::Xeromyces_ReadColor and
CGUIColor::ParseString.
Deregister copying <CGUIColor>GetSetting functions, refs 8f4f8e240f.
Uses the const ref GetSetting from 3dfa23cd25.
This was SVN commit r22694.
2019-08-19 14:53:58 +02:00
|
|
|
{
|
|
|
|
LOGERROR("GUI: Error parsing sprite 'color' (\"%s\")", utf8_from_wstring(value));
|
|
|
|
return;
|
|
|
|
}
|
2010-10-21 23:22:40 +02:00
|
|
|
|
2014-01-03 21:19:43 +01:00
|
|
|
Sprite->AddImage(Image);
|
2010-10-21 23:22:40 +02:00
|
|
|
|
|
|
|
Sprites[SpriteName] = Sprite;
|
|
|
|
}
|
2015-12-13 17:03:17 +01:00
|
|
|
it = Sprites.find(SpriteName);
|
|
|
|
|
|
|
|
// Otherwise, just complain and give up:
|
|
|
|
if (it == Sprites.end())
|
2006-04-24 01:14:18 +02:00
|
|
|
{
|
2015-12-13 17:03:17 +01:00
|
|
|
SAFE_DELETE(Sprite);
|
2015-01-22 21:36:24 +01:00
|
|
|
LOGERROR("Trying to use a sprite that doesn't exist (\"%s\").", SpriteName.c_str());
|
2006-04-24 01:14:18 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-03 21:19:43 +01:00
|
|
|
Calls.reserve(it->second->m_Images.size());
|
2006-04-24 01:14:18 +02:00
|
|
|
|
|
|
|
// Iterate through all the sprite's images, loading the texture and
|
|
|
|
// calculating the texture coordinates
|
2014-01-03 21:19:43 +01:00
|
|
|
std::vector<SGUIImage*>::const_iterator cit;
|
|
|
|
for (cit = it->second->m_Images.begin(); cit != it->second->m_Images.end(); ++cit)
|
2006-04-24 01:14:18 +02:00
|
|
|
{
|
2014-01-03 21:19:43 +01:00
|
|
|
SDrawCall Call(*cit); // pointers are safe since we never modify sprites/images after startup
|
2006-04-24 01:14:18 +02:00
|
|
|
|
2019-10-01 17:06:13 +02:00
|
|
|
CRect ObjectSize = (*cit)->m_Size.GetSize(Size);
|
2006-04-24 01:14:18 +02:00
|
|
|
|
|
|
|
if (ObjectSize.GetWidth() == 0.0 || ObjectSize.GetHeight() == 0.0)
|
|
|
|
{
|
|
|
|
// Zero sized object. Don't report as an error, since it's common for e.g. hitpoint bars.
|
|
|
|
continue; // i.e. don't continue with this image
|
|
|
|
}
|
|
|
|
|
|
|
|
Call.m_Vertices = ObjectSize;
|
2014-01-03 21:19:43 +01:00
|
|
|
if ((*cit)->m_RoundCoordinates)
|
2011-08-20 19:17:53 +02:00
|
|
|
{
|
|
|
|
// Round the vertex coordinates to integers, to avoid ugly filtering artifacts
|
|
|
|
Call.m_Vertices.left = (int)(Call.m_Vertices.left + 0.5f);
|
|
|
|
Call.m_Vertices.right = (int)(Call.m_Vertices.right + 0.5f);
|
|
|
|
Call.m_Vertices.top = (int)(Call.m_Vertices.top + 0.5f);
|
|
|
|
Call.m_Vertices.bottom = (int)(Call.m_Vertices.bottom + 0.5f);
|
|
|
|
}
|
2006-04-24 01:14:18 +02:00
|
|
|
|
2014-01-03 21:19:43 +01:00
|
|
|
if (!(*cit)->m_TextureName.empty())
|
2006-04-24 01:14:18 +02:00
|
|
|
{
|
2014-10-15 18:04:37 +02:00
|
|
|
CTextureProperties textureProps(g_L10n.LocalizePath((*cit)->m_TextureName));
|
2014-01-03 21:19:43 +01:00
|
|
|
textureProps.SetWrap((*cit)->m_WrapMode);
|
2010-09-10 23:02:10 +02:00
|
|
|
CTexturePtr texture = g_Renderer.GetTextureManager().CreateTexture(textureProps);
|
|
|
|
texture->Prefetch();
|
|
|
|
Call.m_HasTexture = true;
|
|
|
|
Call.m_Texture = texture;
|
2006-04-24 01:14:18 +02:00
|
|
|
|
2010-09-10 23:02:10 +02:00
|
|
|
Call.m_EnableBlending = false; // will be overridden if the texture has an alpha channel
|
2006-04-24 01:14:18 +02:00
|
|
|
|
2010-09-10 23:02:10 +02:00
|
|
|
Call.m_ObjectSize = ObjectSize;
|
|
|
|
Call.m_CellID = CellID;
|
2006-04-24 01:14:18 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-09-10 23:02:10 +02:00
|
|
|
Call.m_HasTexture = false;
|
2006-04-24 01:14:18 +02:00
|
|
|
// Enable blending if it's transparent (allowing a little error in the calculations)
|
2014-01-03 21:19:43 +01:00
|
|
|
Call.m_EnableBlending = !(fabs((*cit)->m_BackColor.a - 1.0f) < 0.0000001f);
|
2006-04-24 01:14:18 +02:00
|
|
|
}
|
|
|
|
|
Stop copying color every draw call for every GUI object using colors.
Avoid color copies for rendering Draw calls in
GUIRenderer::UpdateDrawCallCache, CButton::Draw, CChart::DrawAxes,
CDropDown::Draw, CList::DrawList, COList::DrawList, refs #1984,
8f4f8e240f, 3028551b91, a905fbbc98.
Avoid color copies during XML loading in CGUI::Xeromyces_ReadImage,
CGUI::Xeromyces_ReadEffects, COList::HandleAdditionalChildren.
Add CGUI::HasPreDefinedColor and mark m_PreDefinedColors,
CGUI::GetPreDefinedColor, IGUIButtonBehavior::ChooseColor() as const for
consistency with the other "databases", refs 3028551b91.
Mark CGUIColor as NONCOPYABLE to add compiler errors if there is an
unexplicit copy, refs 3028551b91.
Explicit ugly copy in CGUI::Xeromyces_ReadColor and
CGUIColor::ParseString.
Deregister copying <CGUIColor>GetSetting functions, refs 8f4f8e240f.
Uses the const ref GetSetting from 3dfa23cd25.
This was SVN commit r22694.
2019-08-19 14:53:58 +02:00
|
|
|
Call.m_BackColor = &(*cit)->m_BackColor;
|
|
|
|
Call.m_BorderColor = (*cit)->m_Border ? &(*cit)->m_BorderColor : nullptr;
|
2014-01-03 21:19:43 +01:00
|
|
|
Call.m_DeltaZ = (*cit)->m_DeltaZ;
|
2012-02-08 21:43:38 +01:00
|
|
|
|
|
|
|
if (!Call.m_HasTexture)
|
|
|
|
{
|
2013-09-29 15:19:52 +02:00
|
|
|
Call.m_Shader = g_Renderer.GetShaderManager().LoadEffect(str_gui_solid);
|
2012-02-08 21:43:38 +01:00
|
|
|
}
|
2014-01-03 21:19:43 +01:00
|
|
|
else if ((*cit)->m_Effects)
|
2006-04-24 01:14:18 +02:00
|
|
|
{
|
2019-07-26 20:57:28 +02:00
|
|
|
if ((*cit)->m_Effects->m_AddColor != CGUIColor())
|
2006-04-24 01:14:18 +02:00
|
|
|
{
|
2013-09-29 15:19:52 +02:00
|
|
|
Call.m_Shader = g_Renderer.GetShaderManager().LoadEffect(str_gui_add);
|
2014-01-03 21:19:43 +01:00
|
|
|
Call.m_ShaderColorParameter = (*cit)->m_Effects->m_AddColor;
|
2006-04-24 01:14:18 +02:00
|
|
|
// Always enable blending if something's being subtracted from
|
|
|
|
// the alpha channel
|
2014-01-03 21:19:43 +01:00
|
|
|
if ((*cit)->m_Effects->m_AddColor.a < 0.f)
|
2006-04-24 01:14:18 +02:00
|
|
|
Call.m_EnableBlending = true;
|
|
|
|
}
|
2014-01-03 21:19:43 +01:00
|
|
|
else if ((*cit)->m_Effects->m_Greyscale)
|
2006-04-24 01:14:18 +02:00
|
|
|
{
|
2013-09-29 15:19:52 +02:00
|
|
|
Call.m_Shader = g_Renderer.GetShaderManager().LoadEffect(str_gui_grayscale);
|
2006-04-24 01:14:18 +02:00
|
|
|
}
|
2019-07-26 20:57:28 +02:00
|
|
|
else if ((*cit)->m_Effects->m_SolidColor != CGUIColor())
|
2015-12-13 17:03:17 +01:00
|
|
|
{
|
|
|
|
Call.m_Shader = g_Renderer.GetShaderManager().LoadEffect(str_gui_solid_mask);
|
|
|
|
Call.m_ShaderColorParameter = (*cit)->m_Effects->m_SolidColor;
|
|
|
|
Call.m_EnableBlending = !(fabs((*cit)->m_Effects->m_SolidColor.a - 1.0f) < 0.0000001f);
|
|
|
|
}
|
2006-04-24 01:14:18 +02:00
|
|
|
else /* Slight confusion - why no effects? */
|
|
|
|
{
|
2013-09-29 15:19:52 +02:00
|
|
|
Call.m_Shader = g_Renderer.GetShaderManager().LoadEffect(str_gui_basic);
|
2006-04-24 01:14:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-09-29 15:19:52 +02:00
|
|
|
Call.m_Shader = g_Renderer.GetShaderManager().LoadEffect(str_gui_basic);
|
2006-04-24 01:14:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Calls.push_back(Call);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-09-10 23:02:10 +02:00
|
|
|
CRect SDrawCall::ComputeTexCoords() const
|
|
|
|
{
|
|
|
|
float TexWidth = m_Texture->GetWidth();
|
|
|
|
float TexHeight = m_Texture->GetHeight();
|
|
|
|
|
|
|
|
if (!TexWidth || !TexHeight)
|
|
|
|
return CRect(0, 0, 1, 1);
|
|
|
|
|
|
|
|
// Textures are positioned by defining a rectangular block of the
|
|
|
|
// texture (usually the whole texture), and a rectangular block on
|
|
|
|
// the screen. The texture is positioned to make those blocks line up.
|
|
|
|
|
|
|
|
// Get the screen's position/size for the block
|
2019-10-01 17:06:13 +02:00
|
|
|
CRect BlockScreen = m_Image->m_TextureSize.GetSize(m_ObjectSize);
|
2010-09-10 23:02:10 +02:00
|
|
|
|
2011-08-20 19:17:53 +02:00
|
|
|
if (m_Image->m_FixedHAspectRatio)
|
|
|
|
BlockScreen.right = BlockScreen.left + BlockScreen.GetHeight() * m_Image->m_FixedHAspectRatio;
|
|
|
|
|
2010-09-10 23:02:10 +02:00
|
|
|
// Get the texture's position/size for the block:
|
|
|
|
CRect BlockTex;
|
|
|
|
|
|
|
|
// "real_texture_placement" overrides everything
|
|
|
|
if (m_Image->m_TexturePlacementInFile != CRect())
|
|
|
|
{
|
|
|
|
BlockTex = m_Image->m_TexturePlacementInFile;
|
|
|
|
}
|
|
|
|
// Check whether this sprite has "cell_size" set (and non-zero)
|
|
|
|
else if ((int)m_Image->m_CellSize.cx)
|
|
|
|
{
|
|
|
|
int cols = (int)TexWidth / (int)m_Image->m_CellSize.cx;
|
|
|
|
if (cols == 0)
|
|
|
|
cols = 1; // avoid divide-by-zero
|
|
|
|
int col = m_CellID % cols;
|
|
|
|
int row = m_CellID / cols;
|
|
|
|
BlockTex = CRect(m_Image->m_CellSize.cx*col, m_Image->m_CellSize.cy*row,
|
|
|
|
m_Image->m_CellSize.cx*(col+1), m_Image->m_CellSize.cy*(row+1));
|
|
|
|
}
|
|
|
|
// Use the whole texture
|
|
|
|
else
|
|
|
|
BlockTex = CRect(0, 0, TexWidth, TexHeight);
|
|
|
|
|
|
|
|
// When rendering, BlockTex will be transformed onto BlockScreen.
|
|
|
|
// Also, TexCoords will be transformed onto ObjectSize (giving the
|
|
|
|
// UV coords at each vertex of the object). We know everything
|
|
|
|
// except for TexCoords, so calculate it:
|
|
|
|
|
|
|
|
CPos translation (BlockTex.TopLeft()-BlockScreen.TopLeft());
|
|
|
|
float ScaleW = BlockTex.GetWidth()/BlockScreen.GetWidth();
|
|
|
|
float ScaleH = BlockTex.GetHeight()/BlockScreen.GetHeight();
|
|
|
|
|
|
|
|
CRect TexCoords (
|
|
|
|
// Resize (translating to/from the origin, so the
|
|
|
|
// topleft corner stays in the same place)
|
|
|
|
(m_ObjectSize-m_ObjectSize.TopLeft())
|
|
|
|
.Scale(ScaleW, ScaleH)
|
|
|
|
+ m_ObjectSize.TopLeft()
|
|
|
|
// Translate from BlockTex to BlockScreen
|
|
|
|
+ translation
|
|
|
|
);
|
|
|
|
|
|
|
|
// The tex coords need to be scaled so that (texwidth,texheight) is
|
|
|
|
// mapped onto (1,1)
|
|
|
|
TexCoords.left /= TexWidth;
|
|
|
|
TexCoords.right /= TexWidth;
|
|
|
|
TexCoords.top /= TexHeight;
|
|
|
|
TexCoords.bottom /= TexHeight;
|
|
|
|
|
|
|
|
return TexCoords;
|
|
|
|
}
|
|
|
|
|
2015-08-21 19:08:41 +02:00
|
|
|
void GUIRenderer::Draw(DrawCalls& Calls, float Z)
|
2006-04-24 01:14:18 +02:00
|
|
|
{
|
|
|
|
// Called every frame, to draw the object (based on cached calculations)
|
|
|
|
|
2012-02-08 21:43:38 +01:00
|
|
|
// TODO: batching by shader/texture/etc would be nice
|
|
|
|
|
2012-01-29 21:04:21 +01:00
|
|
|
CMatrix3D matrix = GetDefaultGuiMatrix();
|
|
|
|
|
2006-04-24 01:14:18 +02:00
|
|
|
glDisable(GL_BLEND);
|
|
|
|
|
|
|
|
// Iterate through each DrawCall, and execute whatever drawing code is being called
|
|
|
|
for (DrawCalls::const_iterator cit = Calls.begin(); cit != Calls.end(); ++cit)
|
|
|
|
{
|
2012-02-12 21:45:31 +01:00
|
|
|
cit->m_Shader->BeginPass();
|
|
|
|
CShaderProgramPtr shader = cit->m_Shader->GetShader();
|
2013-09-29 15:19:52 +02:00
|
|
|
shader->Uniform(str_transform, matrix);
|
2012-02-12 21:45:31 +01:00
|
|
|
|
2010-09-10 23:02:10 +02:00
|
|
|
if (cit->m_HasTexture)
|
2006-04-24 01:14:18 +02:00
|
|
|
{
|
2013-09-29 15:19:52 +02:00
|
|
|
shader->Uniform(str_color, cit->m_ShaderColorParameter);
|
|
|
|
shader->BindTexture(str_tex, cit->m_Texture);
|
2006-04-24 01:14:18 +02:00
|
|
|
|
2012-02-08 21:43:38 +01:00
|
|
|
if (cit->m_EnableBlending || cit->m_Texture->HasAlpha()) // (shouldn't call HasAlpha before BindTexture)
|
2010-09-10 23:02:10 +02:00
|
|
|
{
|
|
|
|
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
|
|
|
glEnable(GL_BLEND);
|
|
|
|
}
|
|
|
|
|
|
|
|
CRect TexCoords = cit->ComputeTexCoords();
|
|
|
|
|
2012-02-05 23:57:58 +01:00
|
|
|
// Ensure the quad has the correct winding order, and update texcoords to match
|
|
|
|
CRect Verts = cit->m_Vertices;
|
|
|
|
if (Verts.right < Verts.left)
|
|
|
|
{
|
|
|
|
std::swap(Verts.right, Verts.left);
|
|
|
|
std::swap(TexCoords.right, TexCoords.left);
|
|
|
|
}
|
|
|
|
if (Verts.bottom < Verts.top)
|
|
|
|
{
|
|
|
|
std::swap(Verts.bottom, Verts.top);
|
|
|
|
std::swap(TexCoords.bottom, TexCoords.top);
|
|
|
|
}
|
|
|
|
|
2012-02-09 18:55:25 +01:00
|
|
|
std::vector<float> data;
|
|
|
|
#define ADD(u, v, x, y, z) STMT(data.push_back(u); data.push_back(v); data.push_back(x); data.push_back(y); data.push_back(z))
|
|
|
|
ADD(TexCoords.left, TexCoords.bottom, Verts.left, Verts.bottom, Z + cit->m_DeltaZ);
|
|
|
|
ADD(TexCoords.right, TexCoords.bottom, Verts.right, Verts.bottom, Z + cit->m_DeltaZ);
|
|
|
|
ADD(TexCoords.right, TexCoords.top, Verts.right, Verts.top, Z + cit->m_DeltaZ);
|
2006-04-24 01:14:18 +02:00
|
|
|
|
2012-02-09 18:55:25 +01:00
|
|
|
ADD(TexCoords.right, TexCoords.top, Verts.right, Verts.top, Z + cit->m_DeltaZ);
|
|
|
|
ADD(TexCoords.left, TexCoords.top, Verts.left, Verts.top, Z + cit->m_DeltaZ);
|
|
|
|
ADD(TexCoords.left, TexCoords.bottom, Verts.left, Verts.bottom, Z + cit->m_DeltaZ);
|
|
|
|
#undef ADD
|
2006-04-24 01:14:18 +02:00
|
|
|
|
2012-02-09 18:55:25 +01:00
|
|
|
shader->TexCoordPointer(GL_TEXTURE0, 2, GL_FLOAT, 5*sizeof(float), &data[0]);
|
|
|
|
shader->VertexPointer(3, GL_FLOAT, 5*sizeof(float), &data[2]);
|
|
|
|
glDrawArrays(GL_TRIANGLES, 0, 6);
|
2006-04-24 01:14:18 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
Stop copying color every draw call for every GUI object using colors.
Avoid color copies for rendering Draw calls in
GUIRenderer::UpdateDrawCallCache, CButton::Draw, CChart::DrawAxes,
CDropDown::Draw, CList::DrawList, COList::DrawList, refs #1984,
8f4f8e240f, 3028551b91, a905fbbc98.
Avoid color copies during XML loading in CGUI::Xeromyces_ReadImage,
CGUI::Xeromyces_ReadEffects, COList::HandleAdditionalChildren.
Add CGUI::HasPreDefinedColor and mark m_PreDefinedColors,
CGUI::GetPreDefinedColor, IGUIButtonBehavior::ChooseColor() as const for
consistency with the other "databases", refs 3028551b91.
Mark CGUIColor as NONCOPYABLE to add compiler errors if there is an
unexplicit copy, refs 3028551b91.
Explicit ugly copy in CGUI::Xeromyces_ReadColor and
CGUIColor::ParseString.
Deregister copying <CGUIColor>GetSetting functions, refs 8f4f8e240f.
Uses the const ref GetSetting from 3dfa23cd25.
This was SVN commit r22694.
2019-08-19 14:53:58 +02:00
|
|
|
shader->Uniform(str_color, *cit->m_BackColor);
|
2006-04-24 01:14:18 +02:00
|
|
|
|
2010-09-10 23:02:10 +02:00
|
|
|
if (cit->m_EnableBlending)
|
|
|
|
{
|
|
|
|
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
|
|
|
glEnable(GL_BLEND);
|
|
|
|
}
|
|
|
|
|
2012-02-05 23:57:58 +01:00
|
|
|
// Ensure the quad has the correct winding order
|
|
|
|
CRect Verts = cit->m_Vertices;
|
|
|
|
if (Verts.right < Verts.left)
|
|
|
|
std::swap(Verts.right, Verts.left);
|
|
|
|
if (Verts.bottom < Verts.top)
|
|
|
|
std::swap(Verts.bottom, Verts.top);
|
|
|
|
|
2012-02-09 18:55:25 +01:00
|
|
|
std::vector<float> data;
|
|
|
|
#define ADD(x, y, z) STMT(data.push_back(x); data.push_back(y); data.push_back(z))
|
|
|
|
ADD(Verts.left, Verts.bottom, Z + cit->m_DeltaZ);
|
|
|
|
ADD(Verts.right, Verts.bottom, Z + cit->m_DeltaZ);
|
|
|
|
ADD(Verts.right, Verts.top, Z + cit->m_DeltaZ);
|
|
|
|
|
|
|
|
ADD(Verts.right, Verts.top, Z + cit->m_DeltaZ);
|
|
|
|
ADD(Verts.left, Verts.top, Z + cit->m_DeltaZ);
|
|
|
|
ADD(Verts.left, Verts.bottom, Z + cit->m_DeltaZ);
|
|
|
|
|
|
|
|
shader->VertexPointer(3, GL_FLOAT, 3*sizeof(float), &data[0]);
|
|
|
|
glDrawArrays(GL_TRIANGLES, 0, 6);
|
2006-04-24 01:14:18 +02:00
|
|
|
|
Stop copying color every draw call for every GUI object using colors.
Avoid color copies for rendering Draw calls in
GUIRenderer::UpdateDrawCallCache, CButton::Draw, CChart::DrawAxes,
CDropDown::Draw, CList::DrawList, COList::DrawList, refs #1984,
8f4f8e240f, 3028551b91, a905fbbc98.
Avoid color copies during XML loading in CGUI::Xeromyces_ReadImage,
CGUI::Xeromyces_ReadEffects, COList::HandleAdditionalChildren.
Add CGUI::HasPreDefinedColor and mark m_PreDefinedColors,
CGUI::GetPreDefinedColor, IGUIButtonBehavior::ChooseColor() as const for
consistency with the other "databases", refs 3028551b91.
Mark CGUIColor as NONCOPYABLE to add compiler errors if there is an
unexplicit copy, refs 3028551b91.
Explicit ugly copy in CGUI::Xeromyces_ReadColor and
CGUIColor::ParseString.
Deregister copying <CGUIColor>GetSetting functions, refs 8f4f8e240f.
Uses the const ref GetSetting from 3dfa23cd25.
This was SVN commit r22694.
2019-08-19 14:53:58 +02:00
|
|
|
if (cit->m_BorderColor != nullptr)
|
2006-04-24 01:14:18 +02:00
|
|
|
{
|
Stop copying color every draw call for every GUI object using colors.
Avoid color copies for rendering Draw calls in
GUIRenderer::UpdateDrawCallCache, CButton::Draw, CChart::DrawAxes,
CDropDown::Draw, CList::DrawList, COList::DrawList, refs #1984,
8f4f8e240f, 3028551b91, a905fbbc98.
Avoid color copies during XML loading in CGUI::Xeromyces_ReadImage,
CGUI::Xeromyces_ReadEffects, COList::HandleAdditionalChildren.
Add CGUI::HasPreDefinedColor and mark m_PreDefinedColors,
CGUI::GetPreDefinedColor, IGUIButtonBehavior::ChooseColor() as const for
consistency with the other "databases", refs 3028551b91.
Mark CGUIColor as NONCOPYABLE to add compiler errors if there is an
unexplicit copy, refs 3028551b91.
Explicit ugly copy in CGUI::Xeromyces_ReadColor and
CGUIColor::ParseString.
Deregister copying <CGUIColor>GetSetting functions, refs 8f4f8e240f.
Uses the const ref GetSetting from 3dfa23cd25.
This was SVN commit r22694.
2019-08-19 14:53:58 +02:00
|
|
|
shader->Uniform(str_color, *cit->m_BorderColor);
|
2012-02-09 18:55:25 +01:00
|
|
|
|
|
|
|
data.clear();
|
|
|
|
ADD(Verts.left + 0.5f, Verts.top + 0.5f, Z + cit->m_DeltaZ);
|
|
|
|
ADD(Verts.right - 0.5f, Verts.top + 0.5f, Z + cit->m_DeltaZ);
|
|
|
|
ADD(Verts.right - 0.5f, Verts.bottom - 0.5f, Z + cit->m_DeltaZ);
|
|
|
|
ADD(Verts.left + 0.5f, Verts.bottom - 0.5f, Z + cit->m_DeltaZ);
|
|
|
|
|
|
|
|
shader->VertexPointer(3, GL_FLOAT, 3*sizeof(float), &data[0]);
|
|
|
|
glDrawArrays(GL_LINE_LOOP, 0, 4);
|
2006-04-24 01:14:18 +02:00
|
|
|
}
|
2012-02-09 18:55:25 +01:00
|
|
|
#undef ADD
|
2006-04-24 01:14:18 +02:00
|
|
|
}
|
2015-08-21 19:08:41 +02:00
|
|
|
|
2012-02-12 21:45:31 +01:00
|
|
|
cit->m_Shader->EndPass();
|
2006-04-24 01:14:18 +02:00
|
|
|
|
2010-09-24 23:42:41 +02:00
|
|
|
glDisable(GL_BLEND);
|
2006-04-24 01:14:18 +02:00
|
|
|
}
|
|
|
|
}
|