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

Author Topic: Fps switches between 60 and 50  (Read 1230 times)

0 Members and 1 Guest are viewing this topic.

Hawy

  • Newbie
  • *
  • Posts: 8
    • View Profile
Fps switches between 60 and 50
« on: May 10, 2014, 09:02:54 pm »
Hello.
Im trying to run my main loop at a static 60 fps, but sometimes it decides to limit the fps to 50. I guess this problem could be caused by many reasons, I have checked my cpu speed and seems to be running at full clockspeed. Any ideas?

#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
#include "model.h"
#include <iostream>
int main() {
    sf::RenderWindow window(sf::VideoMode(1024, 768), "Azyrium");

    Model * model = new Model();
    sf::Clock clock;
    sf::View view2;
    view2.setSize(1920,1080);
    view2.setCenter(960,540);
    window.setView(view2);
    while (window.isOpen()) {
        clock.restart();
        sf::Event event;
        while (window.pollEvent(event)) {
            if (event.type == sf::Event::Closed) {
                window.close();
            }
            else if (event.type == sf::Event::KeyPressed || event.type == sf::Event::KeyReleased ) {
                model->handleInput(event);
            }
        }
        model->updateGame();
        window.clear();
        model->drawGame(window);
        window.display();
        while (clock.getElapsedTime().asMicroseconds() < 16667) {
            sf::sleep(sf::microseconds(16667-clock.getElapsedTime().asMicroseconds()));
            std::cout << 1/((double)(clock.getElapsedTime().asMicroseconds())/1000000 ) << std::endl;
        }

    }
    delete model;
    return 0;
}
Main loop.

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: Fps switches between 60 and 50
« Reply #1 on: May 10, 2014, 09:06:03 pm »
See this thread (and many others like it) : http://en.sfml-dev.org/forums/index.php?topic=15175.0

Update: why do you feel a need to allocate your "Model" class on the heap rather than just on the stack? If there is a good reason (I doubt it) then at least use a std::unique_ptr/std::make_unique rather than evil manual new/delete.
Your manual calculations of elapsed time and sf::sleep call would be better served by setFramerateLimit() or even better by setVerticalSyncEnabled().
« Last Edit: May 10, 2014, 09:27:55 pm by Jesper Juhl »

Hawy

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: Fps switches between 60 and 50
« Reply #2 on: May 10, 2014, 10:07:32 pm »
Thanks for your post, will try some solution with delta time instead.
I use new/delete out of habit I guess, no need to worry about about stack memory limit :)