Also has this weird jerky movement and screen tearing in another game I made but I decided to ignore it. Now I finally want to fix it somehow so I am able to get smooth gameplay in my next game.
I have an Asus k52n laptop running windows 7 64bit.
Cpu: AMD Athlon X2 P320 2,1GHz
Memory: 4 GB
Videocard: AMD Radeon HD 4200
Example:
#include <SFML/Graphics.hpp>
// 1 second / 60
const sf::Time timePerFrame = sf::seconds( 1.f / 60.f );
int main() {
// Init window
sf::RenderWindow window( sf::VideoMode( 720, 540 ), "Demo" );
window.setVerticalSyncEnabled( true );
// Init variables
float playerSpeed = 200.f;
bool isMovingUp = false;
bool isMovingDown = false;
bool isMovingLeft = false;
bool isMovingRight = false;
// Load images
sf::Texture background_tex;
if ( !background_tex.loadFromFile( "background.png" ) ) {
// Error handling..
}
sf::Texture player_tex;
if ( !player_tex.loadFromFile( "player.png" ) ) {
// Error handling..
}
// Setup sprites
sf::Sprite background;
background.setTexture( background_tex );
sf::Sprite player;
player.setTexture( player_tex );
player.setOrigin( player.getLocalBounds().width / 2,
player.getLocalBounds().height / 2 );
// Create view and set size equal to window
sf::View camera;
camera.setSize( sf::Vector2f(window.getSize()) );
// Main loop, update at 60. Render at unlimited but locked
// with v-sync at 60.
sf::Clock clock;
sf::Time timeSinceLastUpdate = sf::Time::Zero;
while ( window.isOpen() ) {
timeSinceLastUpdate += clock.restart();
while ( timeSinceLastUpdate > timePerFrame ) {
timeSinceLastUpdate -= timePerFrame;
// event handling
sf::Event event;
while ( window.pollEvent( event ) ) {
if ( event.type == sf::Event::Closed )
window.close();
if ( event.type == sf::Event::KeyPressed ) {
if ( event.key.code == sf::Keyboard::Up )
isMovingUp = true;
else if ( event.key.code == sf::Keyboard::Down )
isMovingDown = true;
else if ( event.key.code == sf::Keyboard::Left )
isMovingLeft = true;
else if ( event.key.code == sf::Keyboard::Right )
isMovingRight = true;
}
if ( event.type == sf::Event::KeyReleased ) {
if ( event.key.code == sf::Keyboard::Up )
isMovingUp = false;
else if ( event.key.code == sf::Keyboard::Down )
isMovingDown = false;
else if ( event.key.code == sf::Keyboard::Left )
isMovingLeft = false;
else if ( event.key.code == sf::Keyboard::Right )
isMovingRight = false;
}
}
// update logic
camera.setCenter( player.getPosition() );
sf::Vector2f movement( 0.f, 0.f );
if ( isMovingUp )
movement.y -= playerSpeed;
if ( isMovingDown )
movement.y += playerSpeed;
if ( isMovingLeft )
movement.x -= playerSpeed;
if ( isMovingRight )
movement.x += playerSpeed;
player.move( movement * timePerFrame.asSeconds());
}
// render
window.setView( camera );
window.clear();
window.draw( background );
window.draw( player );
window.display();
}
return 0;
}