Jump to content
  • Advertisement
Sign in to follow this  

SDL IMG_Load_RW with pngs

This topic is 3014 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

Hi there, I've been following a tutorial from here. However I'm having trouble loading PNG images, .bmp works fine, the code I have so far is:
SDL_Surface *Graphics::load_image(char *resfilename, char *imgfilename) {
  // Get the images buffer and size from the resource file
  int filesize = 0;
  char *buffer = get_buffer_from_resource(resfilename, imgfilename, &filesize);
  // Load the buffer into a surface using RWops
  SDL_RWops *rw = SDL_RWFromMem(buffer, filesize);
  SDL_Surface *temp = IMG_Load_RW(rw, 1);
  // Release the image buffer memory
  // Were we able to load the image?
  if(temp == NULL) {
    std::cout << "Unable to load image: " << SDL_GetError();
  // Convert the image to optimal display format
  g_Image = SDL_DisplayFormatAlpha(temp);
  // Free the temp surface
  return g_Image;

I've tried:
SDL_Surface *temp = IMG_LoadTyped_RW(rw, 1, "PNG");

but no luck. The stdout.txt file reads, "Unable to load image: Error reading the PNG file." Any ideas/tips will be appreciated, thanks.

Share this post

Link to post
Share on other sites
Are you using Windows? If so, did you place the libpng DLL in the same location as SDL_image.dll? SDL_image has trouble loading PNG files on Windows if the libpng DLL is not in the right location.

Share this post

Link to post
Share on other sites
Make sure you have zlib.dll in the same directory as SDL_image.dll. SDL_image uses zlib for PNG compression.

If that doesn't work, try loading another file format like JPEG or TIFF to see if you can narrow the problem down to PNG files. Make sure the image files and DLLs are in the proper location. Step through your file loading code in the debugger. There's not much else to suggest.


Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!