RPG handwriting (2) initializing d3d rendering Devices

Source: Internet
Author: User

Before going to bed, I decided to update the second article in the series "RPG production notebook". I wrote RPG for the first time and encountered many problems. Most of the problems were caused by reading previous articles online, the problem can be solved. Think about it carefully. If the predecessors do not write it, we will not be able to learn this knowledge. Therefore, Yu Gong and Yu Gong must complete this series of articles.

In the second article, there are many ways to render devices and draw images. Most of them are very efficient and fully meet the needs of 2D game development, the highly encapsulated OpenGL and DirectX, and the scattered BGI and svgacc can all be used as powerful tools for game development. Even if we were known as GDI, we could also develop excellent Games, the key lies in how we use them. However, the blogger decided to choose DirectX, because the first time the blogger came into contact with the graphics library was DirectX, the so-called preemptive (Laugh ).

Direct3d is the core component of DirectX. It is responsible for drawing all the images (d3d for short). Because of the encapsulated relationship, it is very convenient to use. To use d3d, we need to do this:

  1. Create an idirect3d object. If you are a beginner, you can understand it as a class with no data members but only function members. The essence of idirect3d9 is a COM interface. Therefore, we can't see any internal details. We can only use the DirectX DK document to learn how to use it. idirect3d is responsible for implementing the top-level functions, such: query the video card information, query the features supported by the video card, and list the display modes supported by the video card.
  2. The idirect3d object is used to create an idirect3ddevice object. All rendering work is done through the idirect3ddevice9 object, for example, implementing the dual-buffer mechanism, calling the video card function, and drawing graphics.
  3. For game programs in form mode, we need to obtain the pixel format of the current system desktop, because in form mode, the program works with the system desktop. Therefore, in color mode, it must be consistent with the system desktop. Otherwise, other programs may crash, or your programs may crash.
  4. Tell d3d what rendering devices we need to create, such as the buffer size, pixel format, number of buffers, whether to use full screen mode, and whether to use graphics card hardware for acceleration.
  5. Destroy idirect3ddevice objects and idirect3d objects correctly.

OK. Let's check the Code directly:

 1     D3DPRESENT_PARAMETERS pp; 2     D3DDISPLAYMODE pm; 3  4     memset( & pp, 0, sizeof( pp ) ); 5     memset( & pm, 0, sizeof( pm ) ); 6  7     d3d = Direct3DCreate8( D3D_SDK_VERSION ); 8     d3d->GetAdapterDisplayMode( D3DADAPTER_DEFAULT, & pm ); 9 10     pp.BackBufferFormat = pm.Format;11     pp.BackBufferWidth = 640;12     pp.BackBufferHeight = 480;13     pp.BackBufferCount = 1;14     pp.SwapEffect = D3DSWAPEFFECT_DISCARD;15     pp.hDeviceWindow = hWnd;16     pp.Windowed = true;17 18     d3d->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, & pp, & device );

There are so many initialization code. The blogger uses d3d 8, but it should not be difficult to convert it into d3d 9 code.

In the above Code, we first call the direct3dcreate8 function to create an idirect3d object, then use the idirect3d getadapterdisplaymode method to obtain the display mode of the current system desktop, and then set the rendering device parameters, the buffer size is 640x480 and runs in form mode. The pixel format is the same as that of the desktop. Finally, the idirect3d createdevice method is called to create an idirect3ddevice object, the parameters here are indeed troublesome to explain, so they are skipped.

After the rendering device is created, rendering can basically be done. However, there is a little bit of work to do. Before you start rendering anything, you must first call the beginscene method of the idirect3ddevice object, after rendering all the images, call the beginscene method of the idirect3ddevice object. To clear the buffer content, call the clear method of the idirect3ddevice object, we can call the present method to display the buffered data in the background to the frontend buffer.

For ease of use, we encapsulate it into a class, which looks like this:

1 # ifndef _ renderd3d1_h _ 2 # DEFINE _ renderd3d1_h _ 3 4 # include <d3d8. h> 5 # pragma comment (Lib, "d3d8. lib ") 6 7 // 8 // Renderer d3d 8 9 // 10 11 class crenderd3d 12 {13 public: 14 15 crenderd3d () 16 {17 d3d = NULL; 18 device = NULL; 19} 20 21 ~ Crenderd3d () 22 {23} 24 25 public: 26 27 // 28 // initialize d3d rendering device 29 // 30 void Init (hwnd, bool full_screen = false) 31 {32 rect client; 33 d3dpresent_parameters pp; 34 d3ddisplaymode PM; 35 36 memset (& PP, 0, sizeof (PP); 37 memset (& PM, 0, sizeof (PM); 38 39 getclientrect (hwnd, & client); 40 41 d3d = direct3dcreate8 (d3d_sdk_version); 42 d3d-> getadapterdisplaymode (d3dadapter_d Efault, & PM); 43 44 pp. backbufferformat = PM. format; 45 pp. backbufferwidth = client. right; 46 pp. backbufferheight = client. bottom; 47 pp. backbuffercount = 1; 48 pp. swapeffect = d3dswapeffect_discard; 49 pp. hdevicewindow = hwnd; 50 pp. export wed =! Full_screen; 51 52 d3d-> createdevice (d3dadapter_default, d3ddevtype_hal, hwnd, 53 d3dcreate_software_vertexprocessing, & PP, & device ); 54} 55 56 // 57 // destroy d3d rendering device 58 // 59 void Shutdown (void) 60 {61 If (device) device-> release (); 62 if (d3d) d3d-> release (); 63 64 d3d = NULL; 65 device = NULL; 66} 67 68 // 69 // clear the buffer zone, and tell d3d to prepare to start rendering 70 // 71 void clear (DWORD color = d3dcolor_xrgb (0, 0, 0) 72 {73 device-> clear (0, null, d3dclear_target, color, 0, 0); 74 device-> beginscene (); 75} 76 77 // 78 // tell d3d to complete rendering, then, the buffered content in the background is displayed on the front-end buffer 79 // 80 void present (void) 81 {82 device-> endscene (); 83 device-> present (null, null, null, null); 84} 85 86 // 87 // return idirect3ddevice 88 // 89 lpdirect3ddevice8 getdevice (void) 90 {91 return device; 92} 93 94 PRIVATE: 95 96 // 97 // idirect3d object 98 // 99 lpdirect3d8 d3d; 100 101 102 // 103 // idirect3ddeice object 104 // 105 lpdirect3ddevice8 device; 106}; 107 # endif

Bytes -----------------------------------------------------------------------------------

To be honest, I can't write anything in this blog post. If I write about d3d, there are a lot of articles about d3d on the Internet, but d3d is the core link, so I have to write it.

However, the blogger has a tips to share with you:

When reading other people's articles, we often encounter new terms or terms. If you have any doubts, do not guess them by yourself. You should first query them online and understand them, continue to read the article. In this way, I learned not only the knowledge in the article, but also the knowledge involved in the article; if you encounter a point of knowledge that you have never learned when reading an article, you can skip it all at once. The content that follows will inevitably become more obscure, therefore, I did not pay attention to the interpretation of terms and knowledge points when writing articles, because the explanation of these knowledge points on the Internet is very clear.

In this case, the second part of the RPG production ceremony is over. Finally, we recommend you to read a good book about d3d 2D games-2D programming in direct3d and a thin book, I want to explain how to use d3d for 2D implementation. If you don't have a hard time, buy one and read it several times. Don't worry. It's worth the effort, but when I was reading this book, it's borrowed from the library ).

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.