From e5758d047e48c79bdf894365921d216fac62affc Mon Sep 17 00:00:00 2001 From: Leon Styhre <leon@leonstyhre.com> Date: Sat, 29 Jul 2023 18:41:43 +0200 Subject: [PATCH] Improved menu system font rendering on GPUs without proper texture filtering support --- es-core/src/components/ComponentGrid.cpp | 4 ++-- es-core/src/components/ComponentList.cpp | 2 +- es-core/src/guis/GuiInfoPopup.cpp | 20 ++++++++++---------- es-core/src/guis/GuiMsgBox.cpp | 11 +++++------ 4 files changed, 18 insertions(+), 19 deletions(-) diff --git a/es-core/src/components/ComponentGrid.cpp b/es-core/src/components/ComponentGrid.cpp index 98de624d6..1b3e3a4bb 100644 --- a/es-core/src/components/ComponentGrid.cpp +++ b/es-core/src/components/ComponentGrid.cpp @@ -60,7 +60,7 @@ float ComponentGrid::getRowHeight(int row) assert(row >= 0 && row < mGridSize.y); if (mRowHeights[row] != 0) - return mRowHeights[row] * mSize.y; + return std::round(mRowHeights[row] * mSize.y); // Calculate automatic height. float freeHeightPerc {1.0f}; @@ -71,7 +71,7 @@ float ComponentGrid::getRowHeight(int row) ++between; } - return (freeHeightPerc * mSize.y) / static_cast<float>(between); + return std::round((freeHeightPerc * mSize.y) / static_cast<float>(between)); } void ComponentGrid::setColWidthPerc(int col, float width, bool update) diff --git a/es-core/src/components/ComponentList.cpp b/es-core/src/components/ComponentList.cpp index 58e0f7b96..84e5e2ca8 100644 --- a/es-core/src/components/ComponentList.cpp +++ b/es-core/src/components/ComponentList.cpp @@ -433,7 +433,7 @@ void ComponentList::updateElementPosition(const ComponentListRow& row) const auto comp = row.elements.at(i).component; // Center vertically. - comp->setPosition(x, (mRowHeight - comp->getSize().y) / 2.0f + yOffset); + comp->setPosition(x, (mRowHeight - std::floor(comp->getSize().y)) / 2.0f + yOffset); x += comp->getSize().x; } } diff --git a/es-core/src/guis/GuiInfoPopup.cpp b/es-core/src/guis/GuiInfoPopup.cpp index 51c5cc3f0..d8842e738 100644 --- a/es-core/src/guis/GuiInfoPopup.cpp +++ b/es-core/src/guis/GuiInfoPopup.cpp @@ -44,13 +44,13 @@ GuiInfoPopup::GuiInfoPopup(std::string message, int duration) } // Add a padding to the box. - int paddingX = static_cast<int>(Renderer::getScreenWidth() * 0.03f); - int paddingY = static_cast<int>(Renderer::getScreenHeight() * 0.02f); - mSize.x = mSize.x + paddingX; - mSize.y = mSize.y + paddingY; + int paddingX {static_cast<int>(Renderer::getScreenWidth() * 0.03f)}; + int paddingY {static_cast<int>(Renderer::getScreenHeight() * 0.02f)}; + mSize.x += paddingX; + mSize.y += paddingY; - float posX = Renderer::getScreenWidth() * 0.5f - mSize.x * 0.5f; - float posY = Renderer::getScreenHeight() * 0.02f; + float posX {Renderer::getScreenWidth() * 0.5f - mSize.x * 0.5f}; + float posY {Renderer::getScreenHeight() * 0.02f}; setPosition(posX, posY, 0); @@ -61,9 +61,9 @@ GuiInfoPopup::GuiInfoPopup(std::string message, int duration) // We only initialize the actual time when we first start to render. mStartTime = 0; - mGrid = new ComponentGrid(glm::ivec2 {1, 3}); + mGrid = new ComponentGrid(glm::ivec2 {1, 1}); mGrid->setSize(mSize); - mGrid->setEntry(s, glm::ivec2 {0, 1}, false, true); + mGrid->setEntry(s, glm::ivec2 {0, 0}, false, true); addChild(mGrid); } @@ -76,7 +76,7 @@ GuiInfoPopup::~GuiInfoPopup() void GuiInfoPopup::render(const glm::mat4& /*parentTrans*/) { // We use getIdentity() as we want to render on a specific window position, not on the view. - glm::mat4 trans {getTransform() * Renderer::getIdentity()}; + const glm::mat4 trans {getTransform() * Renderer::getIdentity()}; if (mRunning && updateState()) { // If we're still supposed to be rendering it. mRenderer->setMatrix(trans); @@ -86,7 +86,7 @@ void GuiInfoPopup::render(const glm::mat4& /*parentTrans*/) bool GuiInfoPopup::updateState() { - int curTime = SDL_GetTicks(); + const int curTime {static_cast<int>(SDL_GetTicks())}; // We only initialize the actual time when we first start to render. if (mStartTime == 0) diff --git a/es-core/src/guis/GuiMsgBox.cpp b/es-core/src/guis/GuiMsgBox.cpp index 478308f47..9c60e7638 100644 --- a/es-core/src/guis/GuiMsgBox.cpp +++ b/es-core/src/guis/GuiMsgBox.cpp @@ -89,14 +89,14 @@ GuiMsgBox::GuiMsgBox(const HelpStyle& helpstyle, width = mButtonGrid->getSize().x; } - // Now that we know width, we can find height. + // Now that we know the width, we can calculate the height. mMsg->setSize(width, 0.0f); // mMsg->getSize.y() now returns the proper length. const float msgHeight {std::max(Font::get(FONT_SIZE_LARGE)->getHeight(), mMsg->getSize().y * VERTICAL_PADDING_MODIFIER)}; - setSize(width + std::ceil(HORIZONTAL_PADDING_PX * 2.0f * mRenderer->getScreenWidthModifier()), - msgHeight + mButtonGrid->getSize().y); + setSize(std::round(width + std::ceil(HORIZONTAL_PADDING_PX * 2.0f * + mRenderer->getScreenWidthModifier())), + std::round(msgHeight + mButtonGrid->getSize().y)); - // Center for good measure. setPosition((mRenderer->getScreenWidth() - mSize.x) / 2.0f, (mRenderer->getScreenHeight() - mSize.y) / 2.0f); @@ -133,7 +133,7 @@ void GuiMsgBox::changeText(const std::string& newText) width = mButtonGrid->getSize().x; } - // Now that we know width, we can find height. + // Now that we know the width, we can calculate the height. mMsg->setSize(width, 0.0f); // mMsg->getSize.y() now returns the proper height. newSize = mMsg->getSize(); newSize.y *= VERTICAL_PADDING_MODIFIER; @@ -169,7 +169,6 @@ void GuiMsgBox::onSizeChanged() mGrid.setSize(mSize); mGrid.setRowHeightPerc(1, mButtonGrid->getSize().y / mSize.y); - // Update messagebox size. mMsg->setSize(mSize.x - HORIZONTAL_PADDING_PX * 2.0f * Renderer::getScreenWidthModifier(), mGrid.getRowHeight(0)); mGrid.onSizeChanged();