Hi,
I've been having a problem with erratic framerate and stutter/slowdown in SFML. This problem has happened both in previous versions of SFML as well as the current release candidate of SFML-2, which I just switched to after hearing some timing issues were fixed. Weirdly enough, this only seems to happen in windowed mode -- everything works fine in fullscreen (no stutter at all.) Enabling VSync, frame limiting to 60fps, or both doesn't help the stuttering, and even without any limiting at all the program still slows down (e.g. it'll be running at 500fps for a small bit then slow down to 100 or lower, then speed up again, etc.)
Here's a small test case (I first encountered this problem when working on a relatively large-scale game project:)
#include <iostream>
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
int SCR_WIDTH = 640;
int SCR_HEIGHT = 480;
bool FULLSCREEN = false;
bool VSYNC = true;
bool LIMIT_FRAMERATE = false;
int FRAMERATE = 60;
int main() {
unsigned long style = ((FULLSCREEN == true) ? sf::Style::Fullscreen : sf::Style::Close);
sf::RenderWindow game(sf::VideoMode(SCR_WIDTH,SCR_HEIGHT,32), "Test", style);
game.setVerticalSyncEnabled(VSYNC);
if (LIMIT_FRAMERATE) game.setFramerateLimit(FRAMERATE);
sf::Clock frameClock;
sf::Image redSquareImg;
redSquareImg.create(128, 128, sf::Color(sf::Color::Red));
sf::Texture redSquareTex;
redSquareTex.loadFromImage(redSquareImg);
sf::Sprite redSquare;
redSquare.setTexture(redSquareTex);
int squareX = 20, squareY = 20;
while (game.isOpen()) {
frameClock.restart();
game.clear();
redSquare.setPosition(squareX, squareY);
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) squareX -= 2;
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) squareX += 2;
game.draw(redSquare);
game.display();
int ft = 1000 / frameClock.getElapsedTime().asMilliseconds();
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)) game.close();
std::cout << ft << std::endl;
}
}
Running Windows 7, graphics card is NVIDIA GeForce GTX 260. Any help would be greatly appreciated.
Thanks,
Sri