| Language: | C++ |
| Category: | Multimedia |
| Date: | 2009-03-01 |
| Author: | Network Security Developer |
First, an instance of the IDirect3D9 interface is created using the Direct3DCreate9 function and the result is saved in the ppiD3D9 variable:
if ((*ppiD3D9 = Direct3DCreate9(D3D_SDK_VERSION)) == NULL) return E_FAIL;
This interface is used for creating Direct3D objects and for setting up the environment. The only parameter passed to the Direct3DCreate9 function is the D3D_SDK_VERSION constant. This constant is defined in the d3d9.h header file; its value is increased with each new version of the DirectX SDK. The value of this constant for my DirectX SDK is 31. DirectX uses this constant to verify that the correct header files are used. If the versions do not match, the function will fail. Then the correct header file must be used and the program must be recompiled.
Next, the d3dpp structure of the D3DPRESENT_PARAMETERS type, which describes the presentation parameters, is defined. Before the structure is used, it is filled with zeros so that the default values will be used for the fields whose values are not specified directly.
Right after the d3dpp structure is zeroed out, the properties that will be the same for the full screen and window mode – the width and the height – are set as follows:
d3dpp.BackBufferWidth = iWidth; d3dpp.BackBufferHeight = iHeight;
Here the width and the height of the buffer are specified. Next, a MessageBox is used to ask the user whether the program should be run in the full-screen mode. If the user clicks Yes, the structure is filled with the full-screen parameters:
d3dpp.BackBufferFormat = D3DFMT_R5G6B5; d3dpp.SwapEffect = D3DSWAPEFFECT_FLIP; d3dpp.Windowed = FALSE; // Use windowed mode d3dpp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT; d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT;
The pixel format is selected as D3DFMT_R5G6B5, with 5 of the 16 color bits used for red, 6 for green and 5 for blue. The 16 bits makes 65 535 colors available, which is enough for creating realistic effects yet maintaining performance and memory savings.
Instead of 16 bits, 24 bits could be used, with each color part specified by 8 bits. This would make it easier to work with colors, because machine instructions do not handle bits well; they are more suitable for byte operations. On the other hand, this would slow the graphic output. It would also be possible to use 8-bit color, which would give even higher operation speed then 16-bit color. But this would make only 256 colors available, which if far from enough for decent graphic.
The most important thing is setting the screen refresh rate to D3DPRESENT_RATE_DEFAULT . For CRT displays, the minimum flicker-free screen refresh rate is 85Hz don't specify this value, because for most of LCD monitors the maximum refresh rate is 60Hz, which is acceptable for the eye. It is better to specify the default refresh frequency or determine the frequencies supported be the monitor and offer the user the choice of the optimal frequency.
For the window mode, the window dimensions must be changed to correspond to the selected values. This is done be determining the current position of the window and then using the MoveWindow Windows API (WinAPI)
function to set the new dimensions:RECT wndRect; RECT clientRect; GetWindowRect(hWnd, &wndRect); GetClientRect(hWnd, &clientRect); iWidth = iWidth + (wndRect.right-wndRect.left) - (clientRect.right-clientRect.left); iHeight = iHeight + (wndRect.bottom-wndRect.top) - (clientRect.bottom-clientRect.top); MoveWindow(hWnd, wndRect.left, wndRect.top, iWidth, iHeight, TRUE);
Note that the window size is adjusted to account for the client area. This is because the MoveWindow function sets the size of the overall window and the client area is just a part of it. For the client area to be 800x600, the dimensions of the window frame, caption and menu size must be added to the desired dimensions of the client area.
The only problem that is not taken into account in this code is a window that exceeds the screen dimensions. If this happens, DirectX may have problems.
Because the window mode will be used, you cannot choose any pixel format. You use current Windows pixel format. This format is determined be using the GetAdapterDisplayMode method of the IDirect3D9 interface. After execution the GetAdapterDisplayMode method, the current pixel format can be gotten from the Format property of the D3DDISPLAYMODE structure. The following code demonstrates calling the GetAdapterDisplayMode method:
(*ppiD3D9)->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &d3ddm);
Now the view parameters for the window mode can be set as shown in the following code:
d3dpp.BackBufferFormat = d3ddm.Format;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.Windowed = TRUE;
The preceding code specifies the pixel format, the buffer swap effect, and the window mode should be used. For window applications, the D3DSWAPEFFECT_DISCARD buffer swap effect is optimal. However, you should keep it in mind that when this mode is used the back buffer is discarded.
The code that follows setting the view parameters is the same for both modes. Such a large view structure was developed to create a Direct3D device. At this moment, additional buffers are created and, if the full-screen mode was selected, the view mode is switched according to the selected parameters.
A Direct3D device is created using the CreateDevice method of the IDirect3D interface, which was initialized at the beginning of the code.
The following code shows calling the method in the DX3DInit function:
HRESULT hRes; if(FAILED(hRes = (*ppiD3D9)->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, Flags, &d3dpp, ppiD3DDevice9))) return hRes;
After the device is created, you are ready to work; however, it is desirable to configure the camera parameters. This is done by calling D3DXMatrixPerspectiveFovLH. The next picture shows the parameters of the functions:
The camera is located at point P; this is the point, from which we view the world. Everything before the ZN plane or beyond the ZF plane does not fall into the camera view. The angle between two view lines is the value of the field of view in the y direction (fovy)
.The IDirect3DDevice has to set to the created matrix. This is done using the SetTransform function as follows:
(*ppiD3DDevice9)->SetTransform(D3DTS_PROJECTION, &matProjection);
Now the initialization function is complete. Run the program to make sure that it works. You will only be able to switch into the full-screen mode and set the window dimensions. In all other respects, the function looks like any other application.
Direct3D interfaces are released using the Release method. The method should be called first, and then the variable should be zeroed out. Resouces must be released in the reverse order of that in which they were created.
Sergey : ExcellentInteresting but both parts of the article might be in one