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 --- include/Text.hpp | 3 +++ src/Text.cpp | 21 +++++++++++++-------- src/main.cpp | 18 +++++++++++++++--- 3 files changed, 31 insertions(+), 11 deletions(-) diff --git a/include/Text.hpp b/include/Text.hpp index 925dc94..286a0b1 100644 --- a/include/Text.hpp +++ b/include/Text.hpp @@ -7,6 +7,7 @@ #include #include #include +#include #include namespace dchat @@ -98,6 +99,7 @@ namespace dchat bool dirtyCaret; bool plainText; bool editable; + bool visible; CaretMoveDirection caretMoveDirection; sf::FloatRect boundingBox; float lineSpacing; @@ -105,5 +107,6 @@ namespace dchat int caretIndex; sf::Vector2f caretPosition; + sf::Clock lastSeenTimer; }; } 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) diff --git a/src/main.cpp b/src/main.cpp index 4440315..cf916e2 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -117,11 +117,11 @@ int main(int argc, char **argv) boost::filesystem::current_path(parentPath); // Ensures loading of resources works no matter which path we run this executable from */ - const int FRAMERATE_FOCUSED = 200; - const int FRAMERATE_NOT_FOCUSED = 30; + const sf::Int64 FRAMERATE_FOCUSED = 144; + const sf::Int64 FRAMERATE_NOT_FOCUSED = 10; XInitThreads(); - sf::RenderWindow window(sf::VideoMode(1920, 1080), "dchat"); + sf::RenderWindow window(sf::VideoMode(1280, 768), "dchat"); window.setVerticalSyncEnabled(false); window.setFramerateLimit(FRAMERATE_FOCUSED); @@ -659,8 +659,12 @@ int main(int argc, char **argv) Channel::getCurrent()->addLocalMessage(msg, Channel::getCurrent()->getSystemUser()); }); + sf::Clock lastFocusedTimer; + sf::Clock frameTimer; + while (window.isOpen()) { + frameTimer.restart(); Channel *currentChannel = Channel::getCurrent(); sf::Event event; @@ -702,8 +706,16 @@ int main(int argc, char **argv) GlobalContextMenu::processEvent(event); currentChannel->processEvent(event, cache); } + + lastFocusedTimer.restart(); } + if(lastFocusedTimer.getElapsedTime().asMilliseconds() > 3000) + { + this_thread::sleep_for(chrono::seconds(1)); + continue; + } + window.clear(ColorScheme::getBackgroundColor()); ChannelSidePanel::draw(window); currentChannel->draw(window, cache); -- cgit v1.2.3