Search code examples
c++open-sourcetransformeditsfml

How to edit SFML source code to add a new Drawable Object?


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...


Solution

  • 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.