SFML:Object 的形状未在 window 中呈现

SFML: Object's shape not rendered in window

我希望在 window 上按下鼠标按钮时能够渲染 sf::CircleShape(代表逐点收费)。问题很简单,但是我要绘制的形状是 class Charge 的属性。 Scene class 实现了 window 管理和事件轮询/渲染方法,它有一个属性 std::vector<Charge> distribution.

想法是在每次记录事件 sf::Mouse::isButtonPressed 时更新 distribution 变量,然后在该矢量内绘制电荷的形状。出于某种原因,我无法让它工作,我认为这是由于 object 在事件循环中被创建和销毁。

我有一个看起来像这样的主

#include "Scene.h"

int main(){

  Scene scene;

  while(scene.running())
  {
    scene.update();
    scene.render();
  }
  return 0;
}

使用 header Scene.h 声明用于 window 管理和事件轮询的 class 方法

#include "Charge.h"

class Scene
{
    private:
        sf::RenderWindow* window;
        sf::Event event;
        std::vector<Charge> distribution;
    
public:
        Scene();
        virtual ~Scene();

        bool running();
        void polling();
        void render();
        void update();
};

游戏循环中实例化的方法定义为

void Scene::update(){this -> polling();}

void Scene::polling()
{
    while(this -> window -> pollEvent(this -> event))
    {
        switch(this -> event.type)
        {
            case sf::Event::Closed: this -> window -> close();
                break;

            case sf::Event::MouseButtonPressed:
                this -> distribution.push_back(Charge(*this -> window, sf::Mouse::getPosition(*this -> window));
                std::cout << "Distribution size = " << distribution.size() << "\n";
                break;
        }
    }
}

void Scene::render()
{
    this -> window -> clear(sf::Color::Black);

    for(auto charge: this -> distribution)
    {
        charge.render(*this -> window);
    }

    this -> window -> display();
}

window object 实例化在 Scene 的构造函数中。现在 Charge.h 声明 class

class Charge
{
    private:
        sf::CircleShape shape;

    public:
        Charge(const sf::RenderWindow& window, sf::Vector2i position);
        virtual ~Charge();

        void render(sf::RenderTarget& target);
};

其方法定义如下

Charge::Charge(const sf::RenderWindow& window, sf::Vector2i position)
{
    std::cout << "Charge's object created!" << std::endl;
    this -> shape.setOrigin(sf::Vector2f(static_cast<float>(position.x), static_cast<float>(position.y)));
    this -> shape.setFillColor(sf::Color(255,50,50));
    this -> shape.setRadius(25.f);
}

Charge::~Charge(){std::cout << "Charge's object destroyed!" << std::endl;}

void Charge::render(sf::RenderTarget& target)
{
    target.draw(this -> shape);
}

我在构造函数和析构函数中添加了终端打印。按下鼠标按钮时,程序的执行不会呈现任何 object 的形状。然而终端报告

Charge's object created! # <-- Here I pressed the mouse's button
Charge's object destroyed!
Distribution size = 1
Charge's object destroyed!
Charge's object destroyed!
Charge's object destroyed!
Charge's object destroyed!
Charge's object destroyed!
Charge's object destroyed!
Charge's object destroyed! # <-- And it goes on and on as long as the window is not closed.

我尝试以各种方式解决问题,但 none 到目前为止都有效。有什么想法吗?

您的基于范围的 for 循环使用 auto 而不是 auto&,因此您不断地创建 Charge class.[=16= 的临时副本]

此外,您不应将 sf::Mouse::getPosition() 与事件混用。鼠标事件已经提供了位置:event.mouseButton.x / event.mouseButton.y.