// DiretxTest.cpp : Defines the entry point for the application. // #include "stdafx.h" #include <stdio.h> #include <d3d9.h> #pragma comment(lib,"d3d9.lib") #pragma comment(lib,"d3dx9.lib") char myclass[]="my win"; LPDIRECT3D9 g_D3D=NULL; LPDIRECT3DDEVICE9 g_D3DDevice=NULL; #define CUSTOMVERCTX_FVF (D3DFVF_XYZRHW|D3DFVF_DIFFUSE) struct CUSTOMVERTEX{ float x,y,z,rhw; D3DCOLOR color; }; BOOL linitiateD3d(HWND hwnd,BOOL fullscreen); void RenderScene(); void ShutDown(); int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { LRESULT CALLBACK WindProc( HWND hwnd, // handle to window UINT uMsg, // message identifier WPARAM wParam, // first message parameter LPARAM lParam // second message parameter ); WNDCLASS wnd; HWND hwn; MSG msg; wnd.cbClsExtra=0; wnd.cbWndExtra=NULL; wnd.hbrBackground=(HBRUSH)COLOR_3DLIGHT; wnd.hCursor=NULL; wnd.hIcon=NULL; wnd.hInstance=hInstance; wnd.lpfnWndProc=WindProc; wnd.lpszClassName=myclass; wnd.lpszMenuName=NULL; wnd.style=CS_BYTEALIGNCLIENT; RegisterClass(&wnd); hwn=CreateWindow(myclass,"这里是窗口名称",WS_SYSMENU,300,200,500,500,0,NULL,hInstance,0); ShowWindow(hwn,nCmdShow); if ( linitiateD3d(hwn,FALSE)) { RenderScene(); while (GetMessage(&msg,NULL,0,0)!=WM_QUIT) { TranslateMessage(&msg); DispatchMessage(&msg); } } ShutDown(); return 0; } LRESULT CALLBACK WindProc( HWND hwnd, // handle to window UINT uMsg, // message identifier WPARAM wParam, // first message parameter LPARAM lParam // second message parameter ) { switch (uMsg) { case WM_CLOSE: exit(0); PostMessage(hwnd,WM_QUIT,wParam,lParam); break; case WM_PAINT: RenderScene(); break; default: return DefWindowProc(hwnd,uMsg,wParam,lParam); } } BOOL linitiateD3d(HWND hwnd,BOOL fullscreen) { D3DDISPLAYMODE displaymode; D3DPRESENT_PARAMETERS d3dpp; g_D3D=Direct3DCreate9(D3D9b_SDK_VERSION); if (g_D3D==NULL) { return false; } if (FAILED(g_D3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT,&displaymode))) { return false; } ZeroMemory(&d3dpp,sizeof(d3dpp)); if (fullscreen) { d3dpp.Windowed=FALSE; d3dpp.BackBufferHeight=800; d3dpp.BackBufferWidth=1280; } else { d3dpp.Windowed=TRUE; } d3dpp.SwapEffect=D3DSWAPEFFECT_DISCARD; d3dpp.BackBufferFormat=displaymode.Format; if (FAILED(g_D3D->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,hwnd,D3DCREATE_SOFTWARE_VERTEXPROCESSING,&d3dpp,&g_D3DDevice))) { return false; } return TRUE; } void RenderScene() { //创建顶点数据 CUSTOMVERTEX customVertex[]={ {150.0f,50.0f,0.0f,1.0f,D3DCOLOR_XRGB(255,0,0)}, {250.0f,250.0f,0.0f,1.0f,D3DCOLOR_XRGB(0,255,0)}, {50.0f,250.0f,0.0f,1.0f,D3DCOLOR_XRGB(0,0,255)}, }; //创建顶点缓冲区 IDirect3DVertexBuffer9 *m_pVertexBuffer; if (FAILED( g_D3DDevice->CreateVertexBuffer(3*sizeof(CUSTOMVERTEX),0,CUSTOMVERCTX_FVF,D3DPOOL_MANAGED,&m_pVertexBuffer,NULL)) ) { MessageBox(GetDesktopWindow(),"创建设置缓冲失败","警告",MB_OK); return; } //将数居写入顶点缓冲区 BYTE *pVertexData; if (FAILED(m_pVertexBuffer->Lock(0,0,(void**)&pVertexData,0))) { MessageBox(GetDesktopWindow(),"写入设置缓冲失败","警告",MB_OK); return; } memcpy(pVertexData,customVertex,sizeof(customVertex)); m_pVertexBuffer->Unlock(); g_D3DDevice->Clear(0,NULL,D3DCLEAR_TARGET,D3DCOLOR_XRGB(0,0,0),1.0f,0); g_D3DDevice->BeginScene(); g_D3DDevice->SetStreamSource(0,m_pVertexBuffer,0,sizeof(CUSTOMVERTEX));//将数据放入directx的渲染管道 g_D3DDevice->SetFVF(CUSTOMVERCTX_FVF);//设置顶点格式 g_D3DDevice->DrawPrimitive(D3DPT_TRIANGLELIST,0,1);//设置渲染方式 g_D3DDevice->EndScene(); g_D3DDevice->Present(NULL,NULL,NULL,NULL); } void ShutDown() { if (g_D3DDevice!=NULL)g_D3DDevice->Release(); if(g_D3D!=NULL)g_D3D->Release(); g_D3DDevice=NULL; g_D3D=NULL; }