Please click on the link below to go to my new pages:

http://www.riemers.net

See you there!!

Set up
Contact
  • Overview 
  • Device
  • Vertices
  • Camera
  • Rotation
  • Indices
  • Terrain
  • Landscape
  • Keyboard
  • - Website design & DirectX code : Riemer Grootjans -


    - Initializing the Device

    In this chapter, we are going to create the device. In short, a device is a direct link to your graphical adapter. It is an object that gives you direct access to the piece of hardware inside your computer.

    To start, you have to declare the device as a variable within your WinForm-class. Just add this line as the first line in your class :

      private Device device;

    Now, we are going to create a method InitializeDevice()  to activate the device. Here's the code, I'll explain it afterwards:

    public void InitializeDevice()

            {

                PresentParameters presentParams = new PresentParameters();

                presentParams.Windowed = true;

                presentParams.SwapEffect = SwapEffect.Discard;

                device = new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, presentParams);

            }

    The first line creates the Presentation Parameters, which we will need to tell the device how to behave. We will tell the device that we don't want a fullscreen application, and to discard the Swapeffect, so you write to the device immediately, and not to an extra back buffer that will be presented (= swapped) at runtime. Then, the device is created. The 0 selects the first graphical adapter in your PC. We want to render the graphics using the hardware. If you don't have a hardware card, you can use DeviceType.Reference, which supports all possible features, but is a lot slower. Next we bind 'this' window to the device and we for now we want all 'vertex processing' to happen on the CPU. More on vertices in the next chapter. Finally we pass our presentParams, and our device has been created!

    Of course, we have to call this method, so add this line to your Main method :

      our_dx_form.InitializeDevice();

    Running the program will still give you an empty form, but in the background the device has been initialized! Next we are going to overwrite the OnPaint method, so we can control what to draw on the screen. Add the following code below the InitializeDevice method:

                protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)

                {

                      device.Clear(ClearFlags.Target, Color.DarkSlateBlue , 1.0f, 0);

                      device.Present();

                }

    This method will be called every time something is drawn to the screen. The Clear method will fill the window with a solid color, darkslateblue in our case. The ClearFlags indicate what we actually want to clear, in our case the target window. To actually update our display, we have to Present the updates to the device. Running this code will give you a blueish window. For even more stunning results, read on!

     

    Here is the complete code:

    using System;

    using System.Drawing;

    using System.Collections;

    using System.ComponentModel;

    using System.Windows.Forms;

    using System.Data;

    using Microsoft.DirectX;

    using Microsoft.DirectX.Direct3D;

    namespace DirectX_Tutorial

    {

         public class WinForm : System.Windows.Forms.Form

         {

             private Device device;

             private System.ComponentModel.Container components = null;

             public WinForm()

             {

                  InitializeComponent();

             }

             public void InitializeDevice()

             {

                  PresentParameters presentParams = new PresentParameters();

                  presentParams.Windowed = true;

                  presentParams.SwapEffect = SwapEffect.Discard;

                  device = new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, presentParams);

             }

     

             protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)

             {

                  device.Clear(ClearFlags.Target, Color.DarkSlateBlue , 1.0f, 0);

                  device.Present();

             }

             protected override void Dispose (bool disposing)

             {

                  if (disposing)

                  {

                       if (components != null)

                       {

                            components.Dispose();

                       }

                  }

                  base.Dispose(disposing);

             }

             private void InitializeComponent()

             {

                  this.components = new System.ComponentModel.Container();

                  this.Size = new System.Drawing.Size(500,500);

                  this.Text = "DirectX Tutotial";

             }

        

             static void Main()

             {

                  using (WinForm our_dx_form = new WinForm())

                  {

                       our_dx_form.InitializeDevice();

                       Application.Run(our_dx_form);

                  }

             }

         }

    }