Sign in to follow this  

Problem with IMG_Load() in SDL

This topic is 4204 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites

This topic is 4204 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this