Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- This is a response to the question "Is using SharpDX or SlimDX easier than using DirectX directly from C++?" posted on gamestackexchange (http://gamedev.stackexchange.com/questions/23109/is-using-sharpdx-or-slimdx-easier-than-using-directx-directly-from-c)
- _____________
- In short, It is much easier if you don't know anything about C++ but still requires almost the same API knowledge in order to fully understand and manage DirectX correctly under C#. Even if tutorials would help a little to bootstrap some users using C# with SlimDX/SharpDX, they would not be enough to cover all the subtleties of the whole DirectX API.
- As said before, in the end, they are just thin wrappers for programmers that want to leverage on this kind granularity while still being able to play with their favorite C# language. When there is a question about DirectX API, I refer to MSDN C++ DirectX documentation or to a C++ DirectX book, and the translation is almost direct into those wrappers. For example, most of the SharpDX Direct2D samples were directly ported from their C++ counterpart from the C++ DirectX SDK without any particular difficulties.
- But more specifically, what does bring C# and a managed DirectX wrapper against a plain C++/DirectX approach?
- - **All methods in managed wrapper are checking the HRESULT for you by throwing an exception when an unexpected error occurs**. If you want to achieve the same level of quality in your C++ code, you will have to wrap every function/method call that return a HRESULT by a small function (often defined as a macro). Lets take a simple example with ID3D11Device::CreateTexture2D
- in C++:
- D3D11_TEXTURE2D_DESC desc = {
- 1024, // Width
- 1024, // Height
- 1, // MipLevels;
- 1, // ArraySize;
- DXGI_FORMAT_R8G8B8A8_UNORM, // Format;
- {1, 0}, // SampleDesc;
- D3D11_USAGE_DEFAULT, // Usage;
- D3D11_BIND_RENDER_TARGET, // UINT BindFlags;
- 0, // UINT CPUAccessFlags;
- 0, // UINT MiscFlags;
- };
- ID3D11Texture2D* texture2D;
- CHECKDX(d3d11device->CreateTexture2D(&desc, NULL, &texture2D));
- in C#:
- var texture2D = new Texture2D(device, new Texture2DDescription() {
- Width = 1024,
- Height = 1024,
- MipLevels = 1,
- ArraySize = 1,
- Format = Format.R8G8B8A8_UNorm,
- SampleDescription = new SampleDescription(1, 0),
- Usage = ResourceUsage.Default,
- BindFlags = BindFlags.None,
- OptionFlags = ResourceOptionFlags.None,
- CpuAccessFlags = CpuAccessFlags.None,
- };
- - Lots of enums in C++ are declared as macros or used as plain integer. If you take the previous example, BindFlags, CPUAccessFlags and MiscFlags fields are declared as UINT in the D3D11_TEXTURE2D_DESC, meaning that it is not possible from the struct to know which values are allowed. **In C# SlimDX/SharpDX, all enums are fully declared and it saves you the time to discover what the values could be**. There are plenty of places like this in C++, where sometimes you don't have any enums and you have to go through the headers or browse msdn documentation.
- - **Also, lots of small additional methods are available from managed DirectX wrappers to simplify your life**. Some of the managed wrapper (Like DirectSound/DirectInput) are even much more elaborate and are adding a substantial amount of layer to make things easiers. For example, some methods in plain C++ DirectX requires that you call them twice, a first time to get the length of the data to receive, in order to know how much to allocate, a second time to pass your allocated buffer: this kind of laborious workflow are handled by managed wrappers.
- - I won't go into all the benefits of using C#/.Net against a C++ (you should know it, but if you don't know C++, you don't know the mess it CAN be), but in short, you have: a great framework (.NET) with almost all you need for collections handling, network, multithreading, await/async...etc, fast compilation round-trip reducing test-and-run development phases, a lightweight template solution called generics in C# (have you ever faced a template error at linking time in C++? It is sometimes just deadly awful), correct and accurate auto-completion (that is sometimes not working at all for C++, because of the complexity of parsing the language itself), running in x86 or x64 without having to recompile your application... etc.
- **So managed DirectX wrappers are thin wrappers that makes your life easier, but they require you to at least dig into the C++ DirectX documentation/books in order get the best from them and understand what's happening**. It means, that if you are not familiar with C++, you just have to understand the C++ API which is a lot easier than having to deal with all the C++ language/compilation infrastructure black hole.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement