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

Author Topic: Trying to draw vector of vertices.  (Read 3188 times)

0 Members and 1 Guest are viewing this topic.

int k

  • Newbie
  • *
  • Posts: 6
    • View Profile
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);
}
 

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Trying to draw vector of vertices.
« Reply #1 on: March 24, 2018, 05:02:58 pm »
Your code is strange in several ways: you shouldn't pass a pointer to a derived array into draw and you shouldn't derive from sf::Vertex (and then you have sf::Vertex and sf::PrimitiveType as members of Streamer too for some reason).
Back to C++ gamedev with SFML in May 2023

int k

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Trying to draw vector of vertices.
« Reply #2 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.

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Trying to draw vector of vertices.
« Reply #3 on: March 24, 2018, 08:35:52 pm »
You should store whatever you need yourself in any way you want and then copy it to an array of sf::Vertex when you need to draw it.

You shouldn't pass an array of objects of a derived class to a function that expects an array of objects of base class (one object passed like that is fine and legal, AND it'll work on almost all compilers if there is no extra data in derived class but it's still dangerous, might break later and is apparently not legal C++: https://wiki.sei.cmu.edu/confluence/display/cplusplus/CTR56-CPP.+Do+not+use+pointer+arithmetic+on+polymorphic+objects ). It's very clear and obvious why you can't do this if you have an understanding of how C++ memory model works.

See this stack overflow question: https://stackoverflow.com/questions/7018183/provide-array-of-derived-objects-to-function-that-operates-on-base-objects

And this image (a bit oversimplified, sf::Vertex data can start in the middle of derieved object and a few other things): https://imgur.com/a/IEewm
Back to C++ gamedev with SFML in May 2023

int k

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Trying to draw vector of vertices.
« Reply #4 on: March 24, 2018, 08:41:13 pm »
Okay, that makes sense. Thank you for the response.