Game Programming Tutorials
dogonfire.dk Index Tutorials Farbrausch Forum Register FAQ

 Getting Started 

Introduction

Welcome to the DirectX tutorials. This is the first tutorial of many that should at least help you on the way to make games using Microsoft DirectX 9. I write these with memory of the issues i had myself when finding out how to get started. Wanting to learn about the 3D principles, i found there is a lack of quality walkthroughs and the DirectX SDK help file can appear abit heavy to read thrugh for a beginner. Hopefully these tutorials "hands on" approach will provide a help for understanding whats going on :)

What you need to proceed

  • Microsoft Visual C++ 6
  • DirectX 9.0 SDK (Downloadable from http://msdn.microsoft.com/directx/directxdownloads)
  • General knowledge of Windows programming
  • General knowledge of C++ programming


  • COM

    What is COM? Well, the Component Object Model is basically a library of methods. You can create COM objects in your program and then call the methods that they expose to you. Methods are grouped together in collections of related methods. These collections are known as Interfaces. You could think of a COM object as a library of functions arranged by subject. DirectX provides a whole host of these libraries that will enable you to create your 3D game. The best part is, that DirectX takes care of a lot of the hard stuff for you, so it is pretty easy to get something simple up and running.

    Of course there is a lot more to COM than this, for a full description take a look in the DirectX SDK. All you really need to worry about is that you release all of your COM objects/interfaces before your program terminates. You should make sure that you release them in the reverse order to that which you created them. For example:

  • 1. Create interface A.
  • 2. Create interface B.
  • 3. Release interface B.
  • 4. Release interface A.


  • You release the COM objects by calling their "Release" method.

    Page Flipping

    What is "Page Flipping"? Think of flipping through a book. This is a number of pages with a slightly different drawing on each page. Then, when you hold the corner and "flip" the pages, it looks like the picture is moving. This is how DirectX graphics (and most other rendering API's) works. You draw all of your objects onto a hidden page, known as the "Back Buffer". Then when you have finished, flip it to the "Front Buffer" (the screen) and repeat the process. While the user is looking at the new front buffer, your program will be drawing onto the back buffer.

    What would happen without Page Flipping?

    Without Page Flipping, the user would see each object appear while it was being drawn, which is not what you want; You would see a terrible flickering effect while looking at what the computer draws.

    Your game will basically run inside of a loop, known as the Game Loop. Each time around the loop you process your game physics and animation so you know where your objects will be. Next, you clear the Back Buffer. Then draw the current scene onto it. When this is done, flip it to the front and run the loop again. This will continue until the game is shut down. You may have a number of Back Buffers, this is known as a Swap Chain.

    Devices

    What is a device? Basically, a device (as far as DirectX Graphics is concerned) is your machines 3D graphics card. You can create an interface that represents your device and then use it to draw objects onto the back buffer.

    Game Loop

    What is the game loop? Well, the game loop is a code loop that loops until the program is shut down. Inside the game loop is where it all happens: objects are drawn (rendered), game logic is processed (AI, moving objects and scoring etc) and Windows messages are processed. Then it's all done again until the program is closed down.

    Creating Your First Project

    Okay, that’s the theory so lets get started. Follow the step-by-step guide below to create your first DirectX Graphics project.

    1. In Visual C++ create a new Win32 Application.
      a. File > New
      b. From the Projects tab select Win32 Application
      c. Enter a name for your project such as "Project 1"
      d. Select a folder for the location of your source code files
      e. Click Next
      f. Select the "empty project" option.
      g. Click "Finish"
    2. Make sure that your project settings are correct.
      a. Project > Settings...
      b. On the Link tab, make sure that "d3d9.lib" is in the list of Object/Library Modules. If it isn’t simply type it in.
    3. Make sure that your search paths are correct.
      a. Tools > Options > Directories Tab
      b. In the "Show directories for" drop-down, select "include files".
      c. If it does not exist already, add the following path: \include.
      d. Make sure that this path is at the top of the list by clicking on the up arrow button (if needed).
      e. In the "Show directories for" drop-down, select "library files".
      f. If it does not exist already, add the following path: \lib.
      g. Make sure that this path is at the top of the list by clicking on the up arrow button (if needed).
    4. Add the source code.
      a. File > New
      b. From the Files tab, select "C++ Source File"
      c. Enter a filename such as "main.cpp"
      d. Copy the code segment below, and then paste it into your new file.
    5. Build and Run the program.
      a. Press F7 to build your project
      b. Press F5 to run