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
When this method returns, the IDirectDrawSurface4 object will point to the primary surface. However, we really want to perform all of our animation on the backbuffer surface, so we must instantiate an object to point to this surface. This is accomplished through the IDirectDrawSurface4’s GetAttachedSurface method. When this method is called on a complex surface with implicitly attached surfaces, it instantiates an IDirectDrawSurface4 object to point to the specified surface. The GetAttachedSurface method is defined as:
    function GetAttachedSurface(

    var lpDDSCaps: TDDSCaps2;                         // surface capabilities

    var lplpDDAttachedSurface: IDirectDrawSurface4    // a surface object

    ): HResult;                                       // returns an error code
The first parameter is a TDDSCaps2 structure whose dwCaps member must be set to DDSCAPS_BACKBUFFER. The second parameter is an IDirectDrawSurface4 object that will be set to point to the backbuffer surface when the function returns. This function returns the first attached surface whose capabilities match those defined in the function call. In our case, we receive a pointer to our only backbuffer surface. The TDDSCaps2 structure is defined as:
    TDDSCaps2 = packed record

      dwCaps: DWORD;         // capabilities flags

      dwCaps2 : DWORD;       // more capability flags

      dwCaps3 : DWORD;       // yet more capability flags

      dwCaps4 : DWORD;       // still more capability flags

    end;
The use of the CreateSurface and GetAttachedSurface functions will be demonstrated below. #### Rendering with GDI We’ve created a page flipping surface structure that will demonstrate DirectDraw’s animation capabilities, but unless we actually draw something to the screen, we won’t be able to tell if anything is happening when we actually flip the surfaces. Let’s cover a cool DirectDraw and Delphi trick that will make some tasks incredibly easy. The IDirectDrawSurface4 object contains a method called GetDC which returns a handle to a GDI device context. This allows us to use any GDI function call to render graphics, or text, directly onto the surface. If we create a TCanvas object and set its handle to the value returned by GetDC, we can take full advantage of all TCanvas methods. This is a very powerful Delphi technique that can make graphics programming incredibly simple. The GetDC method is defined as:
    function GetDC(

    var lphDC: HDC                     // a variable that receives the DC

    ): HResult;                        // returns a DirectX error code
The only parameter to this method is a variable that will receive the device context handle for the surface. After we are through using the device context, we must release it before performing any DirectDraw methods that work on the surface, such as flipping or copying surface memory to or from the surface in question. We accomplish this by calling the IDirectDrawSurface4 method ReleaseDC, which is defined as:
    function ReleaseDC(

    hDC: Windows.HDC                    // a variable containing the DC to release

    ): HResult;                         // returns a DirectX error code
This method’s only parameter must be set to the device context handle returned from the previous call to GetDC.
| Caution: It is absolutely imperative that every call to GetDC be matched with a call to ReleaseDC. When a device context is retrieved, the normal execution of Windows is put on hold. Therefore, any code between GetDC and ReleaseDC must execute as quickly as possible, as messages and other system events could get backed up. Also, any other DirectDraw methods that work directly on the surface must not be called until the device context has been released. Therefore, it is good practice to use a try..finally block, putting the ReleaseDC call in the Finally part to ensure that the device context is released. Otherwise, your system will crash violently.
Any GDI function or TCanvas method can be used for rendering graphics to a surface. Bear in mind that you will be suffering from GDI’s sluggishness, but you may be able to reuse proprietary GDI graphics rendering methods and leverage your former development investments. Specifically, we’ll be using these techniques to perform text output in a number of examples. We will also examine how this makes the process of loading and displaying bitmaps incredibly simple. #### Flipping the Surfaces OK, we’ve created our complex flipping structure, we’ve drawn some text on the front and backbuffer in order to differentiate them, now we’re ready for action. To actually flip the contents of the surfaces, call the Flip method of the primary surface object. The Flip method is defined as:
    function Flip(

    lpDDSurfaceTargetOverride: IDirectDrawSurface4;   // surface to flip

    dwFlags: DWORD                                    // wait flags

    ): HResult;                                       // returns an error code
Typically, the first parameter is set to nil as the default behavior is to flip to the next buffer in the chain. However, if there is more than one backbuffer, this parameter can be set to the particular buffer in order to flip it to the screen. The last parameter is set to either zero or DDFLIP_WAIT. If it is set to zero, the function immediately returns, regardless if the flip operation could be set up. If the DDFLIP_WAIT flag is specified, the function does not return until the flip operation occurs. Setting this parameter to zero allows the application to perform extra processing while waiting for the display hardware to set up the flip operation. However, the Flip method must continuously be called in order to set up the flip operation. If the flip operation could not be set up, this function will return DDERR_WASSTILLDRAWING. In most cases, it is easiest to set this parameter to DDFLIP_WAIT, which is the flag we will use in the examples throughout this book.
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.