1
General discussions / Is better sf::sleep(sf::milliseconds()); or window.setFramelimit(); ?
« on: April 06, 2024, 08:31:45 pm »
Hello everyone, I'm trying to understand delta time once again, and with this code:
Is better to use sleep or setFramelimit? what are the differences in this case? Btw when I use none of them, my cpu usage is high.
#include <chrono>
#include <SFML/Graphics.hpp>
int main() {
constexpr unsigned char MAX_FPS = 60;
constexpr std::chrono::microseconds FRAME_DURATION(1000000 / MAX_FPS);
std::chrono::steady_clock::time_point previous_time;
std::chrono::microseconds lag(0);
sf::Event event;
sf::RenderWindow window(sf::VideoMode(500, 500), "SFML", sf::Style::Close);
//window.setFramerateLimit(MAX_FPS);
previous_time = std::chrono::steady_clock::now();
while (window.isOpen()) {
std::chrono::microseconds dt = std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::steady_clock::now() - previous_time);
lag += dt;
previous_time += dt;
while (FRAME_DURATION <= lag) {
lag -= FRAME_DURATION;
while (window.pollEvent(event)) {
switch (event.type) {
case sf::Event::Closed: {
window.close();
break;
}
}
}
if (FRAME_DURATION > lag) {
window.clear();
window.display();
//sf::sleep(sf::milliseconds(50));
}
}
}
}
#include <SFML/Graphics.hpp>
int main() {
constexpr unsigned char MAX_FPS = 60;
constexpr std::chrono::microseconds FRAME_DURATION(1000000 / MAX_FPS);
std::chrono::steady_clock::time_point previous_time;
std::chrono::microseconds lag(0);
sf::Event event;
sf::RenderWindow window(sf::VideoMode(500, 500), "SFML", sf::Style::Close);
//window.setFramerateLimit(MAX_FPS);
previous_time = std::chrono::steady_clock::now();
while (window.isOpen()) {
std::chrono::microseconds dt = std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::steady_clock::now() - previous_time);
lag += dt;
previous_time += dt;
while (FRAME_DURATION <= lag) {
lag -= FRAME_DURATION;
while (window.pollEvent(event)) {
switch (event.type) {
case sf::Event::Closed: {
window.close();
break;
}
}
}
if (FRAME_DURATION > lag) {
window.clear();
window.display();
//sf::sleep(sf::milliseconds(50));
}
}
}
}
Is better to use sleep or setFramelimit? what are the differences in this case? Btw when I use none of them, my cpu usage is high.