Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: [Android] SFML Application runs very slow  (Read 2574 times)

0 Members and 1 Guest are viewing this topic.

Cadisol87

  • Full Member
  • ***
  • Posts: 129
  • C++ Programmer
    • View Profile
[Android] SFML Application runs very slow
« on: December 30, 2014, 01:46:37 pm »
A few days ago I compiled SFML 2.2 for Android and already made a few small Apps. Now I'm trying to create something bigger, but there's a problem: The application needs a relatively long time to start up and react, for example when I rotate the phone. I think my phone is fast enough to run the App smoothly( I have an OnePlus One with a Qualcomm Snapdragon 801 and 3GB RAM), and other Apps created with SFML (For example Kroniax) run smoothly. If I compile the code on my PC, it runs fast, so I guess my code is just very resource-heavy.
The following code reproduces the problem:
#include <SFML/Graphics.hpp>

inline void adjustCharacterSize(sf::Text& text, const sf::Vector2u& windowSize, unsigned short factor)
{
        while(text.getGlobalBounds().width < windowSize.x - windowSize.x / factor && text.getCharacterSize() < 500)
                text.setCharacterSize(text.getCharacterSize() + 1);
                 
        while(text.getGlobalBounds().width > windowSize.x - windowSize.x / factor && text.getCharacterSize() > 10)
                  text.setCharacterSize(text.getCharacterSize() - 1);

}


int main(int argc, char *argv[])
{
    sf::RenderWindow window(sf::VideoMode::getDesktopMode(), "", sf::Style::Fullscreen);
    window.setVerticalSyncEnabled(true);

    sf::Font font;
    font.loadFromFile("Lato-Light.ttf");

    sf::Text text;
    text.setFont(font);
    text.setString("Hello World!");
    text.setColor(sf::Color::Green);
    text.setCharacterSize(100);
    adjustCharacterSize(text, window.getSize(), 4);
    text.setPosition(150, 150);

    sf::View view = window.getDefaultView();


    while (window.isOpen())
    {

        sf::Event event;

        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
            {
                window.close();
            }

            if (event.type == sf::Event::Resized)
            {
                view.setSize(event.size.width, event.size.height);
                view.setCenter(event.size.width/2, event.size.height/2);
                adjustCharacterSize(text, window.getSize(), 4);
                window.setView(view);
            }
        }


        window.clear();
        window.draw(text);
        window.display();
    }
}
 
I already found out that the function  'adjustCharacterSize' seems to be responsible for the slow behavior, but I don't know why. I modified the function several times, but with no success.
Any ideas oft what I could do?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: [Android] SFML Application runs very slow
« Reply #1 on: December 30, 2014, 02:06:14 pm »
Changing the text size and then requesting it's bounding rectangle will force sf::Text to reconstruct its internal vertex array every time. So yes this can be pretty heavy on a smartphone.

Instead of a sequential search you could do a binary search, that would give you log(N) iterations instead of N.

You can also try to start directly from a "close enough" size, by calculating the ratio between the original bounding rectangle and the desired one, and applying it to the initial size. That would also limit the search to only a few sizes.
Laurent Gomila - SFML developer

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: [Android] SFML Application runs very slow
« Reply #2 on: December 30, 2014, 02:56:18 pm »
Probably just stating the obvious, but make sure you are building SFML and your app in release mode with optimization enabled rather than unoptimized debug mode.

Cadisol87

  • Full Member
  • ***
  • Posts: 129
  • C++ Programmer
    • View Profile
Re: [Android] SFML Application runs very slow
« Reply #3 on: December 30, 2014, 06:56:18 pm »
@Laurent: Thanks for the tips! I will try to implement them tomorrow.

@Jesper Juhl: I did build them in release mode, but thanks for the answer. I wouldn't have been surprised if I built them in debug mode by accident...

 

anything