Hello there
I have a strange problem with drawing sprites on screen.
Here is an example:
main.cpp
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
int main()
{
sf::RenderWindow window(sf::VideoMode(800, 600, 32), "SFML works!");
sf::Text text("Hello SFML\nand here am i");
sf::Font font;
font.loadFromFile("XPDR06.TTF");
text.setFont(font);
text.setCharacterSize(8);
sf::Texture texture;
texture.loadFromFile("sprite.png");
sf::Sprite sprite(texture);
sf::Clock clock;
while (window.isOpen())
{
if(clock.getElapsedTime().asMilliseconds() > 1000/30)
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
window.clear();
window.draw(text);
sprite.move(1,0);
window.draw(sprite);
window.display();
clock.restart();
}
}
return 0;
}
It works fine, but if i delete "window.draw(text);" it freeze drawing on about second frame.
What i do wrong? :'(
I spend a lot of time to find the place, that affect on process...
Is it bug? or it's my mistake?
Thanks anyway =)
PS: Sorry for bad English
Define "it freeze drawing on about second frame" further...
Does the sprite stop moving? (If so this shouldn't happen)
Obviously if you remove the draw call the text won't get drawn anymore = it will not show up.
Additionally don't do this:
if(clock.getElapsedTime().asMilliseconds() > 1000/30)
Instead just insert after the window creation this:
window.setFramerateLimit(30);
The reason is that your method creates a busy-waiting-loop (google it for more information) and thus using all the CPU power it can get, whereas setFramerateLimit always puts the application to sleep and thus freeing CPU time. ;)
Define "it freeze drawing on about second frame" further...
Does the sprite stop moving? (If so this shouldn't happen)
Obviously if you remove the draw call the text won't get drawn anymore = it will not show up.
If i compile code with "window.draw(text);" everithing is OK.
But if remove it sprite stop moving - yes =) but loop still continues...
I don't need text drawing, but i found that it affects on all window drawing
Additionally don't do this:
if(clock.getElapsedTime().asMilliseconds() > 1000/30)
Instead just insert after the window creation this:
window.setFramerateLimit(30);
The reason is that your method creates a busy-waiting-loop (google it for more information) and thus using all the CPU power it can get, whereas setFramerateLimit always puts the application to sleep and thus freeing CPU time. ;)
Thanks =) I keep it in mind)