SFML community forums

Help => Graphics => Topic started by: WithoutBrain on December 11, 2016, 08:54:00 am

Title: How can i do Vertex to dynamic?
Post by: WithoutBrain on December 11, 2016, 08:54:00 am
How can i do this to dynamic?(I am sorry for my bad english)

sf::Vertex vertices[5] =
{
    sf::Vertex(...),
    sf::Vertex(...),
    sf::Vertex(...),
    sf::Vertex(...),
    sf::Vertex(...)
};
Title: Re: How can i do Vertex to dynamic?
Post by: WithoutBrain on December 11, 2016, 09:04:23 am
SOLVED, I FIXED IT OVER VECTOR
Title: Re: How can i do Vertex to dynamic?
Post by: WithoutBrain on December 11, 2016, 09:26:23 am
Damn, its works, but it doing +1 line, i dont know why. CODE:

#include <SFML/Graphics.hpp>
#include <iostream>
#include <windows.h>
using namespace std;
using namespace sf;
int main()
{

RenderWindow window(sf::VideoMode(500, 500), "LINES");
int x,y;
vector<sf::Vertex> vertices;


    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
            if (event.type == sf::Event::MouseButtonPressed)
                {
                if (event.mouseButton.button == sf::Mouse::Left)
                {
                    x = Mouse::getPosition().x - 394;   // Its doing mouse position for window
                    y = Mouse::getPosition().y - 259;   // Its doing mouse position for window
                    vertices.push_back(sf::Vertex(Vector2f(x,y)));
                }
                }
        }

       window.clear();
       if(vertices.size() != 0)
       {for(int i = 0;i < vertices.size();++i){window.draw(&vertices,vertices.size(),LinesStrip);}}


        window.display();
        }

    return 0;
}


Title: Re: How can i do Vertex to dynamic?
Post by: Hapax on December 11, 2016, 11:11:42 am
Firstly, your code doesn't compile.
The draw method should point to the first element (or the beginning of the actual data):
window.draw(vertices.data(), vertices.size(), sf::LinesStrip);
or
window.draw(&vertices.front(), vertices.size(), sf::LinesStrip);
or
window.draw(&vertices[0], vertices.size(), sf::LinesStrip);

There no need to loop through the vertices either. Drawing the object will draw them all:
if (vertices.size() > 1)
        window.draw(vertices.data(), vertices.size(), LinesStrip);
Note that there's no point drawing if there is only one vertex as a line requires at least two.

Your subtractions for the window position to get the current mouse position is highly unreliable. Mainly because the window could be in any position.
To get the current position relative to the mouse, you can pass the window to getPosition, thus:
sf::Mouse::getPosition(window)

Note that the event actually provides the correct pixel position already:
const sf::Vector2i eventPosition(event.mouseButton.x, event.mouseButton.y);

Be aware, though, that this position should be mapped to account for any view:
const sf::Vector2f mousePosition(window.mapPixelToCoords(eventPosition));

Then it is just simply a matter of constructing the vertex from that Vector2f:
vertices.push_back(sf::Vertex(mousePosition));

(click to show/hide)
Title: Re: How can i do Vertex to dynamic?
Post by: WithoutBrain on December 11, 2016, 01:38:29 pm
Nice, thank you very much.