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

Author Topic: Sprite Transformation  (Read 1805 times)

0 Members and 1 Guest are viewing this topic.

GamDev

  • Newbie
  • *
  • Posts: 29
    • View Profile
Sprite Transformation
« on: October 11, 2018, 05:48:15 pm »
Hi, please tell me how to do this on spml without shaders

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10821
    • View Profile
    • development blog
    • Email
Re: Sprite Transformation
« Reply #1 on: October 11, 2018, 05:54:00 pm »
You can just draw one on top of the other, as long as the grass is transparent.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

GamDev

  • Newbie
  • *
  • Posts: 29
    • View Profile
Re: Sprite Transformation
« Reply #2 on: October 11, 2018, 06:17:37 pm »
I did that.
But now I redid the map on VertexArray.
Quote
class DrawTileMapDungeon : public sf::Drawable//, public sf::Transformable
{
    public:
        void            load                (const Texture tileset, int ( &TileMap )[500][500]);
        void            update              (int ( &TileMap )[500][500]);
    private:
        virtual void    draw                (sf::RenderTarget& target, sf::RenderStates states) const;
        VertexArray     m_vertices;
        Texture         m_tileset;
        Vector2u        tileSize            = {64, 64};
};
Quote
#include "DrawTileMapDungeon.h"
//==================================================================================================
void DrawTileMapDungeon::load(const Texture tileset, int ( &TileMap )[500][500])
{
    m_tileset = tileset;

    m_vertices.setPrimitiveType(sf::Quads);
    m_vertices.resize(500 * 500 * 4);
}
//==================================================================================================
void DrawTileMapDungeon::update(int ( &TileMap )[500][500])
{
    int tu = 0;
    int tv = 0;

    for(unsigned int x = 0; x < 500; ++x)
        for(unsigned int y = 0; y < 500; ++y)
            {
                switch(TileMap
  • [y])

                    {
                        case 1:     tu = 0;tv = 2;break;
                        case 2:     tu = 1;tv = 2;break;
                        case 3:     tu = 2;tv = 2;break;
                        case 4:     tu = 3;tv = 2;break;
                        case 5:     tu = 4;tv = 2;break;
                        case 6:     tu = 0;tv = 3;break;
                        case 7:     tu = 1;tv = 3;break;
                        case 8:     tu = 2;tv = 3;break;
                        case 9:     tu = 3;tv = 3;break;
                        case 10:    tu = 4;tv = 3;break;

                        default:tu = 0;tv = 0;break;
                    }

                sf::Vertex* tile64 = &m_vertices[(x + y * 500) * 4];

                tile64[0].position = sf::Vector2f(x * tileSize.x, y * tileSize.y);
                tile64[1].position = sf::Vector2f((x + 1) * tileSize.x, y * tileSize.y);
                tile64[2].position = sf::Vector2f((x + 1) * tileSize.x, (y + 1) * tileSize.y);
                tile64[3].position = sf::Vector2f(x * tileSize.x, (y + 1) * tileSize.y);

                tile64[0].texCoords = sf::Vector2f(tu * tileSize.x, tv * tileSize.y);
                tile64[1].texCoords = sf::Vector2f((tu + 1) * tileSize.x, tv * tileSize.y);
                tile64[2].texCoords = sf::Vector2f((tu + 1) * tileSize.x, (tv + 1) * tileSize.y);
                tile64[3].texCoords = sf::Vector2f(tu * tileSize.x, (tv + 1) * tileSize.y);
            }
}
//==================================================================================================
void DrawTileMapDungeon::draw(sf::RenderTarget& target, sf::RenderStates states) const
{
    //states.transform *= getTransform();
    states.texture = &m_tileset;
    target.draw(m_vertices, states);
}
//==================================================================================================

I understand that you need to first draw VertexArray and then again grass.
but I have over 30 sprites now.
Could it be easier?

Hapax

  • Hero Member
  • *****
  • Posts: 3351
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Sprite Transformation
« Reply #3 on: October 15, 2018, 10:05:35 pm »
Do you mean that your grass is multiple sprites? Just use another vertex array for the grass layer. You could, technically, use a single vertex array for both layers; the grass layer just needs to be after the lower layer.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*