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

Author Topic: [SOLVED] 100% cpu usage on linux  (Read 1631 times)

0 Members and 1 Guest are viewing this topic.

stav

  • Newbie
  • *
  • Posts: 10
    • View Profile
[SOLVED] 100% cpu usage on linux
« on: December 30, 2018, 11:32:13 pm »
Hi
I recently switched to linux ( Ubuntu 18.04 )

Before when i ran the following code on my windows machine the cpu usage would stay fairly low but now when i run it on linux my cpu usage instantly goes to 100% (which makes sense since its a never ending while loop - now that i think about it)

This is my code:
Code: [Select]
#include <SFML/Window.hpp>

int main()
{
sf::Window window(sf::VideoMode(800, 600), "My window");

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

return 0;
}

but im just curious why it didnt use 100% cpu when i ran it on my windows machine. Did i do something wrong? or is it this supposed to happen?

My theory is that on windows sfml automatically sleeps to match the screens refresh rate and for whatever reason it doesnt on linux.
Is there any way to turn this back on? i know i can decrease the cpu usage myself, by sleeping after each loop but id rather let sfml handle it if possible

I tried adding:
Code: [Select]
window.setFramerateLimit(10u);
window.setVerticalSyncEnabled(true);
but that doesn't fix it :<
« Last Edit: December 31, 2018, 01:44:16 am by stav »

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: 100% cpu usage on linux
« Reply #1 on: December 31, 2018, 12:37:39 am »
Quote
https://www.sfml-dev.org/documentation/2.5.1/classsf_1_1Window.php#af4322d315baf93405bf0d5087ad5e784
If a limit is set, the window will use a small delay after each call to display() to ensure that the current frame lasted long enough to match the framerate limit.
Call window.display() and your framerate limit should work.

stav

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: 100% cpu usage on linux
« Reply #2 on: December 31, 2018, 01:44:02 am »
thanks
im an idiot, sorry