Hello,
I have written a simple program which draws a spaceship which can be moved with WASD keys. The problem is, that when moving continuously, the FPS starts decreasing until it reaches 1. When I release movement keys, FPS starts going back to ~60. So, as long as the ship is being moved, the FPS is dropping, otherwise FPS stays normal or increases to 60.
I am developing in 64bit Linux Mint 17.1. Before posting here, I have read the FAQ, updated my video card drivers. My video card is Nvidia GeForce GT 540M, I am using nvidia-346 drivers. I wrote the program using vim, compiled with g++. SFML version is 2.2 and I have set it up using tutorial:
http://www.sfml-dev.org/tutorials/2.2/start-linux.php. I have put all the code to one file, if anyone would like to test it.
Does anyone know why this lag is happening and how to fix it? Thanks in advance
#include <SFML/Graphics.hpp>
class Game {
public:
Game();
void run();
private:
void processEvents();
void update(sf::Time deltaTime);
void render();
void handlePlayerInput(sf::Keyboard::Key key, bool isPressed);
private:
static const float PlayerSpeed;
static const sf::Time TimePerFrame;
sf::RenderWindow mWindow;
sf::Texture mTexture;
sf::Sprite mPlayer;
bool mIsMovingUp, mIsMovingDown, mIsMovingLeft, mIsMovingRight;
};
const float Game::PlayerSpeed = 100.f;
const sf::Time Game::TimePerFrame = sf::seconds(1.f/60.f);
Game::Game():
mWindow(sf::VideoMode(640, 480), "SFML Application"),
mTexture(),
mPlayer(),
mIsMovingUp(false),
mIsMovingDown(false),
mIsMovingRight(false),
mIsMovingLeft(false)
{
if(!mTexture.loadFromFile("resources/Ship.png")){/* Handle loading error */}
mPlayer.setTexture(mTexture);
mPlayer.setPosition(100.f, 100.f);
}
void Game::run() {
sf::Clock clock;
sf::Time timeSinceLastUpdate = sf::Time::Zero;
while(mWindow.isOpen()) {
timeSinceLastUpdate += clock.restart();
while(timeSinceLastUpdate > TimePerFrame) {
timeSinceLastUpdate -= TimePerFrame;
processEvents();
update(TimePerFrame);
}
render();
}
}
void Game::processEvents() {
sf::Event event;
while(mWindow.pollEvent(event)) {
switch(event.type) {
case sf::Event::KeyPressed:
handlePlayerInput(event.key.code, true);
break;
case sf::Event::KeyReleased:
handlePlayerInput(event.key.code, false);
break;
case sf::Event::Closed:
mWindow.close();
break;
}
}
}
void Game::update(sf::Time deltaTime) {
sf::Vector2f movement(0.f, 0.f);
if(mIsMovingUp) {
movement.y -= PlayerSpeed;
}
if(mIsMovingDown) {
movement.y += PlayerSpeed;
}
if(mIsMovingLeft) {
movement.x -= PlayerSpeed;
}
if(mIsMovingRight) {
movement.x += PlayerSpeed;
}
mPlayer.move(movement * deltaTime.asSeconds());
}
void Game::render() {
mWindow.clear();
mWindow.draw(mPlayer);
mWindow.display();
}
void Game::handlePlayerInput(sf::Keyboard::Key key, bool isPressed) {
if(key == sf::Keyboard::W) {
mIsMovingUp = isPressed;
} else if(key == sf::Keyboard::S) {
mIsMovingDown = isPressed;
} else if(key == sf::Keyboard::A) {
mIsMovingLeft = isPressed;
} else if(key == sf::Keyboard::D) {
mIsMovingRight = isPressed;
}
}
int main()
{
Game game;
game.run();
return 0;
}