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

Author Topic: My vertex line is blinking  (Read 2490 times)

0 Members and 1 Guest are viewing this topic.

delio

  • Newbie
  • *
  • Posts: 49
    • View Profile
    • Email
My vertex line is blinking
« 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
« Last Edit: March 09, 2014, 04:56:04 pm by delio »

The Terminator

  • Full Member
  • ***
  • Posts: 224
  • Windows and Mac C++ Developer
    • View Profile
My vertex line is blinking
« Reply #1 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.
Current Projects:
Technoport

didii

  • Full Member
  • ***
  • Posts: 122
    • View Profile
Re: My vertex line is blinking
« Reply #2 on: March 10, 2014, 04:26:33 am »
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. It mentions how you should do it and also refers to double-buffering which is why the lines are blinking.

Please do read the tutorials as they are very useful and clear.
« Last Edit: March 10, 2014, 04:28:56 am by didii »

delio

  • Newbie
  • *
  • Posts: 49
    • View Profile
    • Email
Re: My vertex line is blinking
« Reply #3 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. It mentions how you should do it and also refers to double-buffering which is why the lines are blinking.

Please do read the tutorials as they are very useful and clear.

I'm clearly understand.
Thanks!
« Last Edit: March 10, 2014, 07:22:12 am by delio »