C++ Class problem

I am loading TGA images using a class that I am making. Once the class is created, i create an instance in the initialize stage of my program, and make a texture from the loaded image. I am getting no compiler errors but my window flashes on and then off when i run the program. Here is some of the code I think is the problem…

 //This is the class definition
private:
  unsigned char* imageData;
  int height width etc...

public:  
  unsigned char* getImageData() { return imageData; }
//Same for height and width

bool STGA::loadTGA(const char *filename)
{
   //loading function 
}

//Now here is the texture bit from Init()

glTexImage2D(GL_TEXTURE_2D, 0,                3,mySTGA.getImageWidth() , mySTGA.getImageHeight(), 0, GL_RGB, GL_UNSIGNED_BYTE, mySTGA.getImageData());

Those bits point to the class using mySTGA as the instance. Really hope you guys can help as its got everyone baffled. Oh there is one thing that may help. When I debug each line it fails when it gets to the image data part of the instance. it says “Expression cannot be evaluated” but I cant see the cause of the problem.

THanks lots an lots!!

the snippet is pretty short. maybe you should post some more.

anyway, here’s a guess: maybe glTexImage is called before you’ve created the gl context and made it current?

Thanks for the reply, i’m pretty sure thats not the case because I have set it up so that it is created before the Init() function. I know the class is correct because it was previously a structure to do the same thing, and it worked fine. Just when I put it into a class it goes mad!

I thought I’d add some more info on the class. Here is the Constructor and Destructor.

STGA::STGA()
   {
      imageData = (unsigned char*)0;
      imageWidth = 0;
      imageHeight = 0;
      imageTypeCode = 0;
      bitCount = 0;
   }

STGA::~STGA() {if (!imageData) delete[] imageData; }

The variables in the constructor are taken from the private members and initialised. There are some others that are declared for use in the LoadTGA member function.

I forgot to show how I specified the file to load:

//mySTGA is the instance of the class remember
if (mySTGA.loadTGA("image.tga" ))
glGenTextures() etc...

One thing I have noticed is that if the file is not presesnt, the window loads fine, just with no images of course. If the file is detected, the same flash on flash off happens. If anyone has any ideas please help! thanks

Try unit-testing your STGA class without using OpenGL, just to make sure the data loaded correctly. Do this by printing out a very simple image. Don’t just assume it worked because you had a previous struct.

Then, instead of loading a file into your STGA, make a method in STGA that creates a simple checkerboard image. Then draw that using the rest of your code.

Finally, put the two together, and keep the unit-tests around as part of your “check” target, so if you break something later you’ll know.

One obvious bug is that your destructor will delete imageData when imageData is NULL.

hope that helps… good luck

  • Taylor

I tried taking out the destructor and it doesnt help. If I comment out the instance of the class and the calls to it from glTexImage2D… it loads fine, so it must be in my load function, I really cant get this one but I am really stuck! please help

by the way i just tried loading the image without texturing it. I specified a raster position and then called the glDrawPixels() function. Specifiying the image height width and data, the same way as the texture function calls.

The window loads fine but no image is displayed, when i debug, then step into, again it says imageData cannot be evaluated. hope this helps find a sloution. :frowning:

Random suggestion:
Try running GLIntercept with the full debug profile and see if it finds any errors?
http://glintercept.nutty.org/

Originally posted by jaymystro:
hope this helps find a sloution. :frowning:
So do I - get to it!
Good luck, and let us know what it was.