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.


Topics - ThoseCoolGamers

Pages: [1]
1
General / SFML slows down over time
« on: July 18, 2018, 01:58:09 am »
So I've been using SFML for a little bit now and I've ran into and bug.
After about 6 minutes my game will slow down going from around 1000 - 400 fps down to 100- 20 fps. This causes the player movement to jerk around, going forward and back spontaneously, I don't know if this is due to poorly written code or if its to do with my computer.

Here's the code I wrote:

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

using namespace std;

int main()
{
        sf::RenderWindow window(sf::VideoMode(800, 800), "game");

        sf::Sprite Player;
        Player.setScale(5,5);
        Player.setPosition(400, 400);
       
        sf::Texture Texture;
        Texture.loadFromFile("t.png");
        Player.setTexture(Texture);

        sf::Clock clock;
        sf::Time dt;

        sf::Clock FPSClock;
        sf::Time FPS;

        int Speed = 500;
        sf::Vector2f Pos;
       
        while (window.isOpen())
        {
                sf::Event event;
                while (window.pollEvent(event))
                {
                        if (event.type == sf::Event::Closed)
                                window.close();
                }
       
                sf::Time time = FPSClock.getElapsedTime();

                if (1.0f / time.asSeconds() < 100)
                        cout << 1.0f / time.asSeconds() << endl;

                FPSClock.restart().asSeconds();
                dt = clock.restart();

                Player.setPosition(Pos);

                if (sf::Keyboard::isKeyPressed(sf::Keyboard::W))
                        Pos.y -= Speed * dt.asSeconds();

                if (sf::Keyboard::isKeyPressed(sf::Keyboard::S))
                        Pos.y += Speed * dt.asSeconds();

                if (sf::Keyboard::isKeyPressed(sf::Keyboard::A))
                        Pos.x -= Speed * dt.asSeconds();

                if (sf::Keyboard::isKeyPressed(sf::Keyboard::D))
                        Pos.x += Speed * dt.asSeconds();

                window.clear();

                window.draw(Player);

                window.display();
        }

        return 0;
}]

Pages: [1]
anything