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

Author Topic: High Cpu Usage in Sfml 2.0 again  (Read 2557 times)

0 Members and 1 Guest are viewing this topic.

nonikel

  • Newbie
  • *
  • Posts: 13
    • View Profile
High Cpu Usage in Sfml 2.0 again
« on: December 29, 2012, 01:15:09 pm »
Hello everyone,

when i run this project my cpu usage is at 25%. Normally i think should be around 5%!
I had the same problem a few weeks ago and you helped me to fix it. There it was something with the joystick update.
It cant be the same problem because i commented out the line in the sfml code and recompiled it.

Here is the code:
#include <SFML/Graphics.hpp>

void main()
{
    sf::RenderWindow App(sf::VideoMode(1280, 720, 32), "Test");
    sf::Sprite SPlayer;
    sf::Texture TPlayer;
        sf::Clock clock;

        sf::Time ElapsedTime;

    TPlayer.loadFromFile("tank1.png");
    SPlayer.setTexture(TPlayer);
       
        App.setVerticalSyncEnabled(true);

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

                if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
                {
                        SPlayer.move(-50 * ElapsedTime.asSeconds(), 0);
                }
                if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
                {
                        SPlayer.move(50 * ElapsedTime.asSeconds(), 0);
                }
                if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
                {
                        SPlayer.move(0, -50 * ElapsedTime.asSeconds());
                }
                if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
                {
                        SPlayer.move(0, 50 * ElapsedTime.asSeconds());
                }

        App.clear();

        App.draw(SPlayer);

        App.display();
    }
}

And here is an image of the output from very sleepy:
http://imgur.com/T8vWe

My Pc:
Intel Core i5 2500k
gtx 560 ti

Im using sfml 2.0. Its the build i have recompiled to fix the old problem.

Hope someone can help me :)

MorleyDev

  • Full Member
  • ***
  • Posts: 219
  • "It is not enough for code to work."
    • View Profile
    • http://www.morleydev.co.uk/
Re: High Cpu Usage in Sfml 2.0 again
« Reply #1 on: December 29, 2012, 01:39:13 pm »
You need to release the CPU in any looping program, or it'll spin as fast as it can and eat the whole CPU. This is not a fault of SFML.

Basically Fix Your Timestep and sleep for any time left over. For sleeping, look at sf::Sleep.
« Last Edit: December 29, 2012, 01:41:33 pm by MorleyDev »
UnitTest11 - A unit testing library in C++ written to take advantage of C++11.

All code is guilty until proven innocent, unworthy until tested, and pointless without singular and well-defined purpose.

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: High Cpu Usage in Sfml 2.0 again
« Reply #2 on: December 29, 2012, 06:33:44 pm »
You can use setFramerateLimit, that's what it's for, while(1) seems to consume 100/#of Cores % of cpu no matter what.
Back to C++ gamedev with SFML in May 2023

nonikel

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: High Cpu Usage in Sfml 2.0 again
« Reply #3 on: December 30, 2012, 10:56:24 am »
Thx for your help. :)
When i use this in a Singleplayer game it works pretty well. For example i sleep in the beginning of  loop for 10 milliseconds.
But when i use this in a Multiplayer game the Multiplayer is lagging. A movement from a character needs 2 seconds do go to the other player.
I set the sleep time to 1, 2 and 5 milliseconds but it doesnt helped. Have you any idea how i should do this with network stuff? :)
I know that sleeping every loop a constant time isnt perfect but i wanted to test it fast.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10913
    • View Profile
    • development blog
    • Email
AW: High Cpu Usage in Sfml 2.0 again
« Reply #4 on: December 30, 2012, 12:08:17 pm »
Are you talking about network latency now? Because that doesn't have much to do with the framerate itself...

Also don't use 'magic' sleep numbers, you should rather use a sf::Clock to get the elapsed time and if after all the calculations/updating/drawing there's still some time left for your usual frametime (i.e 1/fps) than you can sleep for the left over time.

For networking you'll probably have to do some interpolation for the other players to get a smoother movement.
« Last Edit: December 30, 2012, 12:10:12 pm by eXpl0it3r »
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

 

anything