Search Tips Advanced Search |
| 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
• DirectX objects are COM based, consequently involving all of the associated benefits and drawbacks of using COM objects. Working with DirectX objects has some similarities to working with other COM objects, but there are a few differences. The primary difference is the fact that DirectX objects do not need to be created by using the CoCreateInstance function. All DirectX objects are created through custom function calls that return an instantiated COM object. A COM object can almost be thought of as a VCL object with no properties, only methods. The methods of a COM object are known as an interface. A COM object can have more than one interface, and it can be extended by added new interfaces, similar to deriving a new component from an ancestor. However, the COM standard dictates that an interface can never change. COM objects determine when they can destroy themselves, and thus do not need to be freed by the creating application. A unique identifier called a GUID is used to distinguish individual COM objects. • Perhaps the single most important concept in DirectDraw programming is that of the surface. A surface is directly analogous to Delphi’s TBitmap or TCanvas object. It represents a virtual “picture” upon which graphical output can be drawn. In reality, a surface is a linear chunk of memory that is managed as if it were rectangular. Surfaces have width and height, among many other properties. The memory for a surface can be located in a number of places, but primarily it will be found in system memory or in the memory on the display card itself. There are many different types of surfaces, some of which are very specialized for a particular purpose. For the purposes of this book, we will be concerned with only three different types: the primary surface, the backbuffer surface, and off-screen surfaces. • Page flipping is an incredibly powerful technique for producing flicker-free animation. The term “page flipping” comes from the inspiration for this animation technique, that of traditional cartoon animation. Cartoons (and movies, for that matter) create the illusion of animation by displaying to the viewer a series of images, one after the other. Someone can create a crude animation by taking a stack of paper, drawing an image on each page, and then flipping through it rapidly. This is the architecture of page flipping animation. The idea is to create two surfaces, the primary surface and the backbuffer surface. The next frame of animation is composed on the backbuffer surface, and then it is “flipped” to the display screen. This updates the entire display screen in one fell swoop, resulting in a flickerless animation. • There are several tasks an application is required to perform before DirectDraw can be instantiated and the display mode modified. Some of these tasks are optional, some are not. The application may want to enumerate all attached display devices, although this is not specifically required. After the user picks an enumerated display device, or if the application simply decides to go with the primary display device, the DirectDraw object can be created. Supported display modes can then be enumerated; while not specifically required, the display mode into which the display hardware is switched must be a supported mode, which can be verified by enumerating supported display modes. The cooperative level can then be set, determining if the application will run full screen or windowed. Finally, setting the display mode causes the hardware to switch over to the indicated resolution and color depth. Bear in mind that certain requirements are imposed on the main window before the display mode can be set properly. • A device context handle can be retrieved from a surface, allowing the application to use GDI rendering functions. When combined with a TCanvas object, the full power of Delphi’s built-in graphical rendering functionality can be brought to bear. However, the sluggishness of the GDI will also be present. It is very important that any device contexts retrieved be released before performing any DirectDraw functions that manipulate the surface. • Bitmaps can be easily loaded and stored into a surface by using the TBitmap object combined with the drawing capabilities of a TCanvas object and a device context retrieved from a surface. • The memory associated with surfaces located in display memory can be lost if the user Alt+Tabs out of the application. When the DirectDraw application receives focus again, it must call the Restore method of any display memory surfaces, including the primary surface. Their contents will be undefined, so they must be reinitialized by reloading bitmaps or redrawing graphics. • DirectX was written so that a developer can take advantage of certain hardware features if they are available, such as overlays or the ability to create off-screen surfaces wider than the primary surface. We can determine what features are available by calling the IDirectDraw4’s GetCaps method. • Just about every DirectX function and method returns an HResult, a 32-bit number that maps to a DirectX error code. There are hundreds of error codes, all of which are located in the DDraw unit. These codes can be translated into a textual form by calling the DXErrorString function located in the DXTools unit. You should always check the results of any extremely critical methods, such as blits, flips, and initialization code. Methods such as these can cause an application to waltz off into limbo if they fail and are not gracefully handled, so checking the result code is very important for a robust application. • Sometimes it is necessary to determine which version of DirectX is installed before initializing objects, particularly if the application is to be distributed without the DirectX run-time redistributables, such as on floppy or over the Internet. Specifically, you should not be checking for an overall DirectX version, but instead you should check for the availability of specific interfaces. The QueryInterface method implemented in every COM object can determine if a specific interface is supported.
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.