(was "Game programming with DirectX")
Copyright (C) David Joffe 1997-2001.
Target audience: C/C++ Windows programmers who want to learn to write applications that use Microsoft's confusing API, DirectX. There is some MFC stuff in here, but mostly I am trying to get rid of it. You don't need to know MFC.
Bugs: I have been made aware of a couple of bugs in the code sample. One day far in the future when I find time I will fix them.
1999/12: I have improved the sample ddsamp, and upgraded chapter 3 to be based on this. This demonstrates the PutPixel method described in Chapter 5. I made it with Visual C++ 6, so it includes VC project files, but you don't need VC to use this.
My opinion of DirectX:
Conceptually, the existence of an API like DirectX is a good thing for the industry. Having a single, coherent, standard, continually evolving API that allows programmers to take advantage of the latest hardware relatively transparently is a good thing. In its own way, DirectX has fulfilled its promise of bringing decent games to Windows 95 and family.
However, DirectX is (in my opinion) the most horribly designed API I've ever seen, and I would hate for any newbie game programmers, whose first API might be DirectX, to walk away thinking that it is in any way an example of how a games API should look. DirectX is bulky, poorly documented, unnecessarily overcomplicated, proprietary, and there was never any technical reason for the existence of large portions of it, such as Direct3D (Microsoft ignored the existing industry standard OpenGL simply because it wasn't theirs and it wasn't tied to the Windows platform, not because they thought they could offer a better solution.)
To be fair to Microsoft though, they have slowly made improvements to the DirectX API (the GIGO ("Garbage In, Garbage Out") principle notwithstanding). Getting rid of execute buffers was a good first step, and DirectX 7 shows a number of other cleanups, such as phasing out retained mode, and a cleaner rework of the concepts of viewports and texture objects. They are continually evolving the API, and in the process doing a good job of keeping up with the latest hardware features. OpenGL, on the other hand, appears to be comparatively stagnant. It is a good API, and it will supply you with everything you need to write a 3D game today - but it needs some momentum.
None of the above is meant to change your mind on whether or not you want to learn DirectX. You can't really go wrong learning DirectX. Its not about to go away, and if you want to write games for PC's DirectX is pretty much what you're going to need to learn. There aren't really any alternatives at the moment anyway, and there isn't likely to be for as long as Microsoft rules the roost.
"The Microsoft implementation of OpenGL in the Microsoft Windows NT, Windows 2000, Windows 95, and Windows 98 operating systems is an implementation of the industry-standard OpenGL three-dimensional (3-D) graphics software interface with which programmers create high-quality still and animated 3-D color images" - Microsoft Knowledge Base
SDL: Simple DirectMedia Layer
A very nice cross-platform (Linux, Windows, MacOS, BeOS) games API. Wraps DirectX on Windows. Uses OpenGL for 3D. I've been using this for a little while now and I like it very much. Free, with source code (GPL). Much easier to use than DirectX, produces simpler, shorter, cleaner code, and requires less development time to get going. A bit like "what DirectX should have been". (recommendation)
The DirectX eXperience
Various DirectX articles from all over.
The official DirectX homepage
Information, news, samples; download the latest SDK or end-user release of DirectX. Please do not ask me for help on how to install or set up DirectX! I am not Microsoft tech support nor do I wish to be.
CDX: Free C++ DirectX class library
C++ class library, DirectX wrapper classes. Free, with source code.
The Delphi Games Creator
Game programming library based on DirectX, for Delphi. Kind of like CDX, but for Delphi.
DirectX with Borland C++
Using DirectX With Borland C++, by Michael Lundberg.
DirectX 8 Borland LIBs.
Borland C++ 5.0 Setup For DirectX 5.0.
"... documents and utilities to let the DirectX 3 Examples work with Borland C++ 4.52". Information on using DirectX with Borland C++. Interesting piece of information there that Microsoft "forgot" to include some stuff for the Borland version of the dxguid lib. (Borland C++ just happens to compete with MS Visual C++.)
DirectX with Visual Basic
Information on using DirectX with VisualBasic.
"Yet another directX wrapper" (by Alexander Russell)
DirectDraw, sound, music, I/O (no 3D). Sample source, with documentation, 2 editors for sprites and GUIs.
These are links to DirectX and graphics related books on Amazon.com. Additional user reviews of these books can be found there.
Learn Computer Game Programming with DirectX 7.0 (Ian Parberry)
I think this is a good book for programmers who are new to game development and want to 'get into it'. It takes the reader through the entire process of writing a game in fair detail, and covers a lot of ground. I found the writing style fairly pleasant. I think this book would be useful for novices. Note: this book does not cover Direct3D. 3D graphics is a pretty broad topic on its own, and I think in general novice game programmers would be better off working through a book like this one first, then follow it by a book on 3D graphics programming.
Computer Graphics: Principles and Practice (Foley/van Dam/Feiner/Hughes)
This book is often referred to as the 'computer graphics bible' by those in the industry, and is generally regarded as a "must-have" book for anyone who is interested seriously in going into computer graphics programming. Although the book is slightly dated, most of the 1000+ pages of content are still relevant and useful, and the book contains a lot of information on 2D and 3D algorithms. This is the one book I do own a copy of. The book is heady reading, its not a "friendly tutorial" - if you are serious about going into graphics programming, you should definitely get this book, otherwise don't. This book is not specifically about game programming but more generally about graphics and animation. It does not cover DirectX, but from it you will be able to learn the principles of APIs such as DirectX.
Beginning Direct3D Game Programming (Andre LaMothe)
See user reviews for this book on Amazon.com.
FAQ (Frequently Asked Questions)
How do I get started in game programming?
I've never done commercial game programming, so I'm not sure I'm qualified to answer that. But my advice would be lots of reading and lots of practise. And write something - anything - even a very very small game - it's better to write a tiny game that is 'professional and polished and close to finished', than to try write an overly-ambitious large game that remains hopelessly incomplete. Being able to show sample code of a game that you have worked on will be a huge help when applying for a job in the industry, as will being able to show that you are professional enough to have actually finished something (unlike this tutorial, haha).
Are you still working on the tutorial?
Nope, and it's very out of date, sorry - it was a hobby project, and I no longer have time for it. I'll keep it online as some people still seem to find parts of it useful.
How do I go about using DirectX with Visual Basic?
I don't have a clue. See above links.
I get a linker error saying external symbol
main not found. What's wrong?
It's looking for "main" because it thinks
you're trying to write a console mode (DOS-style) (command-line)
application. Win32 functions have a
WinMain instead of a
main. Create a project with the target platform being a
What's my opinion on wrapper class libraries for DirectX?
Anything to hide away the DirectX interface is a good thing, if you ask me. But I would recommend checking out what already exists before you develop your own. The ideal is to use cross-platform wrappers, of course (e.g. SDL, and/or even use OpenGL for 3D).
How do I create .x files?
The conv3ds.exe program that comes with the DirectX SDK (SDK\BIN directory) converts 3d studio (.3ds) files to .x files. You can get loads of 3ds files from the Internet; a good starting point is http://www.3dcafe.com/.
How do I set a 3D scene's background color?
Your code fails when I try it in 256-color mode. What gives?
I have no plan to support 256 colour modes.
stdafx.h I see #included in your source?
stdafx.h is a header file generated
by the MFC AppWizard. It is not strictly necessary, but is used by Visual
C++ for header precompilation in order to speed up program compiles.
It should be included in every .cpp file for your project, and heres a handy tip: include it
*before* any other includes.
I'm out of questions. Tell me something.
*Never* hard-code an absolute directory/filename. It's annoying to download code with a makefile or project workspace that specifies absolute directories, such that you have to change it yourself to get it to compile.
Tell me more.
You can really mess up your fingers typing, so *be careful*. Don't "race" when you type; take it easy. Take frequent breaks. Get yourself one of those "natural" keyboards, they're pretty good. If your fingers/wrists start to hurt, stop typing and get to a doctor. Also good posture is crucial - you are going to mess up your body if you don't sort this out while you are still young.
Tell me even more.
Check out Linux. It's great.
The tutorial really sucks, especially the code, when are you going to improve it?
Sorry, I don't have time anymore.
Your TraceErrorDD function is kind of MFC-specific. That sucks.
Yes it does.
What is this D3DVAL stuff?
A D3DVALUE is pretty much the same as a float; in fact, D3DVALUE is defined as a float. However, it is incorrect to use float and D3DVALUE interchangably, as there is no guarantee that your code will still work if a future version of DirectX changes D3DVALUE to, say, a double. Therefore you should use D3DVALUE's throughout. There is a macro (#define) to convert floats and doubles to D3DVALUE's, called D3DVAL. Hence: D3DVAL(3.4);
Shouldn't you explain a bit more about creating and releasing DirectX objects, as well as the reference count stuff?
Why do I get some sort of palette error when I try create a device from the surface?
You're probably in 256-color mode, in which case you need to attach a palette to the surface. I recommend running Windows in 16-bit or higher color modes instead.
What is the difference between Direct3D Retained Mode and Immediate Mode?
Immediate mode is a lower-level 3D API, in which you
have to manage lists of vertices and faces, generally doing sorting
yourself etc. Rendering is done via Immediate Mode "execute
buffers" which contain lists of rendering instructions to give
to the 3D hardware. Rendering can also be done using any of 17
DrawPrimitive functions, which let you draw triangles
etc without going through the execute buffers. Retained Mode is built
on top of Immediate Mode, and wrap up a lot of functionality in
classes to manage a 3D scene, 3D objects etc.
Why is the area around the window in the DirectDraw sample a mess?
Because I didn't bother to solid-color-blit the back buffer black when I created it. It happened to be black on my machine, so I never noticed the bug. Consider it an "exercise".
When am I going to put actual sample code here?
Quit bugging me, okay? :)
What compiler can I recommend?
Whatever you prefer. I like Visual C++; it has a very nice IDE, a *great* debugger, and the compiler's pretty good. DJGPP is an excellent option for the poorer amongst you: it's free.
Can I use DJGPP for DirectX?
I think so (not sure) but when I get time I will find out and place the relevant info here. DJGPP is a free C/C++ compiler; that way you won't have to go buy an expensive compiler.
What are all those strange little letters in front of your variable names, like
Hungarian Notation. The idea is to try make code more readable by including the type of the variable in the name, such as a b for boolean or a dw for a DWORD. Thus a boolean variable indicating whether or not the program is busy animating would be bAnimating. "m_" in front of a variable means 'member'. It takes only an hour or two at most to get used to, and it really helps make code more readable. I recommend it.
What is the 'UINT' in your sample code?
What does the Afx in AfxMessageBox mean?
I think it means "application framework"; its part of MFC-specific stuff.
Isn't it silly that you've named the function that creates both primary and back surfaces
What's your email address? Can I email you?
No. I'm not "DirectX tech support" and I'm not a DirectX helpline.