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...
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.
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.
精彩评论