Help with initializing SDL_Image

Dear friends,

I am trying to learn SDL2, and I when I try to execute the eg program given in Lazy Foo
I was not able to initiliaze SDL_Image and hence run the program. Please help me with this.

The code is given below.

Thanking,
Jose Mathew

/This source code copyrighted by Lazy Foo’ Productions (2004-2015)
and may not be redistributed without written permission.
/

//Using SDL, SDL_image, standard IO, and strings
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <stdio.h>
#include

//Screen dimension constants
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;

//Starts up SDL and creates window
bool init();

//Loads media
bool loadMedia();

//Frees media and shuts down SDL
void close();

//Loads individual image
SDL_Surface* loadSurface( std::string path );

//The window we’ll be rendering to
SDL_Window* gWindow = NULL;

//The surface contained by the window
SDL_Surface* gScreenSurface = NULL;

//Current displayed PNG image
SDL_Surface* gPNGSurface = NULL;

bool init()
{
//Initialization flag
bool success = true;

//Initialize SDL
if( SDL_Init( SDL_INIT_VIDEO ) < 0 )
{
	printf( "SDL could not initialize! SDL Error: %s\n", SDL_GetError() );
	success = false;
}
else
{
	//Create window
	gWindow = SDL_CreateWindow( "SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN );
	if( gWindow == NULL )
	{
		printf( "Window could not be created! SDL Error: %s\n", SDL_GetError() );
		success = false;
	}
	else
	{
		//Initialize PNG loading
		int imgFlags = IMG_INIT_PNG;
		if( !( IMG_Init( imgFlags ) & imgFlags ) )
		{
			printf( "SDL_image could not initialize! SDL_image Error: %s\n", IMG_GetError() );
			success = false;
		}
		else
		{
			//Get window surface
			gScreenSurface = SDL_GetWindowSurface( gWindow );
		}
	}
}

return success;

}

bool loadMedia()
{
//Loading success flag
bool success = true;

//Load PNG surface
gPNGSurface = loadSurface( "loaded.png" );
if( gPNGSurface == NULL )
{
	printf( "Failed to load PNG image!\n" );
	success = false;
}

return success;

}

void close()
{
//Free loaded image
SDL_FreeSurface( gPNGSurface );
gPNGSurface = NULL;

//Destroy window
SDL_DestroyWindow( gWindow );
gWindow = NULL;

//Quit SDL subsystems
IMG_Quit();
SDL_Quit();

}

SDL_Surface* loadSurface( std::string path )
{
//The final optimized image
SDL_Surface* optimizedSurface = NULL;

//Load image at specified path
SDL_Surface* loadedSurface = IMG_Load( path.c_str() );
if( loadedSurface == NULL )
{
	printf( "Unable to load image %s! SDL_image Error: %s\n", path.c_str(), IMG_GetError() );
}
else
{
	//Convert surface to screen format
	optimizedSurface = SDL_ConvertSurface( loadedSurface, gScreenSurface->format, NULL );
	if( optimizedSurface == NULL )
	{
		printf( "Unable to optimize image %s! SDL Error: %s\n", path.c_str(), SDL_GetError() );
	}

	//Get rid of old loaded surface
	SDL_FreeSurface( loadedSurface );
}

return optimizedSurface;

}

int main( int argc, char* args[] )
{
//Start up SDL and create window
if( !init() )
{
printf( “Failed to initialize!\n” );
}
else
{
//Load media
if( !loadMedia() )
{
printf( “Failed to load media!\n” );
}
else
{
//Main loop flag
bool quit = false;

		//Event handler
		SDL_Event e;

		//While application is running
		while( !quit )
		{
			//Handle events on queue
			while( SDL_PollEvent( &e ) != 0 )
			{
				//User requests quit
				if( e.type == SDL_QUIT )
				{
					quit = true;
				}
			}

			//Apply the PNG image
			SDL_BlitSurface( gPNGSurface, NULL, gScreenSurface, NULL );

			//Update the surface
			SDL_UpdateWindowSurface( gWindow );
		}
	}
}

//Free resources and close SDL
close();

return 0;

}

What error messages are you getting?

I am getting the error messages

"SDL_image could not initialize! SDL_image Error: PNG images are not supported
Failed to initialize

Process returned 0 (0x0) execution time : 0.089 s
Press Enter to continue

This is basically the output of my program and I am getting similar outputs when ever I try to use SDL2/SDL_Image.h

Looks like either SDL_image was built without PNG support or it couldn’t find libpng

please tell me, what should I do in order to rectify the problem

what platform are you developing on?

I am using Linux, also I would like to inform you that there is no problem for JPG images

Show your Android.mk file and how you run build.

what does android have to do with it?

no, it have nothing to do with android. I am trying to learn SDL2 on linux

Did you build SDL_image yourself or are you using the version from your linux distribution (which distro are you using)?

Shortcut (and shameless plug): https://github.com/DanielGibson/Snippets/blob/master/SDL_stbimage.h should be easier to integrate and get to work, as it doesn’t need additional libs/dlls (just two header files in your project); otherwise usage is the same as SDL_image, except the functions start with STBIMG_ instead of IMG_ and there is no equivalent of IMG_Init() and IMG_Shutdown() because that’s not needed.

Bonus: Protip about posting code: putting three backticks before and behind the code block makes it look better, if you add c++ behind the beginning three backticks it even gets syntax hilighted, example:

```c++
SDL_Surface* surf = STBIMG_Load("bla.png");
SDL_FreeSurface(surf);
```

will look like

SDL_Surface* surf = STBIMG_Load("bla.png");
SDL_FreeSurface(surf);