|
|
|
|
|
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();
When you try to compile your program now, you’ll probably get some errors like ‘The type or namespace name 'DirectX' does not exist in the namespace’. Make sure you add references to Microsoft.DirectX and Microsoft.Direct3D by clicking Project -> Add References to correct these errors. If you have multiple versions installed of these references, make sure you select references of the same version. If these 2 references aren’t included in your list, check if you have installed the full (free) Microsoft DirectX SDK. If they’re still missing, check out the first paragraphs of ‘Linking to the Device’ in the C++ part of this tutorial.
Running this code 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!

Click here to go to the forum on this chapter!
Or click on one of the topics on this chapter to go there: LOADERLOCK Thought this might help beginners (like me, heh):
...InitializeDevice() problem in source code InitializeDevice() is underlining w...BadImageFormatException I really want to thank you for this tutorial its r...Red X When I ran the application all I saw was a red X a...Specify part of a form for the device? Howdy,
I would find it useful to be able to...stem.MissingFieldException On step 2 of your tutorial I get the following err...Color command not recognized in the line
device.Clear(ClearFlags.Tar...error@onpaint method I'm getting this error message when I insert the ...low FPS without Firefox Hi,
I have a strange problem. While is Firefox ru...prob with 2nd step of dx tutorial hi,
i have an exception when i run the second st...Form or any of Forms? Can the DX device be linked to any of System.Windo...DirectX and Menu Bar Does anyone know how I can create a menu bar with ...Device Creation Problem Hi,
Your approach is just fantastic. Clearly ai...Dispose() function? Hi! Thanx for this very fine tutorial! So fa...Error exception What is error exception : Microsoft.DirectX.Direct...Direct3D.dll problem?!? I did the 2nd step of the DirectX C# Tutorial.
an...Getting an error Hi , im not good at programming but trying anyway ...device = null Hi,
first of all congrats to your absolute fantas...This error appears : when using Windowed The line below causes an error:
Microsoft.DirectX...Device Not Found I got the following Error
The type or namespace...
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); } } } }
- Website design & XNA + DirectX code : Riemer Grootjans - ©2003 - 2008 Riemer Grootjans
|
|
|
|
|