1

I am beginning programming in SDL 2.0. I would like to be able to load .PNG files into my program, as .bmp files don't have all the neat features .png files do.

I have the following code from http://www.sdltutorials.com/sdl-image:

SDL_Surface* loadSurf(char* File) {
SDL_Surface* Surf_Temp = NULL;
SDL_Surface* Surf_Return = NULL;

if((Surf_Temp = IMG_Load(File)) == NULL) {
    return NULL;
}

Surf_Return = SDL_DisplayFormatAlpha(Surf_Temp);
SDL_FreeSurface(Surf_Temp);

return Surf_Return;
}

The point of the function is to load an image, you would go about calling it by doing something like:

SDL_Surface* character = loadSurf("sprite.png");

However, when I run it, an error appears at the "Surf_Return = SDL_DisplayFormatAlpha(Surf_Temp); " line. The error is: Use of undeclared identifier 'SDL_DisplayFormatAlpha'

I figure that the reason for this is because the code was written for SDL 1.2, and I am using SDL 2.0. Is there a way to convert this function into SDL 2.0?

I am running Mac OSX 10.13.2, if that is applicable.

Shades
  • 667
  • 1
  • 7
  • 22
  • Use ``` SDL_Surface* Surf_Return = IMG_Load(File); ``` To load the IMG from file. Then I believe you want to convert it to a texture, in which case: ```SDL_CreateTextureFromSurface``` should do. What are you trying to achieve? – pappix Mar 05 '18 at 22:29
  • Also: If you are also looking for tutorials for SDL2, I found Lazy Foo tutorial very useful in the past: http://lazyfoo.net/tutorials/SDL/index.php – pappix Mar 05 '18 at 22:32
  • @pappix I am trying to load a .PNG file from my computer, turn it into a texture, then display it on my screen using SDL_RenderCopy() and RenderPresent(). – Shades Mar 06 '18 at 15:55
  • @pappix the reason I am doing this is so I can load images with transparent backgrounds, this way I won't have any white boxes around the sprites. – Shades Mar 06 '18 at 16:03

1 Answers1

6

I think they removed SDL_DisplayFormatAlpha (and SDL_DisplayFormat) from SDL2

If you want to add an alpha channel you want to use SDL_ConvertSurfaceFormat with your surface loaded from IMG_Load, for example

// Returns a new surface with an alpha channel added
SDL_Surface *new_s = SDL_ConvertSurfaceFormat(surf, SDL_PIXELFORMAT_RGBA8888, 0);

By the way that tutorial looks very old, you might want to look into the SDL_Image documentation.

aram
  • 1,415
  • 13
  • 27
  • That's looking good! I just have a few questions about it. So, SDL_DisplayFormatAlpha() would change the images format so that it has an alpha layer (As best as I can tell) does SDL_ConvertSurfaceFormat() do basically the same thing? If I am loading a .png file with a transparent background, what pixel format am I going to want to use? Thanks for the help. – Shades Mar 06 '18 at 16:09
  • @Shades I think SDL detects the format, so in case of loading pngs with alpha channel you would get a RGBA format by default. – aram Mar 06 '18 at 16:15