Saturday, March 14, 2009

Second managed DirectX tutorial

Today I'm going to show you how to create two Direct3D devices on the same form that work indendantly of each other. This is an easy way to develop a split screen game where you could play multiplayer games where each player has their own view.

To get started, create a new project in Visual C# and add the following references to your project Microsoft.DirectX and Microsoft.DirectX.Direct3D as you did in the last tutorial. Now with Form1 open in design view edit the properties of the size of the form to be: 610, 429. Next drag to panels from the toolbox onto the form. Technically you should give them meaningful names but this is a simple tutorial so you don't have to.

Change the location of panel1 to: 0, 0. Then change the size of panel1 to: 300, 400. Now change the location of panel2 to : 301, 0 and the size to 300, 400. That is all that needs to be done to set up the form for two Direct3D devices.

Like in the last tutorial two using statements have to be added to the code of Form1. They are:

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

Now you need to add two device variables:

pulic Device device1 = null;
pulic Device device2 = null;

Switch back to the design view of Form1 and either double click the title bar of the form or open the properties window and click the Events button, scroll down to the Load event handler and double click that to add the Form1_Load event to the code. If you have the properties window open go ahead on scroll down to the Paint event and double click that now as well to add the Form1_Paint event handler.

Add the following code to the Form1_Load event handler:

InitializeDirectX();

if (device1 == null || device2 == null)
{
    MessageBox.Show("Error creating DirectX device.");
    Application.Exit();
}

After you have added the code to the Form1_Load event handler add this code to the Form1_Paint event handler.

device1.Clear(ClearFlags.Target, Color.Blue, 1.0f, 0);
device1.Present();
device2.Clear(ClearFlags.Target, Color.Red, 1.0f, 0);
device2.Present();

Now all you have to do is add the InitializeDirectX method. It is as follows:

private void InitializeDirectX()
{
    PresentParameters presentParams = new PresentParameters();

    presentParams.Windowed = true;
    presentParams.SwapEffect = SwapEffect.Discard;

    device1 = new Device(0, DeviceType.Hardware,
        panel1,
        CreateFlags.SoftwareVertexProcessing,
        presentParams);
    device2 = new Device(0, DeviceType.Hardware,
        panel2,
        CreateFlags.SoftwareVertexProcessing,
        presentParams);
}

If you build and run your program you should see two panels, one of them blue and the other red. One thing worth mentioning is that you do not have to use the same presentation paramaters when you create your devices so you can have different effects for both panels.

That is all the time that I have today. I will try and post another tutorial tomorrow but it is spring break for the kids here and we have company coming so I might not get around to it. If I can find the time I will try and put the projects for this tutorial on my website for download. Check back soon, hopefully I will have another tutorial ready. What I am planning is showing how to go full screen and be able to press the Escape key to exit the program.

No comments:

Post a Comment