03 05


Delphi Graphics and Game Programming Exposed! with DirectX For versions 5.0-7.0:Basic Graphics Programming                       Search Tips   Advanced Search        Title Author Publisher ISBN    Please Select ----------- Artificial Intel Business & Mgmt Components Content Mgmt Certification Databases Enterprise Mgmt Fun/Games Groupware Hardware IBM Redbooks Intranet Dev Middleware Multimedia Networks OS Productivity Apps Programming Langs Security Soft Engineering UI Web Services Webmaster Y2K ----------- New Arrivals









Delphi Graphics and Game Programming Exposed with DirectX 7.0

by John Ayres

Wordware Publishing, Inc.

ISBN: 1556226373   Pub Date: 12/01/99














Search this book:
 



Previous Table of Contents Next Pens and Brushes TCanvas objects contain several properties, two of which are extremely important when performing graphical output: the Pen property and the Brush property. The Pen property determines the characteristics of lines drawn on the TCanvas, such as a line’s thickness and color. The Brush property determines the appearance of the interiors of polygons, such as the color and pattern. Using the TPen and TBrush objects, several pens and brushes can be constructed by the application and assigned to the appropriate properties when necessary. However, most of the time, it is sufficient to simply change the properties of the TCanvas as needed. Drawing Pixels The Pixels property of the TCanvas object gives the application access to individual pixels of the surface. This is accessed like a two-dimensional array, with the first index acting as the horizontal coordinate and the second index acting as the vertical coordinate. The Pixels property reports or receives a color in the form of a TColor type (which is an RGB color value, regardless of the video mode). The following example demonstrates using the Pixels property of a TCanvas object to draw random pixels. Listing 3-1: Using the Pixels property procedure TfrmPixelDemo.TimerTimer(Sender: TObject); var iCount: Integer; begin {draw 100 random pixels in a random RGB color} for iCount := 0 to 99 do Canvas.Pixels[Random(ClientWidth),Random(ClientHeight)] := RGB(Random(256), Random(256), Random(256)); end; Unfortunately, this method of accessing individual pixels of an image is incredibly slow, and should never be used for fast graphical output. Figure 3-8:  Drawing individual pixels Drawing Lines Lines are drawn using the attributes set by the Pen property. The PenPos property determines the current position of the pen or the starting point of any drawing operation that uses the pen (such as drawing lines). This position can be modified by directly setting the PenPos property or by calling the MoveTo method. The LineTo method is called to actually draw the line, from the position indicated by the PenPos property to the position passed in the LineTo method. This pen position is updated to the coordinates passed to the LineTo position after the line is drawn. The following example demonstrates how to use these methods to draw various lines of random length, color, and width. Listing 3-2: Using the MoveTo and LineTo methods procedure TfrmLineDemo.TimerTimer(Sender: TObject); begin {set a random pen color and width} Canvas.Pen.Color := RGB(Random(256), Random(256), Random(256)); Canvas.Pen.Width := Random(5); {draw a line between two random points} Canvas.MoveTo(Random(ClientWidth), Random(ClientHeight)); Canvas.LineTo(Random(ClientWidth), Random(ClientHeight)); end; Figure 3-9:  Drawing random lines Drawing Polygons Polygons are drawn using the attributes set by both the Pen and Brush properties. The Pen property determines the outline of the polygon, and the Brush property determines how the polygon is filled. Several methods exist that draw polygons of different shapes, from rectangles to circles. The Polygon method can be used to draw polygons of any arbitrary shape simply by passing it an open array of TPoint structures. The following example demonstrates how to use several polygon drawing methods to draw polygons of various shapes and colors. Listing 3-3: Using various polygon drawing methods procedure TfrmPolygonDemo.TimerTimer(Sender: TObject); type {enumerated type representing several standard polygon drawing routines} TPolyType = (ptChord, ptEllipse, ptPie, ptRectangle, ptRoundRect); var X1, X2, X3, X4, Y1, Y2, Y3, Y4: Integer; begin {initialize a set of random coordinates} X1 := Random(ClientWidth); X2 := Random(ClientWidth); X3 := Random(ClientWidth); X4 := Random(ClientWidth); Y1 := Random(ClientHeight); Y2 := Random(ClientHeight); Y3 := Random(ClientHeight); Y4 := Random(ClientHeight); {set the brush to a random RGB color} Canvas.Brush.Color := RGB(Random(256), Random(256), Random(256)); {draw a random polygon} case TPolyType(Random(Ord(ptRoundRect)+1)) of ptChord : Canvas.Chord(X1, Y1, X2, Y2, X3, Y3, X4, Y4); ptEllipse : Canvas.Ellipse(X1, Y1, X2, Y2); ptPie : Canvas.Pie(X1, Y1, X2, Y2, X3, Y3, X4, Y4); ptRectangle : Canvas.Rectangle(X1, Y1, X2, Y2); ptRoundRect : Canvas.RoundRect(X1, Y1, X2, Y2, X3, Y3); end; end; Figure 3-10:  Drawing random polygons Drawing Text Perhaps the most common use of TCanvas graphical output techniques is for drawing text. The Font property of the TCanvas object determines the attributes of the text, including its typeface, size, and style. The TextOut method is used to draw the specified string at the indicated coordinates. These coordinates denote the top left corner of the text string. The following example demonstrates how to use the TextOut method to draw randomly colored text at random positions on the form. Tip: By setting the Style property of the canvas’s Brush property to bsClear, the background behind text will be transparent. This allows images underneath the text to show through, and is useful for drawing text on top of bitmap images. Listing 3-4: Using the TextOut method procedure TfrmTextDemo.TimerTimer(Sender: TObject); begin {set a random text color} Canvas.Font.Color := RGB(Random(256), Random(256), Random(256)); {set a random text size} Canvas.Font.Size := Random(10)+5; {set the text typeface} Canvas.Font.Name := Arial’; {set the brush style to clear so that the image behind the text will be visible} Canvas.Brush.Style := bsClear; {draw text at a random coordinate} Canvas.TextOut(Random(ClientWidth), Random(ClientHeight), Delphi Graphics’); end; Figure 3-11:  Drawing random text Bitmap Essentials It is quite possible for one to create a really engaging and entertaining game using nothing but line and polygon drawing techniques. However, if such graphical output were used in all games, everything would look very similar, and there would be little visual variety. In order to achieve the desired visual quality and variety that would be necessary in almost any game, bitmaps must be used. Bitmaps come in many varieties, from two color to true color, and in many different formats, such as the Windows BMP format, JPEG, TIFF, GIF, etc. Each format presents its own benefits and weaknesses, and each is useful under specific circumstances. Obviously, the only format native to Windows is the Windows BMP format. This format can easily by accessed by low-level Windows API functions as well as Delphi VCL objects and functions, so the BMP format is the one on which we will focus. Previous Table of Contents Next Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.

Wyszukiwarka

Podobne podstrony:
Iracki żołnierz zabił dwóch żołnierzy USA (03 05 2009)
PM1 03 05
OST D 05 03 05 Nawierzchnia z betonu asfaltowego
Amerykański wywiad rozpracowuje Smoleńsk Nasz Dziennik, 2011 03 05
Artykuł Dolar międzynarodowy (2007 03 05)
FPJ wyk 03 05
ZL2 03 05
TI 03 05 03 B pl(1)
2007 03 05 gazeta prawna
TI 03 05 19 T B pl(1)
03 05

więcej podobnych podstron