问题
I am wrapping the IDirect3D8 class (basically, re-implementing it) and I used DXGI to enumerate adapter modes. There comes the problem.
D3DDISPLAYMODE's Format member requires that it be a member of the D3DFORMAT enumerated type, though IDXGIOutput::GetDisplayModeList returns, obviously, a member of the DXGI_FORMAT enumerated type, which is completely different from D3DFORMAT. I need to link the DXGI_FORMAT members to the D3DFORMAT ones.
My first idea was to write a function which checks the type and redirects it to the proper D3DFORMAT member, accordingly, but this isn't a very nice idea (they're 67 members in D3DFORMAT). Then I came up with the idea that I could have an array whose indices would be the values of the DXGI_FORMAT members and their values would be the corresponding D3DFORMAT members, but I'm not sure - there might be a better way and I'll be wasting time. Is there a better or easier way of doing this?
回答1:
Whether or not you really require this mapping depends on your specific implementation. However, I can't see a better way to map those enumerators. A static array constant is more memory and runtime efficient than std::map
or std::multimap
, because the keys/indices are contiguous and both, keys and values require just (8 - ) 32 bits each.
But initializing the array constant can be more error prone than putting values into a map or an array dynamically.
To avoid erros, annotate the key (with a value equal to the actual array index)...
const D3DFORMAT dxgi_d3d_format_mapping[] = {
/*DXGI_FORMAT_UNKNOWN*/ D3DFMT_UNKNOWN,
/*DXGI_FORMAT_R32G32B32A32_TYPELESS*/ D3DFMT_A32B32G32R32F,
//.
//.
//.
};
... or if available, use the C99 syntax, which should be preferred in this case:
const D3DFORMAT dxgi_d3d_format_mapping[] = {
[DXGI_FORMAT_UNKNOWN] = D3DFMT_UNKNOWN,
[DXGI_FORMAT_R32G32B32A32_TYPELESS] = D3DFMT_A32B32G32R32F,
//.
//.
//.
};
Usage is obvious, but perhaps, index checking wouldn't hurt:
assert( 0 <= dxgi_fmt && dxgi_fmt < (sizeof(dxgi_d3d_format_mapping)/sizeof(D3DFORMAT)) );
D3DFORMAT d3d_fmt = dxgi_d3d_format_mapping[dxgi_fmt];
来源:https://stackoverflow.com/questions/18784796/link-two-enumated-type-members