Topic : DirectX Programming
Author : Ian Fleeton
Page : 1 Next >>
Go to page :


Games Programming with DirectX 8 and C++


I am always updating this tutorial in response to feedback. If you find any problems with the information then please e-mail me. This tutorial is hosted at the openrpgs.com website.

Recent Updates
01 June 01
Added downloadable project files with source (MSVC)
31 May 01
Corrected information about interface identifiers
03 April 01
Added some palette tips and 16 bit notes and downloadable .zip
2nd Edition
This is the 2nd edition of the DirectX tutorials and I would like to thank all the people out there who gave me feedback, encouragement or asked me questions about the last version. So, thank-you! This revision will contain more detail about how and why the code samples were written so you can have a more hands-on experience and be able to tweak the code for yourself. This is a free, online resource and you are welcome to contribute.

If you would like to support our projects, please order a copy of this tutorial. For $15 US or ?10 UK we will post you a printed copy of this document. If you would like to peruse offline then you may download the compressed version [dx_tutorial.zip].

Free Documentation
Copyright (c) 2001 Ian Fleeton.

Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.1 or any later version published by the Free Software Foundation; with no Invariant Sections, with no Front-Cover Texts, and with no Back-Cover Texts. A copy of the license is included in the section entitled "GNU Free Documentation License".

- Ian Fleeton, January 2001


Table of Contents
Front Page
  2nd Edition
  Free Documentation
Table of Contents
What is this Collection of Tutorials About?
Background
  Getting Set Up
    The First Option
    The Second Option
    What Compiler?
  COM
  Creating the Project
     MS Visual C++ 6
     Other Compilers
  Linking
     MS Visual C++ Specific
DirectDraw
  Introduction
  Warning
  The Windows Bit
  DirectDraw
  DirectDraw Interface
  Setting the Co-operation Level
  Setting the Display Mode
    Common Resolutions
  Creating the Flipping Surfaces
  Flip
  Clearing the Back Surface
  A SetPixel for DirectDraw
  Clearing Up
  256 Colour Palettes
    Introduction to 256 Colour Palettes
    Palette Problems and Benefits
    Can I program one now?
    Paint Shop Pro Palette Loading
    Grey Scale Palettes
16-bit Colour




What is this Collection of Tutorials About?


These tutorials are here to help programmers who are new to DirectX get acquainted with the libraries, in particular DirectDraw and DirectInput. The cooler components such as Direct3D and DirectPlay aren't going to be covered yet as I'm not familiar enough with them :).

DirectSound is going to be skimmed over briefly as I am using a great sound library called FMOD which you can get from www.fmod.org. However, I will warn you that FMOD is only free for non-commercial use and that it is closed-source unless you have a lot of money.

I am assuming that you have a reasonable grasp of C++ (or at least C) but I will explain anything that gets too tricky. DirectX is fairly language neutral so it is available for many other languages. I believe Visual Basic is supported quite well but these tutorials won't be of much help.

If you have ?36.50 or $49.99 spare I also recommend getting the book Tricks of the Windows Game Programming Gurus by Andre LaMothe. He also has another book, Windows® Game Programming For Dummies® but it has a lot less material and is targetted at an older version - DirectX 5.

As an alternative to the DirectX graphics libraries there is also OpenGL. As I have not used OpenGL yet it is up to you to find out which is best for you.

DirectX, like many things when you get to know them, is fairly easy to use. However, some of the concepts might be tricky to pick up. As these tutorials are very new, please, please send me feedback. If I haven't explained something properly then email me your questions and I'll make sure that these tutorials get updated.

Surprisingly (I say that because not everyone likes Microsoft as much as me), there is a lot of good documentation packed with the DirectX SDK and I recommend reading through it. Only the most persistent beginners will be able to learn from it alone, though. When I start learning something new I find that most documentation just doesn't address the fact that I really don't have a clue. I've got some spare time on my hands before I have to get a job so I'm going to share what I've learned. Not all of it is accurate (that's why my e-mail address is at the bottom) but most of it is close enough.


Background
Background — Getting Set Up — COM — Creating the Project — Linking


DirectX is a set of multimedia libraries provided by Microsoft. The aim of them is to allow Windows programmers to control hardware without knowing exactly how each piece works. A DirectDraw programmer needs to know a little bit about video cards in general but nothing about the specifics of any certain model. This is great! Just imagine how hard it would be to write a program that had a different sprite blitting function for a thousand different video cards. Thanks to DirectX, DirectX compliant hardware and drivers have to meet certain rules that makes them compatible with DirectX. And when they are, you can use them with ease.

DirectX is free for the end user and for the programmer. At the time of writing, the current version is DirectX 8. Users of your game can download it from the MS site. For them it's only a mere 7MB or so download. However, you will need the SDK (software development kit). This weighs in at well over a hundred megabytes compressed and needs about four times that space when you install! If you have the patience you can order a CD from MS at a reasonably low price. If you are downloading, make sure that you have a good fast connection or that you have a downloading program capable of resuming. Currently I am using the shareware program, GetRight.

The DirectX libraries are compatible with a few different compilers but I really do recommend Microsoft Visual C++ 6 as your games compiler.


Getting Set Up


If you downloaded the SDK, first extract the package. It is a self extracting WinZip file. If you get a CRC error that means one or more of the files is corrupt and you'll notice that WinZip aborts. If this happens, all is not lost. Instead, try to unzip the files manually, leaving out the ones that are causing trouble. Many of the DirectX files are documentation or samples, only a small amount of them are the libraries and headers that you need. When you have the package extracted into a directory click on the Setup or Install program. When this completes you can delete the intermediate directory. If you downloaded the SDK then now is a good time to make a backup so that you don't have to do it again.

You'll be asked whether you want the debug or retail runtime libraries installed. If you don't play many games on your development PC then I suggest you use the debug version. Supposedly (and I believe it), it helps find bugs.

Now here's the slightly tricky part. You now want to get your new programs to use the new .lib and .h files from the SDK. You have about two choices. One of them I could never be bothered to do until a kind reader enlightened me.

The First Option
This works with most compilers. Copy the files from the include and lib subdirectories where the SDK is installed to the include and lib subdirectories where your compiler is installed. You should get a few messages warning you that you are about to overwrite some files, especially if you already have some older DirectX files in your compiler's libraries. I just choose to go right ahead and overwrite everything but you might want to make backups.

The Second Option
This is MSVC 6 specific. Add the SDK directories to the compiler's search paths for include and library files by following the steps below:

Go to the Tools menu
Choose Options
Move to the Directories tab
Find the little button to the top right of the list that looks like a transparent piece of paper with a yellow star - that's the new directory button
Now add the folders where your

Page : 1 Next >>