Phoenix, Setting up the DirectX Device

Sunday, April 17 2005

Setting up the DirectX Device,

In this post I am going to go through setting up the main directx device and have it run in 2 modes, this first will be a windowed mode that will be used to debug the application, and the second will be a full screen mode that will be used for the final release. The first step will be to set up the Using directives for the directx classes, for this application I am going to keep all of the directx display device systems under the main form so we will add them there.

Open the project that we have been working on and select the code view for the MainDisplay.cs file. To do this highlight the forms node on the solution explorer right click and select view code, the other way is to yet again highlight the forms node on the explorer and select the view code button on the solution explorer’s toolbar. Now that the references have been added to the project, we need to add the directives for the Microsoft.DirectX assemblies.

Add the below code just below the using directives on the MainDisplay.cs code view.

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

Next, we need to add the following section of code so that we can change the display of the form.
Add this to the section just under the main definition for the MainDisplay Class

public const int ScreenWidth = 800;
public const int ScreenHeight = 600;

private void MainDisplay_Load(object sender, EventArgs e)
{
   // Change the forms border style to none so that we can remove 
   // the title bar and buttons.
   this.FormBorderStyle = FormBorderStyle.None;   

   // Set the forms title so that the text appears in the taskbar at the
   
// bottom of the screen as well as the applications list in the task manager

   this.Text = "Phoenix - Main Display";
   
// Set the Form t be displayed as 800 by 600 in size, This will also be the
   
// size of the full screen device that we are going to be creating...
   
this.Width = ScreenWidth;
   
this.Height = ScreenHeight;

}

The above code will set the forms border style to none so that there will be no min, max, and close buttons displayed as well as removing the title bar on the form. At the same time we will set the forms text property to a descriptive name that will be displayed in the task bar at the bottom of the screen (When in Windowed/Debug mode) and also in the applications list in the task manager. Next we set the screen size that we are going to use for the application. When in Debug mode the application will display as a windows a specific size with no title bar, and when in release mode the application will display in full screen mode with a resolution of 800 by 600. I need to hard code these figures into the application at the moment as several parts of the application need to have a fixed screen size so that they will display correctly, as I go along though I may change this so that the values can be loaded from a configuration file. Just a quick note on the two modes that I am talking about, for this application the system will in the final build run in a full screen mode, and while I am building the application and running it through the debugger the system will run in Windowed mode so it can be easily debugged.

Adding a Way out of the Application.

As we will now be setting up the DirectX device and we really should have a way of exiting the application I will add that now. To do this we will override the OnKeyUp functions of the form, enter the following code. Note that this is only a short term solution as I plan to use other means for the interface.

protected override void OnKeyUp(KeyEventArgs e)
{
     if (e.KeyCode == Keys.Escape) 
          this.Close(); 
     base.OnKeyUp(e);
}

If you run the application now you will see that we have a windows form on the screen that is displayed with out the title bar, and if you press escape the applcation with exit.

On to the DirectX Stuff

The first thing we need to do is to set up the main Device for our DirectX application, we are going to do this by adding the following line of code below where we added the screen dimensions earlier.

private Device device = null; // set up the directX Device

This device will be the main drawing canvas for our application. Next we will need to set up a function that will initialize our graphics hardware and the device that we just created. At the moment I am just going to use a standard setup that I have adapted from ‘Managed DirectX Kick start by Tom Miller’. Over time this section will change as I delve deeper into the hardware, but as this is only supposed to be a simple project to help me and others get started I will keep it simple. One thing that you will notice in the code is the following couple of lines.

#if (DEBUG) 
     presentParams.Windowed = true;
#endif

These few lines will allow the program to run in windowed mode when it is run with in the IDE and in debug mode. You will also see the last few lines will turn off some functions that we do not need as we are just developing in 2D and not 3D. Any way Add the following function to the code.

public void InitializeGraphics()
{
   // Set our presentation parameters 
   PresentParameters presentParams = new PresentParameters();
   presentParams.SwapEffect = SwapEffect.Discard;
   
// Start up full screen 
   
Format current = Manager.Adapters[0].CurrentDisplayMode.Format; 
   if (Manager.CheckDeviceType(0, DeviceType.Hardware, current, current, false)) 
   
{
      // Perfect, this is valid
      
presentParams.Windowed = false
      
// To make it easier to debug the code I am Adding the If Debug statement 
      
// to run the application in windowsed mode.
#if (DEBUG) 
        presentParams.Windowed = true;
#endif 
      presentParams.BackBufferFormat = current; 
      
presentParams.BackBufferWidth = ScreenWidth;
      
presentParams.BackBufferHeight = ScreenHeight; 
      
presentParams.PresentationInterval = PresentInterval.Default; 
      
  /* 
         * presentParams.PresentationInterval = PresentInterval.Default; 
        
         * The driver waits for the vertical retrace period (the runtime 
         * will beam trace to prevent tearing). Device.Present operations 
         * are not affected more frequently than the screen refresh rate; the 
         * runtime completes one Device.Present operation per adapter refresh 
         * period, at most. This option is always available for both windowed 
         * and full-screen swap chains. 
         */ 
   
   else
   
{
      
presentParams.Windowed = true;
   
}
   
// Create our device
   
device = new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, presentParams);
   
// Disable lighting/culling, and zbuffer.
   
this.device.RenderState.Lighting = false;
   
this.device.RenderState.CullMode = Cull.None;
   
this.device.RenderState.ZBufferEnable = false;
}

Now that we have the initialization function in we will need to add a way to call it, to do this I am going to go back to the program.cs file (The main entry point for the application) and change it around a bit. Change the main function to the one below, this will set up the form and then run the initialization function before it hands itself over to the main form.

static void Main()
{
   
Application.EnableVisualStyles();
   
using (MainDisplay frm = new MainDisplay())
   
{
      
// Show our form and initialize our graphics engine
      
frm.Show();
      
frm.InitializeGraphics();
      
Application.Run(frm);
   
}
}

The final section we are going to do is on the MainDisplay.cs code page.

We now have the windows form designed and change, the DirectX device created and configured, now the last thing that we need to do here is to actually do some work to the device and present it to the viewers.

To do this we are going to have to override the OnPaint Event for the windows form. Add the Following code.

protected override void OnPaint(PaintEventArgs e)
{
   
device.Clear(ClearFlags.Target, Color.Black, 1.0f, 0);
   
device.BeginScene();
   
device.EndScene();
   
device.Present();
   
this.Invalidate();
}

And add this line of code to the MainDisplay.cs Constructor, this will assist in removing the flicker of the device as we are processing on it.

this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.Opaque, true);

If you run the application now you will see that a window that is 800x600 will be displayed on the screen, the background will be black and there will be no title bar displayed. You should also see that the Applications Text on the Taskbar should be "Phoenix – Main Display". Just to show yourself what you have done, you can change the color of the screen by changing the device.clear call in the OnPaint Override.

In the next Section I will start to display some sprites on the screen that we will be using for the background, as well as set up some support objects that we can use to assist us with the rest of the project.

kick it on DotNetKicks.com kick it on gamedevkicks.com

Similar Posts

  1. Phoenix, Adding to the Background
  2. Creating a DirectX Device plus more.
  3. Phoenix, Drawing Text on the Screen
Clicky Web Analytics