0

How do I create an array of GLTexture nullptrs with a size calculated at runtime? . The implementation below creates an array GLTexture pointers initialised with nullptr with a constant size of [11][32]. I want the 11 and 32 shown in the header file below to be interchanged with a value calculated at runtime.

Header File

#pragma once
    #include <GLEW\glew.h>
    #include "GLTexture.h"

        namespace Nova
        {
            class TextureBinder
            {
            private:
                GLuint      m_activeUnit;
                GLint       m_maxTextureUnits;
                GLint       m_maxTextureTargets;
                GLTexture*  m_boundTextures[11][32] = {nullptr};

            public:

                static TextureBinder& GetInstance()
                {
                    static TextureBinder binder;
                    return binder;
                }

                TextureBinder(TextureBinder const&) = delete;
                void operator=(TextureBinder&) = delete;


            private:
                TextureBinder();
            };
        }

CPP File

#pragma once
#include "TextureBinder.h"

namespace Nova
{
    /* zero is the default opengl active texture unit
    - glActiveTexture(unit) only needs to be called for multitexturing
*/
            TextureBinder::TextureBinder()
        :
        m_activeUnit(0),
        m_maxTextureTargets(11)
    {
        glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &m_maxTextureUnits);
    }   
}
10
  • Which variable is the array size you want? Your code is lengthy. Commented Mar 13, 2016 at 20:07
  • @ArifBurhan sorry missed that! m_boundTextures[m_maxTextureTargets][m_maxTextureUnits] Commented Mar 13, 2016 at 20:08
  • are the m_boundTextures pointers pointing to textures that this class owns, or are they managed elsewhere? Commented Mar 13, 2016 at 20:16
  • @M.M this class does not own them they are managed elsewhere. so they should not be constructed just assigned Commented Mar 13, 2016 at 20:18
  • 1
    I think you meant to address that to Richard Hodges Commented Mar 13, 2016 at 21:01

2 Answers 2

2

Assuming you actually want a dynamically sized array (i.e. can be calculated at runtime and works with different sizes), you need to use a loop in your constructor:

GLTexture*  **m_boundTextures;

TextureBinder() {
    /* ... */
    m_boundTextures = new GLTexture* *[HEIGHT];
    for(int i = 0; i < HEIGHT; i++) {
        m_boundTextures[i] = new GLTexture* [WIDTH];
        for(int j = 0; j < WIDTH; j++) {
            m_boundTextures[i][j] = nullptr;
        }
    }
    /* ... */
}

And of course make sure to clean up the memory using delete[] (in the same format) in your destructor.

Sign up to request clarification or add additional context in comments.

2 Comments

Or maybe use std::vector with which you can set a default value when you resize it ;)
I prefer the other answer but +1 because I learnt something new !
1

This simplest alternative to code is to use:

std::vector< std::vector<GLTexture*> > m_boundTextures;

and add to the ctor-initializer list

// Numbers can be replaced by variables,
// or you can set the size later using 'resize' member function
m_boundTextures(11, std::vector<GLTexture*>(32)); 

Another option to consider is using a single vector of size 11 * 32 (or whatever your dimensions are) and then using multiplication to access the right index; you could make a helper function for this, e.g.:

GLTexture* & lookup(size_t row, size_t col) { return m_boundTextures.at(col + row * row_length); }

2 Comments

if I call the resize member function for the m_boundTextures like so m_boundTextures.resize(11); how would I change the size of std::vector<GLTexture*> that is within it?
for a single one, m_boundTextures[n].resize(32);. For all at once, for (auto& x : m_boundTextures) x.resize(32);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.