SFML community forums

General => General discussions => Topic started by: digimikeh on January 12, 2022, 04:10:55 am

Title: about inheriting sf::Drawable and sf::Sprite as property.
Post by: digimikeh on January 12, 2022, 04:10:55 am
Hello

I'm following the "SFML Game Development" book from Packt, in its example it creates a scenenode class that inherits from sf::Drawable.. that's good, as I understand it serves to draw my scenenode as simply typing

myRenderWindow.draw(mySceneNode);

it has so much sense..!

but then, there is a spritenode class that inherits from scenenode class... but spritenode has a sf::Sprite property..

when i try to draw mySpriteNode object, the build is success.. but nothing shows on the screen...
but when i draw mySpriteNode.m_sprite (assuming m_sprite is public), then the sprite shows on the screen...

is this the expected behaviour ?...
Title: Re: about inheriting sf::Drawable and sf::Sprite as property.
Post by: kojack on January 12, 2022, 09:34:35 am
I haven't read that book, but I'm guessing how they've probably structured it.
The scene node class must have a draw method, so the myRenderWindow.draw(mySceneNode); works.
If you make new kinds of scene nodes (like a sprite node), they need their own draw method that overrides (replaces) the scene node draw method.

So in your sprite node class you'd need something like:
void SpriteNode::draw(RenderTarget& target, RenderStates states) const
{
   target.draw(m_sprite);
}
So when you tell the sprite node to draw, it in turn tells the sprite member to draw.

Is that function there? Make sure it's name and parameters are identical to the one in scene node (if you called it Draw instead of draw it will call the wrong one).
Title: Re: about inheriting sf::Drawable and sf::Sprite as property.
Post by: digimikeh on January 13, 2022, 02:51:17 am
yes, that solved my problem!..

i was wondering why the book does not mention this function... may i need to figure it out ..

thanks !
Title: Re: about inheriting sf::Drawable and sf::Sprite as property.
Post by: Nexus on January 13, 2022, 11:19:34 am
The entire source code of the book is also available here:
https://github.com/SFML/SFML-Game-Development-Book

It helps understand the context. The code is separated by chapters, so you're not overwhelmed with content the book hasn't taught yet.