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

Author Topic: Custom shapes with textures  (Read 1350 times)

0 Members and 1 Guest are viewing this topic.

Sakman

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
Custom shapes with textures
« on: June 24, 2018, 11:57:23 am »


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.
« Last Edit: June 24, 2018, 11:59:17 am by Sakman »

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Custom shapes with textures
« Reply #1 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::Rectangles, two sf::Sprites or a single sf::VertexArray 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).
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*