Skip to main content
GameDev.net gamedev.net
🔒 Locked

glTexImage2D error checking

Started by icecubeflower Apr 22, 2009 at 11:07 PM 0 replies 4.7k views
Original Post
icecubeflower
icecubeflower
Hey I've been adding try catch blocks wherever I dynamically declare stuff. So I went back into my old Picloader.cpp:

//This is only for PNG's and JPG's.  If anything else works then it's a coincidence.
//If you try loading a file that is already in this ImageVault then funnum will just
//be set to the correct number
bool ImageVault::ImageLoader(std::string filename, GLuint &funnum)
{
  bool breturn=false;
  SDL_RWops *rwop;  //don't remember wtf this is, use it to check if an image is a jpeg
  SDL_Surface *image;
  VaultNode *temp;

  temp=imagehead;
  imagereturn=imagehead;
  //scroll to the point in the list where the file already exists or scroll all the way to the end
  while(imagereturn!=NULL&&imagereturn->imagename!=filename)
  {
    temp=imagereturn;
    imagereturn=imagereturn->imagenext;
  }

  //if(image is already in list)
  if(imagereturn!=NULL)
  {
    funnum=imagereturn->imagenum;  //set funnum to corresponding image in OpenGL land
    breturn=true;
  }
  else  //image is not in list
  {
    if(image=IMG_Load(filename.c_str()))
    {
      glGenTextures(1, &funnum);  //funnum = a number not currently corresponding to an image in OpenGL
      glBindTexture(GL_TEXTURE_2D, funnum);  //we bind funnum but this texture is empty

      //so load a texture (*image) into OpenGL and funnum will correspond to it because it's the currently bound texture
      rwop=SDL_RWFromFile(filename.c_str(),"rb");
      if(IMG_isJPG(rwop))  //if it ain't a JPG then it's a PNG        //image->pixels is what sends the SDL image
         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, image->w, image->h, 0, GL_RGB, GL_UNSIGNED_BYTE, image->pixels);
      else
         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image->w, image->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, image->pixels);

      //magical OpenGL stuff
      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

      //we are at the end of the linked list
      try
      {
        imagereturn=new VaultNode;
        if(imagehead==NULL)
          imagehead=imagereturn;
        else
          temp->imagenext=imagereturn;

        imagereturn->imagename=filename;
        imagereturn->imagenum=funnum;
        breturn=true;
      }
      catch(std::bad_alloc&)
      {
        glDeleteTextures(1,&funnum);
      }
    }
    else  //if image won't load make the GLuint 0 just like glGen Textures
    {
      funnum=0;
    }
    if(image)
      SDL_FreeSurface(image);
  }
  return breturn;
}

ImageVault is a linked list of Nodes which contain the name of an image and its OpenGL name. (a GLuint) So I put try, catch around the part where I allocate a new Node. But now I'm wondering about glTexImage2D and glTexParameteri. Should I be checking to make sure those worked correctly too? Or are they guaranteed to work as long as image=IMG_Load(filename.c_str()) worked? glTexImage2D throws a mess of different errors if it didn't work correctly, all I want to know is if it or glTexParameteri failed so I can make ImageLoader return false and not make a new Node. So... should I bother checking to see if they throw errors? They don't return anything. Should I put them in a try block and then just use catch(...) to catch any errors and then exit ImageLoader and return false? I guess if I put those two in a try block then I should put glGenTextures(1, &funnum) in a try block as well.
Gorax
Gorax
According to this, if you call glTexImage2D using GL_PROXY_TEXTURE_2D as the target, it'll set all of the image parameters to zero if there's a problem with it. I've never used it like that myself, but it should work.

As for your glTexParameteri problem, I wouldn't worry too much about it, since other people can't exactly go changing the code, so it's unlikely to end up broken when you're just setting the texture filters.

One thing you should consider though, is rather than assuming jpegs and PNGs are being used, check the number of bytes per pixel inside the SDL Surface's pixel format. If it's 4, there's a good chance it'll be RGBA, if 3, then RGB.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.