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.pngAs 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 ! ^^