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 ?
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.
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;
}
#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.