DirectX Programming in C# by tomd123

A little background info:

   DirectX projects require more processing power from the computer and they could become really complex because of all the math that is required.

 

Most DirectX projects are games, but it isn’t only limited to games. So for these types of projects it is necessary to keep everything simple and get everything that is not used out of the actual program. Performance is also another issue but only is required for larger, more complex programs.

 

Requirements

   This project was created using Microsoft Visual Studio 2005, if you don’t have it, you could try using earlier versions but I don’t know if it will work the same way. If you don’t have Microsoft Visual Studio 2005 or it is just too expensive you can get a free version on http://msdn.microsoft.com/vstudio/express/visualcsharp/. You will also need the DirectX SDK which can be found on http://msdn.microsoft.com/directx/sdk/.

Programming

This article is intended to be a sort of step by step tutorial to start writing a program for DirectX using C#. 

First open Microsoft Visual Studio 2005 and create an empty C# project. Insert a new class into your project. Next you can delete or comment the following 2 lines of code:

using System.Collections.Generic;
using System.Text;
 

You can also remove the following references from your project:

System.Data System.Xml

You have to specify that the program is a windows program so go to Project-> Project Properties. Put the output type as Windows Application.

Next we have to add the necessary DirectX resources for this project. Put the namespaces in the beginning of the file:

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

Now go to the add reference dialog and click browse. Go to the directory you installed the DXSDK into and go to the “developer runtime” folder then go to x86 folder then go to the “DirectX for Managed Code” folder. Select the Microsoft.DirectX.Direct3D.DLL, Microsoft.DirectX.Direct3DX.DLL and Microsoft.DirectX.DLL references and click ok. Next go to add reference again and select System.Drawing and System.Windows.Forms reference. Now you have the project set up for DirectX.

You start off with:

Device DX_Device = null; // Drawing device

to make the drawing device. Next you have to put the information for your window into the constructor:

public UsingDirectX()
{
   this.ClientSize = new Size(256,256); // Specify the client size
   this.Text = “My First DirectX Program”; // Specify the title
}
 

Next you must tell the device how to render to the screen and handle any exceptions.

public bool InitializeDirect3D()
{
   try
   {
     PresentParameters pps = new PresentParameters();
     pps.Windowed = true; // Specify that it will be in a window
     pps.SwapEffect = SwapEffect.Discard; // After the current screen is drawn, it will be automatically // deleted from memory
     DX_Device = new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, pps); // Put everything into the device
     return true;
   }
   catch(DirectXException e)
   {
     MessageBox.Show(e.ToString(), “Error”); // Handle all the exceptions
     return false;
   }
}
 

The next function handles the main rendering that is done all in one neat package.

private void Render()

   if(DX_Device == null) // If the device is empty don’t bother rendering
   {
      return;
   }

   DX_Device.Clear(ClearFlags.Target, Color.White, 1.0f, 0); // Clear the window to white
   DX_Device.BeginScene();

   // Rendering is done here

   DX_Device.EndScene();
   DX_Device.Present();
}
 

The main function is what its name implies. It is the main function were everything in our program takes place, including the message loop.

static void Main()
{

   UsingDirectX form = new UsingDirectX(); // Create the form
   if (form.InitializeDirect3D() == false) // Check if D3D could be initialized
   {
      MessageBox.Show(“Could not initialize Direct3D.”, “Error”);
      return;
   }
   form.Show(); // When everything is initialized, show the form
   while(form.Created) // This is our message loop
   {
      form.Render(); // Keep rendering until the program terminates
      Application.DoEvents(); // Process the events, like keyboard and mouse input
   }
}
 

I hope you learned the basics of creating a simple directx program. All this program does is basically set up a loop and keep rendering, or in this case just clearing the screen to white since there is no rendering being done. Full code to this project can be found here.


Attachments

Project Files First DirectX Sample