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

Author Topic: class for my own textures using vertex array  (Read 1122 times)

0 Members and 1 Guest are viewing this topic.

CreaM

  • Newbie
  • *
  • Posts: 36
    • View Profile
class for my own textures using vertex array
« on: April 06, 2014, 06:31:52 pm »
Hello, im trying learn vertex arrays and i wrrote very similiar class to title map in 2.1sfml tut.
But when i run the program there are just triangles and other suff which shouldnt be there...
Actually the ounly difference between my class and class from tutorial is that im using just one FOR cycle - the variables to place vertices are derived from it(in tut there are two cycles)

class MAPA : public sf::Drawable, public sf::Transformable
{
public:

    bool nacteni(const std::string& nazev_obrazku, sf::Vector2u velikost_dlazdice, const int* pole, unsigned int pocet_poli_sirka, unsigned int pocet_poli_vyska)
    {
        if (!textura.loadFromFile(nazev_obrazku))
            return false;

        vrcholy.setPrimitiveType(sf::Quads);
        vrcholy.resize(pocet_poli_sirka * pocet_poli_vyska * 4);

                int b = 0,c = 0;  //this is for position of quad

                for (unsigned int a = 0; a < pocet_poli_sirka * pocet_poli_vyska; ++a){
                        int cislo_dlazdice = pole[a];
                        sf::Vertex* finalni = &vrcholy[a];

                        if(b == pocet_poli_sirka){b = 0;c ++;}

            finalni[0].position = sf::Vector2f(b * velikost_dlazdice.x, c * velikost_dlazdice.y);
            finalni[1].position = sf::Vector2f((b + 1) * velikost_dlazdice.x, c * velikost_dlazdice.y);
            finalni[2].position = sf::Vector2f((b + 1) * velikost_dlazdice.x, (c + 1) * velikost_dlazdice.y);
            finalni[3].position = sf::Vector2f(b * velikost_dlazdice.x, (c + 1) * velikost_dlazdice.y);
                         
                        int e, d;
                        if (cislo_dlazdice == 0){e = 22;d = 30;}
                        else if (cislo_dlazdice == 1){e = 228;d = 30;}
                        else if (cislo_dlazdice == 2){e = 10;d = 132;}
                        else if (cislo_dlazdice == 3){e = 216;d = 132;}
                        else std::cout<<"imposible";
                       
                        finalni[0].texCoords = sf::Vector2f(e,d);
            finalni[1].texCoords = sf::Vector2f(e + 10 ,d);
            finalni[2].texCoords = sf::Vector2f(e + 10,d + 10);
            finalni[3].texCoords = sf::Vector2f(e ,d + 10);
                       
                        b ++;
            }

        return true;
    }

private:

    virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const
    {
       
        states.transform *= getTransform();
                states.texture = &textura;
                target.draw(vrcholy, states);
    }

    sf::VertexArray vrcholy;
    sf::Texture textura;
};

 
Im sorry for names of variables, but the original code is realy similiar The setting texture to vertex is just done little bit easier(in my opinion).
Thank you

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: class for my own textures using vertex array
« Reply #1 on: April 06, 2014, 06:56:04 pm »
With this code:

Quote
for (...; ++a)
{
    sf::Vertex* finalni = &vrcholy[a];

    ...

    finalni[0].position = ...;
    finalni[1].position = ...;
    finalni[2].position = ...;
    finalni[3].position = ...;
}

Here you end up doing this:

// first iteration of the loop
vrcholy[0].position = ...;
vrcholy[1].position = ...;
vrcholy[2].position = ...;
vrcholy[3].position = ...;

// second iteration of the loop
vrcholy[1].position = ...;
vrcholy[2].position = ...;
vrcholy[3].position = ...;
vrcholy[4].position = ...;

// etc.
 

There should be a *4 in index calculations, to take in account that 1 tile produces 4 vertices.
Laurent Gomila - SFML developer

CreaM

  • Newbie
  • *
  • Posts: 36
    • View Profile
Re: class for my own textures using vertex array
« Reply #2 on: April 06, 2014, 07:24:55 pm »
Thank you much, - i replaced
sf::Vertex* finalni = &vrcholy[a];
 
with
sf::Vertex* finalni = &vrcholy[(b + c * pocet_poli_sirka) * 4];
 
...and now its working ,thx.
 :)