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

Author Topic: Need help using deltaTime  (Read 2865 times)

0 Members and 1 Guest are viewing this topic.

nullification_

  • Newbie
  • *
  • Posts: 1
    • View Profile
    • Email
Need help using deltaTime
« on: October 16, 2017, 08:37:19 am »
Hello, sorry I am new to SFML and C++ in general and have no clue what I am doing wrong.


#include <SFML/Graphics.hpp>
#include <iostream>
int main()
{
    int speed = 10;
    sf::RenderWindow window(sf::VideoMode(1920, 1080), "My window");
    sf::Texture texture;
    if (!texture.loadFromFile("asher.jpeg"))
    {
        std::cout << "Texture did not load" << std::endl;
    }
    sf::Sprite sprite;
    sprite.setTexture(texture);
    // run the program as long as the window is open
    while (window.isOpen())
    {
        sf::Event event;
        sf::Clock deltaClock;
        while (window.pollEvent(event))
        {
            float sf::Time dt = deltaClock.sf::Clock::restart.asSeconds;
            std::cout << dt << std::endl;
        }
        while (window.pollEvent(event))
        {
            // "close requested" event: we close the window
            if (event.type == sf::Event::Closed)
                {
                window.close();
                }

            if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
                {
                sprite.move(sf::Vector2f (5, 0));
                }
            if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
                {
                    sprite.move (sf::Vector2f(-5, 0));
                }
            if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
                {
                    sprite.move(sf::Vector2f(0, -5));
                }
            if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
                {
                    sprite.move(sf::Vector2f(0, 5));
                }


        }
        window.clear(sf::Color::Black);
        window.draw(sprite);
        window.display();
    }

Where it says,

float sf::Time dt = deltaClock.sf::Clock::restart.asSeconds;
std::cout << dt << std::endl;

I am trying to print the delta time so I can make movement look less choppy. I have been trying for hours but I can't figure out what is wrong with it. Any help would be appreciated, thank you.
« Last Edit: October 16, 2017, 09:24:23 am by Laurent »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Need help using deltaTime
« Reply #1 on: October 16, 2017, 09:28:50 am »
Quote
deltaClock.sf::Clock::restart.asSeconds
Where did you get this syntax from? It makes no sense, remove the class name and add the function call parentheses:
deltaClock.restart().asSeconds()

You should also declare your sf::Clock instance outside the main loop, otherwise you'll get a new clock every iteration, and thus a time that always restarts from zero.

Maybe you should learn C++ a little more, and get the basic syntax right, before trying complicated things.
Laurent Gomila - SFML developer