D3d beginners 1 (configure the development environment and draw the d3d window)

Source: Internet
Author: User

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:

  1. # Include "kwindow. H"
  2. # Include <d3d8. h>
  3. # Pragma comment (Lib, "d3d8. lib") // link library
  4. Class kd3dwindow: Public kwindow
  5. {
  6. PRIVATE:
  7. Void onkeydown (wparam, lparam );
  8. Void ondraw (HDC );
  9. Void getwndclassex (wndclassex & WC );
  10. // D3d related
  11. Void render ();
  12. Void cleanup ();
  13. Hresult initd3d (hwnd );
  14. Public:
  15. Kd3dwindow ();
  16. ~ Kd3dwindow ();
  17. Virtual bool createex (DWORD dwexstyle,
  18. Lpctstr lpszclass, lpctstr lpszname, DWORD dwstyle,
  19. Int X, int y, int nwidth, int nheight, hwnd hparent,
  20. Hmenu, hinstance hinst );
  21. PRIVATE:
  22. Lpdirect3d8 g_pd3d; // used to create a d3d Device
  23. Lpdirect3ddevice8 g_pd3ddevice; // d3d Device
  24. };

The content of kd3dwindow. cpp is as follows:

  1. // Kd3dwindow. cpp: Implementation of the kd3dwindow class.
  2. //
  3. //////////////////////////////////////// //////////////////////////////
  4. # Include "stdafx. H"
  5. # Include "kd3dwindow. H"
  6. //////////////////////////////////////// //////////////////////////////
  7. // Construction/destruction
  8. //////////////////////////////////////// //////////////////////////////
  9. # Include <windows. h>
  10. # Include <assert. h>
  11. # Include <tchar. h>
  12. # Include "kwindow. H"
  13. Const tchar szhint [] = _ T ("Press ESC to quit .");
  14. Void kd3dwindow: getwndclassex (wndclassex & WC)
  15. {
  16. Memset (& WC, 0, sizeof (WC ));
  17. WC. cbsize = sizeof (wndclassex );
  18. WC. Style = 0;
  19. WC. lpfnwndproc = windowproc;
  20. WC. cbclsextra = 0;
  21. WC. cbwndextra = 0;
  22. WC. hinstance = NULL;
  23. WC. hicon = NULL;
  24. WC. hcursor = loadcursor (null, idc_arrow );
  25. WC. hbrbackground = (hbrush) getstockobject (white_brush );
  26. WC. lpszmenuname = NULL;
  27. WC. lpszclassname = NULL;
  28. WC. hiconsm = NULL;
  29. }
  30. Void kd3dwindow: ondraw (HDC)
  31. {
  32. // Use the d3d device for rendering.
  33. Render ();
  34. // Use Windows API to draw images on the screen
  35. Textout (HDC, 0, 0, szhint, lstrlen (szhint ));
  36. }
  37. Void kd3dwindow: onkeydown (wparam, lparam)
  38. {
  39. If (wparam = vk_escape)
  40. {
  41. Postmessage (m_hwnd, wm_close, 0, 0 );
  42. }
  43. }
  44. // Initialize d3d
  45. Hresult kd3dwindow: initd3d (hwnd)
  46. {
  47. // If no window exists, return failure
  48. If (m_hwnd = NULL)
  49. Return e_fail;
  50. // Create a d3d object
  51. If (null = (g_pd3d = direct3dcreate8 (d3d_sdk_version )))
  52. Return e_fail;
  53. // Obtain the current display mode
  54. D3ddisplaymode d3ddm;
  55. If (failed (g_pd3d-> getadapterdisplaymode (d3dadapter_default, & d3ddm )))
  56. Return e_fail;
  57. D3dpresent_parameters d3dpp;
  58. Zeromemory (& d3dpp, sizeof (d3dpp ));
  59. D3dpp. cmdwed = true; // window mode
  60. D3dpp. swapeffect = d3dswapeffect_discard; // you can specify the switch mode.
  61. D3dpp. backbufferformat = d3ddm. format; // set the background buffer format to the current left-side format.
  62. // Create a d3d Device
  63. // The first parameter: use the default video card
  64. // The second parameter: The hardware abstraction layer (HAL) is requested)
  65. // The third parameter is the window handle.
  66. // Fourth parameter: use software to process vertices
  67. // Fifth parameter: The created Parameter
  68. // The sixth parameter is the d3d device pointer created.
  69. If (failed (g_pd3d-> createdevice (d3dadapter_default,
  70. D3ddevtype_hal,
  71. Hwnd,
  72. D3dcreate_software_vertexprocessing,
  73. & D3dpp,
  74. & G_pd3ddevice )))
  75. {
  76. Return e_fail;
  77. }
  78. Return s_ OK;
  79. }
  80. // Release d3d
  81. Void kd3dwindow: cleanup ()
  82. {
  83. If (g_pd3ddevice! = NULL)
  84. G_pd3ddevice-> release ();
  85. If (g_pd3d! = NULL)
  86. G_pd3d-> release ();
  87. }
  88. // Rendering
  89. Void kd3dwindow: render ()
  90. {
  91. If (null = g_pd3ddevice)
  92. Return;
  93. // Clear the blue background
  94. G_pd3ddevice-> clear (0, null, d3dclear_target, d3dcolor_xrgb (0, 0, 255), 1.0f, 0 );
  95. // Draw a scenario
  96. G_pd3ddevice-> beginscene ();
  97. // Draw what you need here
  98. // End the painting
  99. G_pd3ddevice-> endscene ();
  100. // Display to the screen
  101. G_pd3ddevice-> present (null, null );
  102. }
  103. Kd3dwindow: kd3dwindow ()
  104. {
  105. }
  106. Kd3dwindow ::~ Kd3dwindow ()
  107. {
  108. Cleanup ();
  109. }
  110. Bool kd3dwindow: createex (DWORD dwexstyle,
  111. Lpctstr lpszclass, lpctstr lpszname, DWORD dwstyle,
  112. Int X, int y, int nwidth, int nheight, hwnd hparent,
  113. Hmenu, hinstance hinst)
  114. {
  115. // Create a window
  116. Bool Bret = kwindow: createex (dwexstyle, lpszclass, lpszname, dwstyle, X, Y, nwidth, nheight, hparent, hmenu, hinst );
  117. // Initialize the d3d Device
  118. If (initd3d (m_hwnd )! = S_ OK)
  119. Bret = false;
  120. Return Bret;
  121. }

The key points of the Code are annotated and easy to understand.

Then there is how to call the main function, please refer:

  1. Int apientry winmain (hinstance,
  2. Hinstance hprevinstance,
  3. Lpstr lpcmdline,
  4. Int ncmdshow)
  5. {
  6. // Todo: Place code here.
  7. Kd3dwindow win;
  8. // Khellowindow win;
  9. Win. createex (0, _ T ("hello"), _ T ("hello"), ws_overlappedwindow, getsystemmetrics (sm_cxscreen)/4, getsystemmetrics (sm_cyscreen)/4,
  10. Getsystemmetrics (sm_cxscreen)/2,
  11. Getsystemmetrics (sm_cyscreen)/2,
  12. Null, null, hinstance );
  13. Win. showwindow (ncmdshow );
  14. Win. updatewindow ();
  15. Return win. messageloop ();
  16. }

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

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.