1
General / 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.
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.
#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();
}
#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.