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)
-
MahangedDirectX.zip (52.93K)
Number of downloads: 1083





MultiQuote




|