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 = (
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?


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)


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

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(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,AMa sk);
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