SFML community forums

Help => General => Topic started by: delio on March 09, 2014, 04:51:25 pm

Title: My vertex line is blinking
Post by: delio on March 09, 2014, 04:51:25 pm
Here's my code

I want to create lines in every 40 px

cout << "enter width: ";
        cin >> x;
        cout << endl;
        cout << "enter height: ";
        cin >> y;

        sf::RenderWindow window(sf::VideoMode(x*4, y*4), "Window");
        /*sf::CircleShape shape(100.f);
        shape.setFillColor(sf::Color::Green);*/



        for (int i = 1; i <= x; ++i)
        {

                sf::Vertex outlinex[2] =
                {
                        sf::Vertex(sf::Vector2f(i * 40, 0), sf::Color::Blue),
                        sf::Vertex(sf::Vector2f(i * 40, 100), sf::Color::Blue)

                };
                window.draw(outlinex, 2, sf::Lines);
        }

                while (window.isOpen())
        {
                sf::Event event;
                while (window.pollEvent(event))
                {
                        if (event.type == sf::Event::Closed)
                                window.close();
                }

                /*window.clear();*/
                /*window.draw(outlinex, 2, sf::Lines);*/
                window.display();
        }
       
        system("pause");

My problem is When I compiled, my lines is blinking
but when I delete for loop, it stop.

Thanks
Title: My vertex line is blinking
Post by: The Terminator on March 09, 2014, 05:52:39 pm
I suspect it's because you are calling window.draw(outlinex, 2, sf::Lines); before window.clear(). Move your for loop to in between window.clear() and window.display() and see if that fixes your issue.
Title: Re: My vertex line is blinking
Post by: didii on March 10, 2014, 04:26:33 am
Couple of things to note here:

Please do read the tutorials (http://www.sfml-dev.org/tutorials/2.1/) as they are very useful and clear.
Title: Re: My vertex line is blinking
Post by: delio on March 10, 2014, 07:18:34 am
I suspect it's because you are calling window.draw(outlinex, 2, sf::Lines); before window.clear(). Move your for loop to in between window.clear() and window.display() and see if that fixes your issue.
Thanks! Problem solved

Couple of things to note here:
  • As The Terminator mentioned: sf::RenderWindow::clear should always be called before sf::RenderWindow::draw.
  • Your array outlines is created inside the for-loop. When the for loop ends, the scope where your vertex was created ends and the object gets destroyed.
  • You draw your vertices only once, not every frame. Read the big red box on this tutorial (http://www.sfml-dev.org/tutorials/2.1/graphics-draw.php). It mentions how you should do it and also refers to double-buffering (http://en.wikipedia.org/wiki/Multiple_buffering) which is why the lines are blinking.

Please do read the tutorials (http://www.sfml-dev.org/tutorials/2.1/) as they are very useful and clear.

I'm clearly understand.
Thanks!