The Windows Shortfall
If you’ll recall in 02.04 - Game Anatomy 101, I made the distinction between internal and external game elements. In this article, we’re going to take a look at a technology that does it all.
You might be wondering, “Aren’t there already ways to read the keyboard, play sounds and draw on the display right in Windows?” Sure there are, but they aren’t up to the task for games. For instance, GDI drawing routines are far too slow for the kind of frame rates we expect – heck, GDI was designed for business presentation graphics! Windows has no native support for all of the fancy things that sound cards are capable of these days, and you’re going to be fumbling to try and keyboard, mouse and joystick input in a proper and orderly fashion.
Enter: DirectX
Microsoft has long been aware of this, and has responded with a set of software components that interact with the hardware on a lower level, thereby drastically increasing performance. They’re called DirectX components, and address your sound, music, input, output and network needs. Here’s a list of the components (as of version 7.0):
- DirectDraw
- DirectInput
- DirectSound
- DirectMusic
- DirectPlay
- Direct3D
Each of these components are actually COM objects, and are used much like regular C++ objects (if you don't know what COM is, don't worry). Luckily, each is fairly intuitive and straightforward to get working for you. In the sections that follow, I’ll introduce three of these – DirectDraw, DirectSound and DirectInput, and leave the rest for when they’re needed.
DirectDraw
DirectDraw is our ticket to unleashing the power of hardware acceleration, video memory and all of the various features available in today’s high-performance video hardware. We are able to create memory buffers called DirectDraw surfaces, and these surfaces can make use of video and system memory to hold buffers for the actual display, secondary display buffers, and buffers for our game images. The beauty of DirectX is in its ability to ‘flip’ surfaces directly to the video adapter’s active display, removing the need to copy memory. And since our game images can be held in DirectDraw surface buffers in the video card’s memory, hardware acceleration can be used to move these images directly into display buffers for speedy rendering.
DirectInput
The asynchronous state of a wide array of input devices can be ascertained through DirectInput objects. It’s direct (
read: fast ), it’s well organized and it’s a convenient abstraction for all of the data available to your game. You don’t have to worry about the endless types of joysticks, mice, VR headsets, force-feedback devices or anything else on the market – the data you’re looking for is presented to you in a way you can use without the hassle.
DirectSound
Sound can be a complicated aspect of writing a game. DirectSound seeks to simplify the output of sound and puts the power of many specialized effects at your disposal, and allows you to use audio resources in any popular format. Similar to DirectDraw’s surfaces, DirectSound utilizes sound buffers for storing, organizing and playing your audio resources in a straightforward and intuitive manner.
Other Options
Of course, DirectX isn’t the only solution out there for gaming under Windows. Here’s a (very) partial list of some other tools:
- OpenGL (Open graphics library – very popular as well)
- GlIDE (3DFX’s native API for Voodoo-based video cards)
- Allegro (a multi-platform graphics library)
- SDL (another multi-platform graphics library – thanks to pazu for the heads-up)
- MIDAS (a third-party sound support API)
I’m sure that you can think of many that I haven’t listed…
So why did I choose DirectX? One of the advantages to using DirectX is that the libraries needed on the user’s end are either already in Windows, or simple to update. As you’ve no doubt seen, it’s a very popular choice for most of today’s popular games. A main disadvantage to using (only) DirectX is that it’s not portable to other types of systems. After all, there are people using other environments such as UNIX or the MAC. As a matter of fact, I’m already getting differences of opinion from some forum members on choosing DirectX.
Deep down though, you all know I’m right
Multi-Platform Parallel Development
There’s a willingness by many forum members to have the main material in this forum adopted to other graphics, input and sound tools, and for the most part I’m all for supporting as wide an array of multi-platform APIs as possible. Unfortunately, I doubt I’d have the time to perform the necessary translation myself. As is the case with my choice of language, you’re going to be limited to DirectX in the material I present (for the foreseeable future anyhow), but I am willing to entertain requests from others to take on a role in this tutorial series by representing the interests of other tools. More information on this ongoing issue will follow.
Arming Your Machine for DirectX
There are two components for DirectX: the user DLLs and the developer’s SDK. Both are available from Microsoft (http://www.microsoft.com/directx), but before you do, we’d better look at the various versions of DirectX that are available (and in use).
At the time of this writing, DirectX 7.0(a) and 8.0(a) are the main current versions. This forum uses version 7.0, which means that you need at least this version installed on your machine. Remember that you need both the runtime DLLs (for playing games) and the SDK for writing programs that use DirectX components. If you download and install a version later than 7.0, it includes everything you need. Let me rephrase that clearly:
Every version of DirectX is fully compatible with every previous version.
If you have DirectX 8.0, you can write games that use version 1, 3, 5, 6 or 7 (there’s no 2 or 4). Think about it – Microsoft couldn’t just scrap older versions or your games would stop working when you updated DirectX on your machine!
As I’ve mentioned, there are some DirectX-related files
on my webpage, and you can pick and choose what to download instead of grabbing a 150MB file from Microsoft with a lot of useless contents. The one thing that I
strongly encourage you to get (even if you have the DirectX 8.0 SDK) is the documentation for DirectX 7.0.
Trust me, you’ll need this. The version 8.0 documentation isn’t going to help you. And if you proceed without this documentation at your fingertips, you’re
as good as dead .
One question remains – if DirectX 8.0 is available right now, why are we using version 7.0 for the tutorials? Well, this is still under debate by many here, but I basically have two reasons:
- DirectX 7.0 is still very popular – it’s bound to be the most compatible for writing a game with.
- The way things are done in DirectDraw has been massively overhauled in version 8.0 (so much so, DirectDraw doesn’t even exist in 8.0!), and although it’s not the newest anymore, it’s still the most ‘compatible’ when learning to use graphics APIs.
How else can I put it? Version 8.0 is still very new, and 7.0 is well understood by many. It’s in no way out-of-date, and already has more functionality than we even need for a long, long time. The moment I hear a decent argument for abandoning 7.0 in favor of version 8.0, I’ll be the first to concede and update the contents of all material in this forum.
What About the Other DirectX Components?
DirectMusic deals with playing MIDI music in your game. While I agree that it’s an important part of professional games, it’s not really central to game development. We’ll see where this forum takes us.
DirectPlay assists in all of those fun issues involved with network-play. As with DirectMusic, I do not doubt its usefulness – I’m just not concerned with it at the moment.
Direct3D:
oh boy . I bet that many of you are drooling over this one, aren’t you… Well, here’s my philosophy on 2D and 3D games: if you can’t make Pac-Man eat pellets or a sprite jump onto a ledge, you’re not going to be able to write the next Unreal Tournament. We’re starting our game development journey from the ground-up, and that means at the basics. Too many over-eager enthusiasts jump right into 3D engines and find themselves either pasting code together from other websites, getting hopelessly lost in the mountains of complicated code and math, or finally finishing and having a basic, out-of-date 3D engine
ala Quake I/II. The true creativity (in my humble opinion) can be better manifest in 2D games. Ultimately, I’d love to have the forum evolve to such a state where we move into 3D game development… it is, after all, the natural progression…
Pre-Flight Checklist
Before you move onto the next article, I want you to ensure that you have the following installed and ready:
- DirectX 7.0 (or higher) runtime DLLs – you can check this by going to your Start Menu/Run… and typing DXDIAG. Somewhere on the first tab of this utility is the current DirectX version number on your machine.
- DirectX 7.0 SDK (or higher) – you need the C++ libraries at the very least. Also included in the install package is a whack of samples that you may or may not be interested in (but I do highly recommend that you get them – many of the samples are great to learn from)
- DirectX 7.0 Online Documentation – this comes with the complete DirectX 7.0 SDK, but not with version 8.0. I’ve already told you that this is important, and that it’s available on my webpage.
Here's the link to my webpage:
http://teejb.webjump.com
If you’re ready, we’re going to move on to actually starting to use this stuff in the next article.
Questions? Comments? Please reply to this post.
Edited by - teej on April 10, 2001 11:24:37 PM