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

Author Topic: Can't get vertex array to draw when I use a class  (Read 3228 times)

0 Members and 1 Guest are viewing this topic.

Dr Spaceman

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Can't get vertex array to draw when I use a class
« on: June 21, 2013, 03:05:29 am »
I'm very sure this is a very simple answer but i'm still trying to get to grips with things.

What i'm trying to do is just make a class where I define a triangle and then I want to draw that class to the window. I have been following the tutorial and i'm currently as the bit where it shows the particle example, hence why some of the code is copy pasted.

When I run it I just get a black screen and i'm really not sure why.

#include <SFML/Graphics.hpp>
#include "Event.hpp"
#include "Transformable.hpp"
#include "Transform.hpp"

class test : public sf::Drawable , public sf::Transformable
{

public:

        void triangles()
        {
                        // create an array of 3 vertices that define a triangle primitive
        sf::VertexArray triangle(sf::Triangles, 3);

        // define the position of the triangle's points
        triangle[0].position = sf::Vector2f(10, 10);
        triangle[1].position = sf::Vector2f(100, 10);
        triangle[2].position = sf::Vector2f(100, 100);

        // define the color of the triangle's points
        triangle[0].color = sf::Color::Red;
        triangle[1].color = sf::Color::Blue;
        triangle[2].color = sf::Color::Green;



        }

private:

    virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const
    {
        // apply the entity's transform -- combine it with the one that was passed by the caller
        states.transform *= getTransform(); // getTransform() is defined by sf::Transformable

        // apply the texture
        states.texture = NULL;

        // you may also override states.shader or states.blendMode if you want

        // draw the vertex array
        target.draw(m_vertices, states);
    }

    sf::VertexArray m_vertices;

};



int main()
{
    sf::RenderWindow window(sf::VideoMode(800, 600), "My window",sf::Style::Default);

    // run the program as long as the window is open
    while (window.isOpen())
    {
        // check all the window's events that were triggered since the last iteration of the loop
        sf::Event event;
        while (window.pollEvent(event))
        {
            // "close requested" event: we close the window
            if (event.type == sf::Event::Closed)
                window.close();
        }
       
                test shape;
                window.draw(shape);
                window.display();
        }
        return 0;
}

 


I'm sure there is lots wrong with this but i'm still new so any help would be appreciated, thanks.

G.

  • Hero Member
  • *****
  • Posts: 1592
    • View Profile
Re: Can't get vertex array to draw when I use a class
« Reply #1 on: June 21, 2013, 05:12:55 am »
You're drawing the vertex array m_vertices, but it is empty.

Dr Spaceman

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Re: Can't get vertex array to draw when I use a class
« Reply #2 on: June 21, 2013, 10:06:18 pm »
Ah I thought that might be the case.

I tried replacing the  m_vertices with triangles but it doesn't seem to work, I'm a bit of a newbie with this stuff don't suppose you could quickly amend my code so I can see how it should be done?


massive_potato

  • Newbie
  • *
  • Posts: 37
    • View Profile
Re: Can't get vertex array to draw when I use a class
« Reply #3 on: June 21, 2013, 11:25:03 pm »
Your issue is one of inheritance. If you look at your declaration of draw, it's private. This means that when you call draw from an object (object.draw(...)), you won't be calling this function. Your draw function needs to be public in order to be called correctly.

As a test, try putting some sort of output statement in the draw function to see if it is actually being called.


Nevermind. I thought you were trying to call test::draw. Sorry  :-[
« Last Edit: June 22, 2013, 10:33:05 am by massive_potato »

G.

  • Hero Member
  • *****
  • Posts: 1592
    • View Profile
Re: Can't get vertex array to draw when I use a class
« Reply #4 on: June 22, 2013, 12:30:11 am »
Private is OK, Drawable are friends with RenderTarget, I think it's how you're supposed to do.

Fill your m_vertices, and don't forget to call the method which fills it. ;)
I have only changed the triangles method (replaced triangle with m_vertices) and added a call to it.
#include <SFML/Graphics.hpp>
#include "Event.hpp"
#include "Transformable.hpp"
#include "Transform.hpp"

class test : public sf::Drawable , public sf::Transformable
{

public:

    void triangles()
    {
            // create an array of 3 vertices that define a triangle primitive
    m_vertices = sf::VertexArray(sf::Triangles, 3);

    // define the position of the triangle's points
    m_vertices[0].position = sf::Vector2f(10, 10);
    m_vertices[1].position = sf::Vector2f(100, 10);
    m_vertices[2].position = sf::Vector2f(100, 100);

    // define the color of the triangle's points
    m_vertices[0].color = sf::Color::Red;
    m_vertices[1].color = sf::Color::Blue;
    m_vertices[2].color = sf::Color::Green;



    }

private:

    virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const
    {
        // apply the entity's transform -- combine it with the one that was passed by the caller
        states.transform *= getTransform(); // getTransform() is defined by sf::Transformable

        // apply the texture
        states.texture = NULL;

        // you may also override states.shader or states.blendMode if you want

        // draw the vertex array
        target.draw(m_vertices, states);
    }

    sf::VertexArray m_vertices;

};



int main()
{
    sf::RenderWindow window(sf::VideoMode(800, 600), "My window",sf::Style::Default);

    // run the program as long as the window is open
    while (window.isOpen())
    {
        // check all the window's events that were triggered since the last iteration of the loop
        sf::Event event;
        while (window.pollEvent(event))
        {
            // "close requested" event: we close the window
            if (event.type == sf::Event::Closed)
                window.close();
        }
   
        test shape;
                shape.triangles();
        window.draw(shape);
        window.display();
    }
    return 0;
}
 
« Last Edit: June 22, 2013, 12:32:27 am by G. »

Dr Spaceman

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Re: Can't get vertex array to draw when I use a class
« Reply #5 on: June 22, 2013, 01:26:54 am »
Ah I see now, I tried calling every m_verticies as you did but it didn't work because I forgot to call it again.

Thanks for the help :)

 

anything