【问题标题】:SFML: Object's shape not rendered in windowSFML:对象的形状未在窗口中呈现
【发布时间】:2022-01-04 11:48:48
【问题描述】:

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

这个想法是每次记录事件sf::Mouse::isButtonPressed 时更新distribution 变量,然后在这样的向量中绘制电荷的形状。由于某种原因,我无法使其工作,我认为这是由于在事件循环中创建和销毁的对象。

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

#include "Scene.h"

int main(){

  Scene scene;

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

标头Scene.h声明窗口管理和事件轮询的类方法

#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 对象在Scene 的共同导师中实例化。现在Charge.h 声明类

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);
}

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

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.

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

【问题讨论】:

    标签: c++ oop sfml shapes


    【解决方案1】:

    您的基于范围的 for 循环使用 auto 而不是 auto&amp;,因此您不断创建 Charge 类的临时副本。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-08
      • 2014-02-13
      • 2022-12-03
      • 1970-01-01
      • 1970-01-01
      • 2012-12-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多