Click here to Skip to main content
Click here to Skip to main content
Go to top

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.

Introduction

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.

Background

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 Wink | ;) ) 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 Wink | ;)

Prerequisites

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.

Initialization

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="http://msdn.microsoft.com/library/en-us/cpref/html/frlrfsystemint32classtopic.asp" target=_blank>int</A> adapter, 
    <A href="http://msdn.microsoft.com/archive/en-us/directx9_m_dec_2004/directx/ref/ns/microsoft.directx.direct3d/e/devicetype/devicetype.asp" target=_blank>DeviceType</A> deviceType, 
    <A href="http://msdn.microsoft.com/library/en-us/cpref/html/frlrfsystemwindowsformscontrolclasstopic.asp" target=_blank>Control</A> renderWindow, 
    <A href="http://msdn.microsoft.com/archive/en-us/directx9_m_dec_2004/directx/ref/ns/microsoft.directx.direct3d/e/createflags/createflags.asp" target=_blank>CreateFlags</A> behaviorFlags, 
    <A href="http://msdn.microsoft.com/archive/en-us/directx9_m_dec_2004/directx/ref/ns/microsoft.directx.direct3d/c/presentparameters/presentparameters.asp" 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:

//Form1
public cEngine Engine = new cEngine(); 

//Main
using (Form1 GameWindow = new Form1())
{
  GameWindow.presentParams = 
     GameWindow.Engine.SetPParams(true, 
     SwapEffect.Discard);
  GameWindow.device = 
     GameWindow.Engine.SetDevice(GameWindow, 
     GameWindow.presentParams);
  Application.Run(GameWindow);
}

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.

X-CHALLENGE

  • 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!

Contact

Please send all emails to xpyder@magclan.cwhnetworks.com. I do have MSN Messenger, my email address for this is jamespraveen@aol.com.

History

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

License

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

Share

About the Author

James Gupta
Web Developer
United States United States
I live in England, UK in a small town which only just got broadband...
 
In my spare time, I run a small website design, hosting and maintenance business at www.jamesgupta.com

Comments and Discussions

 
GeneralSlight clarification needed... [modified] PinmemberVorlin26-Dec-06 20:07 
GeneralException when creating the device PinmemberiGb2-Nov-06 1:31 
I initially installed the C# DirectX SDK from Microsoft, but this doesn't actually install anything useful (in particular it does not include any of the required DLLs - thanks Microsoft). I removed it. Then I installed the October SDK. Now the project can locate the assmeblies, but when I execute the code I get an invalid call exception when creating the device.
 
Anyone have any ideas? Is there anything I can use to clean up the mess Microsoft has created?
GeneralRe: Exception when creating the device PinmemberiGb2-Nov-06 3:27 
GeneralLooking For A Link Pinmemberxcorporation8-Oct-06 22:45 
GeneralThis is me.... 15 years ago :-) PinmemberSascha Sertel3-Feb-06 9:31 
GeneralRe: This is me.... 15 years ago :-) Pinmemberbootkey11-Jan-07 3:31 

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

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

| Advertise | Privacy | Mobile
Web03 | 2.8.140905.1 | Last Updated 3 Feb 2006
Article Copyright 2006 by James Gupta
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid