WindowsDevCenter.com
oreilly.comSafari Books Online.Conferences.

advertisement


AddThis Social Bookmark Button

Introduction to Programming with Managed DirectX 9.0

by Jacek Artymiak
06/02/2003

The industry standard 3D API, OpenGL, is not the only 3D API that you can use on the .NET platform in the Microsoft Windows environment. Another choice is DirectX, which has been steadily increasing its functionality, stability, and market share among developers writing all sorts of Windows multimedia software, games, and other applications that make use of 2D and 3D graphics. While earlier versions of DirectX often left a lot to be desired, its latest incarnation, DirectX 9.0, is a serious product worth a longer look, especially if you want to target multiple Microsoft platforms: Microsoft Windows, PocketPC, or X-Box.

With DirectX 9.0, you have a choice of two 3D APIs on the Microsoft Windows platform, but that doesn't mean that it must be one or the other. Even if you stay with the OpenGL for 3D API (it's an industry standard after all, and there's a lot of information and talent to draw upon) you may still need DirectX's other components that you will simply not find in OpenGL. Direct3DX is only a part of the DirectX puzzle, and other pieces are equally interesting. If you have never considered using DirectX before, the following summary will give you a better view of what's in store:

  • Direct3D Graphics: The DirectX 3D API. You will use it for 3D graphics and animation programming.
  • DirectDraw: Direct, fast, low-level access to display the hardware's memory, blitter, overlays, etc. Essential in game programming, where timing is everything.
  • DirectInput: Support for various input devices (keyboard, mice, joysticks, paddles, steering wheels, pedals, etc.), including devices with force feedback.
  • DirectSound: Advanced sound playback and recording, including support for three-dimensional sound and sound effects. Works with WAV files; for more playback options, use DirectMusic.
  • DirectMusic: Handles dynamic sound tracks, 3D sound positioning, and hardware acceleration. Uses MIDI, WAV, and DirectMusic Producer tracks. You can think of DirectMusic as the playback side of DirectSound on steroids.
  • DirectPlay: Support for networked gaming, including support for real-time voice conferencing. This could have applications that go beyond gaming.
  • DirectShow: Playback and capture of audio and video streams. This author's favorite feature is support for TV cards.
  • DirectSetup: Simple API for setting up DirectX on user's machines.

Just like OpenGL, DirectX too used to be aimed at developers coding in C/C++. That's natural, because compiled C/C++ code is faster than interpreted Visual Basic. However, Microsoft knows that in order to make .NET a success, they need to attract as many developers as they can. To do that, all of their crucial APIs must be available in managed form, so they can be easily incorporated into applications written in C#, Visual Basic .NET, and other .NET languages.

The recently released DirectX 9.0 SDK is a clear sign that Microsoft wants to broaden the use of DirectX among developers who do not speak C++ and MFC programmers. The official release of the DirectX 9.0 SDK is available in three forms:

  • DirectX 9.0 SDK: The whole package for C/C++, C#, Visual Basic .NET, and other languages available on the .NET platform.
  • DirectX 9.0 SDK for C#: A cut-down, managed version of the DirectX 9.0 SDK for programmers writing C# code.
  • DirectX 9.0 SDK for Visual Basic .NET: A cut-down, managed version of the DirectX 9.0 SDK for programmers writing Visual Basic .NET code.

The managed versions (for C# or VB.NET) can also be used in Visual C++ (when writing managed code), and JScript .NET applications. Other languages available for the .NET platform ought to be able to make use of them, as well. This lowers the barrier of entry for those programmers who code in C# or VB .NET for a living, and could potentially lead to some interesting software titles appearing on the market.

Starting managed DirectX 9.0 development is quite inexpensive; if you do not need the visual tools, you can get by with the free SDKs. But you would really be doing yourself a disservice if you did not use the Visual development products, as they really help developers manage their work better and work more efficiently.

Here's the list of tools you need to start coding with DirectX 9.0:

  • A fast PC (think at least 1GHz).
  • A decent video card with a GPU (for programming shaders), which means a recently manufactured card with the latest chips from ATI or nVidia. Older cards, like the TNT, will also work, but you will not be able to explore the joys of writing shaders, which is the latest craze in 3D graphics programming.
  • A copy of either the Microsoft Windows 2000 or XP operating system with the latest patches installed before you install other components.
  • A copy of one of the Visual Studio development environments: Microsoft Visual Studio, Microsoft Visual C++, Microsoft Visual C#, or Microsoft Visual Basic .NET. If you cannot afford them, you can try to get by with the command-line C# and VB.NET compilers that you can find in the .NET Framework SDK, but you will lose all of the handy features that these environments offer.
  • A copy of the .NET Framework 1.1 SDK (after you install it, run Windows Update to check for the latest patches).
  • A copy of the .NET Framework 1.1 Redistributable (if you plan to distribute applications for the .NET Framework 1.1).
  • A copy of the DirectX 9.0 SDK for C# or VB.NET, if you want to save space on disk, but even if you don't plan to use the full SDK, you might want to install it anyway, because sometimes reading C++ code can help you understand the things that are not explained well enough in the SDK documentation. Again, after you install this component, check for updates.
  • A copy of the DirectX 9.0 Redistributable if you plan to distribute your applications.

Despite their best efforts to provide managed versions of the whole of DirectX, Microsoft does not (yet) provide managed versions of DirectMusic, DirectShow, or DirectSetup. One additional component that you will find is a basic support for audio and video playback.

Installing the DirectX 9.0 SDK

While all components listed earlier in this article behave quite reasonably during installation, DirectX 9.0 SDK is a bit temperamental and may waste some of your time, if you let it. The following tips should help you avoid wasting time.

If you haven't already done so, update your Windows system with the latest patches from Microsoft. Next, download the latest release of the .NET Framework SDK, install it, and check for updates with Windows Update and manually, in MSDN's .NET Framework section. Now you can download your choice of the DirectX 9.0 SDK versions (full, C#, or VB.NET) and double-click on it to unpack the installation files to a temporary directory.

When you run setup, it will ask you for a path to install the SDK. There is a bug in the installer that, in some cases, does not accept the path you choose when you click on the Browse button. If the problem persists and you cannot force the installer to obey you, the solution to this problem is to quit the setup, remove temporary files, unpack the archive again, and then, when the installer asks for the path, change C:\DXSDK to a different path (e.g., E:\DXSDK) manually, by typing it into the Path text field.

Another strange thing that you might run into is a problem with DLL locations. The DirectX DLLs are put into the C:\WINDOWS\assembly\GAC directory, where they are essentially inaccessible to your development environment, be it Visual C# or Visual Basic .NET. It doesn't always happen, but if you open a sample project from the DirectX 9.0 SDK and you see exclamation marks in the References list next to Microsoft.DirectX libraries, then that's the sign of this particular installer misbehavior. Fortunately, it is very easy to fix this problem. Simply open the command line (console) window and type:

cd c:\windows\assembly\gac
dir

and you should see this:

Microsoft.DirectX
Microsoft.DirectX.AudioVideoPlayback
Microsoft.DirectX.Diagnostics
Microsoft.DirectX.Direct3D
Microsoft.DirectX.Direct3DX
Microsoft.DirectX.DirectDraw
Microsoft.DirectX.DirectInput
Microsoft.DirectX.DirectPlay
Microsoft.DirectX.DirectSound

The DLLs we are looking for are located in subdirectories with long names like:

Microsoft.DirectX.Direct3D\1.0.900.31bf3856ad364e35\
    Microsoft.DirectX.Direct3D.dll

Create a normal folder in your chosen location and copy each DLL to it. Then, update the References in your project and Build a sample application. Everything should work fine now.

Note: If you downloaded the DirectX 9.0 Redistributable, do not install it, but keep it on CD-R until you have your application ready for distribution. It contains files that Microsoft lets developers distribute to end users, and is of not much use to programmers. It's the same story with the NET Framework Redistributable.

Where now?

Programming with Managed DirectX 9.0 is currently a somewhat unexplored territory, and you will need a good guide to help you along. Your first resource will be various places on the Web and the C/C++ code in the SDK itself. Good books on this subject are yet to appear, but that should not be the case for too long.

Note that programming with Managed DirectX 9.0 is something that you should attempt after you master programming C# or VB.NET, or you will find it a frustrating experience. O'Reilly has published very good books on this subject: .NET Framework Essentials, .NET Windows Forms in a Nutshell, C# Essentials, and VB.NET Language in a Nutshell ought to be your essential reading if you are new to .NET.

To get a better understanding of computer graphics, get a copy of Computer Graphics: Principles and Practice in C or Computer Graphics. Also, the Cg Tutorial is an excellent introduction to the world of real-time shader programming techniques.

If you want to get deep into the subject, consider SIGGRAPH membership. It is an inexpensive way to gain access to a vast library of SIGGRAPH proceedings, which are a goldmine of ideas.

Happy coding!

Jacek Artymiak started his adventure with computers in 1986 with Sinclair ZX Spectrum. He's been using various commercial and Open Source Unix systems since 1991. Today, Jacek runs devGuide.net, writes and teaches about Open Source software and security, and tries to make things happen.


Return to ONDotnet.com