[D3D(C#)] - 创建设备

来源:网络

2010050515481979.jpg

 

 

 
  
// -----------------------------------------------------------------------------
// 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.
// 注释:这是第一个使用D3D的教学例子,在这个例子中,我们要作的仅仅是创建以个D3D“设备”和刷新窗口
// Copyright (c) Microsoft Corporation. All rights reserved.
// -----------------------------------------------------------------------------
// 此项目是C#控制台程序

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


namespace DeviceTutorial
{
public class CreateDevice : Form
{
Device device
= null ; // 我们的绘图设备

public CreateDevice()
{
this .ClientSize = new System.Drawing.Size( 640 , 480 ); // 设置窗体的初始值
this .Text = " D3D Tutorial 01: CreateDevice " ; // 设置窗体标题
}

public bool InitializeGraphics()
{
try
{
// 现在我们设置D3D的一些选项
PresentParameters presentParams = new PresentParameters();
presentParams.Windowed
= true ; // 标志着程序运行时窗口模式
presentParams.SwapEffect = SwapEffect.Discard; // 返回或设置交换区选项
device = new Device( 0 , DeviceType.Hardware, this , CreateFlags.SoftwareVertexProcessing, presentParams); // 创建设备实例
// 函数说明:new Device(物理设备(0:默认设备),设备的类型(这里选择了硬件),创建图形设备的窗体,创建类型,创建实体)
return true ;
}
catch (DirectXException) // 捕捉DX异常
{
return false ;
}
}

private void Render() // 刷新模块
{
if (device == null ) { return ; }

device.Clear(ClearFlags.Target, Color.Blue,
1.0f , 0 ); // 将设备窗口刷成蓝色
// 函数说明:Clear(刷屏的参数这里选的是目标,使用的颜色,深度(可能用于模板),模板(0:没有使用模板))

device.BeginScene();
// 开始渲染场景(因为没有场景所以一下句是空的就直接结束了场景的渲染)

//
// 可以在这里渲染场景
//

device.EndScene();
// 结束场景的渲染
device.Present();
}

protected override void OnPaint(System.Windows.Forms.PaintEventArgs e) // 重写OnPaint方法
{
this .Render(); // 循环的刷新窗口
}

protected override void OnKeyPress(System.Windows.Forms.KeyPressEventArgs e) // 重写OnKeyPress方法
{
if (( int )( byte )e.KeyChar == ( int )System.Windows.Forms.Keys.Escape)
{
this .Close(); // 如果按下了ESC则退出程序
}
}

/// <summary> 程序的主函数,入口点 </summary>
static void Main()
{
// 使用USING语句创建对象保证对象的销毁
using (CreateDevice frm = new CreateDevice())
{
if ( ! frm.InitializeGraphics()) // 初始化
{
MessageBox.Show(
" Could not initialize Direct3D. This tutorial will exit. " );
return ;
}

frm.Show();

// 消息循环
while (frm.Created)
{
frm.Render();
Application.DoEvents();
}
}
}
}
}

 

 

转载于:https://www.cnblogs.com/hcbin/archive/2010/05/05/1728108.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值