1
1
forked from 0ad/0ad

Removes complex transform from TextRenderer.

This was SVN commit r25648.
This commit is contained in:
Vladislav Belov 2021-06-02 22:16:09 +00:00
parent 94d669c198
commit 83608c9205
6 changed files with 30 additions and 56 deletions

View File

@ -203,7 +203,7 @@ void CCanvas2D::DrawText(CTextRenderer& textRenderer)
CShaderProgramPtr shader = m->Tech->GetShader();
shader->Uniform(str_grayscaleFactor, 0.0f);
textRenderer.Render(shader);
textRenderer.Render(shader, GetDefaultGuiMatrix());
}
void CCanvas2D::Flush()

View File

@ -23,51 +23,29 @@
#include "graphics/FontManager.h"
#include "graphics/ShaderProgram.h"
#include "lib/ogl.h"
#include "maths/Matrix3D.h"
#include "ps/CStrIntern.h"
#include "ps/CStrInternStatic.h"
#include "ps/GameSetup/Config.h"
#include "renderer/Renderer.h"
#include <errno.h>
CTextRenderer::CTextRenderer()
{
ResetTransform();
ResetTranslate();
SetCurrentColor(CColor(1.0f, 1.0f, 1.0f, 1.0f));
SetCurrentFont(str_sans_10);
}
void CTextRenderer::ResetTransform()
void CTextRenderer::ResetTranslate(const CVector2D& translate)
{
float xres = g_xres / g_GuiScale;
float yres = g_yres / g_GuiScale;
m_Transform.SetIdentity();
m_Transform.Scale(1.0f, -1.f, 1.0f);
m_Transform.Translate(0.0f, yres, -1000.0f);
CMatrix3D proj;
proj.SetOrtho(0.f, xres, 0.f, yres, -1.f, 1000.f);
m_Transform = proj * m_Transform;
m_Dirty = true;
}
CMatrix3D CTextRenderer::GetTransform()
{
return m_Transform;
}
void CTextRenderer::SetTransform(const CMatrix3D& transform)
{
m_Transform = transform;
m_Translate = translate;
m_Dirty = true;
}
void CTextRenderer::Translate(float x, float y)
{
CMatrix3D m;
m.SetTranslation(x, y, 0.0f);
m_Transform = m_Transform * m;
m_Translate += CVector2D{x, y};
m_Dirty = true;
}
@ -179,7 +157,7 @@ void CTextRenderer::PutString(float x, float y, const std::wstring* buf, bool ow
{
SBatch batch;
batch.chars = 0;
batch.transform = m_Transform;
batch.translate = m_Translate;
batch.color = m_Color;
batch.font = m_Font;
m_Batches.push_back(batch);
@ -212,24 +190,24 @@ struct SBatchCompare
return true;
if (b.font < a.font)
return false;
// TODO: is it worth sorting by color/transform too?
// TODO: is it worth sorting by color/translate too?
return false;
}
};
void CTextRenderer::Render(const CShaderProgramPtr& shader)
void CTextRenderer::Render(const CShaderProgramPtr& shader, const CMatrix3D& transform)
{
std::vector<u16> indexes;
std::vector<t2f_v2i> vertexes;
// Try to merge non-consecutive batches that share the same font/color/transform:
// Try to merge non-consecutive batches that share the same font/color/translate:
// sort the batch list by font, then merge the runs of adjacent compatible batches
m_Batches.sort(SBatchCompare());
for (std::list<SBatch>::iterator it = m_Batches.begin(); it != m_Batches.end(); )
{
std::list<SBatch>::iterator next = it;
++next;
if (next != m_Batches.end() && it->font == next->font && it->color == next->color && it->transform == next->transform)
if (next != m_Batches.end() && it->font == next->font && it->color == next->color && it->translate == next->translate)
{
it->chars += next->chars;
it->runs.splice(it->runs.end(), next->runs);
@ -252,7 +230,9 @@ void CTextRenderer::Render(const CShaderProgramPtr& shader)
shader->BindTexture(str_tex, batch.font->GetTexture());
}
shader->Uniform(str_transform, batch.transform);
CMatrix3D translation;
translation.SetTranslation(batch.translate.X, batch.translate.Y, 0.0f);
shader->Uniform(str_transform, transform * translation);
// ALPHA-only textures will have .rgb sampled as 0, so we need to
// replace it with white (but not affect RGBA textures)

View File

@ -20,13 +20,14 @@
#include "graphics/Color.h"
#include "graphics/ShaderProgramPtr.h"
#include "maths/Matrix3D.h"
#include "maths/Rect.h"
#include "maths/Vector2D.h"
#include "ps/CStrIntern.h"
#include <list>
class CFont;
class CMatrix3D;
class CTextRenderer
{
@ -34,14 +35,11 @@ public:
CTextRenderer();
/**
* Reset the text transform to the default, with (0,0) in the top-left of the screen.
* Reset the text transform to the default, with (0,0) in the top-left corner.
*/
void ResetTransform();
CMatrix3D GetTransform();
void SetTransform(const CMatrix3D& transform);
void ResetTranslate(const CVector2D& translate = CVector2D{});
const CVector2D& GetTranslate() const { return m_Translate; }
void Translate(float x, float y);
/**
@ -104,7 +102,7 @@ public:
/**
* Render all of the previously printed text calls.
*/
void Render(const CShaderProgramPtr& shader);
void Render(const CShaderProgramPtr& shader, const CMatrix3D& transform);
private:
friend struct SBatchCompare;
@ -151,7 +149,7 @@ private:
struct SBatch
{
size_t chars; // sum of runs[i].text->size()
CMatrix3D transform;
CVector2D translate;
CColor color;
std::shared_ptr<CFont> font;
std::list<SBatchRun> runs;
@ -159,9 +157,7 @@ private:
void PutString(float x, float y, const std::wstring* buf, bool owned);
CShaderProgramPtr m_Shader;
CMatrix3D m_Transform;
CVector2D m_Translate;
CRect m_Clipping;
CColor m_Color;

View File

@ -1433,7 +1433,7 @@ void CInput::Draw(CCanvas2D& canvas)
if (m_MultiLine && buffered_y + m_BufferZone > m_CachedActualSize.GetHeight())
break;
CMatrix3D savedTransform = textRenderer.GetTransform();
const CVector2D savedTranslate = textRenderer.GetTranslate();
// Text must always be drawn in integer values. So we have to convert scroll
if (m_MultiLine)
@ -1504,7 +1504,7 @@ void CInput::Draw(CCanvas2D& canvas)
textRenderer.SetCurrentColor(m_TextColorSelected);
}
textRenderer.SetTransform(savedTransform);
textRenderer.ResetTranslate(savedTranslate);
}
textRenderer.Translate(0.f, ls);

View File

@ -190,11 +190,9 @@ void CConsole::Render()
CTextRenderer textRenderer;
textRenderer.SetCurrentFont(CStrIntern(CONSOLE_FONT));
// animation: slide in from top of screen
CMatrix3D transform = GetDefaultGuiMatrix();
// Animation: slide in from top of screen.
const float DeltaY = (1.0f - m_fVisibleFrac) * m_fHeight;
transform.PostTranslate(m_fX, m_fY - DeltaY, 0.0f); // move to window position
textRenderer.SetTransform(transform);
textRenderer.Translate(m_fX, m_fY - DeltaY);
DrawHistory(textRenderer);
DrawBuffer(textRenderer);
@ -257,7 +255,7 @@ void CConsole::DrawBuffer(CTextRenderer& textRenderer)
if (m_fHeight < m_iFontHeight)
return;
CMatrix3D savedTransform = textRenderer.GetTransform();
const CVector2D savedTranslate = textRenderer.GetTranslate();
textRenderer.Translate(2.0f, m_fHeight - (float)m_iFontOffset + 1.0f);
@ -276,7 +274,7 @@ void CConsole::DrawBuffer(CTextRenderer& textRenderer)
DrawCursor(textRenderer);
}
textRenderer.SetTransform(savedTransform);
textRenderer.ResetTranslate(savedTranslate);
}
void CConsole::DrawCursor(CTextRenderer& textRenderer)

View File

@ -242,14 +242,14 @@ void CLogger::Render()
textRenderer.SetCurrentColor(CColor(1.0f, 0.0f, 0.0f, 1.0f));
}
CMatrix3D savedTransform = textRenderer.GetTransform();
const CVector2D savedTranslate = textRenderer.GetTranslate();
textRenderer.PrintfAdvance(L"[%8.3f] %hs: ", msg.time, type);
// Display the actual message in white so it's more readable
textRenderer.SetCurrentColor(CColor(1.0f, 1.0f, 1.0f, 1.0f));
textRenderer.Put(0.0f, 0.0f, msg.message.c_str());
textRenderer.SetTransform(savedTransform);
textRenderer.ResetTranslate(savedTranslate);
textRenderer.Translate(0.0f, (float)lineSpacing);
}