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

Author Topic: How can i do Vertex to dynamic?  (Read 1864 times)

0 Members and 1 Guest are viewing this topic.

WithoutBrain

  • Newbie
  • *
  • Posts: 20
    • View Profile
    • Email
How can i do Vertex to dynamic?
« 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(...)
};

WithoutBrain

  • Newbie
  • *
  • Posts: 20
    • View Profile
    • Email
Re: How can i do Vertex to dynamic?
« Reply #1 on: December 11, 2016, 09:04:23 am »
SOLVED, I FIXED IT OVER VECTOR

WithoutBrain

  • Newbie
  • *
  • Posts: 20
    • View Profile
    • Email
Re: How can i do Vertex to dynamic?
« Reply #2 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;
}



Hapax

  • Hero Member
  • *****
  • Posts: 3360
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: How can i do Vertex to dynamic?
« Reply #3 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)
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

WithoutBrain

  • Newbie
  • *
  • Posts: 20
    • View Profile
    • Email
Re: How can i do Vertex to dynamic?
« Reply #4 on: December 11, 2016, 01:38:29 pm »
Nice, thank you very much.

 

anything