Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: about inheriting sf::Drawable and sf::Sprite as property.  (Read 4381 times)

0 Members and 1 Guest are viewing this topic.

digimikeh

  • Newbie
  • *
  • Posts: 18
    • View Profile
    • Email
about inheriting sf::Drawable and sf::Sprite as property.
« 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 ?...

kojack

  • Sr. Member
  • ****
  • Posts: 343
  • C++/C# game dev teacher.
    • View Profile
Re: about inheriting sf::Drawable and sf::Sprite as property.
« Reply #1 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).

digimikeh

  • Newbie
  • *
  • Posts: 18
    • View Profile
    • Email
Re: about inheriting sf::Drawable and sf::Sprite as property.
« Reply #2 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 !

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: about inheriting sf::Drawable and sf::Sprite as property.
« Reply #3 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.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

 

anything