PDA

View Full Version : Drawing a TBitmpa onto a Surface



cairnswm
15-01-2005, 08:26 AM
How do I draw an off screen bitmap (stored in TBitmap - as I ant to use the canvas) onto a surface?

cairnswm
15-01-2005, 07:28 PM
procedure TS2DLImage.CopyBitmap(X, Y: Integer; BMP: TBitmap;
TransparentColor: UInt32);
var
bits : Pointer;
I,J, bpp, r, g, b : Uint8;
Color : UInt32;
begin
if ( SDL_LockSurface( ImageSurface ) < 0 ) then
exit;
bpp := ImageSurface.format.BytesPerPixel;
For I := 0 to BMP.Width-1 do
For J := 0 to BMP.Height-1 do
Begin
Color := BMP.Canvas.Pixels[I,J];
If Color <> TransparentColor then
Begin
bits := @(PixelArray(ImageSurface.Pixels^)[((Y+I) * ImageSurface.pitch) + ((X+J) * bpp)]);
// Set the pixel
case bpp of
1:
begin
PUint8( bits )^ := Uint8(Color);
end;
2:
begin
PUint16(bits)^ := Uint16(Color);
end;
3:
begin // Format/endian independent
r := (Color shr ImageSurface.format.Rshift ) and $FF;
g := (Color shr ImageSurface.format.Gshift ) and $FF;
b := (Color shr ImageSurface.format.Bshift ) and $FF;
PixelFormat(bits^)[(ImageSurface.format.Rshift div 8)] := r;
PixelFormat(bits^)[(ImageSurface.format.Gshift div 8)] := g;
PixelFormat(bits^)[(ImageSurface.format.Bshift div 8)] := b;
end;
4:
begin
PUInt32(bits)^ := Uint32( Color );
end;
end;
End;
End;
// Update the display
SDL_UnlockSurface(ImageSurface);
SDL_UpdateRect(ImageSurface, X, Y, BMP.Width, BMP.Height);
end;


Dont think it will be great performance, but it does work :)