I've been working on the template that I talked about yesterday. It is coming along very well. I'm trying to make it so that the size of the window or screen can be set when the Direct3D device is created by just changing a few variables in the Program.cs file. Also, I tried to make it so that there are seperate methods for the logic and the rendering methods. So far the template is coming along quite nicely.
I'm going to write a tutorial about how to seperate the logic and rendering methods.
So, to start Visual C# and create a new project. Like you did in the previous tutorials you will have to add the references to Microsoft.DirectX and Microsoft.DirectX.Direct3D. If you missed the first two tutorials you do this by clicking the Project menu item then selecting Add reference item. When the window pops up select the .NET tab scroll down and click the Microsoft.DirectX entry and holding down the Crtl key click the Microsoft.DirectX.Direct3D entry.
Open the code view for the form and add these two using statements.
using Microsoft.DirectX;
using D3D = Microsoft.DirectX.Direct3D;
You might be wondering why I put the D3D = in front of the second using statement. The reason is simple. In managed DirectX Direct3D, DirectSound and DirectInput (which we are not using right now) every thing is a Device. By adding the D3D = you can easily qualify everything so there is an easy way to know what Device you want to use. You will see how this works in a moment.
Right now we are going to modify the Program.cs file. So open the code for that file. You want to change the Main method.
First delete the line:
Application.Run(new Form1());
You will be replacing this with different code. So go ahead and add the following code:
using (Form1 frm = new Form1())
{
    if (!frm.InitializeDirectX())
    {
        MessageBox.Show("Error creating DirectX.");
        return;
    }
    frm.Show();
    frm.Run();
}
What this code does is create a form and disposes of everything when it leaves the code block. First it tries to initialize DirectX. If that fails it reports an error and exits the program. Then we make sure that the form is visible and then call the Run method of Form1 that we will write shortly. Go back to the code view of Form1 and add the following a variable as follows:
private D3D.Device device = null;
Now we will write two methods. The InitializeDirectX and the Run method. They are both fairly simple.
public bool InitializeDirectX()
{
    D3D.PresentParameters pParam =
        new D3D.PresentParameters();
    pParam.Windowed = true;
    pParam.SwapEffect = D3D.SwapEffect.Discard;
    try
    {
        device = new D3D.Device(0, D3D.DeviceType.Hardware,
        D3D.CreateFlags.SoftwareVertexProcessing,
        pParam);
    }
    catch
    {
        return false;
    }
    return true;
}
public void Run()
{
    while (this.Created)
    {
        GameLogic();
        Render();
        Application.DoEvents();
    }
}
The first method should be familiar to you if you have followed the tutorials. The Run method might require a little explaining. First it is a simple loop that runs while the form is open. The this.Created flag is valid while the form is open. Next there are three method calls. GameLogic and Render are methods that we will write. The last tells the program to run the events for the form.
Right now the GameLogic method is just a stub that can be writen later. The Render method is where we will do the rendering. So create two methods as follows:
private void GameLogic()
{
    // TODO: add game logic here
}
private void Render()
{
    device.Clear(D3D.ClearFlags.Target, Color.Blue, 1f, 0);
    device.Present();
}
The Render method should be familiar to you, it is the same code that was used in the previous tutorials. The GameLogic will be written later when you actually start to write games.
So that is all for today. Later I will make the project available for download so check back a little later.
Subscribe to:
Post Comments (Atom)
Thank you for posting this precious article.
ReplyDeleteBut I think you'v missed 'this' when you create a new 'Device'.
Anyway, this is very good for me.