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

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - int k

Pages: [1]
1
Graphics / Re: Trying to draw vector of vertices.
« on: March 24, 2018, 08:41:13 pm »
Okay, that makes sense. Thank you for the response.

2
Graphics / Re: Trying to draw vector of vertices.
« on: March 24, 2018, 05:55:02 pm »
It's a bit of mess because there are a few different things I tried. Sorry. Trying to implement an sf::VertexArray-like class but I don't understand how to draw it.

Quote
...you shouldn't derive from sf::Vertex...

What do you suggest for a class in which each vertex will have several additional attributes? Similar in a way to the particle system example but the object will encompass a single vertex with multiple attributes.

3
Graphics / Trying to draw vector of vertices.
« on: March 24, 2018, 04:18:09 pm »
I'm trying to create a class using sf::Vertex that will give each vertex additional attributes and methods. Main contains a vector of objects making this quite similar to sf::VertexArray but when I go draw it, nothing appears. Any help why?

main.cpp
#include <iostream>
#include <vector>

#include "src/streamer.h"

int main(){

        sf::RenderWindow window(sf::VideoMode(800, 600), "My Window");

        std::vector<Streamer> streamers;

        while(window.isOpen()){
                sf::Event event;
                while(window.pollEvent(event)){
                        if(event.type == sf::Event::Closed || sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
                                window.close();
                }

                if(sf::Mouse::isButtonPressed(sf::Mouse::Left)){
                        Streamer vertex(sf::Vector2f(sf::Mouse::getPosition().x, sf::Mouse::getPosition().y));
                        streamers.push_back(vertex);
                }      

                std::cout << streamers.size() << std::endl;

                window.clear();
                window.draw(&streamers[0], streamers.size(), sf::Points);
                window.display();
        }
}
 

streamer.h
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>

class Streamer: public sf::Drawable, public sf::Transformable, public sf::Vertex{
public:
        Streamer();
        Streamer(const sf::Vector2f);
        ~Streamer();

private:
        virtual void draw(sf::RenderTarget &target, sf::RenderStates states) const;
        sf::Vertex m_Vertex;
        sf::PrimitiveType m_Type;
        bool m_Status = 0;
};
 

streamer.cpp
#include "streamer.h"

Streamer::Streamer():
        m_Status(1){
}

Streamer::Streamer(const sf::Vector2f location):
        m_Status(1){
}

Streamer::~Streamer(){}

void Streamer::setPosition(sf::Vector2f){
}

void Streamer::draw(sf::RenderTarget& target, sf::RenderStates states) const{
        target.draw(&m_Vertex, 1, sf::Points);
        //target.draw(&m_Vertex, states);
}
 

4
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.

5
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.

6
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.

Pages: [1]
anything