Friday, March 13, 2009

Creating a DirectX Skeleton

Welcome back!

In this post I will show you how to set up a simple DirectX program using Visual C# Express 2005 using Managed DirectX. This post might be of interest of those of you who want to write games using C# but do not have a graphics card that supports Pixel Shader 1.1, which is required to write games with XNA.

You will need to install the DirectX SDK from microsoft. It can be found here:

http://msdn.microsoft.com/en-us/directx/aa937788.aspx

After you have it installed you are ready to begin.

The first thing you will need to do is start Visual C# and create a Windows Application.

Now you have to setup your program to use DirectX. To do this you have to enter two references. Click the Program menu item, then select the Add Reference entry.

You will choose the Microsoft.DirectX and Microsoft.DirectX.Direct3D enteries in the .NET tab.

Open the code view tab for Form1. You will want to add two using statements at the top of the program:

using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;


After adding the using statements you will need to add a variable for the Direct3D device.

public Device device = null;

Go back to the Design view of Form1 then double click on the form (or you could click the events button on the properties window and double click the Load entry.) This will bring up the event handler for the Form Load event. Add the following code:

    InitializeDirectX();

    if (device == null)
    {
        MessageBox.Show("Failed to initiralize DirectX");
        Application.Exit();
    }


Now we will write the InitializeDirectX method.

private void InitializeDirectX()
{
    PresentParameters presParams = new PresentParameters();
    presParams.Windowed = true;
    presParams.SwapEffect = SwapEffect.Discard;

    device = new Device(0, DeviceType.Hardware, this,
            CreateFlags.SoftwareVertexProcessing,
            presParams);
}


The first thing this method does is create an instance of the presentation parameters for our device. In this tutorial I will not explain them too much as this is just to get your feet wet. The two fields that we are changing are Windows, which tells us if we want to use windowed or fullscreen for this device. The second, SwapEffect, tells the device how we want to deal with swapping from the backbuffer to the device. In this case we simply want to discard it. There is one more thing that we have to do to get the program ready to display. We need to add code to the Paint event for the form.

So, switch to the design view of your form and in the properties windon click the events button. Then scroll down to the Paint event and double click it. This will bring up the event handler for the Paint event. For this sample all we are going to do is clear the window then draw the window.
Add the following code to the Paint event:

    device.Clear(ClearFlags.Target, Color.Blue, 1.0f, 0);
    device.Present();

All that we are doing here is clearing the buffer then presenting the scene to our devie. The parameters for the Clear method are: ClearFlags - describes what we want to clear, Color - a System.Drawing.Color, the color we want to clear the buffer to, zBuffer - I will go into this later when we start doing 3D work, stencil - this deals with stencil buffers, for now just set this to 0.

That is all you need to setup a device using Managed DirectX
I will try and make another post in the next day or so. Come back an look form more!

No comments:

Post a Comment