#include "windows.h"
#include "tchar.h"
#include "windowsx.h"
#include "d3d9.h"
#include "d3dx9.h"
#define curectvertex_fvf (D3DFVF_XYZ|D3DFVF_DIFFUSE)
struct curectvertex{
float x,y,z;
DWORD color;
};
HWND g_hwnd;
LPDIRECT3D9 g_d3d9=NULL;
LPDIRECT3DDEVICE9 g_d3d9device=NULL;
LPDIRECT3DVERTEXBUFFER9 g_d3d9_vb=NULL;
LPDIRECT3DINDEXBUFFER9 g_d3d9_ib=NULL;
int ix; //旋转角度
bool iy; //旋转方式
LRESULT initgame(){
D3DPRESENT_PARAMETERS ps;
ZeroMemory(&ps,sizeof(ps));
ps.Windowed=true;
ps.SwapEffect=D3DSWAPEFFECT_DISCARD;
ps.BackBufferCount=1;
ps.EnableAutoDepthStencil=true;
ps.AutoDepthStencilFormat=D3DFMT_D16;
ps.BackBufferFormat=D3DFMT_A8R8G8B8;
ps.MultiSampleQuality=0;
ps.MultiSampleType=D3DMULTISAMPLE_NONE;
g_d3d9 =Direct3DCreate9(D3D_SDK_VERSION);
if FAILED(g_d3d9->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,g_hwnd,D3DCREATE_SOFTWARE_VERTEXPROCESSING,&ps,&g_d3d9device))
return E_FAIL;
g_d3d9device->SetRenderState(D3DRS_LIGHTING,false);
g_d3d9device->SetRenderState(D3DRS_ZENABLE,D3DZB_TRUE);
curectvertex b[]={
{ 0.0f, 1.0f, 0.0f, D3DCOLOR_XRGB(0,255,0), },
{ 1.0f, -1.0f, -0.577f, D3DCOLOR_XRGB(0,255,0), },
{ -1.0f, -1.0f, -0.577f, D3DCOLOR_XRGB(0,255,0), },
{ 0.0f, 1.0f, 0.0f, D3DCOLOR_XRGB(255,0,0), },
{ 0.0f, -1.0f, 1.155f, D3DCOLOR_XRGB(255,0,0),},
{ 1.0f, -1.0f, -0.577f, D3DCOLOR_XRGB(255,0,0), },
{ 0.0f, 1.0f, 0.0f, D3DCOLOR_XRGB(0,0,255), },
{ -1.0f, -1.0f, -0.577f, D3DCOLOR_XRGB(0,0,255), },
{ 0.0f, -1.0f, 1.155f, D3DCOLOR_XRGB(0,0,255),},
};
if FAILED(g_d3d9device->CreateVertexBuffer(
sizeof(b),
0,
curectvertex_fvf,
D3DPOOL_DEFAULT,
&g_d3d9_vb,
NULL))
return E_FAIL;
void *p;
if FAILED(g_d3d9_vb->Lock(0,sizeof(b),(void **)&p,0))
return E_FAIL;
memcpy(p,b,sizeof(b));
g_d3d9_vb->Unlock();
return S_OK;
}
LRESULT Setmatrix(float x,float y,float z){
D3DXMATRIX m_t;
D3DXMatrixTranslation(&m_t,x,y,z);
D3DXMATRIX m_r;
D3DXMatrixRotationY(&m_r,ix++*D3DX_PI/180);
D3DXMATRIX wm;
if (iy)
D3DXMatrixMultiply(&wm,&m_r,&m_t);
else
D3DXMatrixMultiply(&wm,&m_t,&m_r);
g_d3d9device->SetTransform(D3DTS_WORLD,&wm);
D3DXVECTOR3 eye(0.0f,3.0f,-3.0f);
D3DXVECTOR3 lookat(0.0f,0.0f,0.0f);
D3DXVECTOR3 up(0.0f,1.0f,0.0f);
D3DXMATRIX vm;
D3DXMatrixLookAtLH(&vm,&eye,&lookat,&up);
g_d3d9device->SetTransform(D3DTS_VIEW,&vm);
D3DXMATRIXA16 matProj;
D3DXMatrixPerspectiveFovLH(&matProj,45*D3DX_PI/180,1.33f,1.0f,100.0f);
g_d3d9device->SetTransform(D3DTS_PROJECTION,&matProj);
return S_OK;
}
LRESULT game(){
g_d3d9device->Clear(0,NULL,D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER,D3DCOLOR_XRGB(0,0,0),1.0f,0);
g_d3d9device->SetFVF(curectvertex_fvf);
g_d3d9device->SetStreamSource(0,g_d3d9_vb,0,sizeof(curectvertex));
g_d3d9device->BeginScene();
Setmatrix(1.0,0.0,0.0);
g_d3d9device->DrawPrimitive(D3DPT_TRIANGLELIST,0,3);
Setmatrix(0.0,0.0,0.0);
g_d3d9device->DrawPrimitive(D3DPT_TRIANGLELIST,0,3);
Setmatrix(-1.0,0.0,0.0);
g_d3d9device->DrawPrimitive(D3DPT_TRIANGLELIST,0,3);
g_d3d9device->EndScene();
g_d3d9device->Present(NULL,NULL,NULL,NULL);
return S_OK;
}
LRESULT CALLBACK wndproc(HWND hwnd,UINT message,WPARAM wparam ,LPARAM lparam){
switch (message){
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_LBUTTONDOWN:
iy=!iy;
break;
}
return DefWindowProc(hwnd,message,wparam,lparam);
}
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd )
{
WNDCLASSEX wndclass;
ZeroMemory(&wndclass,sizeof(WNDCLASSEX));
wndclass.cbSize=sizeof(WNDCLASSEX);
wndclass.lpfnWndProc=wndproc;
wndclass.lpszClassName=_T("abc");
wndclass.hInstance=GetModuleHandle(NULL);
wndclass.style=CS_HREDRAW|CS_VREDRAW;
wndclass.hbrBackground=GetStockBrush(RGB(0,0,0));
wndclass.hCursor=LoadCursor(hInstance,IDC_ARROW);
RegisterClassEx(&wndclass);
g_hwnd=CreateWindow(_T("abc"),_T("fffff"),WS_OVERLAPPEDWINDOW,0,0,400,600,NULL,NULL,wndclass.hInstance,NULL);
ShowWindow(g_hwnd,SW_SHOW);
UpdateWindow(g_hwnd);
initgame();
MSG message;
while(true){
if (PeekMessage(&message,NULL,0,0,PM_REMOVE)){
if (message.message==WM_QUIT)
{
break;
}
else{
TranslateMessage(&message);
DispatchMessage(&message);
}
}
game();
}
g_d3d9device->Release();
g_d3d9->Release();
return S_OK;
}