Thread: SDL_DisplayFormat() & SDL_DisplayFormatAlpha() what are they used for?

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    61

    SDL_DisplayFormat() & SDL_DisplayFormatAlpha() what are they used for?

    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

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    From my understanding, SDL_DisplayFormat() converts the SDL_Surface into another one which has the same format as the current screen (opened with SDL_SetVideoMode()). This means that it will have the same bpp or bits per pixel (for example, you might have a 32-bit colour screen but only an 8-bit colour BMP). If the original surface is in colour-index mode while your screen is not (which might actually happen when you load a BMP, I don't know) then again, the surface will be converted to whatever is appropriate for the current screen. The purpose of this is to speed up blitting of the surface. If you don't do the conversion with SDL_DisplayFormat(), then the SDL will still be able to display your image, but it will do the conversion on the fly every time you blit it! Which, as you can imagine, will slow your program down drastically.

    Actually the SDL does a lot of translation for you. For example, if you insist on having a specific BPP in your screen, but the hardware doesn't support it, the SDL will set up a special software buffer where you can write in whatever format you like, and then do the translation behind the scenes to whatever the hardware actually supports. So in general the SDL will just work, but if you're finding that it's running slower than you expected, look into what translation it might be doing. A good start would be to use SDL_HWSURFACE for a hardware surface, maybe query the exact screen modes available, maybe be more flexible in the BPP you ask for, etc.

    SDL_DisplayFormatAlpha() is what you should use if the original image has an alpha channel (i.e. some transparent sections) which you want to preserve, or if you want to change one colour to become the transparent colour. Use SDL_DisplayFormat() when you don't care about transparency. Normally BMPs don't have an intrinsic "transparent" colour, although you'll see some programs that take a specific colour (like bright pink) that doesn't occur normally in the image, and denote that as "transparent". SDL_DisplayFormatAlpha() lets you do this. Some images (if you're using SDL_Image and you've loaded a PNG image, for example) will have transparency already and SDL_DisplayFormatAlpha() lets you preserve the transparency.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Dec 2010
    Posts
    61
    Thank you so very much Mr. dwks for your time and effort to unswer my question, sure it's helpful, bless you.

Popular pages Recent additions subscribe to a feed