使用 sfml 从对象向量中绘制

Drawing from a vector of objects using sfml

我有一个名为 Particle 的 class,我想创建一个粒子向量。问题是我的代码没有绘制存储在向量中的对象。我尝试了不同的实现,第一个是:
std::vector<Particle> particles(10);
然后我填充了向量:
for(size_t i = 0; i < 10; ++i) particles.emplace_back(new Particle(false));
我使用的绘图:
for(const auto& x: particles) window.draw(x);
问题是这只绘制了一个对象。然后我尝试创建一个 class 的粒子容器,向量为 shared_ptr:
std::vector<std::shared_ptr<Particle>> particle_container; 而且问题是一样的,它只是绘制了一个粒子,我不知道是我实现 class 粒子有问题还是我使用 sfml 的 draw 函数的方式有问题。这是我的粒子 class:

#include "SFML/Graphics.hpp"
#include <random>
#include <cmath>
#include <limits>
#include <memory>
using namespace sf;
class Particle : public sf::Drawable{
    sf::CircleShape particle;
    Vector2f position; //current position of the particle
    Vector2f velocity;  //velocity of the particle
    double radius; //radius of each particle
    double mass; //mass of the particle
    int count; //number of collisions of each particle
    bool sick; //if the particle is infected
    Color color; //color of the particle
public:
    //constructors
    Particle(Vector2f _position, Vector2f _velocity, double _radius, double _mass, bool _sick);
    Particle(bool sick);
}:

Particle.cpp:


#include "Particle.h"

template<class T>
double Rand(T first, T last) {
    std::random_device dev;
    std::default_random_engine eng(dev());
    std::uniform_real_distribution<double> dis(first, last);
    return dis(eng);
}

Particle::Particle(Vector2f _position, Vector2f _velocity, double _radius, double _mass, bool _sick) {
    position = _position;
    velocity = _velocity;
    radius = _radius;
    mass = _mass;
    sick = _sick;

    if(sick) color = sf::Color::Green;
    else color = sf::Color::Red;

    //giving values to the particle
    particle.setPosition(position);
    particle.setRadius(radius);
    particle.setFillColor(color);
}

Particle::Particle(bool sick) {
    position.x = Rand(0.0, 550.0);
    position.y = Rand(0.0, 550.0);
    velocity.x = Rand(-0.005, 0.005);
    velocity.y = Rand(-0.005, 0.005);
    radius = 2.0;
    mass = 0.5;

    if(sick) color = sf::Color::Green;
    else color = sf::Color::Red;

    particle.setPosition(position);
    particle.setRadius(radius);
    particle.setFillColor(color);
}

这是 particles_container

class particles_container : public sf::Drawable {
    std::vector<std::shared_ptr<Particle>> particle_container;
    //std::vector<Particle> particle_container;
    virtual void draw(sf::RenderTarget &renderTarget, sf::RenderStates renderStates) const;
public:
    //default constructor
    particles_container();

    //constructor with a number of given particles
    explicit particles_container(size_t size);

    //default destructor
    ~particles_container() override;

    //returns the container
    std::vector<std::shared_ptr<Particle>> getContainer();

    //another function to draw
    void drawContainer(sf::RenderWindow &window);
};

particles_container.cpp:

void particles_container::draw(RenderTarget &renderTarget, sf::RenderStates renderStates) const {
    for (const auto & i : particle_container) {
        renderTarget.draw(*i);
    }
}

particles_container::particles_container(size_t size) {
    if(size > 0){
        for(size_t i = 0; i < size; ++i) particle_container.emplace_back(new Particle(false));
    }
}

std::vector<std::shared_ptr<Particle>> particles_container::getContainer() {
    return particle_container;
}

void particles_container::drawContainer(RenderWindow &window) {
    for (auto & i : particle_container) {
        window.draw(*i);
    }
}

最后这是我的 main.cpp:

#include "Particle.h"
#include "particles_container.h"
int main() {
    sf::RenderWindow window(sf::VideoMode (600, 600), "PF");
    std::vector<Particle> particles(10);
    for(size_t i = 0; i < 10; ++i) particles.emplace_back(new Particle(false));
    Particle p1(sf::Vector2f(45.0,45.0), sf::Vector2f(0.01,0.005), 2, 0.5, false);
    particles_container parts(100);
    Particle p3(true);
    std::vector<std::shared_ptr<Particle> > my_vector(10);
    for(size_t i = 0; i < 10; ++i) my_vector.emplace_back(new Particle(false));

    while(window.isOpen()){
        sf::Event event;
        while(window.pollEvent(event)){
            switch (event.type) {
                case sf::Event::Closed:
                    window.close();
                    break;
            }
        }
        window.clear();
        //parts.drawContainer(window);
        //window.draw(parts);
        for(const auto& x: particles) window.draw(x);
        window.display();
    }
}

这是输出: output

如有任何帮助或建议,我们将不胜感激,谢谢。

我正在使用:
SFML 2.5.1 和 c++17

你的Rand函数每次都会构造一个新的随机设备,很有可能总是return相同的数字序列。将 dev 的构造移出 Rand 函数。