Tricks of the Windows Game Programming Gurus:Installing DirectX and Using the C/C++ Compiler

All ITKnowledge Source Code Search Tips Advanced Search |

| Tricks of the Windows Game Programming Gurus by Andre LaMothe Sams, Macmillan Computer Publishing ISBN: 0672313618 Pub Date: 09/27/99

Search this book:

Previous | Table of Contents | Next

## Appendix B
Installing DirectX and Using the C/C++ Compiler

The most important part of the CD that you must install is the DirectX SDK and run-time files. The installation program is located within the `DIRECTX\` directory, along with a `README.TXT` file explaining any last-minute changes.

> Note: You must have the DirectX 6.1 SDK or better to work with this CD and book. If you’re not sure that you have the latest files on your system, run the installation and it will tell you.

When you’re installing DirectX, pay attention to where the installer places the SDK files. You will have to point your compiler’s `LIB` and `HEADER` search paths appropriately when you want to compile.

In addition, when you install the DirectX SDK, the installer will also ask if you want to install the DirectX run-time files. You need the run-time files as well as the SDK to run the programs. However, there are two versions of the run-time libraries:

Debug | This version has hooks for debugging and is the one that I suggest you install for development. However, your DirectX programs will run a little slower.
Retail | This version is the full release retail consumer version that you would expect a user to have. It’s faster than the Debug version. You can always install this one on top of the Debug version later if you want.

> Note: Attention Borland users (if there are any left): The DirectX SDK does have Borland versions of the `DirectX .LIB` import libraries. You will find these in the `BORLAND\` directory of the DirectX SDK installation. You must use these files when compiling. Also, make sure to go to Borland’s Web site and read the `README.TXT` in the `BORLAND\` directory for any last-minute hints on compiling DirectX programs with Borland compilers.

Finally, by the time I finish this book, Microsoft will have approximately six more versions of DirectX. Make sure to keep up-to-date with the SDK by visiting the DirectX site from time to time:

    http://www.microsoft.com/directx/

Also, DirectMedia is a high-level, browser-based DirectX system that you can control via scripts, controls, and Java. You can actually make DirectX games in a browser! Check it out at

    http://www.microsoft.com/directx/overview/dxmedia/default.asp

### Using the C/C++ Compiler

I have received over 17,000 emails in the last three years from people who don’t know how to use the C/C++ compiler. I don’t want another email about compiler problems unless blood is squirting from the screen and the computer is speaking in tongues! Every single problem was a result of a rookie compiler user. You simply can’t expect to use a piece of software as complex as a C/C++ compiler without reading the manual—right, Jules? So please do so before trying to compile programs from this book.

Anyway, here’s the deal about compiling: I used MS VC++ 5.0 and 6.0 for this book, so everything works perfectly with those compilers. I’d also imagine that VC++ 4.0 would work, but don’t hold me to that (I heard there are some problems with DirectX 6.0 and VC++ 4.0). If you have a Borland or Watcom compiler, they should work also, but you may have to do a bit of work to get the right setup to compile. My suggestion is to save yourself the headache and get a copy of VC++ for $99.

The bottom line is that the MS compiler is the best for Windows/DirectX programs, and it just makes everything work better. I have used Borland and Watcom for other things, but for Windows applications, I don’t know many professional game programmers who don’t use MS VC++. It’s just the right tool for the right job. (Note to Bill Gates: My Bank of Cayman account number is 412-0300686-21. Thanks.)

Here are some hints for setting up the MS VC++ compilers. Other compilers are similar:

  Application Type—DirectX programs are Windows programs. More accurately, they’re Win32 `.EXE` applications. Hence, set your compiler to Win32 `.EXE` applications for all DirectX programs. If I tell you that you’re making a console application, set the compiler for console applications. Also, I suggest that you make a single workspace and compile all your programs in it.
  Search Directories—To compile DirectX programs, there are two things the compiler needs: the `.LIB` files and the `.H` files. Now, listen up: Set both search paths in the compiler/linker options to search the DirectX SDK `.LIB` directory and `.H` directory so the compiler can find the files during compilation. However, this isn’t enough! You must also make absolutely sure the DirectX paths are first in the search tree. The reason for this is that VC++ comes with an old version of DirectX, and you will end up linking DirectX 3.0 files if you aren’t careful. Furthermore, make sure that you manually include the DirectX `.LIB` files in your projects. I want to see `DDRAW.LIB`, `DSOUND.LIB`, `DINPUT.LIB`, and so on in the project! This is very important!!!
  Error-Level Setting—Make sure that you turn the error level on your compiler to something reasonable, like level 1 or 2. Don’t turn it off, but don’t put it on full-blast. The code in this book that I wrote is professional-level C/C++, and the compiler will think that there are a lot of things that I didn’t want to do that I intended to do. Hence, turn the warning level down.
  Typecast Errors—If the compiler gives you a typecast error on a line of code (VC++ 6.0 users beware), just cast it! I have over 3,000 emails from people who don’t know what a typecast is. If you don’t, look it up in a C/C++ book. But I will tell you that some of my code may still be missing an explicit typecast here or there. VC++ 6.0 might get belligerent. If you get one of these errors, look at what the compiler expects and just put it in front of the `rvalue` in the expression to fix it.
  Optimization Settings—You aren’t making full release products yet, so don’t set the compiler to a crazy optimization level. Just set it for standard optimizations that prefer speed over size.
  Threading Models—99% of the examples in the book are single-threaded, so use the single-threaded libraries. If you don’t know what that means, read a compiler book. However, when I do use multithreaded libraries, I will tell you. For example, to compile the examples on multithreading from Chapter 11, “Algorithms, Data Structures, Memory Management, and Multithreading,” you must switch to the multithreaded libraries.
  Code Generation—This controls what kind of code the compiler generates. Set it for Pentium. I haven’t seen a 486 in a long time, so don’t worry about being compatible.
  Struct Alignment—This controls the “filling” between structures. PentiumX processors like things that are in multiples of 32 bytes, so set the alignment as high as possible. It will make the code a bit bigger, but also a lot faster.

Finally, when you’re compiling programs, make sure that you include all the source files referenced in the main program. For example, if you see me include `T3DLIB1.H`, chances are there’s a `T3DLIB1.CPP` that needs to be in the project—get it?

Previous | Table of Contents | Next

Copyright © Macmillan Computer Publishing, Inc.

Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.