Page 1 of 1

Using Managed DirectX First managed DirectX program

#1 SixOfEleven  Icon User is offline

  • using Caffeine;
  • member icon

Reputation: 942
  • View blog
  • Posts: 6,342
  • Joined: 18-October 08

Post icon  Posted 17 March 2009 - 09:27 AM

This is the first turorial that I'm posting here. I have noticed that some people or not fortunate enough to be able to use XNA to write games and others want to write games for less capable machines. To try and solve this problem I want to write tutorials on how to write games using Managed DirectX in C#. I am working on a starter kit to help getting started writing games using Managed DirextX. But enough preamble, let's get started.

I'm using Visual C# Express 2005 and the November 2008 DirectX SDK. If you are using Visual C# Express 2008 or Visual Studio full edition, let me know if the code works there.


The first thing that you need to do is create a new project. Before you can use DirectX you must add references to your project. To do this click the Project menu item then click Add Reference. When the popup window appears, click the .NET tab then scroll down until you see Microsoft.DirectX. Click on this item then scroll down a little farther hold down the Crtl key and click Microsoft.DirectX.Direct3D. This will add the refrences you will need for your project.

Next you will want to open the code view for the Program.cs file. There is one line that you will want to delete:

Application.Run(new Form1());


Now you will add a couple new lines of code in place of that one.

using (Form1 frm = new Form1())
{
	if (!frm.InitializeDirect3D())
	{
		MessageBox.Show("Error initizlizing Direct3D");
		return;
	}
   frm.Show();
   frm.Run();
}


This code does four things. First it creates a new form in the using statement that will dispose of everything when the program ends. Then it tries to initialize Direct3D, using a method that has to be writen, and shows an error if that fails and exits the program. Then it makes sure that tht form is visible. Then it calls the Run method in Form1 that we will write.

With that done you need to go to the code view of Form1. The first thing that you will need to do is add two using statements;

using Microsoft.DirectX;
using D3D = Microsoft.DirectX.Direct3D;


You might wonder why I qualified the second using statement. It is because in DirectX everything is a device. That includes Direct3D, DirectInput and DirectSound. So it helps to add a qualifier to help differentiate the devices. Now you have to add a variable for the Direct3D device.

private D3D.Device graphicsDevice = null;


Then you have to write the InitializeDirect3D and the run methods. They are as follows:

public bool InitializeDirect3D()
{
	D3D.PresentParameters pp = new D3D.PresentParameters();
   
	pp.Windowed = true;
	pp.SwapEffect = D3D.SwapEffect.Discard;

	try
	{
		graphicsDevice = new D3D.Device(0,
				D3D.DeviceType.Hardware,
				this,
				D3D.CreateFlags.SoftwareVertexProcessing,
				pp);
	}
	catch
	{
		return false;
	}
	return true;
}

public void Run()
{
	while (this.Created)
	{
		GameLogic();
		Render();
		Application.DoEvents();
	}
}


In InitializeDirect3D the first thing that you have to do is create the presentation parameters. This describes how you want the device to behave. Right now there are only two properties that you are interested in. They are Windowed and SwapEffect. Windowed is pretty self explanitory, it determines if you want to you a window or full screen mode. Going full screen isn't as simple as just switching the Windowed property to false. There is more work that has to be done that I will cover in a future tutorial. I have put the code to create the device in a try-catch block because creating a device improperly may crash your program if you don't.

The constructor for the Device object has several overloaded methods. The one that I are used has five parameters. The first is the adapter that you want to use. Because graphics cards can have more than one monitor or the computer can have more than one card you need to specify which adapter to use. I used the first adapter which is 0. The next parameter is the device type that you want to use. For most situations you want to use DeviceType.Hardware. This specifies that you want to create a hardware device. There are three other device type but I won't go into them here. The next parameter is the control that you want to use. In this case I used the form itself but you don't have to. You could easily use other controls like Panels. You could have two devices that use two different panels to develop a split screen game. The fourth parameter is CreateFlags. There are ten flags. In this case I used CreateFlags.SoftwareVertexProcessing. I used this so that people with less capable hardware can use your games. The last parameter is the presentation parameters that you want to use.

The run method will eventually become the main game loop for the program. Timing should be added eventually but right now all that has to be done is call the GameLogic and Render methods then process any events that are waiting. All of this happens while this.Created is true. If you don't know the Created property is true until the form is closed.

All that is left to be done is code the GameLogic and Render methods. For now there is nothing in the GameLogic method, it is just a stub that needs to be filled in later. The Render method is where you need to add the code to draw your scenes.


private void GameLogic()
{
	// TODO: add your game logic here
}

private void Render()
{
	graphicsDevice.Clear(D3D.ClearFlags.Target,
				Color.Blue, 1.0f, 0);
	graphicsDevice.Present();
}



All that the Render method does right now is clear the backbuffer to blue and present it to the graphicsDevice. The parameters to the Clear method are what you want to clear, the color that you want to clear it to, the zdepth and the stencil. All you need to worry about for now are the ClearFlags and the color. The last two parameters I will go into when they are needed.

Attached File(s)



Is This A Good Question/Topic? 0
  • +

Replies To: Using Managed DirectX

#2 SixOfEleven  Icon User is offline

  • using Caffeine;
  • member icon

Reputation: 942
  • View blog
  • Posts: 6,342
  • Joined: 18-October 08

Posted 09 October 2009 - 09:16 PM

I just notice that I left the Run method out of the tutorial.

This is the code for the Run method:

public void Run()
{
	while(this.Created)
	{
		GameLogic();
		Render();
		Application.DoEvents();
	}
}


Was This Post Helpful? 0
  • +
  • -

#3 emersound  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 19-October 11

Posted 19 October 2011 - 10:36 AM

I'm having trouble getting my IDE to locate the Microsoft.DirectX reference I need to use Managed Direct X.

I did a whole bunch of searches on the internet, and I came up with nothing. Essentially I at one point had it working, and now since I've re-installed windows, It doesn't work anymore.

I am running Visual Studio 2010 Professional. I installed the latest Direct-X SDK which is supposed to be packaged with the managed version of Direct-X, June 2010. I checked to see if it was my version of the SDK, by going out to download the October 2005 Managed SDK, but I couldn't find a link to download it anywhere, so I tried installing SP1 for my IDE and that didn't help either.

do you have any information that could help me? for example is there a way to use this approach with XNA? or a place I can get the proper distribution of the SDK, or even some way to get VS 2010 to find the DX9 references?
Was This Post Helpful? 0
  • +
  • -

#4 awgyd9  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 1
  • Joined: 26-October 11

Posted 26 October 2011 - 07:53 PM

View Postemersound, on 19 October 2011 - 10:36 AM, said:

I'm having trouble getting my IDE to locate the Microsoft.DirectX reference I need to use Managed Direct X.

I did a whole bunch of searches on the internet, and I came up with nothing. Essentially I at one point had it working, and now since I've re-installed windows, It doesn't work anymore.

I am running Visual Studio 2010 Professional. I installed the latest Direct-X SDK which is supposed to be packaged with the managed version of Direct-X, June 2010. I checked to see if it was my version of the SDK, by going out to download the October 2005 Managed SDK, but I couldn't find a link to download it anywhere, so I tried installing SP1 for my IDE and that didn't help either.

do you have any information that could help me? for example is there a way to use this approach with XNA? or a place I can get the proper distribution of the SDK, or even some way to get VS 2010 to find the DX9 references?

I had the same problem. When you have the Add Reference window up click the browse tab and navigate to your window Directory e.g. C:\Windows. Then navigate to C:\Windows\Microsoft.NET. there you should find a Directory name DirectX for Managed Code. In that directory you will find all the folders that contain the .dlls your are looking for more specifically the first folder will contain all the .dlls while the other folders will contain updates for Microsoft.DirectX.Direct3D.dll. Hope this helps. Cheers!!

[quote name='awgyd9' date='26 October 2011 - 07:51 PM' timestamp='1319683893' post='1471084']

View Postemersound, on 19 October 2011 - 10:36 AM, said:

I'm having trouble getting my IDE to locate the Microsoft.DirectX reference I need to use Managed Direct X.

I did a whole bunch of searches on the internet, and I came up with nothing. Essentially I at one point had it working, and now since I've re-installed windows, It doesn't work anymore.

I am running Visual Studio 2010 Professional. I installed the latest Direct-X SDK which is supposed to be packaged with the managed version of Direct-X, June 2010. I checked to see if it was my version of the SDK, by going out to download the October 2005 Managed SDK, but I couldn't find a link to download it anywhere, so I tried installing SP1 for my IDE and that didn't help either.

do you have any information that could help me? for example is there a way to use this approach with XNA? or a place I can get the proper distribution of the SDK, or even some way to get VS 2010 to find the DX9 references?

I had the same problem. When you have the Add Reference window up click the browse tab and navigate to your window Directory e.g. C:\Windows. Then navigate to C:\Windows\Microsoft.NET. there you should find a Directory name DirectX for Managed Code. In that directory you will find all the folders that contain the .dlls your are looking for more specifically the first folder will contain all the .dlls while the other folders will contain updates for Microsoft.DirectX.Direct3DX.dll. Hope this helps. Cheers!!
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1