码迷,mamicode.com
首页 > 编程语言 > 详细

Direct3D C语言的例子

时间:2017-04-29 14:19:20      阅读:417      评论:0      收藏:0      [点我收藏+]

标签:mod   ssi   sage   discard   over   clear   std   direct   span   

  几乎所有的D3D例子都是用COM和C++写的。C语言可以用D3D吗,StackOverflow上给出了答案:directx-programming-in-c

1 hr = IDirect3D9_GetDeviceCaps(d3d, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &d3dcaps9);
2 hr = IDirect3D9_GetAdapterDisplayMode(d3d, D3DADAPTER_DEFAULT, &d3ddm);
3 hr = IDirect3D9_CreateDevice(d3d, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, game_window, D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_MULTITHREADED, &d3dpp, &d3d_dev);

  按照这种格式,可以用C语言写出Direct3D的Demo:

  1 #include<d3d9.h>  
  2 
  3 #pragma comment(lib, "d3d9.lib")   
  4 
  5 #define WINDOW_CLASS "UGPDX"  
  6 #define WINDOW_NAME  "Drawing Lines"  
  7 
  8 // Function Prototypes...  
  9 BOOL InitializeD3D(HWND hWnd, BOOL fullscreen);
 10 BOOL InitializeObjects();
 11 void RenderScene();
 12 void Shutdown();
 13 
 14 
 15 // Direct3D object and device.  
 16 LPDIRECT3D9 g_D3D = NULL;
 17 LPDIRECT3DDEVICE9 g_D3DDevice = NULL;
 18 
 19 // Vertex buffer to hold the geometry.  
 20 LPDIRECT3DVERTEXBUFFER9 g_VertexBuffer = NULL;
 21 
 22 
 23 // A structure for our custom vertex type  
 24 typedef struct 
 25 {
 26     float x, y, z, rhw;
 27     unsigned long color;
 28 }stD3DVertex;
 29 
 30 // Our custom FVF, which describes our custom vertex structure.  
 31 #define D3DFVF_VERTEX (D3DFVF_XYZRHW | D3DFVF_DIFFUSE)   
 32 
 33 
 34 LRESULT WINAPI MsgProc(HWND hWnd, UINT msg, WPARAM wp, LPARAM lp)
 35 {
 36     switch (msg)
 37     {
 38     case WM_DESTROY:
 39         PostQuitMessage(0);
 40         return 0;
 41         break;
 42 
 43     case WM_KEYUP:
 44         if (wp == VK_ESCAPE) PostQuitMessage(0);
 45         break;
 46     }
 47 
 48     return DefWindowProc(hWnd, msg, wp, lp);
 49 }
 50 
 51 
 52 int WINAPI WinMain(HINSTANCE hInst, HINSTANCE prevhInst,
 53     LPSTR cmdLine, int show)
 54 {
 55     // Register the window class  
 56     WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, MsgProc,
 57         0, 0, GetModuleHandle(NULL), NULL, LoadCursor(NULL,IDC_ARROW),
 58         NULL, NULL, WINDOW_CLASS, NULL };
 59     RegisterClassEx(&wc);
 60 
 61     // Create the application‘s window  
 62     HWND hWnd = CreateWindow(WINDOW_CLASS, WINDOW_NAME,
 63         WS_OVERLAPPEDWINDOW, 100, 100,
 64         640, 480, GetDesktopWindow(), NULL,
 65         wc.hInstance, NULL);
 66 
 67     // Initialize Direct3D  
 68     if (InitializeD3D(hWnd, FALSE))
 69     {
 70         // Show the window  
 71         ShowWindow(hWnd, SW_SHOWDEFAULT);
 72         UpdateWindow(hWnd);
 73 
 74         // Enter the message loop  
 75         MSG msg;
 76         ZeroMemory(&msg, sizeof(msg));
 77 
 78         while (msg.message != WM_QUIT)
 79         {
 80             if (PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE))
 81             {
 82                 TranslateMessage(&msg);
 83                 DispatchMessage(&msg);
 84             }
 85             else
 86                 RenderScene();
 87         }
 88     }
 89 
 90     // Release any and all resources.  
 91     Shutdown();
 92 
 93     // Unregister our window.  
 94     UnregisterClass(WINDOW_CLASS, wc.hInstance);
 95     return 0;
 96 }
 97 
 98 
 99 BOOL InitializeD3D(HWND hWnd, BOOL fullscreen)
100 {
101     D3DDISPLAYMODE displayMode;
102 
103     // Create the D3D object.  
104     g_D3D = Direct3DCreate9(D3D_SDK_VERSION);
105     if (g_D3D == NULL) return FALSE;
106 
107 
108     // Get the desktop display mode.  
109     if (FAILED(IDirect3D9_GetAdapterDisplayMode(g_D3D,D3DADAPTER_DEFAULT,
110         &displayMode))) return FALSE;
111 
112 
113     // Set up the structure used to create the D3DDevice  
114     D3DPRESENT_PARAMETERS d3dpp;
115     ZeroMemory(&d3dpp, sizeof(d3dpp));
116 
117 
118     if (fullscreen)
119     {
120         d3dpp.Windowed = FALSE;
121         d3dpp.BackBufferWidth = 640;
122         d3dpp.BackBufferHeight = 480;
123     }
124     else
125         d3dpp.Windowed = TRUE;
126     d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
127     d3dpp.BackBufferFormat = displayMode.Format;
128 
129 
130     // Create the D3DDevice  
131     if (FAILED(IDirect3D9_CreateDevice(g_D3D,D3DADAPTER_DEFAULT,
132         D3DDEVTYPE_HAL, hWnd, D3DCREATE_HARDWARE_VERTEXPROCESSING,
133         &d3dpp, &g_D3DDevice))) return FALSE;
134 
135 
136     // Initialize any objects we will be displaying.  
137     if (!InitializeObjects()) return FALSE;
138 
139     return TRUE;
140 }
141 
142 
143 BOOL InitializeObjects()
144 {
145     unsigned long col = D3DCOLOR_XRGB(255, 255, 255);
146 
147     // Fill in our structure to draw an object.  
148     // x, y, z, rhw, color.  
149     stD3DVertex objData[] =
150     {
151         { 420.0f, 150.0f, 0.5f, 1.0f, col, },
152         { 420.0f, 350.0f, 0.5f, 1.0f, col, },
153         { 220.0f, 150.0f, 0.5f, 1.0f, col, },
154         { 220.0f, 350.0f, 0.5f, 1.0f, col, },
155     };
156 
157     // Create the vertex buffer.  
158     if (FAILED(IDirect3DDevice9_CreateVertexBuffer(g_D3DDevice,sizeof(objData), 0,
159         D3DFVF_VERTEX, D3DPOOL_DEFAULT, &g_VertexBuffer,
160         NULL))) return FALSE;
161 
162     // Fill the vertex buffer.  
163     void *ptr;
164 
165     if (FAILED(IDirect3DVertexBuffer9_Lock(g_VertexBuffer,0, sizeof(objData),
166         (void**)&ptr, 0))) return FALSE;
167 
168     memcpy(ptr, objData, sizeof(objData));
169 
170 
171     IDirect3DVertexBuffer9_Unlock(g_VertexBuffer);
172 
173     return TRUE;
174 }
175 
176 
177 void RenderScene()
178 {
179     // Clear the backbuffer.  
180     IDirect3DDevice9_Clear(g_D3DDevice,0, NULL, D3DCLEAR_TARGET,
181         D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
182 
183     // Begin the scene.  Start rendering.  
184    IDirect3DDevice9_BeginScene(g_D3DDevice);
185 
186     // Render object.  
187    IDirect3DDevice9_SetStreamSource(g_D3DDevice,0, g_VertexBuffer, 0,
188         sizeof(stD3DVertex));
189    IDirect3DDevice9_SetFVF(g_D3DDevice,D3DFVF_VERTEX);
190    IDirect3DDevice9_DrawPrimitive(g_D3DDevice,D3DPT_LINELIST, 0, 2);
191 
192     // End the scene.  Stop rendering.  
193    IDirect3DDevice9_EndScene(g_D3DDevice);
194 
195     // Display the scene.  
196     IDirect3DDevice9_Present(g_D3DDevice,NULL, NULL, NULL, NULL);
197 }
198 
199 void Shutdown()
200 {
201     if (g_D3DDevice != NULL) IDirect3DDevice9_Release(g_D3DDevice);
202     if (g_D3D != NULL) IDirect3D9_Release(g_D3D);
203     if (g_VertexBuffer != NULL) IDirect3DVertexBuffer9_Release(g_VertexBuffer);
204 
205     g_D3DDevice = NULL;
206     g_D3D = NULL;
207     g_VertexBuffer = NULL;
208 }

  这里只是画了两条平行的线段,作为一个例子:

  技术分享

 

Direct3D C语言的例子

标签:mod   ssi   sage   discard   over   clear   std   direct   span   

原文地址:http://www.cnblogs.com/wurui1994/p/6785036.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!