Sign in to follow this  
Slernald

SDL IMG_Load_RW with pngs

Recommended Posts

Slernald    100
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
  free(buffer);
  
  // Were we able to load the image?
  if(temp == NULL) {
    std::cout << "Unable to load image: " << SDL_GetError();
    exit(1);
  }
  
  // Convert the image to optimal display format
  g_Image = SDL_DisplayFormatAlpha(temp);
  
  // Free the temp surface
  SDL_FreeSurface(temp);
  
  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
szymczyk    182
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
szymczyk    182
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.

Mark

Share this post


Link to post
Share on other sites

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