0
votes

I would like to load textures, and then have them be used by multiple objects. Would this work?

class Sprite
{
    GLuint* mTextures; // do I need this to also be a reference?

    Sprite( GLuint* textures ) // do I need this to also be a reference?
    {
        mTextures = textures;
    }

    void Draw( textureNumber )
    {
        glBindTexture( GL_TEXTURE_2D, mTextures[ textureNumber ] );
        // drawing code
    }
};

// normally these variables would be inputed, but I did this for simplicity.
const int NUMBER_OF_TEXTURES = 40;
const int WHICH_TEXTURE = 10;

void main()
{
    std::vector<GLuint> the_textures;
    the_textures.resize( NUMBER_OF_TEXTURES );

    glGenTextures( NUMBER_OF_TEXTURES, &the_textures[0] );

    // texture loading code

    Sprite the_sprite( &the_textures[0] );
    the_sprite.Draw( WHICH_TEXTURE );
}

And is there a different way I should do this, even if it would work?

Thanks.

4
Just a note. If you're defining (vs declaring) something as a reference, it usually means something's wrong with your design. - Falmarri
@Falmarri, what do you mean? Are you suggesting that having a class field being a reference, or a local variable being a reference, is wrong? - Kos
@Kos: I'm talking about defining a class field as a reference. - Falmarri
Sounds like a natural way of implementiong any singular UML association endpoint which is known to be invariant through the whole lifetime of an object. Possible with a const pointer too. - Kos

4 Answers

2
votes
  1. yes, this would work
  2. no need to make them be a reference: you store/pass a copy of a pointer (it's fast), and you don't plan to change this pointer outside
  3. there're many different ways to do this, and the right one depends on your other code requirements.

e.g. you can use a global instance of textures:

textures.cpp:

static std::vector load_once_textures();
std::vector<GLuint> const& get_textures()
{
    static std::vector<GLuint> const the_textures = load_once_textures();
    return the_textures;
}

std::vector load_once_textures()
{
    // loading
}

textures.h

std::vector<GLuint> const& get_textures();

it's a simple approach and safe enough because textures will be loaded once and loading doesn't have a problem of static initialization order ambiguity

2
votes

That particular case should work. However, as soon as "the_textures" goes out of scope, then the pointer held by Sprite will become invalid. That would be the same, even if it was a reference. In this, case, I suggest you put the std::vector<> inside the Sprite class instead, and have it owned and managed by that class instance.

0
votes

I am wondering why Draw cannot just take a reference to the object it is drawing.

class Sprite
{
public:

    Sprite()
    {
    }

    void Draw( GLuint & texture )
    {
        glBindTexture( GL_TEXTURE_2D, texture );
        // drawing code
    }
};
  • Sprite is a type that does drawing in a specific way
  • GLuint is a type that gets drawn

There could be polymorphism somewhere here in that: - you have different draw algorithms - there are polymorphic (virtual) methods in the different types of objects that get drawn

so Draw might be a virtual method and GLuint might be an abstract base class, in which case the actual vector will not be of the objects but of pointers to different types of the object.

You should certainly decouple out the way the objects are drawn from the way the objects are stored though, so storing a vector in the drawing class, or even passing in a pointer that assumes they are in some kind of array is not likely to be a good idea.

By the way, main should return int not void.

0
votes

Since the texture id doesn't change, what about using a GLuint value in Sprite instead of storing the array-ptr and choosing which sprite to draw?

Seems simpler, and no need to worry about scoping

This is unless you need to call glDeleteTextures before app-exit, then I suggest you create a TextureMgr class or something that solves this issue once and for all. ;)