Start
The first thing about the DX program is to create a DX object, which is equivalent to initializing DirectX.
Lpdirect3d9x g_pd3d = direct3dcreate9 (d3d_sdk_version );
If (null = g_pd3d) return e_faile;
Each program starts from this, and the writing method is the same. The parameter must be d3d_sdk_version.
Create a device
DirectX is a device located on the hardware abstraction layer. Programmers indirectly operate the device by accessing interfaces provided by dx. Devices are roughly divided
1. Hal device. Hardware acceleration is the main device in the hardware abstraction layer.
2. software equipment. When a special effect hardware is not supported, you can use the software method for CPU computing. Therefore, this is called a software device.
3. Refer to the device. CPU is used for computing whether or not hardware is available.
After learning about the device types, you can create a device.
Createdevice (uint adapter,
D3ddevtype devicetype,
Hwnd hfocuswindow,
DWORD behaviorflags,
D3dpresent_parameters * ppresentationparameters,
Idirect3ddevice9 ** ppreturneddeviceinterface
);
The adapter specifies the serial number of the display adapter to indicate which video card is used to create a direct3d object. Obviously, this parameter can be used to differentiate when multiple video cards exist or multiple interfaces exist on the video card. I did not find an API for obtaining this value. I only read the document that says "first retrieve the video card device and then enumerate adpater ". Usually this parameter d3dadapter_default (this is a macro whose value is the serial number of the current adapter ).
Devicetype this is the three types of devices mentioned above. D3ddevtype is an enumeration. d3ddevtype_hal indicates the Hal device.
Hfocuswindow indicates that the window gets the focus.
How the behaviorflags device works. Is a number of details, usually using d3dcreate_software_vertexprocessing, indicating that the vertex is operated by software.
D3dpresent_parameters the structure is complex and important.
Typedef struct _ d3dpresent_parameters _{
Uint backbufferwidth, backbufferheight;
D3dformat backbufferformat;
Uint backbuffercount;
D3dmultisample_type multisampletype;
DWORD multisamplequality;
D3dswapeffect swapeffect;
Hwnd hdevicewindow;
Bool implements wed;
Bool enableautodepthstencel;
D3dformat autodepthstencilformat;
DWORD flags;
Uint fullscreen_refreshrateinhz;
Uint presentationinterval;
} D3dpresent_parameters;
● Backbufferwidth and backbufferheight specify the width and height of the backend buffer window. When the programs are in non-full screen mode, if they are 0, they are the default window size.
● Backbufferformat specifies the background buffer pixel format. The pixel format is not much said.
● Backbuffercount the number of backend buffers. Optional values: 0, 1, 2, and 3. When 0 is used, it is the same as 1, indicating 1 buffer zone. The number of buffers is related to the swap chain, which will be discussed later.
● Multisampletype specifies the number of multiple samples. The more samples, the better the anti-sawtooth effect.
● Multisamplequality the quality of multiple sampling. The value ranges from 0 to 1 and is less than the checkdevicemultisampletype return value. Because the anti-sawtooth effect requires a certain amount of resources, if the number of samples is too large or the quality is too high, the system may not meet the requirements. Therefore, if you need anti-aliasing, it is best to use this API to detect whether it is appropriate.
● Swapeffect: sets the way in which the background cache is copied to the foreground. Note that if it is not set to d3dswapeffect_discard (the cached content in the background is cleared after being copied to the foreground), the Multi-sample value must be 0 (d3dmultisample_none ).
● Hdevicewindow: Specifies the drawing window. The window is activated by default.
● If wed is true, it is displayed in window mode, and vice versa in full screen mode.
● Enableautodepthstencel, autodepthstencilformat, flags, fullscreen_refreshrateinhz?
The createdevice parameter is almost complete. The most complex parameter is the d3dpresent_parameters parameter. However, you can use the default value 0 many times. Therefore, the common createdevice is written like this.
D3dpresent_parameters d3dpp;
Zeromemory (& d3dpp, sizeof (d3dpp ));
D3dpp. cmdwed = true; // window mode
D3dpp. swapeffect = d3dswapeffect_discard;
D3dpp. backbufferformat = d3dfmt_unknown;
// Indicates that the pixel mode is the same as that of the adapter
Lpdirect3ddevice9 g_pd3ddevice = NULL;
If (failed (g_pd3d-> createdevice (d3dadapter_default, d3ddevtype_hal, hwnd,
D3dcreate_software_vertexprocessing,
& D3dpp, & g_pd3ddevice )))
Return e_fail;
Basic Structure of direct3d applications
The basic structure of direct3d applications is divided:
1. initialize direct3d
2. Rendering graphics
3. If the program does not exit, Goto 2
3. End direct3d
The above createdevice section is roughly initialized by direct3d, followed by rendering graphics
Void render ()
{
// Clear the background Buffer
G_pd3ddevice-> clear (0, null, d3dclear_target, d3dcolor_xrgb (0, 0, 255), 1.0f, 0 );
If (succeeded (g_pd3ddevice-> beginscene ()))
{
// Drawing the background Buffer
......
// Harness background buffer Rendering
G_pd3ddevice-> endscene ();
}
G_pd3ddevice-> present (null, null );
}
From this code, we can see that DX graphics are drawn in the background buffer, and the code is located between ininscene and endscene.
Clearing the background buffer is also an interesting function. The first and second parameters represent the cleared area. When it is set to 0 and null, the entire window is cleared. The third parameter is a combination of d3dclear enumerated values: d3dclear_target, d3dclear_zbuffer, and d3dclear_stencer. Target is the color of the surface pixels, zbuffer is the depth buffer, and stencer represents the mask. For example, in a racing game, the scenery outside the car window changes, while the car is generally unchanged, therefore, you can set a mask so that each refresh only changes the area outside the window. The fourth, fifth, and sixth parameters correspond to the above three enumerated values respectively.
When it comes to rendering, an important concept is surface and surface management. The surface is like a bitmap stored in the memory (Display memory). The front-end buffer is a piece of memory transmitted by the video card to the display. It cannot be written by DX; the back-end buffer, it is the place where DX really draws. Flipping surfaces refers to the process of submitting the background to the foreground as a surface flip; switching chain, a circular linked list composed of the background and the foreground surface pointer, in order to quickly and orderly submit the background surface to the foreground without having to allocate new memory (the original foreground surface pointer is placed behind the background buffer surface linked list, which is equivalent to applying the foreground surface to the background surface)