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

Author Topic: What's the "correct" way to draw shapes in a list and vertices in vectors?  (Read 4021 times)

0 Members and 1 Guest are viewing this topic.

int k

  • Newbie
  • *
  • Posts: 6
    • View Profile
I'm new to SFML so sorry if this is a noob question.

I'm trying to create a class for a fireworks show. The class will contain an std::list of structs to track each firework/shell. Inside each struct is also an std::vector of sf::VertexArrays (or sf::Vertex?) for the stars after the shell explodes.

Right now, I'm having problems drawing the shells and stars. Normally, an sf::VertexArray can be passed directly to draw funcion [myWindow.draw(myVertexArray)] but what if it's in a list and nested vector? Should this just be drawn using a loop & pointer? So what would be the correct way to draw the stars for each shell and then each shell in flight?

Another part that's confusing me, sf::VertexArray is a vector-like wrapper for sf::Vertex and can be passed directly to the draw() function. Is it possible to do something similar? I wasn't able to find the implementation, but does it also run through a loop or is there an easier way to draw my particles and/or shapes at once?

Firework.hpp
class Firework : public sf::Drawable, public sf::Transformable{
public:
private:
        virtual void draw(sf::RenderTarget&, sf::RenderStates) const;

private:
        struct Burst{
                sf::Vertex vertex;     
        };

        struct Flight{
                sf::CircleShape circle;
                std::vector<Burst> starProperties;
        };

        std::list<Flight> shell;
};
 

Firework.cpp
void Firework::draw(sf::RenderTarget& target, sf::RenderStates states) const {
        states.transform *= getTransform();
//      target.draw(starProperties.vertex);//no
//      target.draw(shell.circle);//no
}
 

Each time the spacebar is pressed, a new shell is pushed onto the [shell] list with a random angle-velocity, X location and number of stars. This is with extraneous code removed; I can post the full (but broken) project as well.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: What's the "correct" way to draw shapes in a list and vertices in vectors?
« Reply #1 on: December 09, 2017, 09:27:22 am »
Iterating over a list/vector, and the basics of STL containers in general, have nothing to do with SFML so please pick some tutorial and learn that first instead of trying to guess :)

Then I'm not sure I understand your other questions about sf::VertexArray. But the source code is dead simple to understand, and there are also the tutorials and documentation that explain it pretty well, so make sure to have a look at all this stuff first.
Laurent Gomila - SFML developer

NGM88

  • Full Member
  • ***
  • Posts: 162
    • View Profile
Re: What's the "correct" way to draw shapes in a list and vertices in vectors?
« Reply #2 on: December 09, 2017, 01:50:57 pm »
Not that I completely understand what you want, but for a fireworks simulation I would say take a look at the particle system example in the tutorial "Designing your own entities with vertex arrays".

int k

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: What's the "correct" way to draw shapes in a list and vertices in vectors?
« Reply #3 on: December 10, 2017, 02:22:03 am »
Iterating over a list/vector, and the basics of STL containers in general, have nothing to do with SFML so please pick some tutorial and learn that first instead of trying to guess :)

That wasn't the question.

Then I'm not sure I understand your other questions about sf::VertexArray. But the source code is dead simple to understand, and there are also the tutorials and documentation that explain it pretty well, so make sure to have a look at all this stuff first.

Like I said, I wasn't able to find the implementation. Maybe it's because I'm looking in the wrong place.

https://github.com/SFML/SFML/blob/master/include/SFML/Graphics/Vertex.hpp
https://github.com/SFML/SFML/blob/master/include/SFML/Graphics/Drawable.hpp
https://github.com/SFML/SFML/blob/master/src/SFML/Graphics/Vertex.cpp
https://github.com/SFML/SFML/blob/master/src/SFML/Graphics/VertexArray.cpp
https://github.com/SFML/SFML/blob/master/src/SFML/Window/Window.cpp

Be nice if you can at least tell me in which file I can find this dead simple source code.

Not that I completely understand what you want, but for a fireworks simulation I would say take a look at the particle system example in the tutorial "Designing your own entities with vertex arrays".

I've done a few projects where the vertex array is a class member like in the examples. I guess what I'm asking is how you would go about for drawing vertices that are inside an std::vector inside a member std::list since the vector can't simply be directly passed to the virtual draw function, if that makes any sense.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11033
    • View Profile
    • development blog
    • Email
You can draw a vector of vertices simply by calling window.draw(vec.data(), vec.size());.

You can't draw a std::list like that because the list doesn't hold the data in a continuous memory block. Instead you'll have to iterate over it and draw each part on its own.

Keep in mind though that draw calls are somewhat expensive and if you can create a vertex array/vector of vertices, you can gain some performance.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

int k

  • Newbie
  • *
  • Posts: 6
    • View Profile
You can draw a vector of vertices simply by calling window.draw(vec.data(), vec.size());.

You can't draw a std::list like that because the list doesn't hold the data in a continuous memory block. Instead you'll have to iterate over it and draw each part on its own.

Keep in mind though that draw calls are somewhat expensive and if you can create a vertex array/vector of vertices, you can gain some performance.

Okay, so it's the list that requires an iteration. That's what I was looking for. Thank you for the clarification.