1
General / Re: FPS drops to 1 when moving sprite
« on: March 21, 2015, 05:30:46 pm »
Okay, I'll try out your suggestions as soon as I can, thanks for all the help
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.
...
while(mWindow.isOpen()) {
timeSinceLastUpdate += clock.restart();
processEvents();
while(timeSinceLastUpdate >= TimePerFrame) {
timeSinceLastUpdate -= TimePerFrame;
update(TimePerFrame);
}
render();
}
...
By the way, I wrote this program according to first chapter of this book: SFML Game Development and after that I have even downloaded the sample code from their git repo to compare performance and source code, and in both of them processEvents() was in that second while and both of them lagged. I am wondering, did they include not working example, or is it a problem specific to linux/my computer?#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;
}