(was "Game programming with DirectX")
Copyright (C) David Joffe 1997-2001.
Menu:
Introduction
Target audience: C/C++ Windows programmers who want to learn to write applications that use Microsoft's 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.
[New Note] Please note my opinions on APIs, including DirectX, have changed over the years - I'm now more positive about Direct3D. The industry has changed a lot. The APIs have changed and improved a lot, the technologies have changed and evolved in many ways (most the things I didn't like have either long been addressed, or don't matter that much in the grander scheme of things); meanwhile, I take a more neutral approach and just use 'whatever tool for the job', I don't have time to sit and be opinionated, I just want to solve problems. Everything has its positive points and negative points. These days the main other alternative is Vulkan as OpenGL has started to show its limitations (Vulkan is better designed to work and perform well in today're much more multi-core, parallel environment) - Vulkan is quite complex and arcane, but powerful (like Direct3D) - graphics APIs have become more complex, and so it tends to be a case of 'you're either working on a lot of low-level code (or a part of something larger), or using wrappers or frameworks or SDKs or tools like Unreal Engine or Unity, in which case many of the 'nuts n bolts' are 'hidden' from most developers (not really literally hidden) in many cases.
"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
Resources/Links
SDL: Simple DirectMedia Layer
A decent, broadly-used cross-platform (Linux, Windows, MacOS, BeOS) games API.
Wraps DirectX on Windows. Uses OpenGL for 3D [2020 update: Supports Vulkan]. 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. (recommendation)
The DirectX eXperience
Various DirectX articles from all over.
Official Microsoft DirectX SDK
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
CDX Lib.
C++ class library, DirectX wrapper classes. Free, with source code.
DirectX with Borland C++
Using DirectX With Borland C++, by Michael Lundberg.
archive:geocities.com/foetsch/borlibs/
DirectX 8 Borland LIBs.
archive:geocities.com/SiliconValley/Pines/7268/
"... 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)
http://www3.telus.net/alexander_russell/directx.html.
DirectDraw, sound, music, I/O (no 3D). Sample source, with documentation, 2 editors for sprites and GUIs.
Books
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.
Other books:
FAQ (Frequently Asked Questions)
How do I get started in game programming?
Lots of reading, lots of practise. 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
Win32 application.
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?
SetSceneBackgroundRGB.
Your code fails when I try it in 256-color mode. What gives?
I have no plan to support 256 colour modes.
What's this stdafx.h
I see #included in your source?
The file 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?
Yes.
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 bAnimating
?
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?
It's an unsigned int
.
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 CreatePrimarySurface
?
Yes.
What's your email address? Can I email you?
No. I'm not "DirectX tech support" and I'm not a DirectX helpline.