SFML community forums

Help => General => Topic started by: nonikel on October 26, 2012, 01:33:30 pm

Title: High Cpu usage SFML 2.0
Post by: nonikel on October 26, 2012, 01:33:30 pm
Hello,

i have a problem with sfml 2.0!
On my pc and my laptop the cpu usage is very high (Pc: 25% / Laptop: 50%).
When i run the same project with a few changes with sfml 1.6 the cpu usage is normal(Pc: 4% / Laptop: 9%).
Here is the example code for SFML 2.0:
#include <SFML\Graphics.hpp>

void main()
{
        sf::Sprite SPlayer;
        sf::Texture TPlayer;
        sf::RenderWindow App(sf::VideoMode(1280, 720, 32), "Test");
        sf::Event Event;
        TPlayer.loadFromFile("test.png");
        SPlayer.setTexture(TPlayer);
        SPlayer.setPosition(500, 500);

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

                App.clear();

                App.draw(SPlayer);

                App.display();
        }
}

In sfml 1.6 i use sf::image instead of sf::texture.
On both pc and laptop my os ist windows 7 (pc 64bit and laptop 32bit).
The pc has i5 2500k cpu and gtx 560ti graphics card. The laptop has a core 2 duo t6500 with 2.1 GHz and geforce 9600m gs graphics card.
On both i use visual studio express 2010 and there are no errors or warnings.

I hope someone could help me ;)
Title: Re: High Cpu usage SFML 2.0
Post by: kimci86 on October 26, 2012, 02:06:11 pm
You have to limit framerate.
You can use setFramerateLimit (http://www.sfml-dev.org/documentation/2.0/classsf_1_1Window.php#af4322d315baf93405bf0d5087ad5e784) or setVerticalSyncEnabled (http://www.sfml-dev.org/documentation/2.0/classsf_1_1Window.php#a59041c4556e0351048f8aff366034f61).
Title: Re: High Cpu usage SFML 2.0
Post by: nonikel on October 26, 2012, 02:52:49 pm
Nothing changed! Cpu usage is as high as before!
Title: Re: High Cpu usage SFML 2.0
Post by: kimci86 on October 26, 2012, 02:56:12 pm
What code did you add exactly ?
Title: Re: High Cpu usage SFML 2.0
Post by: nonikel on October 26, 2012, 02:59:46 pm
I tested both and i added them before the loop.

App.setVerticalSyncEnabled(true);
and
App.setFramerateLimit(60);

And no i dont used both at the same time ;)
Title: Re: High Cpu usage SFML 2.0
Post by: kimci86 on October 26, 2012, 03:09:24 pm
You did it well.
I don't understand why you still have this issue.
I hope someone else can help.
Title: Re: High Cpu usage SFML 2.0
Post by: eXpl0it3r on October 26, 2012, 03:20:36 pm
So if you run the following example you still get a high CPU usage?
#include <SFML/Graphics.hpp>

void main()
{
    sf::RenderWindow App(sf::VideoMode(1280, 720, 32), "Test");
    App.setFramerateLimit(60);

    sf::Sprite SPlayer;
    sf::Texture TPlayer;

    TPlayer.loadFromFile("test.png");
    SPlayer.setTexture(TPlayer, true);

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

        App.clear();

        App.draw(SPlayer);

        App.display();
    }
}

This isn't expected...
Use a profiler to find out where most of the time is spend.

Do you have some Joystick driver installed or use a very high DPI mouse?
Title: Re: High Cpu usage SFML 2.0
Post by: Mario on October 26, 2012, 03:39:42 pm
It's also possible your display driver ignores the VSync preset (i.e. VSync is forced to be off).

Try to count the number of frames per second rendered. Then you should be able to see whether it's simply not slowed down or there's some other issue (cause your frame rate is at the expected value or even lower).
Title: Re: High Cpu usage SFML 2.0
Post by: nonikel on October 26, 2012, 03:48:58 pm
@eXpl0it3r
I used Very Sleepy to profile it.
I dont know what it mean but here is a screenshot of it: http://imageshack.us/photo/my-images/803/sleepyr.png/ (http://imageshack.us/photo/my-images/803/sleepyr.png/)

I have a logitech mx 518 mouse with 800 dpi at default and i have no Joystick.

@Mario
I have tested the framerate with fraps and it shows 60 fps.
Also i have forced VSync at my Nvidea driver.
Title: Re: High Cpu usage SFML 2.0
Post by: eXpl0it3r on October 26, 2012, 03:54:21 pm
Sounds an awful lot like this post (http://en.sfml-dev.org/forums/index.php?topic=9481.0)...

Might be a newly old bug. If you want to prevent it and don't use a joystick you could "comment out the call to Joystick::update (should be hidden under Window::pollEvent)." and then recompile SFML.
Title: Re: High Cpu usage SFML 2.0
Post by: Laurent on October 26, 2012, 04:00:03 pm
Quote
If you want to prevent it
We still don't know if it's that ;)
Title: Re: High Cpu usage SFML 2.0
Post by: eXpl0it3r on October 26, 2012, 04:14:04 pm
Quote
If you want to prevent it
We still don't know if it's that ;)
Did you take a look at the screenshot?
Here's again the most important part:
(https://legacy.sfmluploads.org/cache/pics/277_joy.png)

The question now is just why does it get called so often?
Title: Re: High Cpu usage SFML 2.0
Post by: Laurent on October 26, 2012, 04:54:48 pm
Quote
Did you take a look at the screenshot?
Here's again the most important part
I'm sorry, but... I can't find this screenshot ???
Title: Re: High Cpu usage SFML 2.0
Post by: nonikel on October 26, 2012, 05:47:31 pm
In which file is the window::pollEvent and what are cmakelist.txt files.
I have no clue how to compile sfml  ;D
Title: Re: High Cpu usage SFML 2.0
Post by: Laurent on October 26, 2012, 06:27:50 pm
Quote
In which file is the window::pollEvent
Window.cpp. But you'll probably need to go one step deeper, in WindowImpl (don't remember the name of the function).

Quote
what are cmakelist.txt files.
I have no clue how to compile sfml
That's why I've written a tutorial for that ;)
Title: Re: High Cpu usage SFML 2.0
Post by: nonikel on October 26, 2012, 06:45:28 pm
I can“t find Joystick::update in window.cpp and WindowImpl.cpp.
Should i comment out something different?
Title: Re: High Cpu usage SFML 2.0
Post by: Laurent on October 26, 2012, 06:48:31 pm
Sorry, it's
JoystickManager::getInstance().update();
at line 148 of WindowImpl.cpp.
Title: Re: High Cpu usage SFML 2.0
Post by: nonikel on October 26, 2012, 10:57:01 pm
Now everything runs fine i have 2% Cpu usage  :)
Thank you
Title: Re: High Cpu usage SFML 2.0
Post by: Laurent on October 26, 2012, 11:04:26 pm
By the way, did you have this problem with SFML 2.0 RC or with the latest sources?
Title: Re: High Cpu usage SFML 2.0
Post by: nonikel on October 26, 2012, 11:05:36 pm
With the Rc and the latest source!
Title: Re: High Cpu usage SFML 2.0
Post by: Laurent on October 26, 2012, 11:13:59 pm
Strange, I solved this problem after the RC :-\
Title: Re: High Cpu usage SFML 2.0
Post by: nonikel on October 26, 2012, 11:26:52 pm
It was LaurentGomila-SFML-2.0-rc-97-g109d914.tar
Title: Re: High Cpu usage SFML 2.0
Post by: Mario on October 27, 2012, 03:50:37 pm
Tried to reproduce this out of interest, but couldn't get it to work bug out.

I've got a Logitech Mouse (no drivers) and a Logitech Keyboard (with Logitech drivers), but CPU load sticks to 0% with or without an Xbox360 controller connected.
Title: Re: High Cpu usage SFML 2.0
Post by: Laurent on October 27, 2012, 05:53:35 pm
Quote
It was LaurentGomila-SFML-2.0-rc-97-g109d914.tar
I don't remember all commit hashes :P
From when is it?
Title: Re: High Cpu usage SFML 2.0
Post by: nonikel on October 27, 2012, 08:28:42 pm
I downloaded it on friday a few hours after i created this thread.
Title: Re: High Cpu usage SFML 2.0
Post by: Laurent on October 27, 2012, 08:53:56 pm
Ok, thanks.