C++ Programmer's Cookbook

{C++ 基础} {C++ 高级} {C#界面,C++核心算法} {设计模式} {C#基础}

Managed directx 检查硬件支持和实现全屏显示

c#代码:
//-----------------------------------------------------------------------------
//           Name: dx9cs_fullscreen.cs
//         Author: Kevin Harris
//  Last Modified: 06/15/05
//    Description: This sample demonstrates how to probe the hardware for 
//                 specific Display or Adaptor Modes and hardware support 
//                 suitable for a full-screen application with 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( 640480 );
            
this.Text = "Direct3D (DX9/C#) - Full Screen";
            
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()
        
{
            
//
            
// For each adapter, examine all of its display modes to see if any 
            
// of them can give us the hardware support we desire.
            
//

            
bool desiredAdapterModeFound = false;

            
// For each Adapter
            foreach( AdapterInformation adapter in Manager.Adapters )
            
{
                
// Examine each display mode available.
                foreach( DisplayMode display in adapter.SupportedDisplayModes )
                
{
                    
// Does this adapter mode support a mode of 640 x 480?
                    if( display.Width != 640 || display.Height != 480 )
                        
continue;

                    
// Does this adapter mode support a 32-bit RGB pixel format?
                    if( display.Format != Format.X8R8G8B8 )
                        
continue;

                    
// Does this adapter mode support a refresh rate of 75 MHz?
                    if( display.RefreshRate != 75 )
                        
continue;

                    
// We found a match!
                    desiredAdapterModeFound = true;
                    
break;
                }

            }


            
if( desiredAdapterModeFound == false )
            
{
                
// TO DO: Handle lack of support for desired adapter mode
                return;
            }


            
//
            
// Here's the manual way of verifying hardware support.
            
//

            
// Can we get a 32-bit back buffer?
            if!Manager.CheckDeviceType( Manager.Adapters.Default.Adapter, 
                                          DeviceType.Hardware,
                                          Format.X8R8G8B8,
                                          Format.X8R8G8B8,
                                          
false) )
            
{
                
// TO DO: Handle lack of support for a 32-bit back buffer
                return;
            }


            
// 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, full-screen device.
            
//

            PresentParameters d3dpp 
= new PresentParameters();

            d3dpp.Windowed               
= false;
            d3dpp.EnableAutoDepthStencil 
= true;
            d3dpp.AutoDepthStencilFormat 
= DepthFormat.D16;
            d3dpp.SwapEffect             
= SwapEffect.Discard;
            d3dpp.BackBufferWidth        
= 640;
            d3dpp.BackBufferHeight       
= 480;
            d3dpp.BackBufferFormat       
= Format.X8R8G8B8;
            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(
25502550), 1.0f0 );

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

            d3dDevice.Present();
        }

    }

}

posted on 2006-06-22 16:08 梦在天涯 阅读(828) 评论(1)  编辑 收藏 引用 所属分类: DirectX

评论

# re: Managed directx 检查硬件支持和实现全屏显示 2010-06-08 17:43 ascu

您好,我拜读完您的文章,有些问题想请教,因为最近正初学DirectX,而且需要用它来做个东西。首先我的问题是您的那个检查硬件支持是检查哪个方面的支持,为什么我运行此程序,他最后返回的是无硬件支持,即desiredAdapterModeFound == false,所以,我只能将其强制设置true,最后可以也可以正常运行,所以我想问检查硬件有什么用。还有一个问题,具体的内容我在百度问了个问题http://zhidao.baidu.com/question/158001103.html麻烦您看一下,我很急需去解决这个问题,先谢谢您了。  回复  更多评论   


只有注册用户登录后才能发表评论。
【推荐】超50万行VC++源码: 大型组态工控、电力仿真CAD与GIS源码库
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


公告

EMail:itech001#126.com

导航

统计

  • 随笔 - 461
  • 文章 - 4
  • 评论 - 746
  • 引用 - 0

常用链接

随笔分类

随笔档案

收藏夹

Blogs

c#(csharp)

C++(cpp)

Enlish

Forums(bbs)

My self

Often go

Useful Webs

Xml/Uml/html

搜索

  •  

积分与排名

  • 积分 - 1665743
  • 排名 - 5

最新评论

阅读排行榜