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


    来源:网络

    //-----------------------------------------------------------------------------
    // 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();
    }
    }
    }
    }
    }
  • 相关阅读:
    【Linux】PS命令
    【Linux】多进程与多线程之间的区别
    【杂项】XML Schema和DTD的区别
    推荐一组强大的Collection类
    关于Decorator模式我的理解
    菜鸟白话设计模式系列
    PowerCollections研究: 第1弹竟就发现不少问题
    [白话设计模式] Singleton
    PowerCollection研究:第2枪小谈RemoveALL算法
    yield 关键字
  • 原文地址:https://www.cnblogs.com/hcbin/p/1728108.html
Copyright © 2020-2023  润新知