Original Post
Hey I've been adding try catch blocks wherever I dynamically declare stuff. So I went back into my old Picloader.cpp: 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.
//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;
}