SFML community forums

Help => Graphics => Topic started by: Sakman on June 24, 2018, 11:57:23 am

Title: Custom shapes with textures
Post by: Sakman on June 24, 2018, 11:57:23 am
(https://en.sfml-dev.org/forums/index.php?action=dlattach;topic=24186.0;attach=3854;image)

I want to make a custom shape that has two textures. What I learned from the tutorials is that I need to make a class that inherits from sf::Drawable and sf::Transformable, and then overload the draw function.

This was from the tutorial:
class MyEntity : public sf::Drawable, public sf::Transformable
{
public:

    // add functions to play with the entity's geometry / colors / texturing...

private:

    virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const
    {
        // apply the entity's transform -- combine it with the one that was passed by the caller
        states.transform *= getTransform(); // getTransform() is defined by sf::Transformable

        // apply the texture
        states.texture = &m_texture;

        // you may also override states.shader or states.blendMode if you want

        // draw the vertex array
        target.draw(m_vertices, states);
    }

    sf::VertexArray m_vertices;
    sf::Texture m_texture;
};
 

Could someone post a simple example with two rectangles or give me a better option, please. Thank you.
Title: Re: Custom shapes with textures
Post by: Hapax on June 24, 2018, 10:52:48 pm
Just give them both co-ordinates that place them next to each other, based around (0, 0) being the origin of the object. Then, when they're transformed, they will be transformed together.

Whether you decide to draw the two rectangles as two sf::Rectangle (https://www.sfml-dev.org/tutorials/2.5/graphics-shape.php)s, two sf::Sprite (https://www.sfml-dev.org/tutorials/2.5/graphics-sprite.php)s or a single sf::VertexArray (http://listOfButtons.setText("Hey");) is entirely up to you. The latter is a little bit more complicated but not much and halves the number of draw calls made (2 down to 1).