Recently, I have been dealing with C # stuff. I haven't written C ++ code for a long time. I'm afraid that my hands will forget it. I plan to write c ++ code. What should I do? I want to come and think about it. I have been familiar with OpenGL in my previous work, so let me learn about d3d. I thought there would be a lot of Chinese entry-level documents for d3d, and I didn't find any suitable documents after looking for a long time. Ah, you can just learn it.
I don't want to go to the next large DirectX installation package, so first of all, of course, I have to find the Lib library and header file. I found it on the gameres Game Development Resources Network, it's only 3.2 MB, huh, small enough. The address is as follows:
Directx9.0c SDK Library
After downloading and decompressing the package, if you decompress the package to D:/codelib/dx9csdk, add the INC and Lib directories to the search path of the VC.
In vs2005, select Tools-Option-projects and solutions-VC ++ directories.
Then, you can start writing code.
We use WIN32API to create a window. to reuse the code, we use a kwindow class introduced in my blog to create a window. For details, see:
A fully object-oriented window class based on Win32
We create a new class named kd3dwindow, inherited from kwindow.
The UML diagram is as follows:
For more information about kwindow, see the previous article on my blog.
The content of kd3dwindow. H is as follows:
- # Include "kwindow. H"
- # Include <d3d8. h>
- # Pragma comment (Lib, "d3d8. lib") // link library
- Class kd3dwindow: Public kwindow
- {
- PRIVATE:
- Void onkeydown (wparam, lparam );
- Void ondraw (HDC );
- Void getwndclassex (wndclassex & WC );
- // D3d related
- Void render ();
- Void cleanup ();
- Hresult initd3d (hwnd );
- Public:
- Kd3dwindow ();
- ~ Kd3dwindow ();
- Virtual bool createex (DWORD dwexstyle,
- Lpctstr lpszclass, lpctstr lpszname, DWORD dwstyle,
- Int X, int y, int nwidth, int nheight, hwnd hparent,
- Hmenu, hinstance hinst );
- PRIVATE:
- Lpdirect3d8 g_pd3d; // used to create a d3d Device
- Lpdirect3ddevice8 g_pd3ddevice; // d3d Device
- };
The content of kd3dwindow. cpp is as follows:
- // Kd3dwindow. cpp: Implementation of the kd3dwindow class.
- //
- //////////////////////////////////////// //////////////////////////////
- # Include "stdafx. H"
- # Include "kd3dwindow. H"
- //////////////////////////////////////// //////////////////////////////
- // Construction/destruction
- //////////////////////////////////////// //////////////////////////////
- # Include <windows. h>
- # Include <assert. h>
- # Include <tchar. h>
- # Include "kwindow. H"
- Const tchar szhint [] = _ T ("Press ESC to quit .");
- Void kd3dwindow: getwndclassex (wndclassex & WC)
- {
- Memset (& WC, 0, sizeof (WC ));
- WC. cbsize = sizeof (wndclassex );
- WC. Style = 0;
- WC. lpfnwndproc = windowproc;
- WC. cbclsextra = 0;
- WC. cbwndextra = 0;
- WC. hinstance = NULL;
- WC. hicon = NULL;
- WC. hcursor = loadcursor (null, idc_arrow );
- WC. hbrbackground = (hbrush) getstockobject (white_brush );
- WC. lpszmenuname = NULL;
- WC. lpszclassname = NULL;
- WC. hiconsm = NULL;
- }
- Void kd3dwindow: ondraw (HDC)
- {
- // Use the d3d device for rendering.
- Render ();
- // Use Windows API to draw images on the screen
- Textout (HDC, 0, 0, szhint, lstrlen (szhint ));
- }
- Void kd3dwindow: onkeydown (wparam, lparam)
- {
- If (wparam = vk_escape)
- {
- Postmessage (m_hwnd, wm_close, 0, 0 );
- }
- }
- // Initialize d3d
- Hresult kd3dwindow: initd3d (hwnd)
- {
- // If no window exists, return failure
- If (m_hwnd = NULL)
- Return e_fail;
- // Create a d3d object
- If (null = (g_pd3d = direct3dcreate8 (d3d_sdk_version )))
- Return e_fail;
- // Obtain the current display mode
- D3ddisplaymode d3ddm;
- If (failed (g_pd3d-> getadapterdisplaymode (d3dadapter_default, & d3ddm )))
- Return e_fail;
- D3dpresent_parameters d3dpp;
- Zeromemory (& d3dpp, sizeof (d3dpp ));
- D3dpp. cmdwed = true; // window mode
- D3dpp. swapeffect = d3dswapeffect_discard; // you can specify the switch mode.
- D3dpp. backbufferformat = d3ddm. format; // set the background buffer format to the current left-side format.
- // Create a d3d Device
- // The first parameter: use the default video card
- // The second parameter: The hardware abstraction layer (HAL) is requested)
- // The third parameter is the window handle.
- // Fourth parameter: use software to process vertices
- // Fifth parameter: The created Parameter
- // The sixth parameter is the d3d device pointer created.
- If (failed (g_pd3d-> createdevice (d3dadapter_default,
- D3ddevtype_hal,
- Hwnd,
- D3dcreate_software_vertexprocessing,
- & D3dpp,
- & G_pd3ddevice )))
- {
- Return e_fail;
- }
- Return s_ OK;
- }
- // Release d3d
- Void kd3dwindow: cleanup ()
- {
- If (g_pd3ddevice! = NULL)
- G_pd3ddevice-> release ();
- If (g_pd3d! = NULL)
- G_pd3d-> release ();
- }
- // Rendering
- Void kd3dwindow: render ()
- {
- If (null = g_pd3ddevice)
- Return;
- // Clear the blue background
- G_pd3ddevice-> clear (0, null, d3dclear_target, d3dcolor_xrgb (0, 0, 255), 1.0f, 0 );
- // Draw a scenario
- G_pd3ddevice-> beginscene ();
- // Draw what you need here
- // End the painting
- G_pd3ddevice-> endscene ();
- // Display to the screen
- G_pd3ddevice-> present (null, null );
- }
- Kd3dwindow: kd3dwindow ()
- {
- }
- Kd3dwindow ::~ Kd3dwindow ()
- {
- Cleanup ();
- }
- Bool kd3dwindow: createex (DWORD dwexstyle,
- Lpctstr lpszclass, lpctstr lpszname, DWORD dwstyle,
- Int X, int y, int nwidth, int nheight, hwnd hparent,
- Hmenu, hinstance hinst)
- {
- // Create a window
- Bool Bret = kwindow: createex (dwexstyle, lpszclass, lpszname, dwstyle, X, Y, nwidth, nheight, hparent, hmenu, hinst );
- // Initialize the d3d Device
- If (initd3d (m_hwnd )! = S_ OK)
- Bret = false;
- Return Bret;
- }
The key points of the Code are annotated and easy to understand.
Then there is how to call the main function, please refer:
- Int apientry winmain (hinstance,
- Hinstance hprevinstance,
- Lpstr lpcmdline,
- Int ncmdshow)
- {
- // Todo: Place code here.
- Kd3dwindow win;
- // Khellowindow win;
- Win. createex (0, _ T ("hello"), _ T ("hello"), ws_overlappedwindow, getsystemmetrics (sm_cxscreen)/4, getsystemmetrics (sm_cyscreen)/4,
- Getsystemmetrics (sm_cxscreen)/2,
- Getsystemmetrics (sm_cyscreen)/2,
- Null, null, hinstance );
- Win. showwindow (ncmdshow );
- Win. updatewindow ();
- Return win. messageloop ();
- }
After compilation and running, you can see that the program has both the background scenario of d3d rendering and the text drawn by Win32 GDI. As follows:
This article comes from
Http://blog.csdn.net/zxcred