From 68dcd3c4e17355e1c2b640fe1382743d7cb61ea2 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Sat, 28 Apr 2018 17:15:24 +0200 Subject: Use font pointer instead of copy to reduce memory usage, sfml ffs --- src/ChannelSidePanel.cpp | 6 +++--- src/Chatbar.cpp | 2 +- src/MessageBoard.cpp | 8 ++++---- src/ResourceCache.cpp | 6 +++--- src/Text.cpp | 16 ++++++++-------- src/UsersSidePanel.cpp | 6 +++--- src/main.cpp | 1 + 7 files changed, 23 insertions(+), 22 deletions(-) (limited to 'src') diff --git a/src/ChannelSidePanel.cpp b/src/ChannelSidePanel.cpp index eb5c74c..f5a4063 100644 --- a/src/ChannelSidePanel.cpp +++ b/src/ChannelSidePanel.cpp @@ -32,17 +32,17 @@ namespace dchat rect.setFillColor(sf::Color(30, 30, 30)); window.draw(rect); - const sf::Font &font = ResourceCache::getFont("fonts/Roboto-Regular.ttf"); + const sf::Font *font = ResourceCache::getFont("fonts/Roboto-Regular.ttf"); sf::Vector2f position(10.0f, 10.0f); for(Channel *channel : channels) { // TODO: Remove this shit sf::String str = "# "; str += sf::String::fromUtf8(channel->getName().begin(), channel->getName().end()); - sf::Text text(str, font, FONT_SIZE * Settings::getScaling()); + sf::Text text(str, *font, FONT_SIZE * Settings::getScaling()); text.setPosition(position); window.draw(text); - position.y += font.getLineSpacing(FONT_SIZE * Settings::getScaling()); + position.y += font->getLineSpacing(FONT_SIZE * Settings::getScaling()); } } diff --git a/src/Chatbar.cpp b/src/Chatbar.cpp index 241a808..8c4083b 100644 --- a/src/Chatbar.cpp +++ b/src/Chatbar.cpp @@ -21,7 +21,7 @@ namespace dchat const float PADDING_SIDE = 20.0f; Chatbar::Chatbar() : - text("", ResourceCache::getFont("fonts/Roboto-Regular.ttf"), FONT_SIZE * Settings::getScaling()), + text("", *ResourceCache::getFont("fonts/Roboto-Regular.ttf"), FONT_SIZE * Settings::getScaling()), caretIndex(0), focused(true) { diff --git a/src/MessageBoard.cpp b/src/MessageBoard.cpp index a0d1ab2..a0f1e20 100644 --- a/src/MessageBoard.cpp +++ b/src/MessageBoard.cpp @@ -40,8 +40,8 @@ namespace dchat void MessageBoard::updateStaticContentTexture(const sf::Vector2u &newSize) { - if(!staticContentTexture.create(newSize.x, newSize.y)) - throw std::runtime_error("Failed to create render target for message board!"); + //if(!staticContentTexture.create(newSize.x, newSize.y)) + // throw std::runtime_error("Failed to create render target for message board!"); dirty = true; } @@ -110,7 +110,7 @@ namespace dchat //if(dirty) // staticContentTexture.clear(BACKGROUND_COLOR); - const sf::Font &usernameFont = ResourceCache::getFont("fonts/Roboto-Regular.ttf"); + const sf::Font *usernameFont = ResourceCache::getFont("fonts/Roboto-Regular.ttf"); double deltaTimeMicro = (double)frameTimer.getElapsedTime().asMicroseconds(); frameTimer.restart(); @@ -125,7 +125,7 @@ namespace dchat position.y += scroll; for(Message *message : messages) { - sf::Text usernameText(message->user->getName(), usernameFont, 24 * Settings::getScaling()); + sf::Text usernameText(message->user->getName(), *usernameFont, 24 * Settings::getScaling()); float usernameTextHeight = usernameText.getFont()->getLineSpacing(usernameText.getCharacterSize()); if(position.y + usernameTextHeight > 0.0f && position.y < backgroundSize.y) { diff --git a/src/ResourceCache.cpp b/src/ResourceCache.cpp index ab583bb..3f73189 100644 --- a/src/ResourceCache.cpp +++ b/src/ResourceCache.cpp @@ -8,11 +8,11 @@ namespace dchat unordered_map fonts; unordered_map textures; - const sf::Font& ResourceCache::getFont(const string &filepath) + const sf::Font* ResourceCache::getFont(const string &filepath) { auto it = fonts.find(filepath); if(it != fonts.end()) - return *it->second; + return it->second; sf::Font *font = new sf::Font(); if(!font->loadFromFile(filepath)) @@ -24,7 +24,7 @@ namespace dchat } fonts[filepath] = font; - return *font; + return font; } sf::Texture* ResourceCache::getTexture(const string &filepath) diff --git a/src/Text.cpp b/src/Text.cpp index fa46277..c8da55c 100644 --- a/src/Text.cpp +++ b/src/Text.cpp @@ -11,7 +11,7 @@ namespace dchat const float EMOJI_SCALE_WITH_TEXT = 1.0f; const float EMOJI_SCALE_STANDALONE = 5.0f; - Text::Text(const sf::Font &_font) : + Text::Text(const sf::Font *_font) : font(_font), characterSize(0), maxWidth(0.0f), @@ -23,7 +23,7 @@ namespace dchat } - Text::Text(const sf::String &_str, const sf::Font &_font, unsigned int _characterSize, float _maxWidth, bool _plainText) : + Text::Text(const sf::String &_str, const sf::Font *_font, unsigned int _characterSize, float _maxWidth, bool _plainText) : font(_font), characterSize(_characterSize), vertices(sf::PrimitiveType::Quads), @@ -150,8 +150,8 @@ namespace dchat void Text::updateGeometry() { vertices.clear(); - float hspace = font.getGlyph(' ', characterSize, false).advance; - float vspace = font.getLineSpacing(characterSize); + float hspace = font->getGlyph(' ', characterSize, false).advance; + float vspace = font->getLineSpacing(characterSize); sf::Vector2f glyphPos; sf::Uint32 prevCodePoint = 0; @@ -215,7 +215,7 @@ namespace dchat for(size_t i = 0; i < textElement.text.size; ++i) { sf::Uint32 codePoint = textElement.text[i]; - float kerning = font.getKerning(prevCodePoint, codePoint, characterSize); + float kerning = font->getKerning(prevCodePoint, codePoint, characterSize); prevCodePoint = codePoint; glyphPos.x += kerning; @@ -254,7 +254,7 @@ namespace dchat } } - const sf::Glyph &glyph = font.getGlyph(codePoint, characterSize, false); + const sf::Glyph &glyph = font->getGlyph(codePoint, characterSize, false); if(glyphPos.x + glyph.advance > maxWidth) { // If there was a space in the text and text width is too long, then we need to word wrap at space index instead, @@ -311,7 +311,7 @@ namespace dchat dirty = false; } - float vspace = font.getLineSpacing(characterSize); + float vspace = font->getLineSpacing(characterSize); sf::RenderStates states; sf::Vector2f pos = position; @@ -325,7 +325,7 @@ namespace dchat if(pos.y + totalHeight <= 0.0f || pos.y >= target.getSize().y) return; states.transform.translate(pos); - states.texture = &font.getTexture(characterSize); + states.texture = &font->getTexture(characterSize); target.draw(vertices, states); for(TextElement &textElement : textElements) diff --git a/src/UsersSidePanel.cpp b/src/UsersSidePanel.cpp index 39c0b86..766539b 100644 --- a/src/UsersSidePanel.cpp +++ b/src/UsersSidePanel.cpp @@ -35,16 +35,16 @@ namespace dchat if(!currentChannel) return; - const sf::Font &font = ResourceCache::getFont("fonts/Roboto-Regular.ttf"); + const sf::Font *font = ResourceCache::getFont("fonts/Roboto-Regular.ttf"); sf::Vector2f position(rect.getPosition().x + 10.0f, 10.0f); for(User *user : currentChannel->getUsers()) { // TODO: Remove this shit sf::String str = sf::String::fromUtf8(user->getName().begin(), user->getName().end()); - sf::Text text(str, font, FONT_SIZE * Settings::getScaling()); + sf::Text text(str, *font, FONT_SIZE * Settings::getScaling()); text.setPosition(position); window.draw(text); - position.y += font.getLineSpacing(FONT_SIZE * Settings::getScaling()); + position.y += font->getLineSpacing(FONT_SIZE * Settings::getScaling()); } } diff --git a/src/main.cpp b/src/main.cpp index 6f54820..61319fa 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -249,6 +249,7 @@ int main(int argc, char **argv) odhtdb::DatabaseNode databaseNode = createDatabaseNodeFromJoinKey(args[0]); database.seed(databaseNode); + // TODO: Continue this... }); sf::Event event; -- cgit v1.2.3