Visual Studio 2010!

Read now >

View Now
DevSource RSS FEEDS
XML Want an easy way to keep up with breaking tech news? And the Get DevSource headlines delivered to your desktop with RSS.
ADVERTISEMENT
ADVERTISEMENT

 

DevSource.com: Your Source for Visual Studio on Facebook
ADVERTISEMENT
DirectX and the .NET Programmer
By Peter Aitken

Rate This Article: Add This Article To:

If you're not programming games or video, should you just forget about DirectX? Not necessarily.

A lot of developers have heard of DirectX, but many are not quite sure exactly what it is. If you ask, you are likely to get a general answer along the lines of "It has something to do with graphics and games, right?" A lot of developers are also a bit fuzzy on how DirectX relates to the .NET Framework and whether a .NET programmer can and should incorporate DirectX technology in applications. In this article, I will try to dispel some of this confusion.

DirectX is, to quote Microsoft, "...an advanced suite of multimedia application programming interfaces (APIs) built into Microsoft Windows operating systems." DirectX has been around since 1995, so it's nothing new. It has evolved over the years and is currently in version 9.0. DirectX is intended to provide the best performance on "high end" multimedia tasks. For graphics, this includes things such as video and 3-D animation, and for sound it includes surround sound.

There's a synergy between DirectX and hardware, particularly when it comes to graphics. As the manufacturers of high-end graphics hardware have developed ever more sophisticated chips for complex graphics operations, DirectX has provided a standard API that permits programmers to access the specialized features of the latest hardware. This provides enormous benefits to the programmer, but the close relationship between the DirectX API and hardware creates potential compatibility problems. When you buy the latest cool game that requires DirectX 9.0, it's not enough to have that version of the DirectX runtime installed on your system; your graphics card must also support it.

DirectX is comprised of several parts (earlier versions had more parts but they have been consolidated).

  • DirectX Graphics is a merging of DirectDraw and Direct3D from earlier versions, and comprises an API for 2- and 3-dimensional graphics operations including animation.
  • DirectX Audio is a merging of DirectMusic and DirectSound and is the API for playing and creating sounds of various kinds.
  • DirectPlay provides an API for creating a software interface between programs, permitting programs to be connected over the Internet and other kinds of links. It's main use is connecting games for team play scenarios, and it includes voice connection capabilities.
  • DirectInput is focused on input devices such as joysticks and headgear.
  • DirectShow is the video-related part of DirectX with API support for playing DVD and karaoke disks, video editing, and media stream filters.

You can see from this list that DirectX seems to be aimed primarily at two areas: computer gaming and video. If you're not programming games or video, should you just forget about DirectX? Not necessarily. A skilled programmer needs to think outside the box. Many game-related tasks can have relevance to seemingly unrelated areas of programming. To give just one example, I am currently involved in a programming project at a medical center. To evaluate patients with movement disorders for possible neurosurgery, they will be fitted with a virtual reality glove and asked to perform certain tasks on a computer screen, such as bringing a cursor to a target. This is no game, obviously, but I have a feeling that DirectInput (for input from the glove) and DirectX Graphics (for screen display) will play a prominent role in the final program.

DirectX began long before the advent of the .Net Framework and managed code, so at its heart it is Component Object Model (COM) code. Translation: unmanaged code. Microsoft has addressed this problem with DirectX for Managed Code, a variant designed specifically for use with managed languages (C#, Visual Basic.Net, Visual C++, and JScript.Net). DirectX for Managed Code provides most of the functionality of the original unmanaged API. By eliminating the COM interoperability layer, this improves performance and provides the known advantages of managed code such as freedom from memory management worries. Also, DirectX for Managed Code has a more intuitive interface, inheriting from the powerful and easy to use Net Framework common types.

Be aware, however, that DirectX for Managed Code does not support class inheritance. In other words, you cannot, or at least should not, define new classes that inherit from DirectX classes.

What exactly is involved in programming and using DirectX? From the user's perspective it is simply there, as part of the Windows OS. When you install software that uses DirectX, you may need to verify that you have a recent-enough version. While this is usually done automatically, you can check for yourself using the DirectX Diagnostic Tool. (From the Start menu select Run, then enter DXDIAG.EXE and press Enter.) This tool is intended primarily for diagnosing problems, but the first screen that is displayed includes information abut the DirectX version that is installed. As of this writing, version 9.0c is the latest and greatest. You can download DirectX upgrades from Microsoft (more on downloads below).

For the programmer, the DirectX Software Development Kit (SDK) is a must. It's also available as a free download; but be warned, it is huge, over 220MB in size! This SDK includes, in addition to the latest runtime, documentation for the DirectX core areas, headers and libraries, sample applications and source code, and various other helpful items. Once you install the SDK you are ready to go.

As with other aspects of .NET programming, the DirectX API is subsumed under namespaces, specifically Microsoft.DirectX and several third-level namespaces.

Once the SDK is installed, you are ready to rock and roll, right? Well, sort of. DirectX programming, particularly graphics, is very complex. Just understanding the graphics principles is a necessary first step, and how many of us know what precomputed radiance transfer, spherical harmonic math functions, or vertex alpha blending are? The unavoidable fact is that there's a lot of complex theory and math behind the sophisticated 3D graphics that DirectX supports, and you're not going to get very far without having at least some understanding of it.

Fortunately, Microsoft makes a lot of information about DirectX, including the theory behind it, available to the programmer. You'll find some in the online help documentation for Visual Studio.NET, in the documentation that is installed as part of the SDK, and a lot more on the Web. The best place to start - and to get your downloads too - is at msdn.microsoft.com/directx.




Discuss DirectX and the .NET Programmer
 
>>> Be the FIRST to comment on this article!
 

 
 
>>> More Using Microsoft Visual Studio Articles          >>> More By Peter Aitken