Visual C #'s DirectX Development Series 1 Introduction to DirectX

  • Tags : directx
  • time :
Introduction to DirectX Development Series in Visual C #

How to view the version of DirectX on this computer:

Click "Start" - "Run", enter "dxdiag" in "Run" and enter "Enter" to pop up the DirectX diagnostic tool window. There is a lot of system information on the homepage, and the bottom one is the DirectX version. As shown in the figure:



2. Add a reference to the DirectX class library:

Create a new WinForm application and add three References, which are Microsoft DirectX, Microsoft DirectX Direct3D and Microsoft DirectX Direct3DX, as shown in the figure:





3. First DirectX program:

Firstly, let's introduce the Device class. The Device class is used to complete all drawing operations in DirectX. We can imagine this class as a graphics card, where all other graphic objects in the scene depend on the Device. There can be multiple Device
objects in the computer. So define a drawing device in the global variable, as follows:

Device device = null;//define drawing equipment 

Next, define an initialization function: InitializeDirect3D (), which will tell the drawing device how to render to the screen and handle some exceptions, as shown in the following code:
 public bool InitializeDirect3D()
  {
      try
      {
          PresentParameters presentParams = new PresentParameters();
          presentParams.Windowed = true; //specify to Windows form display 
          presentParams.SwapEffect = SwapEffect.Discard; //after the current screen is drawn, it will be automatically deleted from memory 
          device = new Device(0, DeviceType.Hardware, this,
          CreateFlags.SoftwareVertexProcessing, presentParams); //instantiation device object 
          return true;
      }
      catch (DirectXException e)
      {
          MessageBox.Show(e.ToString(), "Error"); //handling exceptions 
          return false;
      }
  }
Next, we need to define the rendering function Render(). The code is as follows:
   public void Render()
  {
      if (device == null) //if device if empty, do not render 
      {
          return;
      }
      device.Clear(ClearFlags.Target, Color.Red, 1.0f, 0); //clean up windows the interface is red 
      device.BeginScene();
      //add rendering graphic code here 
      device.EndScene();
      device.Present();
  }


Finally, define the program entry, and the code is as follows:

 static void Main()
  {
      Form1 frm1 = new Form1(); //create form object 
      if (frm1.InitializeDirect3D() == false) //inspect Direct3D start or not 
      {
          MessageBox.Show("unable to start Direct3D!", "error !");
          return;
      }
      frm1.Show(); //if everything is initialized successfully, display the form 
      while (frm1.Created) //set up a loop to update rendering status in real-time 
      {
          frm1.Render(); //keep device rendering until the program ends 
          Application.DoEvents(); //handle input events such as keyboard and mouse 
      }
  }
PS: After completing, I encountered an error while running, as shown in the following message:


.

Originally, it was because of When using the Dirext3D managed library in NET 4.0, it appears that "the mixed mode assembly is generated for the runtime of version" v1.1.4322 "and cannot be loaded in the 4.0 runtime without configuring other information." Exception information, view assembly Microsoft DirectX.dll, Microsoft. DirectX. Direct3D. DLL, and Microsoft. DirectX. Direct3DX were found to have a. NET runtime version v1.1.4322, which means that the D3D managed library is located Compiled and generated under NET v1.1 version. In Before NET4.0, due to the inherent nature of the program runtime environment NET 2.0, and. NET 2.0 is compatible with. NET 1.0 and 1.1. However, when upgrading to. NET 4.0, the. NET kernel underwent significant adjustments, as previously done The assembly generated in Net2.0 or. net3.5, if running under. net4.0, needs to specify the common language runtime version and enable supported by this application in the configuration file NET Framework 2.0 runtime activation policy, modify the content of App. config at this time, and the code is as follows:

<?xml version="1.0"?>
<configuration>
<startup useLegacyV2RuntimeActivationPolicy="true">
<supportedRuntime version="v4.0"/>
</startup>
</configuration>


There may also be an error like this, as shown in the figure:


.

As for this issue, the error message is already very clear. You can create a new 32-bit application (i.e. x86) in the Configuration Manager.