如何编辑 SFML 源代码以添加新的可绘制对象?

发布于 2024-11-19 04:26:13 字数 989 浏览 3 评论 0原文

嘿,我正在开发一个名为“Body”的类,它将形状和精灵作为一个对象保存在一起。我想进入源代码并添加一个新的重载RenderWindow的Draw()函数,以便可以轻松地接收和绘制这个新对象。 我该怎么做?

我目前使用的是

  • Windows 7
  • SFML 1.6
  • 新的 msVS++ 2010 编译的静态调试库和 dll
  • 原始包含文件夹

编辑:

我还在 Drawable.hpp 标头中找到了这个:

private :

    friend class RenderTarget;

////////////////////////////////////////////////////////////
/// Draw the object into the specified window
///
/// \param Target : Target into which render the object
///
////////////////////////////////////////////////////////////
void Draw(RenderTarget& Target) const;

////////////////////////////////////////////////////////////
/// Render the specific geometry of the object
///
/// \param Target : Target into which render the object
///
////////////////////////////////////////////////////////////
virtual void Render(RenderTarget& Target) const = 0;

但我可以不知道每个函数的完整代码在哪里,只知道声明。 不幸的是,我也没有在那里找到迷你教程......

Hey i'm working on a class called a "Body" which holds shapes and sprites together as one object. I would like to get into the source code and add a new overload RenderWindow's Draw() function, so this new object can be taken in and drawn easily. How do i do this?

I'm currently using

  • Windows 7
  • SFML 1.6
  • Newly msVS++ 2010 compiled static debug libraries and dlls
  • original include folder

EDIT:

I also found this in the Drawable.hpp header:

private :

    friend class RenderTarget;

////////////////////////////////////////////////////////////
/// Draw the object into the specified window
///
/// \param Target : Target into which render the object
///
////////////////////////////////////////////////////////////
void Draw(RenderTarget& Target) const;

////////////////////////////////////////////////////////////
/// Render the specific geometry of the object
///
/// \param Target : Target into which render the object
///
////////////////////////////////////////////////////////////
virtual void Render(RenderTarget& Target) const = 0;

but i can't figure out where the full code of each function is, just the declarations.
I didn't find a mini tutorial there either unfortunately...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

天煞孤星 2024-11-26 04:26:13

注意:

在派生并实现自己的 Drawable 之前,您可能需要考虑是否需要这样做。 SFML 的作者声明 sf::Drawable 最初并不打算在 SFML 之外进行子类化

除此之外,

对于 SFML 1.6:

看来您所需要做的就是从 sf::Drawable 派生类,然后实现虚拟 Render函数。

class MyDrawable : public sf::Drawable {

private:

    virtual void Render(RenderTarget& target) const {
        // Do some rendering of whatever...
        target.Draw(mySubSprite);
    }

    sf::Sprite mySubSprite;

};

在 SFML 论坛上可以找到这样的示例

对于 SFML 2.0:

< SFML 中的 code>Drawable 头文件包含描述如何派生您自己的 Drawable 类的注释。您无需修改​​ SFML 源代码即可创建新的 Drawable。

它还包括一个简单的示例:

class MyDrawable : public sf::Drawable
{
public :
   ...
private :
    virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const
    {
        // You can draw other high-level objects
        target.draw(m_sprite, states);
        // ... or use the low-level API
        states.texture = &m_texture;
        target.draw(m_vertices, states);
        // ... or draw with OpenGL directly
        glBegin(GL_QUADS);
        ...
        glEnd();
    }
    sf::Sprite m_sprite;
    sf::Texture m_texture;
    sf::VertexArray m_vertices;
};

此示例可能适用于 SFML 2.0,但如果您从任何版本的 SFML 检查 Drawable.hpp,它都应该包含类似的示例。

Note:

Before you derive from and implemented your own Drawable, you may want to consider if you need to do it at all. The author of SFML has stated that sf::Drawable was not initially meant to be subclassed outside of SFML.

That aside,

For SFML 1.6:

It appears that all you need to do is derive your class from sf::Drawable, and then implement a virtual Render function.

class MyDrawable : public sf::Drawable {

private:

    virtual void Render(RenderTarget& target) const {
        // Do some rendering of whatever...
        target.Draw(mySubSprite);
    }

    sf::Sprite mySubSprite;

};

An example of this can be found on the SFML forums.

For SFML 2.0:

The Drawable header file from SFML contains comments that describe how to derive your own Drawable classes. You do not need to modify the SFML source code to create new Drawables.

It also includes a simple example:

class MyDrawable : public sf::Drawable
{
public :
   ...
private :
    virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const
    {
        // You can draw other high-level objects
        target.draw(m_sprite, states);
        // ... or use the low-level API
        states.texture = &m_texture;
        target.draw(m_vertices, states);
        // ... or draw with OpenGL directly
        glBegin(GL_QUADS);
        ...
        glEnd();
    }
    sf::Sprite m_sprite;
    sf::Texture m_texture;
    sf::VertexArray m_vertices;
};

This example may apply to SFML 2.0, but if you inspect the Drawable.hpp from whatever version of SFML you have it should contain a similar example.

絕版丫頭 2024-11-26 04:26:13

RenderWindow::Draw 采用抽象类类型 Drawable 的对象。这意味着,理论上,您可以将您的 Body 类作为 Drawable 的子类,并重载一些虚拟方法以使其呈现。

但事实似乎并非如此。 Drawable 的文档显示只有该类中有一个虚函数:析构函数。这……有点愚蠢。

然而,外表可能具有欺骗性。我正在检查2.0文档,看看他们是否已经弄清楚如何正确地创建继承层次结构,结果证明它们确实有要重写的虚拟方法。只是它们都是私有的(这本身很好,实际上是一件非常好的事情),并且 SFML 人员没有告诉 Doxygen 为私有成员生成文档。我就此向他们提交了一个错误。

在他们更新文档之前,我唯一能说的就是查看标头,也许还有 Sprite 的源代码,并尝试找出如何正确创建派生的 Drawable 类。

RenderWindow::Draw takes an object of the abstract class type Drawable. Which means that, in theory, you can just make your Body class a child of Drawable and overload some virtual methods to make it render.

But that doesn't seem to be the case. The docs for Drawable show that there's only one virtual function in that class: the destructor. Which is... kinda stupid.

However, looks can be deceiving. I was checking the 2.0 documentation to see if they had figured out how to make an inheritance hierarchy correctly, and it turns out that they do have virtual methods to override. It's just that they're all private (which itself is fine, and in fact a very good thing), and the SFML guys didn't tell Doxygen to generate documentation for private members. I filed a bug with them on this.

Until they update their docs, the only thing I can say is to look at the header, and maybe the source code to Sprite, and try to figure out how to create a derived Drawable class correctly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文