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

Author Topic: problem with sf::VertexArray  (Read 2738 times)

0 Members and 1 Guest are viewing this topic.

iTh3End

  • Newbie
  • *
  • Posts: 10
    • View Profile
    • Email
problem with sf::VertexArray
« on: December 23, 2014, 12:26:18 pm »
hey, im beginner in sfml and here i want  draw one vertex between two points but when i call window.draw(lines); I get an error: Use undeclared identifier "lines"
what should i do?
thanks

   sf::RenderWindow window(sf::VideoMode(600, 400), "Convex Hull");

   
   
    std::vector<sf::RectangleShape> rects;
   
    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event) )
        {
       
       
       
       
            if (event.type == sf::Event::Closed)
                window.close();
            else if (event.type == sf::Event::MouseButtonPressed &&
                     event.mouseButton.button == sf::Mouse::Left&&
                     rects.size() < 10) {
               
                posx.push_back(event.mouseButton.x);
                posy.push_back(event.mouseButton.y);
               
//                cout << posx[f] << "   " << posy[f] << endl;
                f++;
               
//                cout << f << endl;
                sf::RectangleShape rect { { 2, 2 } };
                rect.setPosition(sf::Vector2f(sf::Mouse::getPosition(window)));
                rect.setFillColor(sf::Color::Green);
                rects.push_back(rect);
               
                cout << &rects << endl;
               
                sf::VertexArray lines(sf::LinesStrip, 2);
                lines[0].position = sf::Vector2f(posx[0], posy[0]);
                lines[1].position = sf::Vector2f(posx[10], posy[10]);
               
               

               
            }
           
            if (f == 10)
                break;
        }
   
       
        window.clear();
        for (const auto &r : rects)
            window.draw(r);
       
             window.draw(lines); // Error is here
       
        window.display();
    }
       

Gambit

  • Sr. Member
  • ****
  • Posts: 283
    • View Profile
Re: problem with sf::VertexArray
« Reply #1 on: December 23, 2014, 12:42:01 pm »
Your "lines" variable and the draw call are in different scopes which cannot access each other. Try moving your lines variable outside of your main loop and clear it at the end of each frame.

iTh3End

  • Newbie
  • *
  • Posts: 10
    • View Profile
    • Email
Re: problem with sf::VertexArray
« Reply #2 on: December 23, 2014, 01:01:38 pm »
outside of         while (window.pollEvent(event) ) ?

Gambit

  • Sr. Member
  • ****
  • Posts: 283
    • View Profile
Re: problem with sf::VertexArray
« Reply #3 on: December 23, 2014, 01:07:37 pm »
Yes. Either that or your while (window.isOpen()) loop. If you do it your way you do not have to clear it each frame.

iTh3End

  • Newbie
  • *
  • Posts: 10
    • View Profile
    • Email
Re: problem with sf::VertexArray
« Reply #4 on: December 23, 2014, 01:34:47 pm »
i do that, compiled but my window is not open,


Raincode

  • Full Member
  • ***
  • Posts: 118
    • View Profile
Re: problem with sf::VertexArray
« Reply #5 on: December 23, 2014, 01:39:03 pm »
If by "my window is not open" you mean it doesn't appear though your application is running - have you put the necessary dlls in the working directory of your program?

Gambit

  • Sr. Member
  • ****
  • Posts: 283
    • View Profile
Re: problem with sf::VertexArray
« Reply #6 on: December 23, 2014, 01:40:53 pm »
Move the variable not the code for adding lines to it.

iTh3End

  • Newbie
  • *
  • Posts: 10
    • View Profile
    • Email
Re: problem with sf::VertexArray
« Reply #7 on: December 23, 2014, 09:41:53 pm »
If by "my window is not open" you mean it doesn't appear though your application is running - have you put the necessary dlls in the working directory of your program?

i mean window very soon closed after opening,

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: problem with sf::VertexArray
« Reply #8 on: December 23, 2014, 10:59:33 pm »
Can you show your new, complete and minimal code?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/