Long er has nothing to say about this csdn. The translation was gone this afternoon. Fortunately, I have saved some of the papers. Now we can only provide that part to everyone. I hope you will not be surprised. I originally put the screenshot of the Code in the book in this translation, but the picture goes down again, so long still uses the post of my own code.
IDirect3D9 *Direct3DCreate9( UINT SDKVersion
);
The value of sdkversion must be d3d_sdk_version.
This function is easy to use. We only need to pass d3d_sdk_version to the function to determine the DirectX version we want. Once successful, this function returns a pointer to a direct3d object. This pointer runs through the entire application. The code for creating a direct3d object is as follows:
IDirect3D9* g_pD3D = NULL;
if(NULL == (g_pD3D = Direct3DCreate9(D3D_SDK_VERSION)))
return E_FAIL;
Now we have a direct3d application, which can be borrowed to help us complete this application and prepare to draw something. First, create a direct3d device.
Step 3: Create a direct3d Device
In the previous step, we said that direct3d objects (idirect3d9) are the lifeline of direct3d applications. They are basically created at the beginning of the program, destroyed at the end, and used to create other things. In this step, we use idirect3d9 to create the most important thing: direct3d device, which is a COM interface of the idirect3ddevice type. If you want to draw something on the screen, it is the most basic. It represents the computer's graphics card or other drawing hardware. However, it is worth noting that a direct3d device can only represent one video card. If you want to program two cards, you need to create two devices. Each device has a unique ID to distinguish it from other devices. This book focuses on only one device. The main graphics device is connected to the system.
In direct3d, there are two types of devices:
Hal Device
This is the main device type used in this book. It represents the hardware abstraction layer. This device type includes a major 3D accelerator card for playing games.
Reference Device
This is a unique software device that supports all direct3d features. However, this device is slow and is often used by developers to test the features that will be implemented on the hardware. This type is not used in this book.
Create a device
The direct3d device is created by the createdevice function of idirect3d9. This function requires several parameters, including the unique ID, device type, handle, some behavior signs are used to determine how the created device operates. Once successful, this function returns an available idirect3ddevice pointer. The createdevice function syntax is as follows:
HRESULT CreateDevice( UINT Adapter,
D3DDEVTYPE DeviceType,
HWND hFocusWindow,
DWORD BehaviorFlags,
D3DPRESENT_PARAMETERS *pPresentationParameters,
IDirect3DDevice9** ppReturnedDeviceInterface
);
UINT Adapter
Ordinal indicates the video card. This parameter indicates the device to use. This book uses d3dadapter_default to use the main video card.
D3DDEVTYPE DeviceType
The member of the d3ddevtype enumeration, indicating the type of the device to be used. If the type of the device to be used is unavailable, the function will fail. The value of this book is set to d3ddevtype_hal.
HWND hFocusWindow
Window handle to associate with the created device. Idirect3ddevice uses this window as a canvas to draw things. This value will be the handle of the window you created in step 1.
DWORD BehaviorFlags
One or more signs indicate how the device operates. This book uses D3DCREATE_SOFTWARE_VERTEX-PROCESSING. Currently, you do not need to worry too much about the importance of this parameter.
D3DPRESENT_PARAMETERS *pPresentationParameters
This is a pointer to the d3dpresent_parameters struct. It will determine how the device you created operates. With this struct, you can specify other items, screen resolution, whether the application is in full screen or window mode. The struct is as follows:
typedef struct _D3DPRESENT_PARAMETERS_ {
UINT BackBufferWidth, BackBufferHeight;
D3DFORMAT BackBufferFormat;
UINT BackBufferCount;
D3DMULTISAMPLE_TYPE MultiSampleType;
DWORD MultiSampleQuality;
D3DSWAPEFFECT SwapEffect;
HWND hDeviceWindow;
BOOL Windowed;
BOOL EnableAutoDepthStencil;
D3DFORMAT AutoDepthStencilFormat;
DWORD Flags;
UINT FullScreen_RefreshRateInHz;
UINT PresentationInterval;
} D3DPRESENT_PARAMETERS;
Don't worry about not understanding some parameters. Many of them can be left blank. Most of them are explained in the form of a program in this book. For reference, this struct is briefly described.
UINT BackBufferWidth, BackBufferHeight
Specify the width and height of the backend buffer. If your program runs in full screen mode, the two values must match your screen resolution. If your program runs in window mode, the two parameters can be any value. 0 indicates that the size of the backend buffer is the same as that of the window.
D3DFORMAT BackBufferFormat
This is an enumeration type of d3dformat. It specifies the background buffer color format, such as 256 colors, or 16-bit, 24-bit, 32-bit colors. It also defines how RGB and alpha are allocated. There are many possible values. You can also set it to d3dfmt_unknown to indicate that the format is unknown.
UINT BackBufferCount
The number of backend buffers you want. It can be 0 or greater. In most cases, it is set to 1.
D3DMULTISAMPLE_TYPE MultiSampleType
Multiple sampling can achieve vivid results. Such as anti-saw ruler and dynamic blur. Generally, you do not need it, so you can upload it to d3dmultisample_none.
DWORD MultiSampleQuality
Specify the quality. This parameter can be left white when the multisampletype is set to d3dmultisample_none.
D3DSWAPEFFECT SwapEffect
Describes how the backend buffer acts. Basically, it is always set to d3dswapeffect_discard.
HWND hDeviceWindow
Handle of the window to be rendered
BOOL Windowed
The mode in which the program runs. "True" indicates that the program runs in window mode, and "false" indicates that the program runs in full screen mode.
BOOL EnableAutoDepthStencil
Usually set to false. If set to true, DirectX is allowed to manage your concentration cache.
D3DFORMAT AutoDepthStencilFormat
If enableautodepthstencel is set to true, you must specify a format for your concentration cache, like backbufferformat.
DWORD Flags
You can set it to d3dpresentflag_lockable_backbuffer or leave it white.
UINT FullScreen_RefreshRateInHz
Defines the number of screen refreshes per second. Like 75, it means to refresh 75 times per second. However, in window mode, the value must be 0.
UINT PresentationInterval
Specifies the cache rendering speed. In window mode, this value must be d3dpresent_interval_default. You can also use this value in any application.
IDirect3DDevice9** ppReturnedDeviceInterface
The address of the returned idirect3ddevice interface pointer. Once the function is successfully called, an available pointer will be placed here.
An example of creating an idirect3ddevice:
D3DPRESENT_PARAMETERS d3dp;d3dp.AutoDepthStencilFormat = D3DFMT_D24S8;d3dp.BackBufferFormat = D3DFMT_A8R8G8B8;d3dp.BackBufferCount = 1;d3dp.BackBufferHeight = CY;d3dp.BackBufferWidth = CX;d3dp.EnableAutoDepthStencil = TRUE;d3dp.Flags = 0;d3dp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;d3dp.hDeviceWindow = m_hwnd;d3dp.MultiSampleQuality = 0;d3dp.MultiSampleType = D3DMULTISAMPLE_NONE;d3dp.PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT;d3dp.SwapEffect = D3DSWAPEFFECT_DISCARD;d3dp.Windowed = TRUE;hr = pid->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, m_hwnd, D3DCREATE_HARDWARE_VERTEXPROCESSING, &d3dp, &m_device);if (FAILED(hr)){return FALSE;}
Or create a full screen direct3d device. Remember, when you create a full screen device, direct3d automatically enters a full screen mode and changes the computer resolution to the one you set.
The code is similar to the above and only a little different.
D3DPRESENT_PARAMETERS d3dp;d3dp.AutoDepthStencilFormat = D3DFMT_D24S8;d3dp.BackBufferFormat = D3DFMT_A8R8G8B8;d3dp.BackBufferCount = 1;d3dp.BackBufferHeight = CY;d3dp.BackBufferWidth = CX;d3dp.EnableAutoDepthStencil = TRUE;d3dp.Flags = 0;d3dp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;d3dp.hDeviceWindow = m_hwnd;d3dp.MultiSampleQuality = 0;d3dp.MultiSampleType = D3DMULTISAMPLE_NONE;d3dp.PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT;d3dp.SwapEffect = D3DSWAPEFFECT_DISCARD;d3dp.Windowed = FALSE;hr = pid->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, m_hwnd, D3DCREATE_HARDWARE_VERTEXPROCESSING, &d3dp, &m_device);if (FAILED(hr)){return FALSE;}
More about direct3d Devices
You have seen how the direct3d device represents the drawing hardware and understand its importance to the direct3d application. This section provides a simple overview of what idirect3ddevice can do. These will be explained in detail later.
Direct3d devices can do the following or even more:
Rendering a scenario to a window
A direct3d device will be used to draw a frame by frame on the window associated with it. After this chapter, the direct3d device will load a painting from the file and draw it out in the window. We will combine its functions for implementation, which will be explained later.
Inquire about device capabilities
Idirect3ddevice tells us a lot of hardware performance. Use its getdevicecaps function to fill the d3dcaps9 struct. By checking its properties, we can know what our hardware can do and what it cannot do. The function prototype is as follows:
HRESULT GetDeviceCaps( D3DCAPS9 *pCaps
);
Set the Cursor Image
Idirect3ddevice can be used to display the Cursor Image and set its position on the screen. This will appear later. When we use the setcursorposition and setcursorproperties functions.
Create and manage graphical Resources
The direct3d device can create graphical Resources in the memory. Like loading graphics or 3D models from a file.
Imitating 3D environment
Idirect3ddevice provides functions for 3D models to be correctly drawn in a window in 3D space. All objects are drawn based on their distance from the camera. It will also ensure that those close to the camera will cover the distance.
Step 4: Configure message Loops
The window has been created in step 1, step 2 has a direct3d object, and step 3 has a direct3d device. Now, if you compile and run your program, you will find a problem. It does not run at all. But it does run, but soon you don't see it. This is because there is no message pump. Generally, an application enters a message loop. This loop continues until the user exits. During this period, the application receives and sends messages generated by the event. When the user exits, the wm_quit message is generated. The message exits cyclically and the program ends. The Code is as follows:
while (PeekMessage(&msg, 0, 0, 0, PM_REMOVE)){TranslateMessage(&msg);DispatchMessage(&msg);}
This kind of loop may be the standard loop you are currently using. It is very useful for Windows applications because it means that it consumes less time on the processor. After all, it only needs to sit nearby and send messages when an event occurs. This also means that Windows applications do not interfere with each other. They only consume CPU resources when receiving instructions, and do not impede too much. But the game is different.
The game is greedy. They require different message loops, unlike standard loops. On the contrary, it requires a lot of resources for the following reasons:
Graphics
The game needs to ensure how many times their graphics are updated per second to ensure smooth animation. The number of redraws per second is the so-called FPS.
Input
The game requires a precise understanding of when the user presses the key to ensure that the game responds correctly.
Timing
The game requires a very accurate timer, because many events, such as the interface type, need to be synchronized.
The standard message loop of windows can be modified to adapt to the game's needs. The Code is as follows:
while (msg.message != WM_QUIT){if (PeekMessage(&msg, 0, 0, 0, PM_REMOVE)){TranslateMessage(&msg);DispatchMessage(&msg);} else{m_direct.Display();}}
This game repeatedly calls the render () function reserved by my application to process game data and displays the picture to be drawn on the screen. You can also call your own functions here. Next, let's check how the direct3d device presents a scenario in a loop.
The only thing longer can do for everyone is to provide the source code for this chapter, which is written by longer himself. It may not be involved in some areas. Please forgive me.