新手问题,怎样才能让DrawPrimitive成功执行?
时间:2011-11-10
来源:互联网
代码如下:
C/C++ code
求帮助!!
C/C++ code
#include <d3d9.h> #pragma comment (lib, "d3d9.lib") struct s_CustomVertex { float x, y, z, rhw; DWORD color; }; HINSTANCE hInst; HWND hwndHandle; LPDIRECT3D9 pD3D; LPDIRECT3DDEVICE9 pd3dDevice; LPDIRECT3DVERTEXBUFFER9 g_pVB; LRESULT CALLBACK WndProc (HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { switch (message) { case WM_DESTROY: PostQuitMessage (0); return 0; } return DefWindowProc (hWnd, message, wParam, lParam); } bool initWindow (HINSTANCE hInstance) { WNDCLASSEX wcex; wcex.cbSize = sizeof (WNDCLASSEX); wcex.style = CS_HREDRAW | CS_VREDRAW; wcex.lpfnWndProc = (WNDPROC) WndProc; wcex.cbClsExtra = 0; wcex.cbWndExtra = 0; wcex.hInstance = hInstance; wcex.hIcon = 0; wcex.hCursor = LoadCursor (0, IDC_ARROW); wcex.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1); wcex.lpszMenuName = 0; wcex.lpszClassName = "DirectXExample"; wcex.hIconSm = 0; RegisterClassEx (&wcex); hwndHandle = CreateWindow ( "DirectXExample", "DirectXExample", WS_EX_TOPMOST | WS_POPUP | WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT, 800, 600, 0, 0, hInstance, 0); if (!hwndHandle) return false; ShowWindow (hwndHandle, SW_SHOW); UpdateWindow (hwndHandle); return true; } HRESULT SetupVB () { HRESULT hr; s_CustomVertex g_Vertices[] = { {320, 50, 0.5, 1, D3DCOLOR_ARGB (0,255,0,0)}, {250, 400, 0.5, 1, D3DCOLOR_ARGB (0,0,255,0)}, {50, 400, 0.5, 1, D3DCOLOR_ARGB (0,0,0,255)} }; hr = pd3dDevice->CreateVertexBuffer ( 3 * sizeof (s_CustomVertex), 0, D3DFVF_XYZRHW | D3DFVF_DIFFUSE, D3DPOOL_DEFAULT, &g_pVB, 0); if (FAILED (hr)) return hr; void* pVertices; hr = g_pVB->Lock (0, 3 * sizeof (s_CustomVertex), (void**) &pVertices, 0); if (FAILED (hr)) return hr; memcpy (pVertices, g_Vertices, 3 * sizeof (s_CustomVertex)); g_pVB->Unlock (); return S_OK; } bool initDirect3D () { // create pD3D object pD3D = 0; pd3dDevice = 0; if (0 == (pD3D = Direct3DCreate9 (D3D_SDK_VERSION))) return false; // create pd3dDevice object D3DPRESENT_PARAMETERS d3dpp; ZeroMemory (&d3dpp, sizeof (d3dpp)); d3dpp.Windowed = TRUE; d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8; d3dpp.BackBufferCount = 1; d3dpp.BackBufferHeight = 800; d3dpp.BackBufferWidth = 600; d3dpp.hDeviceWindow = hwndHandle; if (FAILED (pD3D->CreateDevice ( D3DADAPTER_DEFAULT, D3DDEVTYPE_REF, hwndHandle, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &pd3dDevice))) return false; if (FAILED (SetupVB ())) return false; return true; } void render () { pd3dDevice->Clear (0, 0, D3DCLEAR_TARGET, D3DCOLOR_XRGB (0, 0, 255), 1.0f, 0); IDirect3DSurface9* backbuffer = 0; pd3dDevice->GetBackBuffer ( 0, 0, D3DBACKBUFFER_TYPE_MONO, &backbuffer); if (FAILED (pd3dDevice->SetStreamSource (0, g_pVB, 0, sizeof (s_CustomVertex)))) throw "SetStreamSource"; if (FAILED (pd3dDevice->SetFVF (D3DFVF_XYZRHW | D3DFVF_DIFFUSE))) throw "SetFVF fail"; /////////////////////////////////////////////////////////////////////////////////// /*下面这条语句不能成功执行*/ if (FAILED (pd3dDevice->DrawPrimitive (D3DPT_TRIANGLESTRIP, 0, 1))) throw "DrawPrimitive fail"; /////////////////////////////////////////////////////////////////////////////////// pd3dDevice->Present (0, 0, 0, 0); } void cleanUp () { pd3dDevice->Release (); pD3D->Release (); } int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE, LPTSTR lpCmdLine, int nCmdShow) { if (!initWindow (hInstance)) return 0; if (!initDirect3D()) return 0; MSG msg; ZeroMemory (&msg, sizeof (msg)); while (msg.message != WM_QUIT) { if (PeekMessage (&msg, 0, 0, 0, PM_REMOVE)) { TranslateMessage (&msg); DispatchMessage (&msg); } else { render (); } } cleanUp (); return (int) msg.wParam; }
求帮助!!
作者: smwbcs 发布时间: 2011-11-10
DNF中新手怎么较快的入手呢
作者: KLLKDKDK 发布时间: 2011-11-11
缺少IDirect3DDevice9::BeginScene();与IDirect3DDevice9::EndScene()语句
所有绘制必须在这两个语句之间调用。
例:
pd3dDevice->BeginScene();
绘制函数
pd3dDevice->EndScene();
pd3dDevice->Present (0, 0, 0, 0);
所有绘制必须在这两个语句之间调用。
例:
pd3dDevice->BeginScene();
绘制函数
pd3dDevice->EndScene();
pd3dDevice->Present (0, 0, 0, 0);
作者: freeman040921 发布时间: 2011-11-11
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28