Results 1 to 2 of 2

Thread: Calculate masks for SDL_CreateRGBSurface?

  1. #1

    Calculate masks for SDL_CreateRGBSurface?

    Hi all,
    I want to be able to simply create a SDL surface using the SDL_CreateRGBSurface() routine, but only if I am given a width, height, and one of a few common bit depths. I need to calculate the 4 masks for that routine that depends on the endiness of the system.

    So given a bit depth like so:

    Code:
    Type
        TBitDepth = (
            eBitDepth_8Bit,
            eBitDepth_15Bit,
            eBitDepth_16Bit,
            eBitDepth_24Bit,
            eBitDepth_32Bit
        );
    Is there any 'easy' way of calculating the R, G, B and A masks for that depth and endiness?

    Code:
    Procedure CalculateRGBAMasks(Const ABitDepth               : TBitDepth;
                                 Var   RMask,GMask,BMask,AMask : LongWord);
    Begin
        // calculate masks required for bit depth?
    End;
    I am figuring it may be something like so (for little endian)

    Code:
    Procedure CalculateRGBAMasks(Const ABitDepth               : TBitDepth;
                                 Var   RMask,GMask,BMask,AMask : LongWord);
    Begin
        RMask := 0;
        GMask := 0;
        BMask := 0;
        AMask := 0;
        If SDL_BYTEORDER = SDL_LIL_ENDIAN Then
        Begin
            If ABitDepth In[eBitDepth_15Bit,eBitDepth_16Bit] Then
            Begin
                RMask := 31;         // 5 bits wide
                GMask := 63 Shl 5;   // 6 bits wide
                BMask := 31 Shl 11;  // 5 bits wide
            End
            Else
            If ABitDepth = eBitDepth_24Bit Then
            Begin
                RMask := 8;
                GMask := 8 Shl 8;
                BMask := 8 Shl 16;
            End
            Else
            If ABitDepth = eBitDepth_32Bit Then
            Begin
                RMask := 8;
                GMask := 8 Shl 8;
                BMask := 8 Shl 16;
                AMask := 8 Shl 24;
            End;
        End
        Else
        // bit endian format
        Begin
        End;
    End;
    If the little endian part is correct, I am not sure what the big endian version should be...

    Thanks for any help you can give me :-)
    cheers,
    Paul

  2. #2

    Calculate masks for SDL_CreateRGBSurface?

    Ok, after testing on my Windows PC, the small endian version seems correct, but I am not sure what the big endian code would look like, and I can't test it anyway...

    if someone could workout some code and maybe test it on their big endian machine (if possible) that would be great

    Below is the routine that would use the calculated masks.

    Code:
    Function CreateSurface(Const AWidth,AHeight : Word;
                           Const ABitDepth      : TBitDepth) : PSDL_Surface;
    Var
        bpp        : Byte;
        RMask      : LongWord;
        GMask      : LongWord;
        BMask      : LongWord;
        AMask      : LongWord;
    Begin
        Case ABitDepth Of
            eBitDepth_8Bit  : bpp := 8;
            eBitDepth_15Bit : bpp := 15;
            eBitDepth_16Bit : bpp := 16;
            eBitDepth_24Bit : bpp := 24;
            eBitDepth_32Bit : bpp := 32;
        Else
            bpp := 16;
        End;
        CalculateRGBAMasks(ABitDepth,RMask,GMask,BMask,AMask);
        Result := SDL_CreateRGBSurface(SDL_SWSURFACE,
                                       AWidth,
                                       AHeight,
                                       bpp,
                                       RMask,
                                       GMask,
                                       BMask,
                                       AMask);
        FillChar(PByte(Result^.pixels)^,AWidth * AHeight * Result^.format^.BytesPerPixel,0);
    End;

    cheers,
    Paul

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •