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

Author Topic: [SFML] Program slows down  (Read 1649 times)

0 Members and 2 Guests are viewing this topic.

firey6543

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
[SFML] Program slows down
« on: November 24, 2014, 04:49:21 pm »
#include <SFML/Graphics.hpp>

int main()
{
    sf::RenderWindow window(sf::VideoMode(640,480,32), "SFML works!");
    const float gravity = 0.0002f;
    int groundheight=440;
    const float moveSpeed = 0.1f,jumpSpeed = 0.2f;
    sf::Vector2f velocity(sf::Vector2f(0,0));
    sf::RectangleShape rect(sf::Vector2f(20,20));
    rect.setPosition(0,0);
    rect.setFillColor(sf::Color::Green);
    sf::Texture texture;
    sf::Texture Block;

    bool onGround = false;
    if(!texture.loadFromFile("map.png"))
    {

    }
    if(!Block.loadFromFile("block.png"))
    {

    }
    sf::Sprite backround(texture);
    backround.setPosition(0,460);

    sf::Sprite block(Block);
    block.setPosition(300,200);


    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
        }
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
        {
            velocity.x = moveSpeed;
        }
        else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
        {
            velocity.x = -moveSpeed;
        }
        else{
           velocity.x=0;
        }
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Space))
        {
            if(onGround==true)
            {
                  velocity.y = -jumpSpeed;
            }
        }
        if(rect.getPosition().y + rect.getSize().y < groundheight || velocity.y < 0)
        {
           velocity.y+=gravity;
           onGround=false;
        }
        if(rect.getGlobalBounds().intersects(backround.getGlobalBounds()))
        {
            velocity.y=0;
            onGround = true;
            rect.move(0,-0.01);
        }


        rect.move(velocity.x,velocity.y);


        window.clear();
        window.draw(backround);
        window.draw(rect);
        window.display();
    }

    return 0;
}
 

I dont know why but after about 8-10 seconds my game will just slow down to about half the original framerate.Any help would be appreciated.  Thanks :)
« Last Edit: November 24, 2014, 04:56:40 pm by eXpl0it3r »

Gambit

  • Sr. Member
  • ****
  • Posts: 283
    • View Profile
Re: [SFML] Program slows down
« Reply #1 on: November 24, 2014, 04:53:00 pm »
Unless the goal of this program is to slow SFML down, I think this is posted in the wrong section. Please wrap your code in code tags as well, also which version of SFML  are you using? (You should be using the latest unofficial nightly builds or build from the latest source).

firey6543

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Re: [SFML] Program slows down
« Reply #2 on: November 24, 2014, 04:55:21 pm »
Im Using SFML 2.1,is that a problem?

Gambit

  • Sr. Member
  • ****
  • Posts: 283
    • View Profile
Re: [SFML] Program slows down
« Reply #3 on: November 24, 2014, 05:12:55 pm »
SFML 2.1 is pretty old now, you should consider building from the latest source or the nightly builds.

 

anything