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

Author Topic: std::vector<sf::Vertex> vs std::vector<sf::Vertex*>  (Read 1623 times)

0 Members and 1 Guest are viewing this topic.

StrawberrySwing2008

  • Newbie
  • *
  • Posts: 1
    • View Profile
    • Email
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.
« Last Edit: January 19, 2019, 11:51:30 pm by StrawberrySwing2008 »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: std::vector&lt;sf::Vertex&gt; vs std::vector&lt;sf::Vertex*&gt;
« Reply #1 on: January 20, 2019, 08:06:06 am »
As I already pointed out on Discord, SFML's draw function doesn't take an array of pointers, but it takes an array of sf::Vertex options, or more specifically it takes a pointer to the first element in an array.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/