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

Author Topic: Can't see my vertex array.  (Read 1798 times)

0 Members and 1 Guest are viewing this topic.

Bonediggerninja

  • Newbie
  • *
  • Posts: 10
    • View Profile
Can't see my vertex array.
« on: January 02, 2018, 02:32:33 pm »
Based on the tileset from the vertex array tutorial, I have tried to make my own variant for hearts, since i know the tile size and that it will only be one row, I can eliminate some variables.

class health: public sf::Drawable, public sf::Transformable {
public:
        int hp;
        sf::Texture heartTexture;
       
        sf::VertexArray m_vertices;
        void init(void) {
                heartTexture.loadFromFile("Heart.png");
                hp = 12;
        }
        void load(void) {
                int x = 0;
                m_vertices.setPrimitiveType(sf::Quads);
                m_vertices.resize(ceil(hp / 4));
                //Looping throughout each heart.
                for (x = 0; x <= m_vertices.getVertexCount(); x++) {
                       
                        if (x == m_vertices.getVertexCount()) {
                                sf::Vertex* quad = &m_vertices[x];
                                quad[0].position = sf::Vector2f(10 + x * 32, 50);
                                quad[1].position = sf::Vector2f(10 + ((x + 1) * 32), 50);
                                quad[2].position = sf::Vector2f(10 + ((x + 1) * 32), 82);
                                quad[3].position = sf::Vector2f(10 + x * 32, 82);

                                quad[0].texCoords = sf::Vector2f((4-(hp % 4)) * 32, 0);
                                quad[1].texCoords = sf::Vector2f(4-((hp % 4)) * 32 + 32, 0);
                                quad[2].texCoords = sf::Vector2f((4-(hp % 4)) * 32 + 32, 32);
                                quad[3].texCoords = sf::Vector2f((4-(hp % 4)) * 32, 32);
                        }
                        else {
                                sf::Vertex* quad = &m_vertices[x];
                                quad[0].position = sf::Vector2f(10 + x * 32,50);
                                quad[1].position = sf::Vector2f(10 + ((x + 1) * 32), 50);
                                quad[2].position = sf::Vector2f(10 + ((x + 1) * 32), 82);
                                quad[3].position = sf::Vector2f(10 + x * 32, 82);

                                quad[0].texCoords = sf::Vector2f(0, 0);
                                quad[1].texCoords = sf::Vector2f(0 + 32, 0);
                                quad[2].texCoords = sf::Vector2f(0 + 32, 32);
                                quad[3].texCoords = sf::Vector2f(0, 32);

                        }// End If else
                }
        }// end of function
private:
        virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const
        {
                // apply the transform
                states.transform *= getTransform();
                states.texture = &heartTexture;

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

and a simplified version of the main that uses it.
int main() {
       
       
       
        sf::RenderWindow windows(sf::VideoMode(800,600), "Game");
        windows.setFramerateLimit(60);
health p1Health;
        p1Health.init();
        p1Health.load();
while (windows.isOpen()) {
       
                        // check all the window's events that were triggered since the last iteration of the loop
                        sf::Event event;
                        while (windows.pollEvent(event))
                        {
                                // "close requested" event: we close the window
                                if (event.type == sf::Event::Closed)
                                        windows.close();
                        }
        windows.clear();
                        windows.draw(p1.charSprite);
                        windows.draw(box.hurtbox);
                        windows.draw(p1Health);
                        windows.display();

        }
       
        return 0;
}
however when I load up the program I see no heart, but there are no errors. Each heart image is 32*32, and there are four of them in one texture. The image is attached.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Can't see my vertex array.
« Reply #1 on: January 02, 2018, 02:51:31 pm »
Your code has some more or less serious mistakes, but most importantly: why are you basing the whole stuff on tilemap creation, while you only want to display a single heart from your spritesheet?

Unless I'm wrong about what you're trying to do, the following code is enough:
// assuming hp is between 0 and 3
auto frame = 3 - hp; // because your spritesheet is reversed (it goes from full hp to empty)

sf::Sprite heart(heartTexture);
heart.setTextureRect(sf::IntRect(frame * 32, 0, 32, 32));
Laurent Gomila - SFML developer

Bonediggerninja

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: Can't see my vertex array.
« Reply #2 on: January 02, 2018, 02:56:03 pm »
I want to display a whole row of hearts, not just one. Also, could you point out some of the serious mistakes, I know a few of them that I'm going to fix but it'll be good to know the rest. Thanks.

NGM88

  • Full Member
  • ***
  • Posts: 162
    • View Profile
Re: Can't see my vertex array.
« Reply #3 on: January 02, 2018, 04:51:48 pm »
the argument of resize should be: the number of hearts you want to display * 4 (because quads);

your loop should not be running for vertexcount, it should be running for quad count.

not gonna check the positions / texcoords, numbers make me dizzy :P

 

anything