Jump to content
  • Advertisement
Sign in to follow this  
rakoon2

loading a png with sdl + sdl_image.

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

Hello guys! How can I load a png file using SDL_Image? My problem is that IMG_LoadPNG_RW( rwop ); returns a null pointer! Here is the code:
 if( sprite != NULL )
    {	
		  SDL_FreeSurface( sprite );
		  sprite = NULL;
	}
	
	SDL_RWops* rwop(NULL);
    rwop = SDL_RWFromFile( filename, "rb");
    
    if( rwop == NULL )
    {
        LOG << "SDL_RWFromFile error " << filename << "  "  << ENDL; 
    }        

	sprite = IMG_LoadPNG_RW( rwop );
	
	delete rwop;

    if ( sprite == NULL )
    {
	    LOG << "Couldn't load " << filename << " -- "  << ENDL; 
        return false;
    }

Thank you! :/

Share this post


Link to post
Share on other sites
Advertisement
If your loading from an actual PNG file and not from memory then you can simoly use IMG_Load("Blah.png"). SDL_image will do the rest in determining which function to use to load the image based on it's extension.

Share this post


Link to post
Share on other sites
You need to link to SDL_image, add -lSDL_image to your linker options and make sure you have the SDL import library where the linker can find it.

Share this post


Link to post
Share on other sites
Here's what I did to load a png (this is in C++):

string filename = "my_image.png";
SDL_Surface *new_surf; // this points to the image data once it is loaded
new_surf = IMG_Load(filename.c_str()); // We need to convert the string to a C-type string
new_surf = SDL_DisplayFormat(new_surf); // convert the image format to fit the screen for fast blitting
if (new_surf == NULL) { // typo in filename arg or missing/corrupt file are the most likely errors to cause this
cout << "ERROR: Could not load image " << filename << endl;
exit(1);
}

To compile it (in Linux):

$g++ myfile.cpp -o test `sdl-config --cflags --libs` -lSDL_image

Share this post


Link to post
Share on other sites
Quote:
Original post by Spudder
You need to link to SDL_image, add -lSDL_image to your linker options and make sure you have the SDL import library where the linker can find it.



Hey thanks Spudder! I've been trying to compile my game for the last two hours and I couldn't figure out what I was doing wrong. I _was_ using




gcc -Wall asteroids.c -o asteroids `sdl-config --libs --cflags`



Which was giving me undefined references errors. I'm now compiling like this:




gcc -Wall asteroids.c -lSDL_gfx -o asteroids `sdl-config --libs --cflags`



and it is working perfectly! Thanks for the tip.

- Stephen

Share this post


Link to post
Share on other sites
Quote:
Original post by Roots
SDL_Surface *new_surf; // this points to the image data once it is loaded
new_surf = IMG_Load(filename.c_str()); // We need to convert the string to a C-type string
new_surf = SDL_DisplayFormat(new_surf); // convert the image format to fit the screen for fast blitting


Careful.. that's a memory leak (and a segfault waiting to happen)

SDL_DisplayFormat creates a NEW surface. The old one is still there.
You need to store the DisplayFormat pointer somewhere else, and SDL_FreeSurface() the new_surf (which is now the old surf)

Also if IMG_Load returns NULL, you'll segfault in SDL_DisplayFormat. Instead you should do something like:

new_surf = IMG_Load(filename.c_str());
if (!new_surf){
printf("Couldn't load %s:%s\n",filename.c_str(),IMG_GetError());
//Exit, return error code, or throw an exception
}

Share this post


Link to post
Share on other sites
I just copied over the meat of the function, there is a lot of other stuff that goes on there. When my program gets to that code I already know that the image is valid (I store it in a cache before I come to this part of the code, and if it can't be loaded into the cache I return an error). So I've got the IMG_Load failure check covered.

Thanks for the tip on the memory leak though, I totally spaced that out. Yeah I just looked it up in my book and you are absolutely correct. My code has been running without seg faulting though. Hmm. Oh well, its an easy fix :D

Share this post


Link to post
Share on other sites
Thank you! But I still get

[Linker error] undefined reference to `IMG_Load'




../../../Programme/dev-c++/sdl_ming/libSDL.dll.a
../../../Programme/dev-c++/sdl_ming/libSDL_image.a
../../../Programme/dev-c++/sdl_ming/libSDLmain.a
../../../Programme/dev-c++/sdl_ming/libSDL.a
E:/Programme/dev-c++/lib/libopengl32.a



:(?

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!