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

Author Topic: Drawable and Transformable class  (Read 3384 times)

0 Members and 1 Guest are viewing this topic.

kubawal

  • Newbie
  • *
  • Posts: 9
    • View Profile
Drawable and Transformable class
« on: January 16, 2014, 04:07:20 pm »
I think all drawable and transformable things could have the same parent class.
For example:
namespace sf
{
class Object
: sf::Drawable, sf::Transformable
{
};

// and all drawable and transformable classes derives it
// for exmaple:
class Sprite
: sf::Object
{
//...
};
 

I came up with this idea when i written SFML and Box2d framework, when I needed to write method returning drawable object which i can set it position.

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: Drawable and Transformable class
« Reply #1 on: January 16, 2014, 04:16:36 pm »
And how would this help?  ???

Quote
returning drawable object which i can set it position.

Just return a sf::Transformable type... Or use a template  ;)

Changing the entire library just because it doesn't necessarily match your design is not the best idea. Maybe you should consider improving on your overall design.  8)
« Last Edit: January 16, 2014, 04:24:26 pm by zsbzsb »
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

kubawal

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Drawable and Transformable class
« Reply #2 on: January 16, 2014, 04:23:30 pm »
And how i can draw it?
win.draw(*((Drawable*)transformablePtr));
???

This is a very bad code, because i don't know whether transformablePtr is Drawable.
« Last Edit: January 16, 2014, 04:28:12 pm by kubawal »

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: Drawable and Transformable class
« Reply #3 on: January 16, 2014, 04:29:00 pm »
Separate your update code from your drawing code... And even better make your own object inherit from sf::Drawable
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Drawable and Transformable class
« Reply #4 on: January 16, 2014, 04:42:38 pm »
It would've been better to include the reason for such a request in the first post.

So here it is: It's cumbersome to have a container of sf::Transformable, because it requires additional tracking and casting to access the sf::Drawable functionalities. Or the other way around, it's cumbersome to have a container of sf::Drawable, because it requires additional tracking and casting to access the sf::Transformable functionalities. This is not issue for custom classes, but when it comes to reusing SFML's classes it won't work.
A class that aids as base class for Sprites and Shapes and combines Drawable and Transformable would make possible again to mix them all.

This was just to clarify what's going on here, since the OP didn't include the reasoning.

If you use anything like that in your code, make sure to give it a different name "object" is about as generic as you could ever get and nobody will understand what it actually is.

While I certainly see the issue, I'd say that your design might be flawed. I can't fully judge, since I didn't see your code. I generally wouldn't go with one container hold all objects that can be drawn or transformed, instead I'd most likely go with some sort of Entity class that has its own public API, that gets used inside the game, while the Entity itself holds a sprite/vertex array/shape and forwards the calls of its API to the inner drawable.
That way, it's easy to just create a container of Entity and you don't have to bother with Drawables and Transformables. But it really depends on what you do. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/