C++ OpenGL, SDL 2.0 Error While Trying to Load PNG Files

Vipervgts

Honorable
Oct 12, 2012
12
0
10,560
Every time I try to run my program, it gives me a reading violation when it reaches glTexImage2D. This is my code:

C++:
unsigned int loadImage(string filename, bool alpha = true) {
	// You should probably use CSurface::OnLoad ... ;)
	//-- and make sure the Surface pointer is good!
	unsigned int id;

	SDL_Surface* Surface;
	SDL_RWops *rwop;
	rwop = SDL_RWFromFile(filename.c_str(), "rb");
	Surface = IMG_LoadPNG_RW(rwop);


	glGenTextures(1, &id);
	glBindTexture(GL_TEXTURE_2D, id);

	int Mode = GL_RGB;

	if (Surface->format->BytesPerPixel == 4) {
		Mode = GL_RGBA;
	}

	glTexImage2D(GL_TEXTURE_2D, 0, Mode, Surface->w, Surface->h, 0, Mode, GL_FLOAT, Surface->pixels);

	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

	return id;
}

<Mod Note: Next time use the [ code ][ /code ] tags>
 

Pinhedd

Distinguished
Moderator
Okay, problem one: your code is a mess. Declare and initialize all of your local scope variables at the top of the code block. Stack memory is allocated all at once regardless of how big the allocation is. Get into the practice of doing this. If an initialization is redundant, the compiler will optimize it out.

Rewrite your code so that it looks something like this:
C++:
unsigned int id = 0;
int Mode = GL_RGB;
SDL_Surface* Surface = NULL;
SDL_RWops* rwop = NULL;

problem two: you're not checking to see if the operations are succeeding before moving on.

SDL_RWFromFile returns an address, or NULL on failure. The same is true for IMG_LoadPNG_RW. You must test failure cases for each before moving on.

C++:
rwop = SDL_RWFromFile(filename.c_str(), "rb");
if (rwop)
{
Surface = IMG_LoadPNG_RW(rwop);
if (surface)
{
/*more stuff goes here*/
}
else
/*handle null surface error here*/
}
else
/*handle null rwop error here*/

Once you know that those objects are valid, you can move on to the rest of the code
 

Vipervgts

Honorable
Oct 12, 2012
12
0
10,560
I keep getting errors. Do you think you could give me a working basic png loader please? This is the only thing in my code that isn't working at the moment. I was able to successfully load bitmaps, but for some reason, I keep getting this reading violation when I try to load pngs.
 

Pinhedd

Distinguished
Moderator


Could you post the errors please? The exact text that the compiler / runtime spits out.

Also, consider using asserts to insert debug statements into your code. Asserts can be enabled/disabled at compile time, so the performance hit on a release build is non-existent.
 

Pinhedd

Distinguished
Moderator


That's an access violation. Look at the address, 0x00000014. There's a null pointer somewhere in your reference chain and your program is attempting to use the null pointer as the base of a class/structure by adding a 20 byte offset.

Setup the error handling like I showed you and add a debug statement like

C++:
cerr << "Null Pointer detected for: Surface\n";

or

C++:
fprintf(stderr,"Null Pointer detected for: Surface");
 

Pinhedd

Distinguished
Moderator


yeah I saw that but I wanted him to figure it out the hard way