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

Author Topic: [SOLVED] [sf::VertexArray VS std::vector] Strange behaviour  (Read 1766 times)

0 Members and 1 Guest are viewing this topic.

Adrisaboss

  • Newbie
  • *
  • Posts: 14
    • View Profile
Hi there !

I am testing SFML 2.0 by coding a little particle system. I don't know much about particles but I only use Vertex as points applying them some physics rules, so that's very simple.

Here is the class managing the displaying and updating of the particles on the screen :
Fenetre::Fenetre() {
        m_window = new sf::RenderWindow(sf::VideoMode(WINDOW_W, WINDOW_H, 32), WINDOW_TITLE);
        m_window->setVerticalSyncEnabled(true);
       
        particles.assign(PARTICLES_MAX, Particle());
        vertexArray.setPrimitiveType(sf::Points); // Added recently
       
        m_pause = false;
        m_continue = true;
       
        clock.restart();
}


int Fenetre::exec() {  
        while (m_continue && m_window->isOpen()) {
                handleEvents();

                if (!m_pause)
                        update();
                clock.restart();

                display();
        }
       
        return EXIT_SUCCESS;
}

void Fenetre::update() {
        float dt = clock.getElapsedTime().asSeconds();
       
        vertexArray.clear(); // Added recently
        for (int i = 0; i != particles.size(); ++i) {
                particles[i].update(dt);
                vertexArray.append(particles[i]);  // Added recently
        }
}

void Fenetre::display() {
        m_window->clear();
       
        m_window->draw(vertexArray); // Added recently
        // I used to draw this way before using VertexArray :
        //m_window->draw((sf::Vertex*) &particles[0], (unsigned int) particles.size(), sf::Points);    
       
        m_window->display();
}

The class Particle extends Vertex, adding members like speed to it, and its update() method handles the physics stuff.

At the beggining, I only used std::vector, causing a strange behaviour to my render :
http://www.noelshack.com/2012-19-1336926110-Capture-1.png
As you can see some particles display on the left, falling verticaly, and I couldn't see them when I tried doing cout on my vector.

So I switched to VertexArray, which works fine, even if I implemented it really poorly (des/allocating a 1000 long array at each frame is not a good idea, I know ^^ ).

So my questions are :
- Why do I get this strange behaviour when storing my Particles in a vector ?
- Is there a sweet method to use VertexArray with a better memory management without changing my Particle class or should my Particle take the Vertex as parameter of their update method ?

Thanks a lot !
PS : SFML rules, you developers are geniuses ! ^^
« Last Edit: May 13, 2012, 07:12:07 pm by Adrisaboss »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: [sf::VertexArray VS std::vector] Strange behaviour
« Reply #1 on: May 13, 2012, 07:05:53 pm »
Quote
Why do I get this strange behaviour when storing my Particles in a vector ?
Because with this:
(sf::Vertex*) &particles[0]
... the compiler expects an array of consecutive sf::Vertex instances, but that's a lie: the pointed memory area contains Particles, which are bigger.
The solution is simple: don't extend sf::Vertex, but rather keep your own particles attributes separated from them.

Quote
Is there a sweet method to use VertexArray with a better memory management without changing my Particle class or should my Particle take the Vertex as parameter of their update method ?
clear() doesn't deallocate memory, so append() doesn't need to reallocate it, so all you do is overwriting the previous contents every frame.
But yes, a cleaner design (and in accordance with what I say above) would probably that Particle::update takes a pointer to the vertices to write in the vertex array.
Laurent Gomila - SFML developer

Adrisaboss

  • Newbie
  • *
  • Posts: 14
    • View Profile
Re: [sf::VertexArray VS std::vector] Strange behaviour
« Reply #2 on: May 13, 2012, 07:11:48 pm »
Thanks for the fast reply !
Consider my problem solved. :D

Adrisaboss

  • Newbie
  • *
  • Posts: 14
    • View Profile
Re: [SOLVED] [sf::VertexArray VS std::vector] Strange behaviour
« Reply #3 on: May 14, 2012, 10:07:01 pm »
In fact I still don't understand something.
From what I understand, you advise me to 2 arrays in my Fenetre class : the VertexArray, which will be displayed by the RenderWindow, and a vector of Particles, where my Particle class would look like :
Code: [Select]
class Particle {
public :
  void update(sf::Vertex &vertex);
  // Constructor etc...
private :
  sf::Vector2f v;
  // All the other members I would need to perform a nice animation...
}

But isn't it sad to be obliged to have 2 arrays ? Plus the correspondence between the Vertex and the Particle is only made by its position in the arrays, like :
Code: [Select]
Fenettre::update() {
  for (int i = 0; i < particles.size(); ++i) {
     particles[i].update(vertexArray[i]);
  }
}

What if I want to make my arrays dynamic ? And this isn't very secure, if vertexArray contains less elements than particles...

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: [SOLVED] [sf::VertexArray VS std::vector] Strange behaviour
« Reply #4 on: May 14, 2012, 10:58:49 pm »
I just told you what I thought to be a good solution. If you don't like it and can find a better one, then go ahead.
Laurent Gomila - SFML developer

 

anything