0

i am trying to do chess with openGL c ++, for now i have created the pawn object, where its constructor takes an unsigned int parameter. So I tried to create an array of these pawns, and the only working way I've found to do this is this :

Pawn *pawn[n];

for (int i = 0; i < n; i++) {
    pawn[i] = new Pawn(Unsigned int var);
}

To call a function of pawn [0], for example, I have to do this :

pawn[0]->function(parameters);

This is the Pawn class :

class Pawn
{
private:

    float vertices [16] = {
        //position       //text coord
        -0.08f, -0.10f,   0.0f, 0.0f,
         0.08f, -0.10f,   1.0f, 0.0f,
         0.08f,  0.10f,   1.0f, 1.0f,
        -0.08f,  0.10f,   0.0f, 1.0f

    };

    GLuint indices[6] {
        0, 1, 2,
        0, 2, 3
    };

    unsigned int shaderID, VBO, VAO, EBO, texture;

public:

    glm::vec2 Position = glm::vec2(0.0f, 0.0f);

    Pawn () {}

    Pawn(GLuint shaderID) {
        ...
    }
    
    ~Pawn() {
         ...
    }

    void setTexture();

    void draw (glm::vec2 position);

};

I also tried this :

Pawn pawn[8];

for (int i = 0; i < 8; i++) {
    pawn[i] = Pawn(shaderID);
}

but when i run it doesn't work.

I was wondering if this method is efficient or not, and if so, why it works, since I didn't understand it. Thanks for your help

  • What speaks against `Pawn pawns[N]`, i.e. an array of objects instead of an array of pointers? – Raildex Jul 11 '21 at 14:15
  • Maybe show us your `Pawn` class as well? There are several ways to create arrays of objects in C++. Which ones have you tried and why didn't they work out? – ShadowMitia Jul 11 '21 at 14:17
  • If the only constructor your Pawn class has takes an int, you can't default-construct an array of them, so you wouldn't be able to do Pawn pawn[n]. You could potentially do Pawn pawn[n] = {1, 2, 3}; (up to whatever n is)... – George Jul 11 '21 at 14:17
  • This should be orf use: https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – eerorika Jul 11 '21 at 14:22
  • 1
    @George or use `placement-new` instead. – Remy Lebeau Jul 11 '21 at 14:35
  • 1
    @Raildex Or std::array... – mfnx Jul 11 '21 at 14:41
  • Efficient in what way? You should elaborate on that... Also, what did you not understand? – mfnx Jul 11 '21 at 14:44
  • @George i tried to do Pawn pawn[2] = { int, int } and it works. Thanks everyone for the help. – o0Matrix0o Jul 11 '21 at 15:11

0 Answers0