Direct9 getting started tutorial | the tutorial file officially provided by direct9 is self-translated | DirectX creates a device

Source: Internet
Author: User

Tutorial 1 in the official help document translated by myself, I felt very happy last night. If a newbie finds it hard to start, let's take a look at my article.

The following are the VC settings. (My SDK version is old. It is 9.0april2006)

 

Step 1 create a Windows window
The first thing a Windows program does is to generate a program window when it is running and display it to the user. To achieve this, the createdevice example starts running in its winmain function. The following code demonstrates window initialization.

Int winapi winmain (hisntace hinst, hinstace, lpsrt, INT) <br/>{< br/> // register the window class. <br/> wndclassex WC = {sizeof (wndclassex), cs_classdc, msgproc, 0l, 0l, <br/> getmodulehandle (null), null, <br/> "direct3d tutorial", null };</P> <p> registerclassex (& WC ); </P> <p> // create the application's window. <br/> hwnd = createwindow ("direct3d tutorial", "direct3d tutorial 01: createdevice", <br/> ws_overlappedwindow, 100,100,300,300, <br/> get1_topwindow (), null, WC. hinstance, null); <br/>

 

This code example is standard Windows Programming :). The example starts by defining and registering a window class called "direct3d tutorial. After the registration class, the code will generate a basic top-level window using the registration class, where the window area is 300 pixels wide, 300 pixels High. There are no menus or subwindows in the window. The example also uses the ws_overlappedwindow type to create a window program with the minimized, maximized, and closed buttons. (If the sample runs in full screen mode, we recommend that you use the window type ws_ex_topmost. This type indicates that the created window should be on top of all non-highest level window programs, even if the window is not positive) after the window is created, the code sample calls the standard Win32 function to display and update the window.
After the program window is ready, you can set the required object direct3d, which will be mentioned in the second part -- initialize direct3d.

Step 2 initialize direct3d
The createdevice example demonstrates direct3d initialization by calling the initd3d program function from winmain after the window is created. After you create a program window, you start to initialize the direct3d object (you will use this object to render the scenario ). This process contains the generated object, set the parameters, and finally create the direct3d device.
After creating a direct3d object, use the createdevice method to generate devices and enumerate devices, types, and modes.
If (null = (g_pd3d = direct3dcreate9 (d3d_sdk_version )))
Return e_fail;
The only parameter passed to direct3dcreate9 is always d3d_sdk_version. This macro. At any time, a small change leads to program re-compilation, which means it will always remain unchanged. If the version does not match, direct3dcreate9 fails to be called.
You can fill d3dpresent parameters to see the effect of a 3D program. In the createdevice example, set wed to true, swapeffect to d3dswapeffect_discard, and backbufferformat to d3dfmt_unknown.

D3dpresent_parameters d3dpp; <br/> zeromemory (& d3dpp, sizeof (d3dpp); <br/> d3dpp. required wed = true; <br/> d3dpp. swapeffect = d3dswapeffect_discard; <br/> d3dpp. backbufferformat = d3dfmt_unknown; 
The final step is to use the createdevice method to generate a direct3d device, as described in the following program.

If (failed (g_pd3d-> createdevice (d3dadapter_default, d3ddevtype_hal, hwnd, <br/> d3dcreate_software_vertexprocessing, <br/> & d3dpp, & g_pd3ddevice )));

The above program generates a device using the default adapter using the d3dadapter_default flag. In most cases, the system only has one adapter unless you have installed Multiple graphics cards. The d3ddevtype_hal parameter table shows that you prefer hardware acceleration to exceed software acceleration. This Code uses d3dcreate_software_vertexprocessing to tell the system to use the software for fixed-point processing. Note: If you tell the system to use the specified d3dcreate_harware_vertexprocessing hardware for fixed-point processing, you will see a significant increase in performance on the video card that supports hardware vertex processing.
Now that the direct3d object has been initialized, the next step is to ensure that you have a mechanism to process system messages. For details, see Step 3 to process system messages.

Step 3 process system messages
After you create an application and initialize direct3d, you should start rendering the scenario. In most cases, window programs process system messages in their message loops, regardless of whether there are messages in the message queue, they will always render the scenario. However, the createdevice routine waits until the message wn_paint appears in the queue, telling the program that it needs to redraw all the windows.

// The message loop <br/> MSG; <br/> while (getmessage (& MSG, null, 0, 0 )) <br/>{< br/> translatemessage (& MSG); <br/> dispatchmessage (& MSG); <br/>}

During each cyclic operation, dispatchmessage calls msgproc to process handle messages in the queue. When wm_paint is in the queue and the program calls render, the program will redraw the window. Then the Win32 function validaterect will be called to verify the entire workspace.

Lresult winapi msgproc (hwnd, uint MSG, wparam, lparam) <br/>{< br/> switch (MSG) <br/>{< br/> case wm_destroy: <br/> postquitmessage (0); <br/> return 0; <br/> case wm_paint: <br/> render (); <br/> validaterect (hwnd, null); <br/> return 0; <br/>}< br/> return defwindowproc (hwnd, MSG, wparam, lparam); <br/>}< br/>

 

Now the program can process system messages. The next step is rendering and display. The rendering and display scenarios in Step 4 are described in detail.

Step 4 rendering and display scenarios
For rendering and display scenarios, the sample code should clear the reserve cache area in blue, convert the reserve cache area into the front cache area, and display the front cache area to the screen.
You can call the clear function to clear the scenario.

// Clear the back buffer to a blue color <br/> g_pd3ddevice-> clear (0, null, d3dclear_target, d3dcolor_rgb (0, 0, 255), 1.0f, 0 );

The first two parameters received by clear notify the size of direct3d and the position of the rectangle to be cleared. The rectangle array describes the rendering target surface to be cleared.
In most cases, you use a rectangle to overwrite the entire rendering target. You only need to set the first parameter to 0 and the second parameter to null. The third determines the behavior of the method. You can define a state to clear the rendering target surface, an associated depth buffer, a template buffer, or a combination of the two. This tutorial does not apply to the depth buffer, so the only d3dclear_target flag is used. The last three values are the rendering target, depth buffer, and template buffer. The createdevice example sets the blue color (d3dcolor_xrgb (255,) for the rendering target surface )). The last two parameters can be ignored because the corresponding flag does not exist.
Then, the createdevice sample notifies direct3d to start rendering and then marks that the rendering is complete, as the following code does:

// Begin the scene <br/> g_pd3ddevice-> beginscene (); </P> <p> // rendering of scene ojects happens here </P> <p> // end the scene <br/> g_pd3ddevice-> endscene (); <br/>

The beginscene and endscene methods are marked to the system at the rendering start and end. You can only call the rendering method between the two methods. Even if the rendering method call fails, you should call endscene again before beginscene.
After rendering a scenario, you should use the present method to display it.
G_pd3ddevice-> present (null, null ); 
The first two are the rectangle of the resource and the rectangle of the destination. In this step, the sample code sets two parameters to null to display all the backup cache areas to the front cache areas. The third parameter is the display setting target window. Because the parameter is set to null, hwnddevicewindow in d3dpresent parameters will be used. The fourth parameter is the dirty rectangle parameter, which should be set to null in most cases.
The last step is to close the program. For more information, see Step 5.

Step 5 close
During running, your program should be closed at last. The DirectX program closes not only one window, but you should also pin the DirectX object and invalidate the pointer. In the createdevice example, cleanup is called, and the system will post-process the received wm_destroy message.
Void cleanup () <br/>{< br/> If (g_pd3ddevice! = NULL) <br/> g_pdeddevice-> release (); <br/> If (g_pd3d! = NULL) <br/> g_pd3d-> release (); <br/>}
To destroy every direct3d object, call the method in iunknown. This tutorial should follow the com rule. The reference times of most objects should be set to zero and will be automatically removed from the memory.
In addition to disabling, even when the sub-ah is running-for example, when the user changes the desktop resolution or color depth-you may need to destroy and recreate the MS d3d object. Therefore, it is a good idea to clear the code in one place of the program.

This tutorial shows you how to create a device. The following tutorial shows how to use a fixed point to draw a set shape.

 

Attachment/Microsoft DirectX SDK (April 2006)/samples/C ++/direct3d/tutorials/tut01_createdevice source code

// ----------------------------------------------------------------------------- <Br/> // file: createdevice. CPP <br/> // Desc: This is the first tutorial for using direct3d. in this tutorial, all <br/> // we are doing is creating a direct3d device and using it to clear the <br/> // window. <br/> // copyright (c) Microsoft Corporation. all rights reserved. <br/> //------------------------- -------------------------------------------------- <Br/> # include <d3d9. h> <br/># Pragma warning (Disable: 4996) // disable deprecated warning <br/> # include <strsafe. h> <br/> # pragma warning (default: 4996) </P> <p> // variable <br/> // global variables <br/> // variable //----------------------------------------------------------------------- ------ <Br/> lpdirect3d9 g_pd3d = NULL; // used to create the d3ddevice <br/> lpdirect3ddevice9 g_pd3ddevice = NULL; // Our rendering device </P> <p> // container <br/> // name: initd3d () <br/> // Desc: initializes direct3d <br/> // ----------------------------------------------------------------------------- <br/> hresult initd3d (hwnd) <Br/>{< br/> // create the d3d object, which is needed to create the d3ddevice. <br/> If (null = (g_pd3d = direct3dcreate9 (d3d_sdk_version) <br/> return e_fail; </P> <p> // set up the structure used to create the d3ddevice. most parameters are <br/> // zeroed out. we set each wed to true, since we want to do d3d in a <br/> // window, and then set the swapeffect to "discard", which is Most <br/> // efficient method of presenting the back buffer to the display. and <br/> // we request a back buffer format that matches the current desktop display <br/> // format. <br/> d3dpresent_parameters d3dpp; <br/> zeromemory (& d3dpp, sizeof (d3dpp); <br/> d3dpp. required wed = true; <br/> d3dpp. swapeffect = d3dswapeffect_discard; <br/> d3dpp. backbufferformat = d3dfmt_unknown; </P> <p> // create t He direct3d device. here we are using the default adapter (most <br/> // systems only have one, unless they have multiple graphics hardware cards <br/> // installed) and requesting the Hal (which is saying we want the hardware <br/> // device rather than a software one ). software vertex processing is <br/> // specified since we know it will work on all cards. on cards that support <br/> // hardwar E vertex processing, though, we wocould see a big performance gain <br/> // by specifying hardware vertex processing. <br/> If (failed (g_pd3d-> createdevice (d3dadapter_default, d3ddevtype_hal, hwnd, <br/> d3dcreate_software_vertexprocessing, <br/> & d3dpp, & g_pd3ddevice ))) <br/>{< br/> return e_fail; <br/>}</P> <p> // device state wocould normally be set here </P> <p> return s_ OK; <br/>}</P> <p> //--- Reset <br/> // name: cleanup () <br/> // Desc: releases all previusly initialized objects <br/> // reset <br/> void cleanup () <br/>{< br/> If (g_pd3ddevice! = NULL) <br/> g_pd3ddevice-> release (); </P> <p> If (g_pd3d! = NULL) <br/> g_pd3d-> release (); <br/>}</P> <p> // else <br/> // name: render () <br/> // Desc: draws the scene <br/> // ----------------------------------------------------------------------------- <br/> void render () <br/>{< br/> If (null = g_pd3ddevice) <br/> return; </P> <p> // clear the backbuffer to a blue color <br/> g_pd3ddevice-> clear (0, null, d3dclear_target, d3dcolor_xrgb (0, 0, 255), 1.0f, 0); </P> <p> // begin the scene <br/> If (succeeded (g_pd3ddevice-> beginscene ())) <br/> {<br/> // rendering of scene objects can happen here </P> <p> // end the scene <br/> g_pd3ddevice-> endscene (); <br/>}</P> <p> // present the backbuffer contents to the display <br/> g_pd3ddevice-> present (null, null ); <br/>}</P> <p> // signature <br/> // name: msgproc () <br/> // Desc: the window's message handler <br/> // handle <br/> lresult winapi msgproc (hwnd, uint MSG, wparam, lparam) <br/>{< br/> switch (MSG) <br/>{< br/> case wm_destroy: <br/> cleanup (); <br/> postquitmessage (0); <br/> return 0; </P> <p> case wm_paint: <br/> render (); <br/> validaterect (hwnd, null); <br/> return 0; <br/>}</P> <p> return defwindowproc (hwnd, MSG, wparam, lparam); <br/>}</P> <p> // signature <br/> // name: winmain () <br/> // Desc: the application's entry point <br/> // timeout <br/> int winapi winmain (hinstance hinst, hinstance, lpstr, INT) <br/>{< br/> // register the window class <br/> wndclassex WC = {sizeof (wndclassex), cs_classdc, msgproc, 0l, 0l, <br/> getmodulehandle (null), null, <br/> "d3d tutorial", null}; <br/> registerclassex (& WC ); </P> <p> // create the application's window <br/> hwnd = createwindow ("d3d tutorial", "d3d tutorial 01: createdevice ", <br/> ws_overlappedwindow, 100,100,300,300, <br/> null, null, WC. hinstance, null); </P> <p> // initialize direct3d <br/> If (succeeded (initd3d (hwnd ))) <br/>{< br/> // show the window <br/> showwindow (hwnd, sw_showdefault); <br/> updatewindow (hwnd ); </P> <p> // enter the message loop <br/> MSG; <br/> while (getmessage (& MSG, null, 0, 0 )) <br/>{< br/> translatemessage (& MSG); <br/> dispatchmessage (& MSG ); <br/>}</P> <p> unregisterclass ("d3d tutorial", WC. hinstance); <br/> return 0; <br/>}</P> <p>

Remember to set the header file and library address during compilation, and remember to add the Library to the database during connection. I ran it with vc6.

The following describes how to compile this file.

Open vc6 create-project-Win32 applecation and write your project name (figure 1)

Select an empty project (figure 2)

Then you need to set the header file tool-Option-directory (figure 3, figure 4) to copy your header file address. Remember to put it in the first one, otherwise, the VC may use its own direct header file, which may cause errors.

 

Next, set the connection, specifically the project-set-connection, and write d3d9. Lib d3dx9. Lib in the red box in Figure 5. Otherwise, it will jump out of the external symbol_direct3dcreate9 @ 4 error. I am stuck here for a long time. Haha

Finally, compilation and running are all OK ~~~~ View results

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.