Click here to Skip to main content
13,295,051 members (87,334 online)
Click here to Skip to main content
Add your own
alternative version


42 bookmarked
Posted 3 Feb 2006

Managed DirectX Tutorials: Part 2 - Initialising Direct3D

, 3 Feb 2006
Rate this:
Please Sign up or sign in to vote.
This is the second in a series of tutorials designed to show you how to create a basic terrain engine.


This is the second in a series of tutorials which will allow you to create your own game engine (from initialization, to a fully rotatable, height-mapped 3D world!).

In this second tutorial, we will actually write code to create a link to the computer's graphics card. To do this, we will initialize Direct3D, and create a Device object to represent the user's graphics card.


The code from these tutorials is taken from my own game engine: MAGEngine.NET at different stages in development.

Using the code

You may use all of the code I provide as how you see fit, except to create another tutorial. You can use it as a sturdy(ish ;)) framework for your own applications, or print loads of copies off so that when I'm rich and famous you can sell them for $100 each ;)


To do this series of tutorials, you will need:

  • C# Compiler (preferably Visual C# 2005 Express)
  • Managed DirectX 9.0 October SDK

Tutorial 1: Setting Up DirectX

Direct3D is the main component of DirectX graphics. With it, you can render and initialize 3D objects. We will not get to render anything in this tutorial, but we will set D3D up so that we can do this easily in the next tutorial.


The point of initialising Direct3D is to get a Device object. This represents the user’s graphics card and has a set of functions to send and return data from it. To get this object, you need to set some variables.

The initialization function is as follows:

public Device( 
    <A href="" target=_blank>int</A> adapter, 
    <A href="" target=_blank>DeviceType</A> deviceType, 
    <A href="" target=_blank>Control</A> renderWindow, 
    <A href="" target=_blank>CreateFlags</A> behaviorFlags, 
    <A href="" target=_blank>PresentParameters</A>[] presentationParameters );

OK, this may look complicated at first, but we can break it down.

  • adapter simply indicates which device to use – so for our purposes, this will always be 0.
  • deviceType is an enumeration of devices. For example, software or hardware.
  • renderWindow is simply a reference to the window it will be created in.
  • behaviourFlags are a set of flags defining how the device will be used.
  • presentationParameters is simply a structure you fill in with more detailed options regarding how the device will be used.

So, our initialization process will consist of two parts:

  • Initialize presentationParameters
  • Initialize device

To start with, add the following variables to the top of your window class:

public Device device; 
public PresentParameters presentParams;

Now, before we get started on making a class and functions to initialize Direct3D, we should explore the PresentParameters structure more.

For this application, we only need to assign two members from the PresetnParameters structure - bool Windowed and SwapEffect swapEffect.

SwapEffect is an enumeration of ways to “flip” the screen. This means, to update or refresh the current frame in the game, we can do:

public PresentParameters SetPParams() { 
  PresentParameters pp = new PresentParameters(); 
  return pp;

This will be set as the framework for the function that will create the presentation parameters for us in our engine. Now, as stated before, we need to input two variables and edit these accordingly, so simply add a bool and SwapEffect to the parameters of the function, and then in the body, in between the PresentParameters() initialization and the return code, put the lines to assign your present parameters. Your finished function (and class) should look like this (put this in a separate code file called Engine.cs):

public partial class cEngine
// the partial keyword specifies that this
// class is declared in multiple files
  public PresentParameters SetPParams (bool wind, SwapEffect swap)
    PresentParameters pp = new PresentParameters();
    pp.Windowed = wind; // Is our device going to be running
    // in a windowed or full screen application?

    pp.SwapEffect = swap;
    // How should we handle the refreshing of our application? 
    return pp; 

So, that covers PresentationParameters - but now we need to create a class to initialize the device.

As mentioned before, the device represents the GPU, or graphics card. Towards the top of this tutorial, you saw a layout of the Device constructor. There are some overloads of this, but the one chosen above is the best for our needs. It shouldn't be too hard to make a function within our cEngine class to handle the device - but if you were wondering:

public partial class cEngine
// the partial keyword specifies that
// this class is declared in multiple files
  public Device SetDevice( Control wnd, PresentParameters pp)
    Device device = new Device(0, DeviceType.Hardware, 
           wnd, CreateFlags.HardwareVertexProcessing, pp);
    return device;

This function simplifies the creation of a device by limiting the number of parameters, as ones which are almost always the same are omitted.

With these two methods, all you need to do to get your D3D application running is add the required variables to the Form class and then call the two methods like so:

public cEngine Engine = new cEngine(); 

using (Form1 GameWindow = new Form1())
  GameWindow.presentParams = 
  GameWindow.device = 

If you get errors, make sure you have using statements in your Form class for the namespace containing your engine declaration.

Now, when you run this application, there is no proof of all that work you did - which I personally find annoying. So, add a paint event handler to the form.


  • Create an overload for the above function which sets the number of back buffers as well as the back buffer width and height.
  • Create a function which merges the Device and PresentParameter initialization functions.

Well done - you should now have a working Direct3D application, along with a basic framework!

Get ready to move on to tutorial 3 - where we will actually render something on screen!


Please send all emails to I do have MSN Messenger, my email address for this is


  • 19/01/06: Submitted tutorials 1-3 to CodeProject.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

James Gupta
CEO Synap
United Kingdom United Kingdom
Founder & CEO of Synap, an online education platform that uses machine learning to help people learn more in less time.

Software developer, main languages currently are Objective-C, MySQL and Javascript, though I got started on C++, C# and PHP.

You may also be interested in...


Comments and Discussions

GeneralSlight clarification needed... [modified] Pin
Vorlin26-Dec-06 21:07
memberVorlin26-Dec-06 21:07 
GeneralException when creating the device Pin
iGb2-Nov-06 2:31
memberiGb2-Nov-06 2:31 
GeneralRe: Exception when creating the device Pin
iGb2-Nov-06 4:27
memberiGb2-Nov-06 4:27 
GeneralLooking For A Link Pin
xcorporation8-Oct-06 23:45
memberxcorporation8-Oct-06 23:45 
GeneralThis is me.... 15 years ago :-) Pin
Sascha Sertel3-Feb-06 10:31
memberSascha Sertel3-Feb-06 10:31 
GeneralRe: This is me.... 15 years ago :-) Pin
bootkey11-Jan-07 4:31
memberbootkey11-Jan-07 4:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.171207.1 | Last Updated 3 Feb 2006
Article Copyright 2006 by James Gupta
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid