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

Author Topic: combining std::vector<name of object> object to vertex array  (Read 6135 times)

0 Members and 1 Guest are viewing this topic.

Flaze07

  • Jr. Member
  • **
  • Posts: 64
    • View Profile
    • Email
hey guys...now I know how vertex array works..
so I have a game that has an enemy that multiplies by 2 when you killed it...
so considering I use loop to draw it...it gets too slow eventually
now.. I have this class
class Zombie
{
private :
    sf::RectangleShape Rect;
    const float speed;
    int health;
public :
    Zombie(const sf::RenderWindow& window, float speedVal, int healthVal) :
        speed(speedVal),
        health(healthVal)
    {
        std::srand(time(0));
        Rect.setSize(sf::Vector2f(37, 37));
        Rect.setOrigin(Rect.getSize().x / 2, Rect.getSize().y / 2);
        sf::Vector2f position;
        rand();
        position.x = rand() % window.getSize().x;
        rand();
        position.y = rand() % window.getSize().y;
        Rect.setPosition(position);
        Rect.setFillColor(sf::Color(0, 200, 0));
    }
    void move(User& user)
    {
        if (Rect.getPosition().x < user.getPosition().x)
        {
            Rect.move(speed, 0);
        }
        else if (Rect.getPosition().x > user.getPosition().x)
        {
            Rect.move(-(speed), 0);
        }
        if (Rect.getPosition().y < user.getPosition().y)
        {
            Rect.move(0, speed);
        }
        else if (Rect.getPosition().y > user.getPosition().y)
        {
            Rect.move(0, -(speed));
        }
        sf::Vector2f difference;
        difference.x = user.getPosition().x - Rect.getPosition().x;
        difference.y = user.getPosition().y - Rect.getPosition().y;
        float face = std::atan2(difference.y, difference.x);
        float degrees = face * pi / 180;
        Rect.setRotation(degrees);
    }
    void reInit(const sf::RenderWindow& window)
    {
        srand(time(0));
        rand();
        sf::Vector2f position;
        position.x = rand() % window.getSize().x;
        rand();
        position.y = rand() % window.getSize().y;
        Rect.setPosition(position);
        health = 50;
    }
    void decHealth(int val)
    {
        health -= val;
    }
    const int& getHealth() const
    {
        return health;
    }
    const sf::Shape& getShape() const
    {
        return Rect;
    }
    bool hit(User& user)
    {
        if (Rect.getGlobalBounds().intersects(user.getGlobalBounds())) return true;
        return false;
    }
};

 

and I have this code
std::vector<Zombie> zombie;
 

considering the fact that when it gets too big it becomes slow...
how do I compress it to vertex array rather how do I transform only a member of the vertex array

Turbine

  • Full Member
  • ***
  • Posts: 100
    • View Profile
Re: combining std::vector<name of object> object to vertex array
« Reply #1 on: May 08, 2017, 07:28:23 pm »
Loop through your zombies and add entries for them into the vertex array - then draw the vertex array. Remember, it's not the long making it slow - it's the individual draw calls with their context switching overheads

Flaze07

  • Jr. Member
  • **
  • Posts: 64
    • View Profile
    • Email
Re: combining std::vector<name of object> object to vertex array
« Reply #2 on: May 09, 2017, 07:26:46 am »
well...I tried to do that...I failed instead....because now they are not drawn on the screen

btw..here's the piece of code to do what you said
if (drawZombie.getVertexCount() < zombie.size() * 4)
        {
            for (int i = 0; i < zombie.at(zombie.size() - 1).getShape().getPointCount(); ++i)
            {
                drawZombie.append(zombie.at(zombie.size() - 1).getShape().getPoint(i));
            }
        }
        for (int i = 0; i < zombie.size(); ++i)
        {
            for (int j = 0; j < zombie.at(i).getShape().getPointCount(); ++j)
            {
                drawZombie[(((i + 1) * 4) - 1) - (3 - j)].position = zombie.at(i).getShape().getPoint(j);
            }
        }
        for (int i = 0; i < drawZombie.getVertexCount(); ++i)
        {
            drawZombie[i].color = sf::Color::Green;
        }
 

and drawZombie's primitive type is of quads
« Last Edit: May 09, 2017, 07:29:45 am by Flaze07 »

Flaze07

  • Jr. Member
  • **
  • Posts: 64
    • View Profile
    • Email
Re: combining std::vector<name of object> object to vertex array
« Reply #3 on: May 09, 2017, 07:39:40 am »
well, rather than not getting drawn...it is drawn but it is stuck on 1 place the left-upper corner of the screen

Flaze07

  • Jr. Member
  • **
  • Posts: 64
    • View Profile
    • Email
Re: combining std::vector<name of object> object to vertex array
« Reply #4 on: May 09, 2017, 08:03:43 am »
I figured the problem already..

zombie.at(i).getShape().getPoint() returns sf;:Vector corresponding to the left hand corner of the screen

Flaze07

  • Jr. Member
  • **
  • Posts: 64
    • View Profile
    • Email
Re: combining std::vector<name of object> object to vertex array
« Reply #5 on: May 09, 2017, 10:48:16 am »
Well, I managed to draw em' using vertex array but the problem still presents..