Problem with IMG_Load() in SDL

Started by
3 comments, last by derefed 17 years, 10 months ago
I've been making a class to hold a bunch of images that will ultimately be used to draw a dialog box in my game, and I've run into a bit of a problem with .pngs. You see, I load all of the component images from one master sprite sheet, but at the end of this particular function, I also read in the color of a pixel in the center of the master image, which will represent the background color of the dialog box. That way, I can just use a FillRect() later with said color to draw the background instead of blitting a single-pixel image hundreds of times. Only problem is, my program crashes when I try to read that pixel. Running through a debugger, I soon found that the problem was not with the pixel-reading function, but rather that IMG_Load() was returning NULL for my .png, and thus there were no pixels to read. Well, I ran some tests, and it turns out that NONE of my calls to IMG_Load() ever returned anything but NULL -- even when I could see the results of the blit right on the screen! Further, I have a wrapper function for IMG_Load() that basically will, in addition to loading the image, add things like optimizing the image to the display and adding a color key. In the debugger, it never executes these as they are under an if (loadedImage != NULL) clause, and yet when the images are blitted, they have transparency where they should and everything. So basically, IMG_Load() is always returning NULL, but the images are still being blitted. This would be fine I guess, until I try to actually read in pixel data, at which point it blows up. Has anyone ever run into this before? Thanks, derefed
Advertisement
Assuming you are on Windows and you have the latest sdl_image build, you need to have libpng12.dll in your program's working directory. When you load a png with IMG_Load, sdl_image will try to load the libpng library dynamically, so if it isn't there it will just return NULL. I would have to check but I believe zlib1.dll is also needed for decoding png's.

If you did have all the libraries then there's also a function called IMG_GetError to check for errors. Perhaps your png files are just in a bad format.
Yeah, I am on Windows and am compiling with VC++ 6. I tried putting those DLLs in the working directory of my program but it didn't seem to work... I think I already had them in my system32 folder anyway, so it wouldn't have made a difference.

IMG_GetError() appears to be returning "Couldn't open <filename>" every time, even on those images that are blitted anyway.

If the .png files are in a bad format, how could I go about testing for this? I've made them all in Photoshop.
Make sure you lock the image(SDL_LockSurface) before trying to read pixel data.

It may be that during debugging, the default directory changes and IMG_Load can't find the image anymore. Test by hardcoding IMGLoad to use full image paths.

The way you are checking for a NULL pointer may be affecting the program run. SDL wouldn't blt a NULL surface, so if figure out why your're getting NULL pointers then you'll be on your way.

Good Luck.
0xa0000000
Ah, yes, that's it! I hadn't locked the surface before because the tutorial I was using didn't do it... perhaps I overlooked it though. And you're probably right about the path changing during debugging... that's what confused me the most.

Thank you very much!

This topic is closed to new replies.

Advertisement