Greetings Everyone
I am new to SDL development, while following a tutorial it got me wondering what SDL_DisplayFormat() and SDL_DisplayFormatAlpha() are used for as the following snippets outputs the same result:
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <SDL.h>
#include <SDL_image.h>
extern SDL_Surface *Screen, *KittensImage;
SDL_Surface *LoadImage(char *ImagePath)
{
// Load the image using SDL image;
SDL_Surface *GFX = IMG_Load(ImagePath);
SDL_Surface *Image;
if(GFX == NULL)
{
printf("Failed To Load Image %s\n", ImagePath);
return NULL;
}
// Make the background transparent;
SDL_SetColorKey(GFX, (SDL_SRCCOLORKEY|SDL_RLEACCEL),
SDL_MapRGB(GFX->format, 0, 0, 0));
// Convert the image to the screen's native format;
Image = SDL_DisplayFormat(GFX);
SDL_FreeSurface(GFX);
if(Image == NULL)
{
printf("Failed To Convert Image %s To Native Format\n",
ImagePath);
return NULL;
}
// Return the processed image;
return Image;
//return GFX;
}
void DrawImage(SDL_Surface *MyImage, int X, int Y)
{
SDL_Rect Dest;
// Set the blitting rectangle to the size of the source
// image;
Dest.x = X;
Dest.y = Y;
Dest.w = MyImage->w;
Dest.h = MyImage->h;
// Blit the entire image onto the screen at
// coordinates X and Y;
SDL_BlitSurface(MyImage, NULL, Screen, &Dest);
}
void UpdateScreen()
{
// Blank the screen;
SDL_FillRect(Screen, NULL, 0);
// Draw the image to 160, 120;
DrawImage(KittensImage, 40, 25);
// Swap the image buffers;
SDL_Flip(Screen);
}
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <SDL.h>
#include <SDL_image.h>
extern SDL_Surface *Screen, *KittensImage;
SDL_Surface *LoadImage(char *ImagePath)
{
// Load the image using SDL image;
SDL_Surface *GFX = IMG_Load(ImagePath);
SDL_Surface *Image;
if(GFX == NULL)
{
printf("Failed To Load Image %s\n", ImagePath);
return NULL;
}
// Make the background transparent;
SDL_SetColorKey(GFX, (SDL_SRCCOLORKEY|SDL_RLEACCEL),
SDL_MapRGB(GFX->format, 0, 0, 0));
// Convert the image to the screen's native format;
Image = SDL_DisplayFormat(GFX);
//SDL_FreeSurface(GFX);
if(Image == NULL)
{
printf("Failed To Convert Image %s To Native Format\n",
ImagePath);
return NULL;
}
// Return the processed image;
//return Image;
return GFX;
}
void DrawImage(SDL_Surface *MyImage, int X, int Y)
{
SDL_Rect Dest;
// Set the blitting rectangle to the size of the source
// image;
Dest.x = X;
Dest.y = Y;
Dest.w = MyImage->w;
Dest.h = MyImage->h;
// Blit the entire image onto the screen at
// coordinates X and Y;
SDL_BlitSurface(MyImage, NULL, Screen, &Dest);
}
void UpdateScreen()
{
// Blank the screen;
SDL_FillRect(Screen, NULL, 0);
// Draw the image to 160, 120;
DrawImage(KittensImage, 40, 25);
// Swap the image buffers;
SDL_Flip(Screen);
}
Tried to google it but all I got is a description for the API and nothing related to the reason of their use.
Thanks In Advance