Png Image Problem?

Hello all

I am using Visual Studio 2022 and I was checking NuGet Package to make sure what Versions of SDL I am using which is SDL2.

I am learning from Lazy Foo and then I decide to change into Png Image but I had two Errors cropping up

#include <SDL.h>
#include <SDL/SDL_Image> // Dont know why this got a ERROR as saying Include is Error which is Strange!

#include <stdio.h>

//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();

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

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

//The image we will load and show on the screen
SDL_Surface* gHelloWorld = 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
		{
			//Get window surface
			gScreenSurface = SDL_GetWindowSurface(gWindow);
		}
	}

	return success;
}

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

	//Load splash image

	

	// gHelloWorld = SDL_LoadBMP("res/FROGGER.bmp"); This BITMAP does work :)
	
	// THERE IS ANOTHER ERROR THAT TELL ME THAT IMG_LOAD IS ERROR!

	SDL_Surface* pTempSurface = IMG_Load("assets/FROGGER.png");  // I am trying to make this work in PNG!
	
	if (gHelloWorld == NULL)
	{
		printf("Unable to load image %s! SDL Error: %s\n", "res/FROGGER.Png", SDL_GetError());
		success = false;
	}

	return success;
}


void close()
{
	//Deallocate surface
	SDL_FreeSurface(gHelloWorld);
	gHelloWorld = NULL;

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

	//Quit SDL subsystems
	SDL_Quit();
}

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
		{
			//Apply the image
			SDL_BlitSurface(gHelloWorld, NULL, gScreenSurface, NULL);

			//Update the surface
			SDL_UpdateWindowSurface(gWindow);

			//Wait two seconds
			SDL_Delay(20000);
		}
	}

	//Free resources and close SDL
	close();

	return 0;
}

Can anyone tell me how to sort the Png Image problem and why there is Errors when there shouldnt be?

Should be #include <SDL2/SDL_Image.h> - with 2 and .h

I got 2 errors saying

cannot open source file SDL2/SDL_Image.h
IMG_Load is undefined

This work fine when I got BMP image on screen
#include <SDL.h>
#include <stdio.h>

This isnt fine when I trying get Png Image on Screen
#include <SDL.h>
#include <SDL2/SDL_Image.h> // Error!
#include <stdio.h>

Note that SDL_Image is an additional library (that most probably must be installed separately), not part of the “main” SDL library/package


I have installed the first one SDL2

Is there anythings else that I need to installed?

I don’t use nuget, but most probably you’ll need a sdl2_image package

Thanks Daniel and it almost fixed the problem but I get the picture of this…

eh

You’ll have to link against both SDL2 and SDL2_Image

How do I do that? Is there links buttons somewhere?

It seems that your SDL2 nuget is very outdated and may be missing a function used by SDL2_image.

The official nuget is named sdl2.nuget by Sam Lantinga and SDL2 contributors.

Similarly, the official nuget for SDL2_image is sdl2_image.nuget by Sam Lantinga and SDL2 contributors.

It should look like this:

Thanks Everyone who help me on here and I got it working with Png Image :slight_smile:

Petruska? Yes, you are right and I had installed the package of more SDL2 and work Great :slight_smile: