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

Author Topic: Is there a way to combine (add) sprites?  (Read 4090 times)

0 Members and 1 Guest are viewing this topic.

deso

  • Newbie
  • *
  • Posts: 13
    • View Profile
Is there a way to combine (add) sprites?
« on: November 15, 2015, 02:56:30 am »
Say, I have this sprite from a robot texture, BUT THE ROBOT IS ON FIRE!!!

And I want to draw a robot on fire.

Luckily I have a semi-transparent fire texture that I want to draw over the robot. I know I can just draw the robot sprite, and then draw the fire sprite over it. But that would require using two sprites.

To keep things simple, I made my robot class give out its own sprite (so it can handle its own movement animations and stuff), but it can only return a single sprite. And I would prefer that the robot class itself detected the robot is on fire and gave out a sprite with the robot on fire to be drawn.

Any way to achieve this (other than duplicating all my sprite sheets and adding a fire on the copies)?

BlueCobold

  • Full Member
  • ***
  • Posts: 105
    • View Profile
Re: Is there a way to combine (add) sprites?
« Reply #1 on: November 15, 2015, 08:47:11 am »
Any way to achieve this (other than duplicating all my sprite sheets and adding a fire on the copies)?
Actually this. You can do that also via code at runtime (by using Images or RenderTextures), but I think you should consider refactoring your code. I also had such a one-sprite-approach (actually a one-bounding-box return). It didn't work out. I had to expand to return multiple. It's the far more flexible approach though. Everything else looks like dirty workarounds. As you already see now.

fallahn

  • Hero Member
  • *****
  • Posts: 507
  • Buns.
    • View Profile
    • Trederia
Re: Is there a way to combine (add) sprites?
« Reply #2 on: November 15, 2015, 06:59:21 pm »
When you say 'can only return a single sprite' I'm assuming you're doing something like

renderWindow.draw(robot.getSprite());

If this is the case a better approach would be to look at the sf::Drawable class. You can then do something like this:

class Robot : public sf::Drawable
{
public:
    Robot();
   
private:
    sf::Sprite m_robotSprite;
    sf::Sprite m_fireSprite;

    void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
}
 
and:
void Robot::draw(sf::RenderTarget& target, sf::RenderStates states) const
{
    target.draw(m_robotSprite);
    if(onFire) target.draw(m_fireSprite);
}
 

and then to draw your robot you only need do:
renderWindow.draw(robot);
 

You can then draw multiple sprite members within your robot class. If you're concerned about using multiple sprites from a performance point of view, however, you can replace all the sprites with a single vertex array, drawing only parts of a sprite sheet containing both the robot and fire texture as needed (the vertex array tutorial goes more into this).

 

anything