When running my app, whenever I press LSHIFT or RSHIFT, regardless of whether I am processing it or not within my event loop, the framerate becomes choppy-- randomly freezing for tenths of seconds and then speeding back up.
I'm using SFML 2.1
int main ( int argc, char *argv[] )
{
// Create a new render-window
sf::RenderWindow window(sf::VideoMode(800, 600), "AAAA");
window.setFramerateLimit(60);
sf::Clock clock;
float lastTime = 0;
sf::Texture texture;
texture.loadFromFile("dot.png");
sf::Sprite sprite;
sprite.setTexture(texture);
sprite.setPosition(500, 400);
sf::Int32 direction = rand()%4+1;
// The main loop
while (window.isOpen())
{
// Event handling
sf::Event Event;
while (window.pollEvent(Event))
{
if (Event.type == sf::Event::Closed)
window.close();
}
// Data
if(direction == 1)
sprite.setPosition(sprite.getPosition().x+1, sprite.getPosition().y);
if(direction == 2)
sprite.setPosition(sprite.getPosition().x, sprite.getPosition().y+1);
if(direction == 3)
sprite.setPosition(sprite.getPosition().x-1, sprite.getPosition().y);
if(direction == 4)
sprite.setPosition(sprite.getPosition().x, sprite.getPosition().y-1);
if(sprite.getPosition().x < 0 || sprite.getPosition().y < 0 || sprite.getPosition().x + 50 > window.getSize().x || sprite.getPosition().y + 50> window.getSize().y)
{
sf::Int32 origDir = direction;
while(origDir == direction)
direction = rand()%4+1;
}
// Clear & Draw window
window.clear();
window.draw(sprite);
window.display();
// FPS output
float currentTime = clock.getElapsedTime().asSeconds();
float fps = 1.f / (currentTime - lastTime);
lastTime = currentTime;
std::cout << fps << "\n";
}
return 0;
}
Is there any reason for this? Any fix?
Thanks!