当前位置:网站首页 > 技术博客 > 正文

wpf编程



 1 using System;  2 using SlimDX;  3 using SlimDX.Direct3D9;  4 using System.Windows.Interop;  5 using System.Windows.Media;  6  7 public class DX  8  {  9 private enum DirectXStatus  10  {  11  Available,  12  Unavailable_RemoteSession,  13  Unavailable_LowTier,  14  Unavailable_MissingDirectX,  15  Unavailable_Unknown  16  };  17  18 public static Device Device { get; private set; }  19 public static bool Available { get { return DX.Device != null; } }// = false;  20  21 private static DX _dx;  22 private static DirectXStatus _status = DirectXStatus.Unavailable_Unknown;  23 private static string _statusMessage = "";  24  25 [System.Runtime.InteropServices.DllImport("user32")]  26 private static extern int GetSystemMetrics(int smIndex);  27 private const int SM_REMOTESESSION = 0x1000;  28  29 // device settings  30 private const Format _adapterFormat = Format.X8R8G8B8;  31 private const Format _backbufferFormat = Format.A8R8G8B8;  32 private const Format _depthStencilFormat = Format.D16;  33 private static CreateFlags _createFlags = CreateFlags.Multithreaded | CreateFlags.FpuPreserve;  34  35 private Direct3D _d3d;  36  37  38 private DX()  39  {  40  initD3D();  41 if (_d3d != null)  42  initDevice();  43 //if (!DX.Available)  44 // MessageBox.Show("DirectX硬件加速不可用! " + _statusMessage, "", MessageBoxButton.OK, MessageBoxImage.Warning);  45  }  46  47 ~DX()  48  {  49 if (DX.Device != null)  50 if (!DX.Device.Disposed)  51  DX.Device.Dispose();  52 if (_d3d != null)  53 if (!_d3d.Disposed)  54  _d3d.Dispose();  55  }  56  57 public static void Init()  58  {  59 if (_dx == null)  60 _dx = new DX();  61  }  62  63 private void initD3D()  64  {  65 if (_d3d != null)  66 return;  67  68 _status = DirectXStatus.Unavailable_Unknown;  69  70 //// assume that we can't run at all under terminal services  71 if (GetSystemMetrics(SM_REMOTESESSION) != 0)  72  {  73 _status = DirectXStatus.Unavailable_RemoteSession;  74 return;  75  }  76  77 int renderingTier = (RenderCapability.Tier >> 16);  78 if (renderingTier < 2)  79  {  80 _status = DirectXStatus.Unavailable_LowTier;  81 _statusMessage = "low tier";  82 return;//注意:发现某些集成显卡,在这里出去!!  83  }  84  85 try  86  {  87 _d3d = new Direct3DEx();  88  }  89 catch  90  {  91 try  92  {  93 _d3d = new Direct3D();  94  }  95 catch (Direct3DX9NotFoundException dfe)  96  {  97 _status = DirectXStatus.Unavailable_MissingDirectX;  98 _statusMessage = "Direct3DX9 Not Found " + dfe.Message;  99 return; 100  } 101 catch (Exception e) 102  { 103 _status = DirectXStatus.Unavailable_Unknown; 104 _statusMessage = e.Message; 105 return; 106  } 107  } 108 109 bool ok; 110  Result result; 111 112 ok = _d3d.CheckDeviceType(0, DeviceType.Hardware, _adapterFormat, _backbufferFormat, true, out result); 113 if (!ok) 114  { 115 //Debug.WriteLine("* failed to CheckDeviceType"); 116 //MessageBox.Show("Failed to CheckDeviceType"); 117 return; 118  } 119 120 ok = _d3d.CheckDepthStencilMatch(0, DeviceType.Hardware, _adapterFormat, _backbufferFormat, _depthStencilFormat, out result); 121 if (!ok) 122  { 123 //Debug.WriteLine("* failed to CheckDepthStencilMatch"); 124 _statusMessage = "Failed to CheckDepthStencilMatch"; 125 return; 126  } 127 128 Capabilities deviceCaps = _d3d.GetDeviceCaps(0, DeviceType.Hardware); 129 if ((deviceCaps.DeviceCaps & DeviceCaps.HWTransformAndLight) != 0) 130 _createFlags |= CreateFlags.HardwareVertexProcessing; 131 else 132 _createFlags |= CreateFlags.SoftwareVertexProcessing; 133 134 _status = DirectXStatus.Available; 135  } 136 137 private void initDevice() 138  { 139 if (_status != DirectXStatus.Available) 140 return; 141 142 HwndSource hwnd = new HwndSource(0, 0, 0, 0, 0, 0, 0, "SlimDX_Wnd", IntPtr.Zero); 143 PresentParameters pp = new PresentParameters(); 144 //pp.SwapEffect = SwapEffect.Copy; 145 //pp.DeviceWindowHandle = hwnd.Handle; 146 pp.Windowed = true; 147 pp.PresentFlags = PresentFlags.Video; 148 pp.SwapEffect = SwapEffect.Discard; 149 //pp.BackBufferCount = 1; 150 //pp.BackBufferWidth = 320; 151 //pp.BackBufferHeight = 240; 152 //pp.BackBufferFormat = _backbufferFormat; 153 //pp.AutoDepthStencilFormat = _depthStencilFormat; 154 try 155  { 156 DeviceType deviceType = DeviceType.Hardware; 157 if (_d3d is Direct3DEx) 158 DX.Device = new DeviceEx((Direct3DEx)_d3d, 0, deviceType, hwnd.Handle, _createFlags, pp); 159 else 160 DX.Device = new Device(_d3d, 0, deviceType, hwnd.Handle, _createFlags, pp); 161  } 162 catch (Exception ex) 163  { 164 //Debug.WriteLine("Exception in Direct3DReset " + ex.StackTrace); 165 //Debug.WriteLine("Exception in Direct3DReset " + ex.Message); 166  } 167  } 168 }

版权声明


相关文章:

  • sql游标的使用方法代码2025-01-05 12:30:04
  • i2c总线的基本操作2025-01-05 12:30:04
  • 分离涡模拟DES2025-01-05 12:30:04
  • mq消息中间件有哪些2025-01-05 12:30:04
  • xdisplay不能全屏2025-01-05 12:30:04
  • crc16校验算法ccitt2025-01-05 12:30:04
  • js输出helloworld2025-01-05 12:30:04
  • python怎么调用py文件2025-01-05 12:30:04
  • cha是什么版本2025-01-05 12:30:04
  • 完全二叉树是什么2025-01-05 12:30:04