From 746687800ff205b22316157c86c8b816307d3aa7 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Sun, 20 May 2018 23:53:36 +0200 Subject: Improve performance and cpu usage Remove Text vertices if text has not been visible on the freen for a while. Stop rendering when window has lost focus for awhile, reducing cpu usage to 0 --- src/Text.cpp | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) (limited to 'src/Text.cpp') diff --git a/src/Text.cpp b/src/Text.cpp index 2292a4e..025cb92 100644 --- a/src/Text.cpp +++ b/src/Text.cpp @@ -31,6 +31,7 @@ namespace dchat dirtyCaret(false), plainText(false), editable(false), + visible(true), caretMoveDirection(CaretMoveDirection::NONE), caretIndex(0) { @@ -49,6 +50,7 @@ namespace dchat dirtyCaret(false), plainText(_plainText), editable(false), + visible(true), caretMoveDirection(CaretMoveDirection::NONE), lineSpacing(0.0f), caretIndex(0) @@ -906,21 +908,24 @@ namespace dchat //sf::FloatRect textRect(pos.x, pos.y, maxWidth, ) //colRect.contains() //if(pos.x + maxWidth <= 0.0f || pos.x >= maxWidth || pos.y + totalHeight <= 0.0f || pos.y >= target.getSize().y) return; - if(pos.y + getHeight() <= 0.0f || pos.y >= target.getSize().y) return false; - /* - if(editable) + if(pos.y + getHeight() <= 0.0f || pos.y >= target.getSize().y) { - sf::RectangleShape editBox(sf::Vector2f(std::max(maxWidth, boundingBox.width), boundingBox.height)); - editBox.setPosition(pos.x, pos.y - floor(vspace)); - editBox.setFillColor(ColorScheme::getBackgroundColor() + sf::Color(10, 10, 10)); - target.draw(editBox); + if(!editable && visible && lastSeenTimer.getElapsedTime().asMilliseconds() > 3000) + { + visible = false; + vertices.resize(0); + } + return false; } - */ + if(!visible) + updateGeometry(); states.transform.translate(pos); states.texture = &font->getTexture(characterSize); target.draw(vertices, states); + lastSeenTimer.restart(); + visible = true; pos.y -= floor(vspace); for(TextElement &textElement : textElements) -- cgit v1.2.3