From 2cc671fd365141e278421b9b19db3173ec56a584 Mon Sep 17 00:00:00 2001 From: vladislavbelov Date: Sun, 30 May 2021 19:10:10 +0000 Subject: [PATCH] Makes 2D text drawing via Canvas2D. This was SVN commit r25607. --- source/graphics/Canvas2D.cpp | 21 +++++++++++++++++++++ source/graphics/Canvas2D.h | 6 ++++++ source/graphics/TextRenderer.cpp | 2 +- source/gui/CGUIText.cpp | 10 ++-------- source/gui/ObjectTypes/CInput.cpp | 10 ++-------- source/ps/CConsole.cpp | 18 ++++-------------- source/ps/CConsole.h | 3 ++- source/ps/CLogger.cpp | 12 ++++-------- source/ps/ProfileViewer.cpp | 13 +------------ source/renderer/TerrainRenderer.cpp | 8 +++----- 10 files changed, 46 insertions(+), 57 deletions(-) diff --git a/source/graphics/Canvas2D.cpp b/source/graphics/Canvas2D.cpp index b542b61f21..4618c8eb00 100644 --- a/source/graphics/Canvas2D.cpp +++ b/source/graphics/Canvas2D.cpp @@ -21,6 +21,7 @@ #include "graphics/Color.h" #include "graphics/ShaderManager.h" +#include "graphics/TextRenderer.h" #include "graphics/TextureManager.h" #include "gui/GUIMatrix.h" #include "maths/Rect.h" @@ -161,3 +162,23 @@ void CCanvas2D::DrawTexture( DrawTextureImpl(texture, vertices, uvs, multiply, add, grayscaleFactor); } + +void CCanvas2D::DrawText(CTextRenderer& textRenderer) +{ + glEnable(GL_BLEND); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + + CShaderDefines defines; + CShaderTechniquePtr tech = g_Renderer.GetShaderManager().LoadEffect( + str_canvas2d, g_Renderer.GetSystemShaderDefines(), defines); + tech->BeginPass(); + CShaderProgramPtr shader = tech->GetShader(); + + shader->Uniform(str_grayscaleFactor, 0.0f); + + textRenderer.Render(shader); + + tech->EndPass(); + + glDisable(GL_BLEND); +} \ No newline at end of file diff --git a/source/graphics/Canvas2D.h b/source/graphics/Canvas2D.h index 1c92907f43..b2f0190585 100644 --- a/source/graphics/Canvas2D.h +++ b/source/graphics/Canvas2D.h @@ -24,6 +24,7 @@ #include class CRect; +class CTextRenderer; struct CColor; @@ -60,6 +61,11 @@ public: * destination rect without color modifications. */ void DrawTexture(CTexturePtr texture, const CRect& destination); + + /** + * Draws a text using canvas materials. + */ + void DrawText(CTextRenderer& textRenderer); }; #endif // INCLUDED_CANVAS2D diff --git a/source/graphics/TextRenderer.cpp b/source/graphics/TextRenderer.cpp index 95383ca79c..0ac388d7bd 100644 --- a/source/graphics/TextRenderer.cpp +++ b/source/graphics/TextRenderer.cpp @@ -264,7 +264,7 @@ void CTextRenderer::Render(const CShaderProgramPtr& shader) if (batch.font->HasRGB()) shader->Uniform(str_colorAdd, CColor(0.0f, 0.0f, 0.0f, 0.0f)); else - shader->Uniform(str_colorAdd, CColor(1.0f, 1.0f, 1.0f, 0.0f)); + shader->Uniform(str_colorAdd, CColor(batch.color.r, batch.color.g, batch.color.b, 0.0f)); shader->Uniform(str_colorMul, batch.color); diff --git a/source/gui/CGUIText.cpp b/source/gui/CGUIText.cpp index ddd27d8428..15b0abf1a2 100644 --- a/source/gui/CGUIText.cpp +++ b/source/gui/CGUIText.cpp @@ -19,8 +19,8 @@ #include "CGUIText.h" +#include "graphics/Canvas2D.h" #include "graphics/FontMetrics.h" -#include "graphics/ShaderManager.h" #include "graphics/TextRenderer.h" #include "gui/CGUI.h" #include "gui/ObjectBases/IGUIObject.h" @@ -430,10 +430,6 @@ bool CGUIText::AssembleCalls( void CGUIText::Draw(CGUI& pGUI, CCanvas2D& canvas, const CGUIColor& DefaultColor, const CVector2D& pos, CRect clipping) const { - CShaderTechniquePtr tech = g_Renderer.GetShaderManager().LoadEffect(str_gui_text); - - tech->BeginPass(); - bool isClipped = clipping != CRect(); if (isClipped) { @@ -466,13 +462,11 @@ void CGUIText::Draw(CGUI& pGUI, CCanvas2D& canvas, const CGUIColor& DefaultColor textRenderer.Put(floorf(pos.X + tc.m_Pos.X), floorf(pos.Y + tc.m_Pos.Y), &tc.m_String); } - textRenderer.Render(tech->GetShader()); + canvas.DrawText(textRenderer); for (const SSpriteCall& sc : m_SpriteCalls) pGUI.DrawSprite(sc.m_Sprite, canvas, sc.m_Area + pos); if (isClipped) glDisable(GL_SCISSOR_TEST); - - tech->EndPass(); } diff --git a/source/gui/ObjectTypes/CInput.cpp b/source/gui/ObjectTypes/CInput.cpp index e22740c0b7..b681399544 100644 --- a/source/gui/ObjectTypes/CInput.cpp +++ b/source/gui/ObjectTypes/CInput.cpp @@ -19,8 +19,8 @@ #include "CInput.h" +#include "graphics/Canvas2D.h" #include "graphics/FontMetrics.h" -#include "graphics/ShaderManager.h" #include "graphics/TextRenderer.h" #include "gui/CGUI.h" #include "gui/CGUIScrollBarVertical.h" @@ -1264,8 +1264,6 @@ void CInput::Draw(CCanvas2D& canvas) float h = (float)font.GetHeight(); float ls = (float)font.GetLineSpacing(); - CShaderTechniquePtr tech = g_Renderer.GetShaderManager().LoadEffect(str_gui_text); - CTextRenderer textRenderer; textRenderer.Font(font_name); @@ -1427,8 +1425,6 @@ void CInput::Draw(CCanvas2D& canvas) // Setup initial color (then it might change and change back, when drawing selected area) textRenderer.Color(m_TextColor); - tech->BeginPass(); - bool using_selected_color = false; for (std::list::const_iterator it = m_CharacterPositions.begin(); @@ -1517,13 +1513,11 @@ void CInput::Draw(CCanvas2D& canvas) textRenderer.Translate(0.f, ls, 0.f); } - textRenderer.Render(tech->GetShader()); + canvas.DrawText(textRenderer); if (cliparea != CRect()) glDisable(GL_SCISSOR_TEST); - tech->EndPass(); - if (m_Caption->empty() && !m_PlaceholderText->GetRawString().empty()) DrawPlaceholderText(canvas, cliparea); diff --git a/source/ps/CConsole.cpp b/source/ps/CConsole.cpp index af6a6c8e2f..c36a42f623 100644 --- a/source/ps/CConsole.cpp +++ b/source/ps/CConsole.cpp @@ -26,7 +26,6 @@ #include "graphics/Canvas2D.h" #include "graphics/FontMetrics.h" -#include "graphics/ShaderManager.h" #include "graphics/TextRenderer.h" #include "gui/CGUI.h" #include "gui/GUIManager.h" @@ -46,7 +45,6 @@ #include "ps/Hotkey.h" #include "ps/Profile.h" #include "ps/Pyrogenesis.h" -#include "renderer/Renderer.h" #include "scriptinterface/ScriptInterface.h" #include "scriptinterface/JSON.h" @@ -186,13 +184,10 @@ void CConsole::Render() PROFILE3_GPU("console"); - glEnable(GL_BLEND); - glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + CCanvas2D canvas; - DrawWindow(); + DrawWindow(canvas); - CShaderTechniquePtr textTech = g_Renderer.GetShaderManager().LoadEffect(str_gui_text); - textTech->BeginPass(); CTextRenderer textRenderer; textRenderer.Font(CStrIntern(CONSOLE_FONT)); // animation: slide in from top of screen @@ -204,14 +199,10 @@ void CConsole::Render() DrawHistory(textRenderer); DrawBuffer(textRenderer); - textRenderer.Render(textTech->GetShader()); - - textTech->EndPass(); - - glDisable(GL_BLEND); + canvas.DrawText(textRenderer); } -void CConsole::DrawWindow() +void CConsole::DrawWindow(CCanvas2D& canvas) { std::vector points = { CVector2D{m_fWidth, 0.0f}, @@ -223,7 +214,6 @@ void CConsole::DrawWindow() for (CVector2D& point : points) point += CVector2D{m_fX, m_fY - (1.0f - m_fVisibleFrac) * m_fHeight}; - CCanvas2D canvas; canvas.DrawRect(CRect(points[1], points[3]), CColor(0.0f, 0.0f, 0.5f, 0.6f)); canvas.DrawLine(points, 1.0f, CColor(0.5f, 0.5f, 0.0f, 0.6f)); diff --git a/source/ps/CConsole.h b/source/ps/CConsole.h index 7d779967d9..83d3372094 100644 --- a/source/ps/CConsole.h +++ b/source/ps/CConsole.h @@ -31,6 +31,7 @@ #include #include +class CCanvas2D; class CTextRenderer; /** @@ -111,7 +112,7 @@ private: bool m_bCursorVisState; // if the cursor should be drawn or not double m_cursorBlinkRate; // cursor blink rate in seconds, if greater than 0.0 - void DrawWindow(); + void DrawWindow(CCanvas2D& canvas); void DrawHistory(CTextRenderer& textRenderer); void DrawBuffer(CTextRenderer& textRenderer); void DrawCursor(CTextRenderer& textRenderer); diff --git a/source/ps/CLogger.cpp b/source/ps/CLogger.cpp index d9cf2f333d..70b0b33174 100644 --- a/source/ps/CLogger.cpp +++ b/source/ps/CLogger.cpp @@ -19,17 +19,17 @@ #include "CLogger.h" +#include "graphics/Canvas2D.h" #include "graphics/FontMetrics.h" -#include "graphics/ShaderManager.h" #include "graphics/TextRenderer.h" #include "lib/os_path.h" #include "lib/timer.h" #include "lib/utf8.h" #include "ps/CConsole.h" +#include "ps/CStr.h" #include "ps/CStrInternStatic.h" #include "ps/Profile.h" #include "ps/Pyrogenesis.h" -#include "renderer/Renderer.h" #include #include @@ -212,9 +212,6 @@ void CLogger::Render() CFontMetrics font(font_name); int lineSpacing = font.GetLineSpacing(); - CShaderTechniquePtr textTech = g_Renderer.GetShaderManager().LoadEffect(str_gui_text); - textTech->BeginPass(); - CTextRenderer textRenderer; textRenderer.Font(font_name); textRenderer.Color(1.0f, 1.0f, 1.0f); @@ -257,9 +254,8 @@ void CLogger::Render() textRenderer.Translate(0.0f, (float)lineSpacing, 0.0f); } - textRenderer.Render(textTech->GetShader()); - - textTech->EndPass(); + CCanvas2D canvas; + canvas.DrawText(textRenderer); } void CLogger::PushRenderMessage(ELogMethod method, const char* message) diff --git a/source/ps/ProfileViewer.cpp b/source/ps/ProfileViewer.cpp index 7e8aae260b..dbbeb9048f 100644 --- a/source/ps/ProfileViewer.cpp +++ b/source/ps/ProfileViewer.cpp @@ -26,7 +26,6 @@ #include "graphics/Canvas2D.h" #include "graphics/FontMetrics.h" -#include "graphics/ShaderManager.h" #include "graphics/TextRenderer.h" #include "gui/GUIMatrix.h" #include "lib/external_libraries/libsdl.h" @@ -38,7 +37,6 @@ #include "ps/Hotkey.h" #include "ps/Profile.h" #include "ps/Pyrogenesis.h" -#include "renderer/Renderer.h" #include "scriptinterface/Object.h" #include @@ -164,9 +162,6 @@ void CProfileViewer::RenderProfile() PROFILE3_GPU("profile viewer"); - glEnable(GL_BLEND); - glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - AbstractProfileTable* table = m->path[m->path.size() - 1]; const std::vector& columns = table->GetColumns(); size_t numrows = table->GetNumberRows(); @@ -198,9 +193,6 @@ void CProfileViewer::RenderProfile() } // Print table and column titles. - CShaderTechniquePtr textTech = g_Renderer.GetShaderManager().LoadEffect(str_gui_text); - textTech->BeginPass(); - CTextRenderer textRenderer; textRenderer.Font(font_name); textRenderer.Color(1.0f, 1.0f, 1.0f); @@ -267,10 +259,7 @@ void CProfileViewer::RenderProfile() textRenderer.Put(0.0f, 0.0f, L"back to parent"); } - textRenderer.Render(textTech->GetShader()); - textTech->EndPass(); - - glDisable(GL_BLEND); + canvas.DrawText(textRenderer); } diff --git a/source/renderer/TerrainRenderer.cpp b/source/renderer/TerrainRenderer.cpp index 2c976a531b..5142f8b253 100644 --- a/source/renderer/TerrainRenderer.cpp +++ b/source/renderer/TerrainRenderer.cpp @@ -25,6 +25,7 @@ #include "renderer/TerrainRenderer.h" #include "graphics/Camera.h" +#include "graphics/Canvas2D.h" #include "graphics/Decal.h" #include "graphics/GameView.h" #include "graphics/LightEnv.h" @@ -614,10 +615,8 @@ void TerrainRenderer::RenderPriorities(int cullGroup) ENSURE(m->phase == Phase_Render); - CShaderTechniquePtr tech = g_Renderer.GetShaderManager().LoadEffect(str_gui_text); - tech->BeginPass(); + CCanvas2D canvas; CTextRenderer textRenderer; - textRenderer.Font(CStrIntern("mono-stroke-10")); textRenderer.Color(1.0f, 1.0f, 0.0f); @@ -625,6 +624,5 @@ void TerrainRenderer::RenderPriorities(int cullGroup) for (size_t i = 0; i < visiblePatches.size(); ++i) visiblePatches[i]->RenderPriorities(textRenderer); - textRenderer.Render(tech->GetShader()); - tech->EndPass(); + canvas.DrawText(textRenderer); }