4
« on: July 28, 2011, 12:13:39 pm »
Hi,
My window doesn't seem to refresh correctly (stuttering / freezing) when I try to display some sprites.
The strange thing is that it only happens when there is between about 2442 and 2507 sprites.
Even stranger, the problem disappears when I use Fraps or some other overlay to display the fps count.
Here's the code:
#include <SFML/Graphics.hpp>
#include <vector>
//#define SPRITES 2442
#define SPRITES 2500
//#define SPRITES 2507
#define WIDTH 1024
#define HEIGHT 768
inline float frand(const float min, const float max)
{
return ((float(rand()) / float(RAND_MAX)) * (max - min)) + min;
}
void main()
{
sf::RenderWindow window(sf::VideoMode(WIDTH, HEIGHT), "Bench");
sf::Image image;
image.LoadFromFile("sprite1.png"); // 64x64
image.SetSmooth(true);
float originX = image.GetWidth() / 2.0f;
float originY = image.GetHeight() / 2.0f;
std::vector<sf::Sprite*> sprites;
for(int i = 0; i < SPRITES; ++i)
{
sf::Sprite* sprite;
sprite = new sf::Sprite(image);
sprite->SetBlendMode(sf::Blend::Alpha);
sprite->SetPosition(frand(0, WIDTH), frand(0, HEIGHT));
sprite->SetOrigin(originX, originY);
sprites.push_back(sprite);
}
sf::Clock timer;
unsigned int lastTime = 0, frameTime = 0, delta = 0;
float step = 1000.0f / 60;
sf::Sprite* sprite;
sf::Event evt;
while (window.IsOpened())
{
while (window.PollEvent(evt))
{
if (evt.Type == sf::Event::Closed)
window.Close();
}
frameTime = timer.GetElapsedTime();
delta = frameTime - lastTime;
if (delta >= step)
{
window.Clear();
lastTime = frameTime;
for (std::vector<sf::Sprite*>::iterator it = sprites.begin(), itEnd = sprites.end(); it != itEnd; ++it)
{
sprite = *it;
sprite->Rotate(1);
window.Draw(*sprite);
}
window.Display();
}
}
}
I doesn't look like it's graphics related because the animation "continues" during the freezes.
I'm using SMLF2, static libs, VS 2010, Win7, ATI graphics card.
Thanks in advance :)