//
-----------------------------------------------------------------------------
//
File: CreateDevice.cs
//
//
Desc: This is the first tutorial for using Direct3D. In this tutorial, all
//
we are doing is creating a Direct3D device and using it to clear the
//
window.
//
//
Copyright (c) Microsoft Corporation. All rights reserved.
//
-----------------------------------------------------------------------------
using
System;
using
System.Drawing;
using
System.Windows.Forms;
using
Microsoft.DirectX;
using
Microsoft.DirectX.Direct3D;

namespace
DeviceTutorial

{
public
class
CreateDevice : Form

{
//
Our global variables for this project
Device device
=
null
;
//
Our rendering device
public
CreateDevice()

{
//
Set the initial size of our form
this
.ClientSize
=
new
System.Drawing.Size(
400
,
300
);
//
And it's caption
this
.Text
=
"
D3D Tutorial 01: CreateDevice
"
;
}
public
bool
InitializeGraphics()

{
try
{
//
Now let's setup our D3D stuff
PresentParameters presentParams
=
new
PresentParameters();
presentParams.Windowed
=
true
;
presentParams.SwapEffect
=
SwapEffect.Discard;
device
=
new
Device(
0
, DeviceType.Hardware,
this
, CreateFlags.SoftwareVertexProcessing, presentParams);
return
true
;
}
catch
(DirectXException)

{
return
false
;
}
}
private
void
Render()

{
if
(device
==
null
)
return
;

//
Clear the backbuffer to a blue color
device.Clear(ClearFlags.Target, System.Drawing.Color.Blue,
1.0f
,
0
);
//
Begin the scene
device.BeginScene();
//
Rendering of scene objects can happen here
//
End the scene
device.EndScene();
device.Present();
}
protected
override
void
OnPaint(System.Windows.Forms.PaintEventArgs e)

{
this
.Render();
//
Render on painting
}
protected
override
void
OnKeyPress(System.Windows.Forms.KeyPressEventArgs e)

{
if
((
int
)(
byte
)e.KeyChar
==
(
int
)System.Windows.Forms.Keys.Escape)
this
.Close();
//
Esc was pressed
}
/**/
///
<summary>
///
The main entry point for the application.
///
</summary>
static
void
Main()

{

using
(CreateDevice frm
=
new
CreateDevice())

{
if
(
!
frm.InitializeGraphics())
//
Initialize Direct3D
{
MessageBox.Show(
"
Could not initialize Direct3D. This tutorial will exit.
"
);
return
;
}
frm.Show();

//
While the form is still valid, render and process messages
while
(frm.Created)

{
frm.Render();
Application.DoEvents();
}
}
}
}
}
1 creating the Direct3D device
PresentParameters object that is used to set the windows display characteristics. presentParameters 用来设置windows的特征. 她的windowed设为 true 表示以窗口显示,没有菜单和子窗口,但是有最大,最小和关闭button.
2 Device的构造函数中,指定了hardware device是优先的,顶点的处理有软件来处理.但是如果我们有好的显卡且我们设置
CreateFlags.
HardwareVertexProcessing参数,则我们有更好的性能.
3 Application.DoEvents(); 不能没有,否则窗口不接受消息,一直render().
4
Device.
Clear method使窗口颜色被设置为blue.
Device.
BeginScene则真正开始render. the
EndScene and
Present methods 表示完成render.
包含检查硬件的支持:
//------------------------------------------------------------------------------
// Name: dx9cs_initialization.cs
// Author: Kevin Harris
// Last Modified: 06/15/05
// Description: This sample demonstrates how to initialize Direct3D.
//------------------------------------------------------------------------------

using System;
using System.Drawing;
using System.Windows.Forms;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;

namespace DX9Sample


{
public class DX9Form : System.Windows.Forms.Form

{
private Device d3dDevice = null;

public DX9Form()

{
this.ClientSize = new System.Drawing.Size( 640, 480 );
this.Text = "Direct3D (DX9/C#) - Initialization";
this.SetStyle( ControlStyles.AllPaintingInWmPaint | ControlStyles.Opaque, true );
}

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

{
this.Render();
this.Invalidate();
}

protected override void OnKeyDown(System.Windows.Forms.KeyEventArgs e)

{
switch( e.KeyCode )

{
case System.Windows.Forms.Keys.Escape:
this.Dispose();
break;
}
}

protected override void Dispose(bool disposing)

{
base.Dispose( disposing );
}

static void Main()

{
using( DX9Form frm = new DX9Form() )

{
frm.Show();
frm.Init();
Application.Run( frm );
}
}


/**//// <summary>
/// This method basically creates and initialize the Direct3D device and
/// anything else that doens't need to be recreated after a device
/// reset.
/// </summary>
private void Init()

{
// Does the hardware support a 16-bit z-buffer?
if( !Manager.CheckDeviceFormat( Manager.Adapters.Default.Adapter,
DeviceType.Hardware,
Manager.Adapters.Default.CurrentDisplayMode.Format,
Usage.DepthStencil,
ResourceType.Surface,
DepthFormat.D16 ) )

{
// POTENTIAL PROBLEM: We need at least a 16-bit z-buffer!
return;
}

//
// Do we support hardware vertex processing? if so, use it.
// If not, downgrade to software.
//

Caps caps = Manager.GetDeviceCaps( Manager.Adapters.Default.Adapter,
DeviceType.Hardware );
CreateFlags flags;

if( caps.DeviceCaps.SupportsHardwareTransformAndLight )
flags = CreateFlags.HardwareVertexProcessing;
else
flags = CreateFlags.SoftwareVertexProcessing;

//
// Everything checks out - create a simple, windowed device.
//

PresentParameters d3dpp = new PresentParameters();

d3dpp.BackBufferFormat = Format.Unknown;
d3dpp.SwapEffect = SwapEffect.Discard;
d3dpp.Windowed = true;
d3dpp.EnableAutoDepthStencil = true;
d3dpp.AutoDepthStencilFormat = DepthFormat.D16;
d3dpp.PresentationInterval = PresentInterval.Immediate;

d3dDevice = new Device( 0, DeviceType.Hardware, this, flags, d3dpp );

// Register an event-handler for DeviceReset and call it to continue
// our setup.
d3dDevice.DeviceReset += new System.EventHandler( this.OnResetDevice );
OnResetDevice( d3dDevice, null );
}


/**//// <summary>
/// This event-handler is a good place to create and initialize any
/// Direct3D related objects, which may become invalid during a
/// device reset.
/// </summary>
public void OnResetDevice(object sender, EventArgs e)

{
// This sample doens't create anything that requires recreation
// after the DeviceReset event.
}

private void Render()

{
d3dDevice.Clear( ClearFlags.Target | ClearFlags.ZBuffer,
Color.FromArgb(255, 0, 255, 0), 1.0f, 0 );

d3dDevice.BeginScene();
// Render geometry here
d3dDevice.EndScene();

d3dDevice.Present();
}
}
}
