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

Author Topic: Drawing vertexarray inside a class doesn't work  (Read 1352 times)

0 Members and 1 Guest are viewing this topic.

perensoep109

  • Newbie
  • *
  • Posts: 2
    • View Profile
Drawing vertexarray inside a class doesn't work
« on: January 07, 2018, 08:31:44 pm »
Hello there! I'm fairly new to Sfml C++,
I have a problem with drawing an vertexarray that's inside a class. This code does work outside the class.
My only output is the phrase: "Tile is created" on the console. Please help!

#include <SFML/Graphics.hpp>
#include <iostream>

using namespace std;

struct Tile64
{
public:
        sf::VertexArray quad;
        Tile64(float xPos, float yPos, sf::Color color)
        {
                sf::VertexArray quad(sf::Quads, 4);

                // define it as a rectangle, located at (10, 10) and with size 100x100
                quad[0].position = sf::Vector2f(xPos, yPos);
                quad[1].position = sf::Vector2f(xPos + 64, yPos);
                quad[2].position = sf::Vector2f(xPos + 64, yPos + 64);
                quad[3].position = sf::Vector2f(xPos, yPos + 64);

                quad[0].color = color;
                quad[1].color = color;
                quad[2].color = color;
                quad[3].color = color;

                cout << "Tile is created" << endl;
        }
};

int main()
{
        sf::RenderWindow window(sf::VideoMode(320, 320), "Tilemap");

        Tile64 tile(10, 10, sf::Color::Red);

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

                window.clear();
                window.draw(tile.quad);
                window.display();
        }

        return 0;
}

Thank you!

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Drawing vertexarray inside a class doesn't work
« Reply #1 on: January 07, 2018, 10:48:37 pm »
Quote
        sf::VertexArray quad;
        Tile64(float xPos, float yPos, sf::Color color)
        {
                sf::VertexArray quad(sf::Quads, 4);
 
There you have 2 variables named "quad", and unfortunately, the one you initialize and populate is not the one that you draw.

What you want is this:
        sf::VertexArray quad;
        Tile64(float xPos, float yPos, sf::Color color) : quad(sf::Quads, 4)
        {
 
Laurent Gomila - SFML developer

perensoep109

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Drawing vertexarray inside a class doesn't work
« Reply #2 on: January 08, 2018, 09:16:49 am »
Well, I thought this would create the variable. And later on in the class. It would give the variable it's data. Because, when I remove the quad from the class. It gives me an error message. I can't reach the quad variable without that quad outside of the constructor.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Drawing vertexarray inside a class doesn't work
« Reply #3 on: January 08, 2018, 09:26:03 am »
You should really spend more time learning the basics of C++. Don't try to guess how to make this work.

And read again my reply, I never said that you should remove the "quad" member from the class. It's the one that is declared locally in the constructor that you should remove.
Laurent Gomila - SFML developer