Issue dated - 16th August 2004

-


Previous Issues

CURRENT ISSUE
INDIA NEWS
SECURE SPACE
COLUMNS
TECH FORUM

THE C# COLUMN

BETWEEN THE BYTES
TECHNOLOGY
SPECIALS <NEW>
Symantec Report
Security Headquarters
JobsDB
MINDPRINTS
HMA BANKBIZ
EC SERVICES
ARCHIVES/SEARCH
IT APPOINTMENTS
Openings At Jobstreet.com
WRITE TO US
SUBSCRIBE/RENEW
CUSTOMER SERVICE
ADVERTISE
ABOUT US

 Network Sites
  IT People
  Network Magazine
  Business Traveller
  Exp. Hotelier & Caterer
  Exp. Travel & Tourism
  Exp. Pharma Pulse
  Exp. Healthcare Mgmt.
  Express Textile
 Group Sites
  ExpressIndia
  Indian Express
  Financial Express

 
Front Page > TechSpace > Story Print this Page|  Email this page

Animation using Direct Draw - I

The C# Column

We will explore the DirectX architecture before starting with an application. In this application we will display a parrot on the display surface using DirectDraw and animate the parrot from one end of screen to another.

The figure shows the relationships between Microsoft DirectX Graphics, the Microsoft Windows Graphics Device Interface (GDI), the Hardware Abstraction Layer (HAL), the Hardware Emulation Layer (HEL) and the hardware.

DirectX Graphics applications exist alongside GDI applications and both have access to the graphics hardware for the graphics card. Unlike GDI, DirectX Graphics can take advantage of hardware features when a HAL device is selected. HAL devices provide hardware acceleration based on the feature set supported by the graphics card.

The HEL provides software-based emulation of features that are not present in hardware. The two channels viz. HAL and HEL allow us to create applications that are independent of the video card on user’s machine.

If we write a simple WinForm application and animate an object from one place to another on the form we get a flickering effect. DirectX uses a double buffering system to render a smooth animation. Let’s see how this system works.

Suppose we have several BMP files, each containing a frame to be displayed in animation. DirectX loads the BMPs on separate offscreen surfaces. From these surfaces, the images are then drawn on the back buffer. After loading in the back buffer, the images are drawn on primary surface one by one. The image currently drawn on the primary surface is what the user sees. The back buffer is used to store new information ready to be displayed on the primary surface.

Here are few terminologies. Get familiar with them before we move on.

- Frame is a snapshot in time

- Sprite are rectangular image to be used for animation

- Drawing Surface is a part of memory where the images to be displayed are assembled. Ex. Primary, Secondary, Offscreen

- Palettes are list of colors available for the display

- Clippers defines the blitting area for DirectDraw

- Blitting - Transfers contents from Offscreen. to Primary/Secondary surfaces.

- BackBuffering - Population of surfaces with new frames information

- Page Flipping is cycling through the drawing surfaces filled in back buffering.

The abovementioned systems are encapsulated in DirectDraw objects. There are separate components that handle primary/secondary surfaces, back buffering and pallets. A C#.NET application use these components through a DirectDraw COM component that provides .NET wrapper around these COM components.

Let us now use this DirectDraw object to animate the sprite in our application. Create a C#.NET application named ‘Sprite’. Select ‘DirectX 9 Visual C# Wizard’ from the template list.

Select the Project Settings tab and then select DirectDraw component. Finish the application wizard. Three classes get created in the application viz. MainClass, GraphicsClass and DxUtil class. The MainClass class is derived from the Windows.Forms class. The execution starts with creation of MainClass’s object and calling of its constructor. The constructor is given below:

public MainClass( )
{
. . . .
target = this ;
graphics = new GraphicsClass ( target ) ;
Show( ) ;
StartLoop( ) ;
}

Here, an object of the GraphicsClass is created. This calls the GraphicsClass’s constructor. In this constructor the CreateSurfaces( ) method is called. In this method we have created the surfaces on which we will draw. The Show( ) method displays the control. Then the StartLoop( ) method is called. In this method a loop is run as long as the application is running. The Created property returns true till the form is alive.

private void StartLoop( )
{
while ( Created )
{
CheckBounds( ) ;
graphics.RenderGraphics
( destination ) ;
Application.DoEvents( ) ;
}
}

In the RenderGraphics( ) method, as the name suggests, we can draw whatever graphics we want. The destination is a Point object that specifies the initial position of the object drawn on display surface.

We will now add our code to animate the sprite. Firstly, change the GraphicsClass constructor so that it will look as given below:

public GraphicsClass ( Control owner )
{
this.owner = owner ;
localDevice = new Device( ) ;
localDevice.SetCooperativeLevel ( owner, CooperativeLevelFlags.FullscreenExclusive |
CooperativeLevelFlags.AllowReboot |
CooperativeLevelFlags.NoWindowChanges ) ;
localDevice.SetDisplayMode
( 800, 600, 24, 0, false ) ;
InitializeDirectDraw( ) ;
CreateSurfaces( ) ;
}

Here the SetCooperativeLevel( ) method is called. It determines the top-level behavior of the application. The CooperativeLevelFlags is an enum.

We have used the constants defined in CooperativeLevelFlags to specify the top level behavior of our window.

When the FullScreenExclusive flag is mentioned, DirectDraw will attempt to resize its window to full screen.

The AllowReboot flag allows CTRL_ALT_DEL to function while in full screen mode.

The NoWindowChanges does not allow DirectDraw to minimize or restore the application window.

The SetDisplayMode( ) method sets the mode of the display device hardware.

It is necessary to call SetCooperativeLevel( ) method to set exclusive level access before the mode can be changed. Both these methods are called using localDevice reference of type Device.

To the SetDisplayMode( ) method we have passed the width, height, bits per pixel and refresh rate. The last parameter is not used and hence is set to false.

We have then called the InitializeDirectDraw( ) and CreateSurfaces( ) user-defined methods. We will see them one by one in the next article

To be Continued…

Yashavant Kanetkar, one of the first Express Computer columnists, is an established software expert, speaker and author with several best-sellers to his credit, including titles like “Let Us C” and the “Fundas” series. Contact him at kanetkar@dcubesoft.com

 

<Back to top>


© Copyright 2003: Indian Express Group (Mumbai, India). All rights reserved throughout the world. This entire site is compiled in
Mumbai by The Business Publications Division of the Indian Express Group of Newspapers.
Please contact our Webmaster for any queries on this site.