4.6 Debugging Direct3D Applications

4.6 Debugging Direct3D Applications

In order to shorten the code and minimize distractions, we omit most error handling in this book. However, we do implement a macro to check the HRESULT return codes returned by many Direct3D functions. Our macro is defined as follows in d3dUtil.h:

 

 
  
1 #if defined(DEBUG) | defined(_DEBUG)
2 #ifndef HR
3 #define HR(x) \
4 { \
5 HRESULT hr = (x); \
6 if (FAILED(hr)) \
7 { \
8 DXTrace(__FILE__, (DWORD)__LINE__, hr, L#x, true ); \
9 } \
10 }
11 #endif
12   #else
13 #ifndef HR
14 #define HR(x) (x)
15 #endif
16   #endif

  

If the returned function’s return code indicates failure, then we pass the return code into the DXTrace function ( #include <dxerr.h>) and link dxerr.lib:
 
    
HRESULT WINAPI DXTraceW( const char * strFile, DWORD dwLine,
HRESULT hr,
const WCHAR * strMsg, BOOL bPopMsgBox);

 

This function displays a nice message box indicating the file and line number where the error occurred, as well as a textual description of the error and the name of the function that generated the error; Figure 4.10 shows an example. Note that if you specify false for the last parameter of DXTrace, then instead of a message box, the debug info will be output to the Visual C++ output window. Observe that the macro HR does nothing if we are not in debug mode. Also, HR must be a macro and not a function; otherwise __FILE__ and __LINE__ would refer to the file and line of the function implementation instead of the file and line where the function HR was called.
 
Figure 4.10: The message box displayed by the DXTrace function if a Direct3D function returns an error.

Now we just use this macro to surround a Direct3D function that returns an HRESULT as this example shows:  

HR(D3DX10CreateShaderResourceViewFromFile(md3dDevice,
      L"grass.dds", 0, 0, &mGrassTexRV, 0 ));

This works well for debugging our demos, but a real application should handle errors more robustly.

Note The L#x turns the HR macro’s argument token into a Unicode string. In this way, we can output to the message box the function call that caused the error.
 

转载于:https://www.cnblogs.com/seanyou/archive/2010/09/20/1831427.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值