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

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - StrawberrySwing2008

Pages: [1]
1
Graphics / std::vector<sf::Vertex> vs std::vector<sf::Vertex*>
« on: January 19, 2019, 11:47:48 pm »
Hi, I'm new here ..

why when I use std::vector<sf::Vertex*> my lines do not appear correctly ?

#include <SFML/Graphics.hpp>
#include <iostream>

int main()
{
    sf::RenderWindow window(sf::VideoMode(400, 400), "Brasil");

    std::vector<sf::Vertex*> points;

    sf::Vector2i mousePosition;

    window.setFramerateLimit(60);

    while (window.isOpen())
    {
        sf::Event event;

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

        window.clear(sf::Color(100, 100, 100));

        if (sf::Mouse::isButtonPressed(sf::Mouse::Left))
        {
            mousePosition = sf::Mouse::getPosition(window);

            points.push_back(new sf::Vertex(sf::Vector2f(mousePosition.x, mousePosition.y)));
        }

        for (std::size_t i = 0; i < points.size(); ++i)
        {
            window.draw(points[i], 2, sf::Lines);
        }

        window.display();
    }

    return 0;
}

 

unlike the code above, it works:


#include <SFML/Graphics.hpp>
#include <iostream>

int main()
{
    sf::RenderWindow window(sf::VideoMode(400, 400), "Brasil");

    std::vector<sf::Vertex> points;

    sf::Vector2i mousePosition;

    window.setFramerateLimit(60);

    while (window.isOpen())
    {
        sf::Event event;

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

        window.clear(sf::Color(100, 100, 100));

        if (sf::Mouse::isButtonPressed(sf::Mouse::Left))
        {
            mousePosition = sf::Mouse::getPosition(window);

            points.push_back(sf::Vertex(sf::Vector2f(mousePosition.x, mousePosition.y)));
        }

        for (std::size_t i = 0; i < points.size(); ++i)
        {
            window.draw(&points[i], 2, sf::Lines);
        }

        window.display();
    }

    return 0;
}

 


I think it may be something related to continuous memory, but I'm not sure.

Pages: [1]
anything