Directx3d draw Line Interface id3dxline

Source: Internet
Author: User

Lines drawn using DrawPrimitive () cannot control their linearity and draw dotted lines. Therefore, DirectX3D provides the ID3DXLine interface to draw lines. In this interface

HRESULT Draw (
CONST D3DXVECTOR2 * pVertexList,
DWORD dwVertexListCount,
D3DCOLOR Color
);

The vertices to be drawn are stored in the pVertexList in the method. The vertices are given in line strip mode, and the vertices are under the screen coordinate. You can useSetPattern ()Method to Control the line type (dotted line or solid line ). For other methods, see ID3DXLine in MSDN.

 

The Code is as follows:

/********************************** Author: rabbit729 * E-mail: wlq_729@163.com * Date: 2011-07-04 * description: d3dxline: *********************************/# include <d3d9. h> # include <d3dx9. h> // ------------------------------------------------------------------------------- // global variable // define lpdirect3d9 g_pd3d = NULL; // direct3d object lpdi Rect3ddevice9 g_pd3ddevice = NULL; // direct3d device object lpd3dxline g_pline = NULL; // direct3d line Object d3dxvector2 * g_plinearr = NULL; // line segment vertex // Desc: initialize direct3d // inithresult initd3d (hwnd) {// create a direct3d object, which is used to create a direct3d device object if (null = (g_pd3d = direct 3dcreate9 (d3d_sdk_version) return e_fail; // set the d3dpresent_parameters structure and create the direct3d device object mongod3dpp; zeromemory (& d3dpp, sizeof (d3dpp); d3dpp. required wed = true; d3dpp. swapeffect = d3dswapeffect_discard; d3dpp. backbufferformat = d3dfmt_unknown; // create a direct3d device object if (failed (g_pd3d-> createdevice (d3dadapter_default, d3ddevtype_hal, hwnd, d3dcreate_software_vertexprocessing, & d3dpp, & G_pd3ddevice) {return e_fail;} // create the direct3d line object if (failed (d3dxcreateline (g_pd3ddevice, & g_pline) {return e_fail ;} // fill the vertex of the line. Note: The line is filled with screen coordinates and does not need to be converted to g_plinearr = new d3dxvector2 [4]; g_plinearr [0]. X = g_plinearr [0]. y = 50; g_plinearr [1]. X = 400; g_plinearr [1]. y = 50; g_plinearr [2]. X = 50; g_plinearr [2]. y = 400; g_plinearr [3]. X = 400; g_plinearr [3]. y = 400; return s_ OK ;}//--------------------------------- ------------------------------------------ // Desc: Release the creation object // release void cleanup () {// release the line object if (g_pline! = NULL) {g_pline-> release () ;}// release the direct3d device object if (g_pd3ddevice! = NULL) g_pd3ddevice-> release (); // release the direct3d object if (g_pd3d! = NULL) g_pd3d-> release ();} // render // Desc: rendering image // define void render () {// clear background buffer g_pd3ddevice-> clear (0, null, d3dclear_target, d3dcolor_xrgb (43, 43, 43), 1.0f, 0); // start drawing the image in the background buffer if (succeeded (g_pd3ddevice-> beginscene ())) {// draw line g_pline-> setwidth (3.f); G _ Pline-> setantialias (true); g_pline-> draw (g_plinearr, 4, 0 xffffffff); // end rendering the graphic g_pd3ddevice in the background buffer-> endscene ();} // submit the drawing drawn in the background buffer to the foreground buffer to display g_pd3ddevice-> present (null, null);} // else // Desc: message Processing // --------------------------------------------------------------------------------- lresult winapi msgproc (hwnd, uint MSG, Wparam, lparam) {Switch (MSG) {Case wm_destroy: cleanup (); postquitmessage (0); Return 0; Case wm_paint: render (); validaterect (hwnd, null); Return 0;} return defwindowproc (hwnd, MSG, wparam, lparam);} // program // Desc: program entry // callback int Winapi winmain (hinstance hinst, hinstance, lpstr, INT) {// register window class wndclassex WC = {sizeof (wndclassex), cs_classdc, msgproc, 0l, 0l, getmodulehandle (null ), null, null, l "classname", null}; registerclassex (& WC); // create a window hwnd = createwindow (L "classname ", L "use direct line", ws_overlappedwindow, 200,100,600,500, null, null, WC. hinstance, null); // initialize direct3d if (succeeded (initd 3D (hwnd) {// display the main window showwindow (hwnd, sw_showdefault); updatewindow (hwnd); // enter the message loop MSG; zeromemory (& MSG, sizeof (MSG); While (MSG. message! = Wm_quit) {If (peekmessage (& MSG, null, 0u, 0u, pm_remove) {translatemessage (& MSG); dispatchmessage (& MSG);} else {render (); // rendered image }}unregisterclass (L "classname", WC. hinstance); Return 0 ;}

 

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.