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

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - rafitasoler

Pages: [1]
1
System / Problem with elapsed asMilliseconds
« on: August 05, 2017, 12:33:16 am »
Problem --> if (elapsed1.asMilliseconds() >= 16.7)

Basically my sprite moves at 60 fps (i know that i'm comparing an int with 16.7, 16 does the same), but when using asMilliseconds it tears/jumps back a bit every second or so.

#include <SFML/Graphics.hpp>
#include <iostream>

using namespace std;
using namespace sf;

int main()
{
        // create the window
        sf::RenderWindow window(sf::VideoMode(800, 600), "My window");
        sf::Texture texture;
        texture.loadFromFile("resources/player.png");
        texture.setSmooth(true);
        sf::Sprite sprite;
        sprite.setTexture(texture);
        sf::Clock clock;

        int speed = 2;

        // run the program as long as the window is open
        while (window.isOpen())
        {
                Time elapsed1 = clock.getElapsedTime();
                if (elapsed1.asMilliseconds() >= 16.7)
                {
                        clock.restart();
                        // check all the window's events that were triggered since the last iteration of the loop
                        sf::Event event;
                        while (window.pollEvent(event))
                        {
                                // "close requested" event: we close the window
                                if (event.type == Event::Closed)
                                        window.close();
                        }

                        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
                        {
                                // left key is pressed: move our character
                                sprite.move(speed, 0);
                        }
                        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
                        {
                                // left key is pressed: move our character
                                sprite.move(0, speed);
                        }
                        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
                        {
                                // left key is pressed: move our character
                                sprite.move(-speed, 0);
                        }
                        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
                        {
                                // left key is pressed: move our character
                                sprite.move(0, -speed);
                        }

                        // clear the window with black color
                        window.clear(sf::Color::White);
                        window.draw(sprite);


                        // draw everything here...
                        // window.draw(...);

                        // end the current frame
                        window.display();
                }
        }

        return 0;
}
 

If i use
if (elapsed1.asSeconds() >= 1.0/60.0)
instead, it moves smoothly.

It happens to my brother too (he found the problem, i've never had it because i always use asSeconds), but he has it even with asSeconds.

I've tried at 30 and 120 fps, and enabling vsync. Still fine with seconds and badly with milliseconds.

Why? Arigato very much.

EDIT: Apparently 58, 59, 61 fps doesn't work well with seconds either. If i don't limit fps it runs fine too. I guess it has something to do with monitor refreshing, but why doesn't vsync fix it, and why videogames look fine without vsync (i always disable it because it breaks my pc) ?

Pages: [1]
anything