BGRABitmap tutorial 8

From Free Pascal wiki
Revision as of 19:04, 1 April 2011 by Circular (talk | contribs) (created)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search

This tutorial shows how to use textures.

Create a new project

Create a new project and add a reference to BGRABitmap, the same way as in the first tutorial.

Using brush textures

The simplest texture is a hatched brush.

With the object inspector, add an OnPaint handler and write : <delphi>procedure TForm1.FormPaint(Sender: TObject); var

 image,tex: TBGRABitmap;
 c: TBGRAPixel;
 x,y,rx,ry: single;

begin

   image := TBGRABitmap.Create(ClientWidth,ClientHeight,ColorToBGRA(ColorToRGB(clBtnFace)));
   c := ColorToBGRA(ColorToRGB(clWindowText));
   //ellipse coordinates
   x := 150;
   y := 100;
   rx := 100;
   ry := 50;
   //loads a "diagcross" brush with white pattern and orange background
   tex := image.CreateBrushTexture(bsDiagCross,BGRAWhite,BGRA(255,192,0)) as TBGRABitmap;
   image.FillEllipseAntialias(x,y,rx-0.5,ry-0.5,tex);
   image.EllipseAntialias(x,y,rx,ry,c,1); //draw outline
   tex.Free;
   image.Draw(Canvas,0,0,True);
   image.free;  

end;</delphi>

As you can see, a texture is just some bitmap. To fill an ellipse with a texture, just pass the texture as a parameter instead of the color.

Two commands define the ellipse. The first is the filling, and the second is the outline. Notice that the radius is 0.5 pixel smaller for the filling. Indeed, when the pen width is 1, the inner radius is 0.5 smaller and the out radius is 0.5 greater.

Using a command for the outline, we achieve to draw a textured ellipse with a border. But if the outline function is not available, you can also use another fill command with a greater radius with the border color first, and then a smaller radius for the inside.

Add the following lines before tex.Free : <delphi> image.RoundRectAntialias(x-rx-10,y-ry-10,x+rx+10,y+ry+10,20,20,c,11);

   image.RoundRectAntialias(x-rx-10,y-ry-10,x+rx+10,y+ry+10,20,20,tex,9); </delphi>

The first command draws a wide round rectangle (of width 11) that includes the border. The second command fill with the texture with a smaller width (9). This works perfectly as long as the texture is not transparent.

Run the program

You should obtain a round rectangle with an ellipse inside it. Each shape is filled with an orange texture.

BGRATutorial8.png

Previous tutorial (splines)