SFML community forums

Help => General => Topic started by: zepto on November 25, 2011, 11:09:14 pm

Title: Gameloops and timing, smooth rotation
Post by: zepto on November 25, 2011, 11:09:14 pm
Goal
Move or rotate a sprite smoothly on the screen with SFML2.

Implementation
This code should rotate the SFML logo sfml-small.png (http://sfml-dev.org/logo/sfml-small.png).
Code: [Select]
#include <SFML/Graphics.hpp>

int main()
{
    sf::RenderWindow window(sf::VideoMode(800, 600), "Gameloops and timing");
    window.EnableVerticalSync(true);

    sf::Texture image;
    if (!image.LoadFromFile("sfml-small.png")) {
        return EXIT_FAILURE;
    }
    sf::Sprite sprite(image);
    sprite.Move(window.GetWidth() / 2.0, window.GetHeight() / 2.0);

    const sf::Uint32 FPS = 50;
    const sf::Uint32 SKIP_TICKS = 1000 / FPS;
    sf::Uint32 sleep_time = 0;
    sf::Clock clock;
    sf::Uint32 next_game_tick = clock.GetElapsedTime();

    while (window.IsOpened())
    {
        sf::Event event;
        while (window.PollEvent(event))
        {
            if (event.Type == sf::Event::Closed) {
                window.Close();
            }
        }

        sprite.Rotate(90.f / FPS);

        window.Clear();
        window.Draw(sprite);
        window.Display();

        next_game_tick += SKIP_TICKS;
        sleep_time = next_game_tick - clock.GetElapsedTime();
        if (sleep_time >= 0) {
            sf::Sleep(sleep_time);
        } else {
            // running behind
        }
    }

    return EXIT_SUCCESS;
}


Observations
Help
What am I doing wrong? Is it inaccurate timing, or is vsync not working? I have no clue what's going on.
Title: Gameloops and timing, smooth rotation
Post by: Laurent on November 25, 2011, 11:13:23 pm
Quote
The problem is independent of the programming language

So please give it in C++ so that more people (including me) can test it ;)
Title: Gameloops and timing, smooth rotation
Post by: zepto on November 27, 2011, 11:27:42 pm
Ok, I implemented the game loop FPS dependent on Constant Game Speed from the blog article deWiTTERS Game Loop (http://www.koonsolo.com/news/dewitters-gameloop/) in C++ with SFML2.

It has the same issues.
Title: Gameloops and timing, smooth rotation
Post by: Laurent on November 28, 2011, 07:35:26 am
Your code doesn't work, on the second iteration sleep_time is huge (negative but its type is unsigned) so it sleeps forever.
Title: Gameloops and timing, smooth rotation
Post by: zepto on November 28, 2011, 04:17:53 pm
Oops my bad, sleep_time should be an integer: new code (http://pastebin.com/FYaBrPtX)

From the output it looks to me like everything is working as expected.
Every (40 +- 1) ms the sprite is rotated by 3.6 degrees, I don't understand why the rotation looks so choppy.
Code: [Select]
0, 1
3.6, 40
7.2, 80
10.8, 120
14.4, 161
18, 201
21.6, 241
25.2, 281
28.8, 320
32.4, 361
36, 401
39.6, 441
43.2, 481
46.8, 521
50.4, 561
54, 601
57.6, 640
61.2, 681
64.8, 720
68.4, 760
72, 801
75.6, 841
79.2, 880
82.8, 921
86.4, 961
90, 1000
Title: Gameloops and timing, smooth rotation
Post by: Atomical on November 28, 2011, 07:05:44 pm
try the built in window.SetFrameratelimit(FPS)
Title: Gameloops and timing, smooth rotation
Post by: zepto on December 13, 2011, 05:06:42 pm
Quote from: "Atomical"
try the built in window.SetFrameratelimit(FPS)


No this didn't work, also the gameloop is already limited by the FPS.
Any other ideas?
Title: Gameloops and timing, smooth rotation
Post by: Anata on December 13, 2011, 05:28:36 pm
for me, it's limited by EnableVerticalSync and not by FPS
Title: Gameloops and timing, smooth rotation
Post by: Naufr4g0 on December 13, 2011, 08:27:51 pm
Let VSync decide the FPS of the game.
Enable window vsync and use GetFrameTime function to obtain smooth movements and rotations. :) It works!