Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: [SOLVED] SFML 2. Stop drawing on window  (Read 5274 times)

0 Members and 3 Guests are viewing this topic.

lzedl

  • Newbie
  • *
  • Posts: 14
    • View Profile
[SOLVED] SFML 2. Stop drawing on window
« on: July 25, 2012, 12:38:40 pm »
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
« Last Edit: July 25, 2012, 09:29:07 pm by lzedl »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: SFML 2. Stop drawing on window
« Reply #1 on: July 25, 2012, 02:05:16 pm »
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. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

lzedl

  • Newbie
  • *
  • Posts: 14
    • View Profile
Re: SFML 2. Stop drawing on window
« Reply #2 on: July 25, 2012, 02:22:55 pm »
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)

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: SFML 2. Stop drawing on window
« Reply #3 on: July 25, 2012, 02:55:56 pm »
It should not and does not on my system, so it's not SFML fault, but something wrong on your end, but I've no idea what. :-\

Btw it's a good practice to keep the updating part (moving sprites around, handeling inputs & events, etc.) seperated from the drawing part. So your sprite.move(1, 0); should come infront of the drawing statements.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

lzedl

  • Newbie
  • *
  • Posts: 14
    • View Profile
Re: SFML 2. Stop drawing on window
« Reply #4 on: July 25, 2012, 03:22:49 pm »
It should not and does not on my system, so it's not SFML fault, but something wrong on your end, but I've no idea what. :-\

It makes me sad =( but thanks anyway)

Btw it's a good practice to keep the updating part (moving sprites around, handeling inputs & events, etc.) seperated from the drawing part. So your sprite.move(1, 0); should come infront of the drawing statements.

Thanks =) But it just an example) I making a game and want to port it from SDL to SFML, because i found it very cool, fast and simple (Captain Obvious), but i faced with this problem =(

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: SFML 2. Stop drawing on window
« Reply #5 on: July 25, 2012, 03:23:50 pm »
I guess the examples that come with the SFML SDK work fine right?
Also is your graphics driver uptodate?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

lzedl

  • Newbie
  • *
  • Posts: 14
    • View Profile
Re: SFML 2. Stop drawing on window
« Reply #6 on: July 25, 2012, 09:28:00 pm »
I guess the examples that come with the SFML SDK work fine right?
Also is your graphics driver uptodate?

Right! Spend some hours with drivers settings i switch my noveau driver to proprietary nvidia driver and all works fine =)
Thanks for advice =)

lzedl

  • Newbie
  • *
  • Posts: 14
    • View Profile
Re: [SOLVED] SFML 2. Stop drawing on window
« Reply #7 on: August 05, 2012, 01:31:26 pm »
I discovered that the problem wasn't exactly in driver, but in nouveau-dri package (ArchLinux) that represents hardware-accelerated 3D support. If you on linux and nvidia video card - keep it in mind.