A modern C + + library for DirectX programming

Source: Internet
Author: User
Tags object model reference wrapper

I've written a lot of DirectX code, and I've written a lot of articles about DirectX. I even wrote an online training course on DirectX. It's not as hard to understand as some developers say. The learning curve will certainly be there, but once you've passed this hurdle, it's not hard to understand how DirectX works and why it works. But I also admit that the DirectX series API should be more user-friendly.

A few days ago, I decided to set about fixing the flaw. I stayed up all night and wrote a small head file. The next few nights, I extended the line of code to nearly 5,000 lines. My goal is to provide something that makes it easier to build applications with DIRECT2D, and to challenge assertions such as "C + + is difficult" or "DirectX is difficult" to prevail today. I'm not planning on developing a bulky DirectX wrapper. In fact, I decided to use c++11 to make a simpler DirectX API, while not generating any space and time overhead on top of the core DirectX API. You can find this library I developed at the URL below: dx.codeplex.com.

The library itself contains only a header file named Dx.h, and the rest of the source files on CodePlex provide examples of how the header file is used.

In this column, I'll show you how to use this library to make it easier to perform a variety of common DirectX-related activities. In addition, I'll explain how the library is designed to help you understand how c++11 helps improve the ease of use of the traditional COM APIs without resorting to packaging that has a significant performance impact, such as the Microsoft. NET Framework.

Obviously, our focus is on direct2d. DIRECT2D is still the simplest and most efficient way to develop the broadest range of applications and games with DirectX. Many developers seem to have joined the two opposing camps. Among them are DirectX hardcore developers: They are constantly learning the various versions of the DirectX API. They have honed in the development of DirectX over the years and are happy to be part of this "VIP club", which is very high on the threshold (few developers can join the club). Developers in the other camp have heard about DirectX's hard news and don't want to have a bit of a relationship with DirectX. Needless to say, they tend to refuse to use C + +.

I don't belong in either camp. I believe C + + and DirectX need not be so difficult. In last month's column (msdn.microsoft.com/magazine/dn198239), I introduced the DIRECT2D 1.1 and Direct3D and DirectX Graphics Infrastructure (DXGI) code as prerequisites to create a device and Manage the exchange chain. The code uses the D3d11createdevice function to create a Direct3D device that is suitable for GPU or CPU rendering, about 35 lines long. However, with the help of my small head file, I can streamline it to:

Auto device = CreateDevice ();

The CreateDevice function returns a Device1 object. Since all Direct3D definitions are located within the Direct3D namespace, this can be written as well (more explicitly):

Direct3D::D Evice1 device = Direct3d::createdevice ();
The Device1 object is simply a wrapper over the Id3d11device1 COM interface pointer (the Direct3D device interface introduced by the DirectX 11.1 version). The Device1 class derives from the Device class, which is the wrapper over the original Id3d11device interface. It represents a reference and does not incur any additional overhead compared to getting the interface pointer directly itself. Note that Device1 and its parent class Device are regular C + + classes, not interfaces. You can treat them as smart pointers, but that's a little simplistic. Of course, they can handle reference counting and provide the "->" operator to invoke the method you choose directly, but when you start using the many non-virtual methods provided by the Dx.h library, that's when they really shine.

For example: Typically, you might need to Direct3D a device's DXGI interface to pass some other method or function. If you are not afraid of trouble, you can do this:

          Auto device = Direct3d::createdevice ();
Wrl::comptr<idxgidevice2> Dxdevice;
HR (Device->queryinterface (Dxdevice). Getaddressof ()));

This certainly works, but now you must also deal directly with the DXGI device interface. In addition, you need to keep in mind that the IDXGIDevice2 interface is the DirectX 11.1 version of the DXGI device interface. In fact, you can also simply invoke the Asdxgi method:

          Auto device = Direct3d::createdevice ();
Auto Dxdevice = device. Asdxgi ();

The returned Device2 object, which is defined in the Dxgi namespace, wraps the IDXGIDevice2 COM interface pointer, providing its own set of non-virtual methods. To take another example, you may want to use the DirectX object model to access the DXGI factory:

          Auto Device   = Direct3d::createdevice ();
Auto Dxdevice = device. Asdxgi ();
Auto Adapter  = Dxdevice. Getadapter ();
Auto Factory  = adapter. GetParent ();

This is, of course, a common pattern, a means by which the Direct3D Device class provides the Getdxgifactory method as a shortcut:

          Auto D3device = Direct3d::createdevice ();
Auto Dxfactory = D3device. Getdxgifactory ();

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.