
|
|
|
|
|
#include #include #include #include #pragma comment(lib,"d3dx9.lib") #pragma comment(lib,"d3d9.lib") #pragma comment(lib,"winmm.lib") #define LEFTTOPX 0 #define LEFTTOPY 0 #define WINDOWWIDTH 640 #define WINDOWHEIGHT 480 #define CUSTOMFVF ( D3DFVF_XYZ | D3DFVF_DIFFUSE ) #define FONTSIZE 25 HRESULT CALLBACK myproc(HWND ,UINT ,WPARAM ,LPARAM ); void renderframe(void); IDirect3D9 *d3d; IDirect3DDevice9 *d3ddev; ID3DXFont * font; IDirect3DVertexBuffer9* vb; IDirect3DIndexBuffer9* ib; struct customVertex{ float _x,_y,_z; D3DCOLOR _color; customVertex(float x,float y , float z,DWORD color){ _x=x; _y=y; _z=z; _color=color; } }; int frmCount=0; int fps=0; RECT rect={0,0,250 ,250}; int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd){ //MessageBox(0,"Hi","the first message",0); HWND mainhwnd; WNDCLASS wc; ZeroMemory(&wc,sizeof(WNDCLASS)); wc.cbClsExtra=0; wc.cbWndExtra=0; wc.hbrBackground=(HBRUSH )COLOR_3DFACE ; wc.hCursor=LoadCursor(0,IDC_HAND ); wc.hIcon=LoadIcon(0,IDI_HAND); wc.hInstance=hInstance; wc.lpfnWndProc=(WNDPROC)myproc; wc.lpszClassName="mywndclass22659"; wc.lpszMenuName=0; wc.style=CS_HREDRAW | CS_VREDRAW ; if (!RegisterClass(&wc)) MessageBox(0,"The Windows Form could not registered.","Error",0); if ((mainhwnd=CreateWindow("mywndclass22659","DirectX test - written by Mohammad Nouraien",WS_EX_OVERLAPPEDWINDOW ,LEFTTOPX ,LEFTTOPY,WINDOWWIDTH ,WINDOWHEIGHT ,0,0,hInstance,0))==NULL) MessageBox(0,"The Windows Form could not Created.","Error",0); ShowWindow(mainhwnd,nShowCmd); D3DPRESENT_PARAMETERS d3dparam; ZeroMemory(&d3dparam,sizeof(D3DPRESENT_PARAMETERS)); d3dparam.SwapEffect=D3DSWAPEFFECT_DISCARD ; d3dparam.Windowed=true; d3dparam.hDeviceWindow= mainhwnd; d3dparam.PresentationInterval=D3DPRESENT_INTERVAL_IMMEDIATE ; //d3dparam.BackBufferFormat=D3DFMT_A8R8G8B8 ; d3d=Direct3DCreate9(D3DX_SDK_VERSION ); d3d->CreateDevice(D3DADAPTER_DEFAULT ,D3DDEVTYPE_HAL ,mainhwnd,D3DCREATE_SOFTWARE_VERTEXPROCESSING ,&d3dparam,&d3ddev); D3DXFONT_DESC lf; lf.Height=25; lf.Width=10; lf.Weight=FONTSIZE ; lf.Italic=false; lf.CharSet=DEFAULT_CHARSET ; strcpy(lf.FaceName,"tahoma"); D3DXCreateFontIndirect(d3ddev,&lf,&font); /////////////////////////////////////////////////////////////////////// d3ddev->CreateVertexBuffer(8*sizeof(customVertex),D3DUSAGE_WRITEONLY,CUSTOMFVF ,D3DPOOL_MANAGED,&vb,0); d3ddev->CreateIndexBuffer(36*sizeof(WORD),D3DUSAGE_WRITEONLY ,D3DFMT_INDEX16 ,D3DPOOL_MANAGED,&ib,0); customVertex * vertices; vb->Lock(0,0,(void**)&vertices,0); vertices[0]=customVertex( -1.0f, -1.0f, -1.0f ,D3DCOLOR_XRGB(255,0,0)); vertices[1]=customVertex( -1.0f, 1.0f, -1.0f ,D3DCOLOR_XRGB(255,50,250)); vertices[2]=customVertex( 1.0f, 1.0f, -1.0f ,D3DCOLOR_XRGB(100,50,250)); vertices[3]=customVertex( 1.0f, -1.0f, -1.0f ,D3DCOLOR_XRGB(0,50,250)); vertices[4]=customVertex( -1.0f, -1.0f, 1.0f ,D3DCOLOR_XRGB(0,50,250)); vertices[5]=customVertex( -1.0f, 1.0f, 1.0f ,D3DCOLOR_XRGB(100,50,250)); vertices[6]=customVertex( 1.0f, 1.0f, 1.0f ,D3DCOLOR_XRGB(0,50,250)); vertices[7]=customVertex( 1.0f, -1.0f, 1.0f ,D3DCOLOR_XRGB(100,50,250)); vb->Unlock(); WORD* indices; ib->Lock(0,0,(void**)&indices,0); indices[0]=0; indices[1]=1; indices[2]=2; indices[3]=0; indices[4]=2; indices[5]=3; indices[6]=4; indices[7]=6; indices[8]=5; indices[9]=4; indices[10]=7; indices[11]=6; indices[12]=4; indices[13]=5; indices[14]=1; indices[15]=4; indices[16]=1; indices[17]=0; indices[18]=3; indices[19]=2; indices[20]=6; indices[21]=3; indices[22]=6; indices[23]=7; indices[24]=1; indices[25]=5; indices[26]=6; indices[27]=1; indices[28]=6; indices[29]=2; indices[30]=4; indices[31]=0; indices[32]=3; indices[33]=4; indices[34]=3; indices[35]=7; ib->Unlock(); D3DXVECTOR3 position(0.0f,-5.0f,-2.0f); D3DXVECTOR3 target(0.0f,0.0f,0.0f); D3DXVECTOR3 up(0.0f,1.0f,0.0f); D3DXMATRIX V; D3DXMatrixLookAtLH(&V,&position,&target,&up); d3ddev->SetTransform(D3DTS_VIEW,&V); D3DXMATRIX proj; D3DXMatrixPerspectiveFovLH(&proj,D3DX_PI * 0.2f,(float)WINDOWWIDTH /(float)WINDOWHEIGHT ,0.2f,1000.0f); d3ddev->SetTransform(D3DTS_PROJECTION ,&proj); d3ddev->SetRenderState(D3DRS_FILLMODE,D3DFILL_WIREFRAME); d3ddev->SetRenderState(D3DRS_LIGHTING ,false); d3ddev->SetRenderState(D3DRS_CULLMODE,D3DCULL_NONE ); //////////////////////////////////////////////////////////// MSG msg; ZeroMemory(&msg,sizeof(MSG)); double lasttime=(double)timeGetTime(); while (true) { if (PeekMessage(&msg,0,0,0,PM_REMOVE)) { if (msg.message==WM_QUIT ) break; TranslateMessage(&msg); DispatchMessage(&msg); } else { double curtime=(double)timeGetTime(); while (curtime-lasttime<30) curtime=(double)timeGetTime(); lasttime=(double)timeGetTime(); renderframe(); fps=frmCount; /*if(curtime-lasttime>=1000.0f){ fps=frmCount; frmCount=0; lasttime=(double)timeGetTime(); }*/ } } d3ddev->Release (); d3d->Release(); ib->Release(); vb->Release(); font->Release(); return msg.wParam ; } HRESULT CALLBACK myproc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam){ switch(msg) { case WM_DESTROY : PostQuitMessage(WM_QUIT); return 0; case WM_KEYDOWN : if (wParam==VK_ESCAPE) DestroyWindow(hwnd); return 0; } return DefWindowProc(hwnd,msg,wParam,lParam); } void renderframe(void){ frmCount++; D3DXMATRIX Ry,Rx; static float y=0.0f; D3DXMatrixRotationX(&Rx,y);//3.14f/4.0f); D3DXMatrixRotationY(&Ry,y); y+=0.025f; if (y>=6.28) y=0.0f; D3DXMATRIX p=Rx * Ry; d3ddev->SetTransform(D3DTS_WORLD,&p); d3ddev->Clear(0,0,D3DCLEAR_TARGET,D3DCOLOR_XRGB(0,0,0),0.0f,0); char *fpsText=new char[80]; char *buffer=new char[20]; d3ddev->BeginScene(); itoa(fps,buffer,10); strcpy(fpsText,"fps : "); strcat(fpsText,buffer); font->DrawText(0,fpsText,-1,&rect,DT_TOP|DT_LEFT ,D3DCOLOR_XRGB(255,255,255)); d3ddev->SetStreamSource(0,vb,0,sizeof(customVertex )); d3ddev->SetFVF(CUSTOMFVF); d3ddev->SetIndices(ib); d3ddev->DrawIndexedPrimitive (D3DPT_TRIANGLELIST,0,0,8,0,12); d3ddev->EndScene(); d3ddev->Present(0,0,0,0); } |
||
|
+
نوشته شده در چهارشنبه 13 شهریور1387ساعت 11:38 توسط محمد نورآئین
|
|
||