PDA

View Full Version : Calculate masks for SDL_CreateRGBSurface?

paul_nicholls
07-07-2008, 02:43 AM
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:

Type
TBitDepth = &#40;
eBitDepth_8Bit,
eBitDepth_15Bit,
eBitDepth_16Bit,
eBitDepth_24Bit,
eBitDepth_32Bit
&#41;;

Is there any 'easy' way of calculating the R, G, B and A masks for that depth and endiness?

Begin
// calculate masks required for bit depth?
End;

I am figuring it may be something like so (for little endian)

Begin
If SDL_BYTEORDER = SDL_LIL_ENDIAN Then
Begin
If ABitDepth In&#91;eBitDepth_15Bit,eBitDepth_16Bit&#93; Then
Begin
RMask &#58;= 31; // 5 bits wide
GMask &#58;= 63 Shl 5; // 6 bits wide
BMask &#58;= 31 Shl 11; // 5 bits wide
End
Else
If ABitDepth = eBitDepth_24Bit Then
Begin
End
Else
If ABitDepth = eBitDepth_32Bit Then
Begin
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...

cheers,
Paul

paul_nicholls
07-07-2008, 02:52 AM
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.

Function CreateSurface&#40;Const AWidth,AHeight &#58; Word;
Const ABitDepth &#58; TBitDepth&#41; &#58; PSDL_Surface;
Var
bpp &#58; Byte;
Begin
Case ABitDepth Of
eBitDepth_8Bit &#58; bpp &#58;= 8;
eBitDepth_15Bit &#58; bpp &#58;= 15;
eBitDepth_16Bit &#58; bpp &#58;= 16;
eBitDepth_24Bit &#58; bpp &#58;= 24;
eBitDepth_32Bit &#58; bpp &#58;= 32;
Else
bpp &#58;= 16;
End;
Result &#58;= SDL_CreateRGBSurface&#40;SDL_SWSURFACE,
AWidth,
AHeight,
bpp,