Direct basic learning (2) the simplest direct3d

Source: Internet
Author: User

If Windows is an enterprise, DirectX is one of its departments. If we create a Windows program, we need the cooperation of the Windows department, the DirectX Department is required to use the DirectX function.

Windows Department appears in our program in the window, DirectX also participates in our program in its way

To use direct plotting, follow these steps:
1) create a direct3d object
2) create a direct3d Device
3) Drawing

Create a direct3d object
Idirect3d9 * direct3dcreate9 (d3d_sdk_version );
The function returns an idirect3d9 pointer. If the pointer fails, null is returned.

The getadaptercount function returns the number of display cards of the system.
Uint idirect3d9: getadaptercount (void );
When there is only one video card in the system, it is the main video card. When there are multiple, the first video card is the main video card.

Create an idirect3ddevice9 Interface
Hresult createdevice (uint adapter,
D3ddevtype devicetype,
Hwnd hfocuswindow,
DWORD behaviorflags,
D3dpresent_parameters * ppresentationparameters,
Idirect3ddevice9 ** ppreturneddeviceinterface );
Our game program calls the video card through this object. Normally, d3d_ OK is returned (this is returned when most direct functions are correctly executed)
Otherwise, a problem occurs:
D3derr_invalidcall parameter error
The d3derr_notavailable device does not support this function.
D3derr_outofvideomemory insufficient memory
PS: checking the return value to determine whether the function is successfully executed is a good habit
The preceding six parameters are described:
Adapter. Type uint. video card number, generally use d3dadapter_default, that is, the default video card (the first)
Devicetype. Type d3ddevtype. There are three different device types available:
D3ddevtype_hal hardware acceleration)
D3ddevtype_ref
D3ddevtype_sw
Hfocuswindow. Type hwnd. idirect3ddevice9 object's window
Behaviorflags. type DWORD. It is one or more d3dcreate structures and specifies the device creation method.
The following example uses d3dcreate_software_vertexprocessing
Presentationparamters. Type d3dpresent_parameters. Control the display mode of the device, such as full screen and window, whether the device contains cache, etc.
Ppreturneddeviceinterface. Type idirect3ddevice9 ** this does not need to be explained (in use, here the parameter is referenced)
Introduction to d3dpresent_parameters
Typedef struct d3dpresent_parameters {
Uint backbufferwidth, backbufferheight;
D3dformat backbufferformat; // type d3dformat. The window mode d3dfmt_unknow uses the current display mode.
Uint backbuffercount; // Number of cache backups
D3dmultisample_type multisampletype; // type d3dmultisample_type. Full Screen anti-aliasing type. d3dmultisample_none is used unless a special level is specified.
DWORD multisamplequality; // The anti-aliasing level. 0 is passed unless anti-aliasing is unavailable.
D3dswapeffect swapeffect; // specify how the surface is exchanged in the switching chain, take the d3dswapeffect member. D3dswapeffect_discard is the most effective
Hwnd hdevicewindow; // subordinate window
Bool implements wed; // whether it is customized
Bool enableautodepthstencel; // controls the depth of the backflush. True automatically creates the depth/template buffer.
D3dformat autodepthstencilformat; // depth buffer format, type d3dformat.
DWORD flags; // some additional features, set to a member of the 0 or d3dpresentflag type
Uint fullscreen_refreshrateinhz; // screen update rate. Set d3dpresent_rate_default to use the default update rate. The window mode is 0.
Uint presentationinterval; // control the switching speed d3dpresent Member
} D3dpresent_parameters, * lpd3dpresent_parameters;

Clearing the screen
I have created a direct object and a direct device. Is there a drawing next? No. Next we need to clear the screen, just as we have created
After the needle variable is assigned a null value, it must be assigned a "null" value to the screen"
Hresult claer (DWORD count,
Const d3drect * prects,
DWORD flags,
D3dcolor color,
Float Z,
DWORD stencel );
Count indicates the number of rectangles to be cleared. If it is 0, prects must be null. If it is not 0, prects must point to a d3drect array.
Knows the rectangle to be removed from the screen
Flags specifies whether the cache is cleared d3dclear:
D3dclear_stencel render target
Color d3dcolor value containing the color to clear the render target to. Multiple macros are available
Such as d3dcolor_xrgb
Z value to store in the depth buffer. ranges from 0.0f to 1.0f
Stencel holds the value to store in the stencel buffer, when the stencel buffer is not in use, the value is 0
// Some terms do not know how to translate!

Displaying the scene
After the screen is cleared, you can start plotting. Use the present function in direct3d to do this. It is executed when it is cached to the current page.
The focus here is to plot in the cache and then flip the page like a screen.
Hresult present (const rect * psourcerect,
Const rect * pdestrect,
Hwnd hdest1_woverride,
Const rgndata * pdirtyregion );
Psourcerect: A rect structure pointer pointing to the structure in the cache. If it is null, it indicates all the caches.
Pdestrect A rect structure pointer pointing to the current structure
Hdestwindowoverride: a pointer to the window. The customer area of the window is the current drawing area. If it is null, bind the corresponding member of d3dpresent_parameters
Pdirtyregion details the region within the buffer that needs to be updated, null to update the whole Buffer

Cleaning up
Finishing work, releasing resources, etc.
Direct is based on COM objects. When the program ends, you need to use the release function to notify the system to release the used object Resources.

Modify code
1) Add the d3d header file and reference d3d Lib.
2) initialize d3d after creating the window
3) Use peekmessage to replace the getmessage in the original message loop
4) Add the esle branch to the message loop. When there is no message, drawing everything to the screen
While (msg. message! = Wm_quit)
{
If (peekmessage (& MSG, null, 0u, 0u, pm_remove ))
{
Translatemessage (& MSG );
Dispatchmessage (& MSG );
}
Else
{
Render ();
}
}
5) release resources at the end of the Function

 

 

 

// D3d2. CPP <br/> # define window_class "test" <br/> # define window_title "test1" <br/> # define window_width 640 <br/> # define window_height 480 </P> <p> # include <windows. h> <br/> # include <winerror. h> // error code definitions <br/> # include <d3d9. h> // Add the direct3d header </P> <p> # pragma comment (Lib, "d3d9. lib ") </P> <p> // global <br/> hwnd g_wndhandle; <br/> lpdirect3d9 pd3d; // d3d object <br/> lpdirect3dde Vice9 pd3dd; // d3d device </P> <p> // forward declarations <br/> lresult callback wndproc (hwnd, uint, wparam, lparam ); <br/> bool initdirect3d (void); <br/> void render (void); <br/> void cleanup (void ); </P> <p> int winapi winmain (<br/> hinstance, // handle to current instance <br/> hinstance hprevinstance, // handle to previous instance <br/> lpstr lpcmdline, // command line <br/> int ncmdshow // Show state <br/>) <br/>{< br/> wndclassex wcex; </P> <p> // fill in the wcex <br/> wcex. cbsize = sizeof (wndclassex); <br/> wcex. style = cs_classdc; <br/> wcex. lpfnwndproc = (wndproc) wndproc; <br/> wcex. cbclsextra = 0l; <br/> wcex. cbwndextra = 0l; <br/> wcex. hinstance = getmodulehandle (null); <br/> wcex. hicon = NULL; <br/> wcex. hcursor = NULL; <br/> wcex. hbrbackground = (hbrush) (color_window + 1); <br/> wcex. lpsz Menuname = NULL; <br/> wcex. lpszclassname = window_class; <br/> wcex. hiconsm = NULL; </P> <p> registerclassex (& wcex); </P> <p> // crate the window <br/> hwnd wndhandle; <br/> wndhandle = createwindow (<br/> window_class, // name of window class <br/> window_title, // Title-bar string <br/> ws_overlappedwindow, // top-level window <br/> 0, // default horizontal position <br/> 0, // default vertical positio N <br/> window_width, // default width <br/> window_height, // default height <br/> (hwnd) null, // No owner window <br/> (hmenu) null, // use Class Menu <br/> wcex. hinstance, // handle to application instance <br/> (lpvoid) null // No window-creation data <br/>); </P> <p> If (! (G_wndhandle = wndhandle) <br/>{< br/> MessageBox (null, "createwindow faild", "createwindow faild", mb_ OK ); <br/>}</P> <p> // int d3d <br/> If (! Initdirect3d () <br/>{< br/> return false; <br/>}</P> <p> // display the window <br/> showwindow (wndhandle, sw_showdefault); <br/> updatewindow (wndhandle ); </P> <p> // main message loop: <br/> MSG; <br/> zeromemory (& MSG, sizeof (MSG )); <br/> while (MSG. message! = Wm_quit) <br/>{< br/> If (peekmessage (& MSG, null, 0u, 0u, pm_remove )) <br/>{< br/> translatemessage (& MSG); <br/> dispatchmessage (& MSG ); <br/>}< br/> else <br/> {<br/> render (); <br/>}</P> <p> cleanup (); <br/> unregisterclass (window_class, wcex. hinstance); </P> <p> return true; <br/>}</P> <p> lresult callback wndproc (hwnd, uint message, wparam, lparam) <br/>{< br/> switch (Messa Ge) <br/>{< br/> case wm_destroy: <br/> postquitmessage (0); <br/> break; <br/> default: <br/> break; <br/>}</P> <p> return defwindowproc (hwnd, message, wparam, lparam ); <br/>}</P> <p> bool initdirect3d (void) <br/>{< br/> pd3d = NULL; <br/> pd3dd = NULL; </P> <p> // create the DirectX object <br/> If (null = (pd3d = direct3dcreate9 (d3d_sdk_version ))) <br/>{< br/> MessageBox (null, "direct3dcreate9", "direct3 Dcreate9 ", mb_ OK); <br/>}</P> <p> // fill the presentation paramenters structure <br/> d3dpresent_parameters d3dpp; <br/> zeromemory (& d3dpp, sizeof (d3dpp); <br/> d3dpp. required wed = true; <br/> d3dpp. swapeffect = d3dswapeffect_discard; <br/> d3dpp. backbufferformat = d3dfmt_unknown; <br/> d3dpp. backbuffercount = 1; <br/> d3dpp. backbufferheight = window_height; <br/> d3dpp. backbufferwidth = window_width; <br /> D3dpp. hdevicewindow = g_wndhandle; </P> <p> // create a default DirectX device <br/> If (failed (pd3d-> createdevice (d3dadapter_default, d3ddevtype_ref, <br/> g_wndhandle, d3dcreate_software_vertexprocessing, <br/> & d3dpp, & pd3dd) <br/>{< br/> return false; <br/>}</P> <p> return true; <br/>}</P> <p> void render (void) <br/>{< br/> // check <br/> If (null = pd3dd) <br/>{< br/> return; <br/>}</P> <p> pd3dd -> Clear (0, null, d3dclear_target, <br/> d3dcolor_xrgb (0, 0,255), 1.0f, 0); <br/> pd3dd-> present (null, null, null, null); <br/>}</P> <p> void cleanup (void) <br/>{< br/> If (pd3dd! = NULL) <br/>{< br/> pd3dd-> release (); <br/>}</P> <p> If (pd3d! = NULL) <br/>{< br/> pd3d-> release (); <br/>}< br/>}

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.