Difference between revisions of "Developing with Graphics/de"
Line 63: | Line 63: | ||
end; | end; | ||
</code> | </code> | ||
− | Diese Funktion in Lazarus | + | Diese Funktion könnte in Lazarus so implementiert werden: |
<code> | <code> | ||
procedure TForm1.FadeIn(ABitMap: TBitMap); | procedure TForm1.FadeIn(ABitMap: TBitMap); |
Revision as of 09:40, 12 April 2007
│
Deutsch (de) │
English (en) │
español (es) │
français (fr) │
italiano (it) │
日本語 (ja) │
한국어 (ko) │
Nederlands (nl) │
português (pt) │
русский (ru) │
slovenčina (sk) │
中文(中国大陆) (zh_CN) │
中文(台灣) (zh_TW) │
Diese Seite wird der Anfang von Tutorials sein bezüglich der Manipulation von Bitmaps und anderen Grafiken. Da ich kein Grafikprogrammierer bin, lade ich alle ein ihr Wissen zu teilen! Fügen sie einfach einen Link zum nächsten Abschnitt hinzu, fügen eine Seite hinzu und erzeugen ihren eigenen Wiki Artikel.
Auf dieser Seite werden einige allgemeine Informationen geboten.
Andere Grafik Artikel
- GLScene - Eine Portierung der visual OpenGL graphics Library GLScene
- TAChart - Chartkomponente für Lazarus
- PlotPanel - Ähnlich wie TAChart
- PascalMagick - ein einfach zu verwendentes API für die Kopplung mit ImageMagick, einer freien multiplattform Softwaresammlung um Bitmaps zu erzeugen, zu bearbeiten und zu entwerfen.
- LazRGBGraphics - Ein Package für fast in memory image processing and pixel manipulations (like scan line).
- Perlin Noise - Ein Artikel über die Verwendung von Perlin Noise in LCL Anwendungen.
Arbeiten mit TBitmap
Das erste woran sie denken sollten ist, dass Lazarus Plattformunabhängig sein soll. Daher kommen alle Methoden, welche die Windows API Funktionalität nutzen, nicht in Frage. Daher wird eine Methode wie ScanLine nicht von Lazarus unterstützt, weil sie für Geräte-unabhängige Bitmaps gedacht ist und Funktionen von GDI32.dll verwendet.
Geben sie acht, dass, wenn sie nicht die Breite und Höhe ihrer TBitmap spezifizieren, sie die Standardmaße haben wird, welche ziemlich klein sind.
Ein fading Beispiel
Sagen wir sie wollen ein Fadendes Bild erstellen. In Delphi können sie etwas wie dieses tun:
type
PRGBTripleArray = ^TRGBTripleArray;
TRGBTripleArray = array[0..32767] of TRGBTriple;
procedure TForm1.FadeIn(aBitMap: TBitMap);
var
Bitmap, BaseBitmap: TBitmap;
Row, BaseRow: PRGBTripleArray;
x, y, step: integer;
begin
Bitmap := TBitmap.Create;
try
Bitmap.PixelFormat := pf32bit; // oder pf24bit
Bitmap.Assign(aBitMap);
BaseBitmap := TBitmap.Create;
try
BaseBitmap.PixelFormat := pf32bit;
BaseBitmap.Assign(Bitmap);
for step := 0 to 32 do begin
for y := 0 to (Bitmap.Height - 1) do begin
BaseRow := BaseBitmap.Scanline[y];
Row := Bitmap.Scanline[y];
for x := 0 to (Bitmap.Width - 1) do begin
Row[x].rgbtRed := (step * BaseRow[x].rgbtRed) shr 5;
Row[x].rgbtGreen := (step * BaseRow[x].rgbtGreen) shr 5; // Fading
Row[x].rgbtBlue := (step * BaseRow[x].rgbtBlue) shr 5;
end;
end;
Form1.Canvas.Draw(0, 0, Bitmap);
InvalidateRect(Form1.Handle, nil, False);
RedrawWindow(Form1.Handle, nil, 0, RDW_UPDATENOW);
end;
finally
BaseBitmap.Free;
end;
finally
Bitmap.Free;
end;
end;
Diese Funktion könnte in Lazarus so implementiert werden:
procedure TForm1.FadeIn(ABitMap: TBitMap);
var
SrcIntfImg, TempIntfImg: TLazIntfImage;
ImgHandle,ImgMaskHandle: HBitmap;
FadeStep: Integer;
px, py: Integer;
CurColor: TFPColor;
TempBitmap: TBitmap;
begin
SrcIntfImg:=TLazIntfImage.Create(0,0);
SrcIntfImg.LoadFromBitmap(ABitmap.Handle,ABitmap.MaskHandle);
TempIntfImg:=TLazIntfImage.Create(0,0);
TempIntfImg.LoadFromBitmap(ABitmap.Handle,ABitmap.MaskHandle);
TempBitmap:=TBitmap.Create;
for FadeStep:=1 to 32 do begin
for py:=0 to SrcIntfImg.Height-1 do begin
for px:=0 to SrcIntfImg.Width-1 do begin
CurColor:=SrcIntfImg.Colors[px,py];
CurColor.Red:=(CurColor.Red*FadeStep) shr 5;
CurColor.Green:=(CurColor.Green*FadeStep) shr 5;
CurColor.Blue:=(CurColor.Blue*FadeStep) shr 5;
TempIntfImg.Colors[px,py]:=CurColor;
end;
end;
TempIntfImg.CreateBitmap(ImgHandle,ImgMaskHandle,false);
TempBitmap.Handle:=ImgHandle;
TempBitmap.MaskHandle:=ImgMaskHandle;
Canvas.Draw(0,0,TempBitmap);
end;
SrcIntfImg.Free;
TempIntfImg.Free;
TempBitmap.Free;
end;
Der Lazarus Code auf dieser Seite wurde dem $LazarusPath/examples/lazintfimage/fadein1.lpi Projekt entnommen. Wenn sie einen einfachen Start mit der Grafikprogrammierung wollen, dann werfen sie einen näheren Blick auf dieses Beispiel.
Transparent zeichnen mit Bitmaps
Ein neues Feature, implementiert in Lazarus 0.9.11, sind farbtransparente Bitmaps. Bitmap Dateien (*.BMP) können keine Informationen über Transparenz speichern, aber sie können dies zur Laufzeit umgehen indem sie eine Farbe wählen um den transparenten Bereich zu representieren. Dies ist ein üblicher Trick, der bei Win32 Anwendungen verwendet wird.
Das folgende Beispiel lädt eine Bitmap aus einer Windows Ressource, wählt eine Farbe für die Transparenz (clFuchsia) und zeichnet es auf den Canvas.
procedure MyForm.MyButtonOnClick(Sender: TObject); var buffer: THandle; bmp: TBitmap; memstream: TMemoryStream; begin bmp := TBitmap.Create; buffer := Windows.LoadBitmap(hInstance, MAKEINTRESOURCE(ResourceID)); if (buffer = 0) then exit; // Fehler beim Laden der Bitmap bmp.Handle := buffer; memstream := TMemoryStream.create; try bmp.SaveToStream(memstream); memstream.position := 0; bmp.LoadFromStream(memstream); finally memstream.free; end; bmp.Transparent := True; bmp.TransparentColor := clFuchsia; MyCanvas.Draw(0, 0, bmp); bmp.Free; // Gibt belegte Ressourcen frei end;
Beachten sie, daß die Speicheroperationen ausgeführt werden mit TMemoryStream. Sie sind notwendig, um das korrekte Laden des Bildes sicherzustellen.
Ein Bildschirmfoto erstellen
Seit Lazarus 0.9.16 können sie die LCL verwenden um plattformunabhängig Bildschirmfotos aufzunehmen. Der folgende Beispielcode zeigt es (funktioniert unter GTK2 und Win32, aber gegenwärtig nicht unter GTK1):
uses LCLIntf, LCLType; ... var MyBitmap: TBitmap ScreenDC: HDC; begin MyBitmap := TBitmap.Create; ScreenDC := GetDC(0); MyBitmap.LoadFromDevice(ScreenDC); ReleaseDC(ScreenDC); ...
Motion Graphics - Wie man flimmern vermeidet
Viele Programme zeichnen ihre Ausgabe als 2D Grafik in die GUI. Wenn diese Grafiken schnell geändert werden müssen, werden sie bald einem Problem begegnen: sich schnell ändernde Grafiken flimmern oft auf dem Bildschirm. Das passiert wenn der Benutzer manchmal das ganze Bild sieht und manchmal wenn es nur teilweise gezeichnet ist. Das geschieht weil der Zeichenprozess Zeit benötigt.
Aber wie kann ich das Flimmern vermeiden und das beste Zeichentempo erreichen? Natürlich können sie mit Hardwarebeschleunigung unter Verwendung von OpenGL arbeiten, aber dieser Ansatz ist ziemlich schwer für kleine Programme oder alte Computer. Dieses Tutorial will sich auf das Zeichnen in ein TCanvas konzentrieren. Wenn sie Hilfe brauchen zu OpenGL, werfen sie einen Blick auf das Beispiel, das mit Lazarus geliefert wird oder GLScene. Sie können auch A.J. Venter's gamepack verwenden, welches einen double-buffered canvas und eine sprite Komponente bereitstellt.
Jetzt wollen wir die Optionen untersuchen, die wir für das Zeichnen in ein Canvas haben:
- Zeichnen in ein TImage
- Draw on the OnPaint event of the form, a TPaintBox or another control
- Create a custom control which draws itself
- Using A.J. Venter's gamepack
Zeichnen in ein TImage
A TImage consists of 2 parts: A TGraphic, usually a TBitmap, holding the persistent picture and the visual area, which is repainted on every OnPaint. Resizing the TImage does not resize the bitmap. The graphic (or bitmap) is accessible via Image1.Picture.Graphic (or Image1.Picture.Bitmap). The canvas is Image1.Picture.Bitmap.Canvas. The canvas of the visual area of a TImage is only accessible during Image1.OnPaint via Image1.Canvas.
Important: Never use the OnPaint of the Image1 event to draw to the graphic/bitmap of a TImage. The graphic of a TImage is buffered so all you need to do is draw to it from anywhere and the change is there forever. However, if you are constantly redrawing, the image will flicker. In this case you can try the other options. Drawing to a TImage is considered slower then the other approaches.
Ändern der Grösse einer Bitmap in TImage
Anmerkung: Tun sie das nicht während OnPaint.
with Image1.Picture.Bitmap do begin Width:=100; Height:=120; end;
Zeichnen auf eine Bitmap in TImage
Anmerkung: Tun sie das nicht während OnPaint.
with Image1.Picture.Bitmap.Canvas do begin // fill the entire bitmap with red Brush.Color:=clRed; FillRect(0,0,Width,Height); end;
Anmerkung: Inside of Image1.OnPaint the Image1.Canvas points to the volatile visible area. Outside of Image1.OnPaint the Image1.Canvas points to Image1.Picture.Bitmap.Canvas.
Ein anderes Beispiel:
procedure TForm1.BitBtn1Click(Sender: TObject);
var
x, y: Integer;
begin
// Zeichnet den Hintergrund
MyImage.Canvas.Pen.Color := clWhite;
MyImage.Canvas.Rectangle(0, 0, Image.Width, Image.Height);
// Draws squares
MyImage.Canvas.Pen.Color := clBlack;
for x := 1 to 8 do
for y := 1 to 8 do
MyImage.Canvas.Rectangle(Round((x - 1) * Image.Width / 8), Round((y - 1) * Image.Height / 8),
Round(x * Image.Width / 8), Round(y * Image.Height / 8));
end;
Painting on the volatile visual area of the TImage
You can only paint on this area during OnPaint. OnPaint is eventually called automatically by the LCL when the area was invalidated. You can invalidate manually the area with Image1.Invalidate. This will not call immediately OnPaint and you can call Invalidate as mny times as you want.
procedure TForm.Image1Paint(Sender: TObject); begin with Image1.Canvas do begin // paint a line Pen.Color:=clRed; Line(0,0,Width,Height); end; end;
Draw on the OnPaint event
In this case all the drawing has to be done on the OnPaint event of the form. It doesn't remain on the buffer, like on the TImage.
Create a custom control which draws itself
Die Erzeugung eines custom control hat den Vorteil der Strukturing ihres Codes und sie können das Bedienelement wiederverwenden. Dieser Ansatz ist sehr schnell, aber er kann dennoch Flimmern erzeugen if you don't draw to a TBitmap first and then draw to the canvas. In diesem Fall besteht keine Notwendigkeit das OnPaint Ereignis des Bedienelements zu verwenden.
Hier ist ein Beispiel custom control:
type
TMyDrawingControl = class(TCustomControl)
public
procedure Paint; override;
end;
implementation
procedure TMyDrawingControl.Paint;
var
x, y: Integer;
Bitmap: TBitmap;
begin
Bitmap := TBitmap.Create;
try
// Initialisiert die Bitmap Größe
Bitmap.Height := Height;
Bitmap.Width := Width;
// Zeichnet den Hintergrund
Bitmap.Canvas.Pen.Color := clWhite;
Bitmap.Canvas.Rectangle(0, 0, Width, Height);
// Zeichnet squares
Bitmap.Canvas.Pen.Color := clBlack;
for x := 1 to 8 do
for y := 1 to 8 do
Bitmap.Canvas.Rectangle(Round((x - 1) * Width / 8), Round((y - 1) * Height / 8),
Round(x * Width / 8), Round(y * Height / 8));
Canvas.Draw(0, 0, Bitmap);
finally
Bitmap.Free;
end;
inherited Paint;
end;
und wie wir es auf dem Formular erzeugen:
procedure TMyForm.FormCreate(Sender: TObject);
begin
MyDrawingControl:= TMyDrawingControl.Create(Self);
MyDrawingControl.Height := 400;
MyDrawingControl.Width := 500;
MyDrawingControl.Top := 0;
MyDrawingControl.Left := 0;
MyDrawingControl.Parent := Self;
MyDrawingControl.DoubleBuffered := True;
end;
nur nicht vergessen es zu löschen:
procedure TMyForm.FormDestroy(Sender: TObject);
begin
MyDrawingControl.Free;
end;
Das Setzen von Top und Left auf null ist nicht notwendig, seit dies die Standardposition ist, but is done so to reinforce where the control will be put.
"MyDrawingControl.Parent := Self;" ist sehr wichtig und sie werden ihr Bedienelement nicht sehen, wenn sie es nicht tun.
"MyDrawingControl.DoubleBuffered := True;" wird benötigt, um das Flimmern unter Windows zu vermeiden. Es hat keinen Effekt unter gtk.
Using A.J. Venter's gamepack
The gamepack approach is to draw everything to one double-buffered canvas, which only gets updated to the visible canvas when you are ready. This takes quite a bit of code, but it has the advantage of being able to do large rapidly changing scenes with multiple sprites on them. If you wish to use this approach, you may be interested in A.J. Venter's gamepack, a set of components for game development in Lazarus, which provides a double-buffered display area component as well as a sprite component, designed to integrate well with one another. Sie können gamepack erhalten mittels subversion:
svn co svn://silentcoder.co.za/lazarus/gamepack