SFML community forums

Help => General => Topic started by: idontmakesense on April 14, 2014, 10:53:46 am

Title: Game Speed Consistency
Post by: idontmakesense on April 14, 2014, 10:53:46 am
How do I make sure that my game runs at the same speed on all computers? The problem occurred when i transferred my game project from my old laptop to my new one. The game runs too fast and is unplayable.

Game background: Its a small top-down car game with three ai's trying to shoot you down. Game ends when a bullet collides.

If any more information is required I'll be more than happy to share.

int main()
{      
        sf::Clock clock;
        bool end_game= false;
        initialize_cars();
        initialize_bullets();
        initialize_explosion_sprite();
    while (window.isOpen())
    {  
        sf::Event event;
        if (window.pollEvent(event))
        {
            if (event.type== sf::Event::Closed || (event.type== sf::Event::KeyReleased) && (event.key.code== sf::Keyboard::Escape))
                window.close();
        }
                       
        move_cars();
        set_bullet_timer(&clock);
        move_bullets();
       
                end_game= bullet_collision(&clock, &black_car, &blue_bullet);
                if(end_game== true)
                break;
               
                end_game= bullet_collision(&clock, &black_car, &red_bullet);
                if(end_game== true)
                break;
               
                for(int i=0; i<2; i++)
        {
                end_game= bullet_collision(&clock, &black_car, &green_bullet[i]);
                if(end_game== true)
                        break;
        }
        if(end_game== true)
                break;
       
                window.clear(sf::Color::White);
                display_cars();
                display_bullets();
        window.display();
    }
    return 0;
}
Title: Re: Game Speed Consistency
Post by: Doodlemeat on April 14, 2014, 10:56:18 am
You have to use deltatime
http://gafferongames.com/game-physics/fix-your-timestep/
Title: Re: Game Speed Consistency
Post by: 4B on April 14, 2014, 03:08:29 pm
You can store
clock.restart().asSeconds();
to a float and pass this to your bullet timer.
multiply your speed(per seconds) * the float(e.g. 0,04). So you can be sure that you move exactly speed/second.

Something else.. You don't need the end_game variable at all and you should make you window-close if-statement smaller.
you can write instead:

//1. There's no need of keyReleased
if(event.type == sf::Event::Closed || sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)
 window.close();
//2. The less variables you have to initialize above the while, the better people can read your code
if(bullet_collision(...))
 break;
 
Title: Re: Game Speed Consistency
Post by: Sub on April 15, 2014, 06:05:40 am
Also, it's a good idea to cap the FPS at something reasonable.  You don't want your game running at 1000 FPS, or something ridiculous.

window.setFramerateLimit(60);
Title: Re: Game Speed Consistency
Post by: Veritas on April 15, 2014, 09:58:19 am
Also, it's a good idea to cap the FPS at something reasonable.  You don't want your game running at 1000 FPS, or something ridiculous.

window.setFramerateLimit(60);

You may want to manage time using sf::Clock instead of setting the limit using the setFramerateLimit() method. If I recall correctly it uses sf::sleep() which is not precise.
Title: Re: Game Speed Consistency
Post by: MadMartin on April 15, 2014, 10:09:02 am
An even better way to handle this is the vsync: http://www.sfml-dev.org/documentation/2.1/classsf_1_1Window.php#a59041c4556e0351048f8aff366034f61

Vsync is a driver option and therefore preferable over unprecise thread sleep operations.
Title: Re: Game Speed Consistency
Post by: Veritas on April 15, 2014, 10:35:46 am
An even better way to handle this is the vsync: http://www.sfml-dev.org/documentation/2.1/classsf_1_1Window.php#a59041c4556e0351048f8aff366034f61

Vsync is a driver option and therefore preferable over unprecise thread sleep operations.

Just noting that because Vsync is controlled by the driver, it is not guaranteed to be enabled. The user may have to enable it as a driver option in order for setVerticalSyncEnabled() to work, so maybe it should be a game setting and not the default way to limit the framerate?
Title: Re: Game Speed Consistency
Post by: MadMartin on April 15, 2014, 11:07:27 am
Yeah, that's right. Even though I never saw a driver with vsync disabled globally, your remark is valid.

But I'm not sure whether we are talking about frame drawing rate limit or game loop iteration limit?
It's always the right way to make your game loop independent of time, like the article "Fix your timestep" suggests.

Reducing your drawing rate is only sensible if you have the program running on a mobile device, to prevent cpu load. For this, you could use an easy sleep(1), but don't let it sleep for more time! The more time you let it sleep, the more lag can be induced. See for example http://gamedev.stackexchange.com/questions/18898/is-it-safe-to-use-sleep-in-game-loop-on-windows or http://gamedev.stackexchange.com/questions/62273/what-are-the-benefits-of-capping-frames-per-second-if-any