456
社区成员
发帖
与我相关
我的任务
分享
#include<windows.h>
#include <stdio.h>
#include "d3dx9.h" //Driect9.0的头文件
#include<string>
#pragma comment(lib,"Msimg32.lib")
#pragma comment(lib,"winmm.lib")
#pragma comment(lib,"d3d9.lib")
#pragma comment(lib,"d3dx9.lib")
#define WINDOW_WIDTH 800
#define WINDOW_HEIGHT 600
#define WINDOW_TITLE "Directx9.0游戏窗口"
#define WINDOW_ICON L"icon.ico"
#define WINDOW_CLASSNAME "GameTest"
IDirect3D9 *_d3d9=0; //声明Direct接口指针
D3DCAPS9 caps;//接口性能结构
D3DDEVTYPE devicetype;//设备类型
D3DPRESENT_PARAMETERS d3dpp; //D3D参数结构
IDirect3DDevice9 *device=0; //DirectDevice接口
WNDCLASSEX wndClass={0};
IDirect3DVertexBuffer9 *VB=0;//顶点缓存对象指针
IDirect3DIndexBuffer9 *IB=0;//索引缓存对象指针
struct Vertex{
Vertex(){}
Vertex(float x,float y,float z){
_x=x;
_y=y;
_z=z;
}
float _x,_y,_z;
static const WORD FVF;
};
const WORD Vertex::FVF=D3DFVF_XYZ;
bool D3D_Setup(); //初始化顶点缓存
void Init_D3D(HWND hwnd);//初始化Direct
void Clean_D3D();//清除释放设备
bool Dispaly_D3D(); //显示
void Fill_Directparam(HWND hwnd);
LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wparam,LPARAM lparam);//窗口回调函数
BOOL RegisterClass(HINSTANCE hInstance); //注册窗口类
BOOL InitInstance(HINSTANCE hInstance,int nShowCmd); //初始化程序实例
/////////////////////////////////////////////////////////////////
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd)
{
if(!RegisterClass(hInstance)) exit(0);
if(!InitInstance(hInstance,nShowCmd)) exit(0);
MSG msg;
while(msg.message!=WM_QUIT)
{
if(PeekMessage(&msg,0,0,0,PM_REMOVE))
{
TranslateMessage(&msg);//把虚拟按键转换成字符消息
DispatchMessage(&msg);//分发消息给窗口
}
else
{
}
}
return (int) msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wparam,LPARAM lparam)
{
switch(message)
{
case WM_PAINT:
Dispaly_D3D();
ValidateRect(hwnd,NULL);
break;
case WM_KEYDOWN:
if(wparam==VK_ESCAPE)
DestroyWindow(hwnd);
break;
case WM_DESTROY:
Clean_D3D();
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd,message,wparam,lparam);
}
return 0;
}
void Init_D3D(HWND hwnd)
{
_d3d9=Direct3DCreate9(D3D_SDK_VERSION);
if(!_d3d9){
MessageBox(hwnd,"DirectX9.0 接口创建失败!","错误!",MB_ICONERROR);
exit(0);
}
_d3d9->GetDeviceCaps(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,&caps);
int vp=0;
if(caps.DevCaps&D3DDEVCAPS_HWTRANSFORMANDLIGHT)
{
vp=D3DCREATE_HARDWARE_VERTEXPROCESSING;
//MessageBox(hwnd,"支持硬件顶点运算","提示",MB_ICONINFORMATION);
}
else
{
vp=D3DCREATE_SOFTWARE_VERTEXPROCESSING;
MessageBox(hwnd,"仅仅支持软件顶点运算","提示",MB_ICONINFORMATION);
}
HRESULT hr=_d3d9->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,hwnd,D3DCREATE_HARDWARE_VERTEXPROCESSING,&d3dpp,&device);
if(FAILED(hr))
{
MessageBox(hwnd,"创建设备失败","错误!",MB_ICONERROR);
exit(0);
}
}
void Clean_D3D()
{
device->Release();
if(VB) VB->Release();
if(IB) IB->Release();
}
bool Dispaly_D3D()
{
if(device){
D3DXMATRIX Rx,Ry;
D3DXMatrixRotationX(&Rx,3.14f/4.0f);
static float y=0.0f;
D3DXMatrixRotationY(&Ry,y); //创建绕y轴旋转矩阵
y+=1.414f;
if(y>=6.28f) y=0.0f;
D3DXMATRIX p=Rx*Ry;
device->SetTransform(D3DTS_WORLD,&p); //转为世界坐标
device->Clear(0,0,D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER,0x00000000,1.0f,0);
device->BeginScene();
device->SetStreamSource(0,VB,0,sizeof(Vertex)); //设置顶点数据输入流
device->SetIndices(IB);
device->SetFVF(Vertex::FVF);
device->DrawIndexedPrimitive(D3DPT_TRIANGLELIST,0,0,8,0,12);
device->DrawPrimitive(D3DPT_TRIANGLELIST,0,8);
device->EndScene();
device->Present(0,0,0,0);
}
return true;
}
BOOL RegisterClass(HINSTANCE hInstance)
{
wndClass.cbSize=sizeof(WNDCLASSEX);
wndClass.style=CS_HREDRAW|CS_VREDRAW;
wndClass.lpfnWndProc=WndProc;
wndClass.cbWndExtra=0;
wndClass.cbClsExtra=0;
wndClass.hInstance=hInstance;
wndClass.hIcon=(HICON)::LoadImage(NULL,"icon.ico",IMAGE_ICON,0,0,LR_DEFAULTSIZE|LR_LOADFROMFILE);
wndClass.hCursor=LoadCursor(NULL,IDC_ARROW);
wndClass.hbrBackground=(HBRUSH)GetStockObject(GRAY_BRUSH);
wndClass.lpszMenuName=NULL;
wndClass.lpszClassName=WINDOW_CLASSNAME;
if(!RegisterClassEx(&wndClass))
return FALSE;
return TRUE;
}
BOOL InitInstance(HINSTANCE hInstance, int nShowCmd)
{
HWND hwnd=CreateWindow(WINDOW_CLASSNAME,WINDOW_TITLE,WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,WINDOW_WIDTH,WINDOW_HEIGHT,NULL,NULL,hInstance,NULL);
if(!hwnd) return FALSE;
MoveWindow(hwnd,250,80,WINDOW_WIDTH,WINDOW_HEIGHT,true);
Fill_Directparam(hwnd);
Init_D3D(hwnd);
ShowWindow(hwnd,nShowCmd);
UpdateWindow(hwnd);
return TRUE;
}
void Fill_Directparam(HWND hwnd)
{
d3dpp.BackBufferWidth=800; //后台缓存表面宽度,单位为像素
d3dpp.BackBufferHeight=600; //后台缓存表面高度,单位为像素
d3dpp.BackBufferFormat=D3DFMT_A8R8G8B8; //后台缓存像素格式,32位
d3dpp.BackBufferCount=1; //后台缓存的个数为1
d3dpp.MultiSampleType=D3DMULTISAMPLE_NONE; //后台采样类型,这里为多重采样
d3dpp.MultiSampleQuality=0; //后台缓存采样质量,这里为默认
d3dpp.SwapEffect=D3DSWAPEFFECT_DISCARD; //后天缓存置换页面的方式,这里的为效率最高的方式
d3dpp.hDeviceWindow=hwnd; //指定要绘制的窗口
d3dpp.Windowed=true; //这里为全屏
d3dpp.EnableAutoDepthStencil=true; //自动创建深度缓存或者模板缓存
d3dpp.AutoDepthStencilFormat=D3DFMT_D24S8; //深度缓存或者模板缓存的像素格式,这里为24位深度8未作为模板缓存
d3dpp.Flags=0; // 是否丢弃深度缓存或者模板缓存,这里默认为丢弃
d3dpp.FullScreen_RefreshRateInHz=D3DPRESENT_RATE_DEFAULT; //显示器刷新的频率,采用默认
d3dpp.PresentationInterval=D3DPRESENT_INTERVAL_IMMEDIATE; //提交页面的方式,这里为立即提交
}
bool D3D_Setup(){
//创建顶点缓存
device->CreateVertexBuffer(
8*sizeof(Vertex),
D3DUSAGE_WRITEONLY,Vertex::FVF,
D3DPOOL_MANAGED,
&VB,
0);
//创建索引缓存
device->CreateIndexBuffer(
36*sizeof(WORD),
D3DUSAGE_WRITEONLY,
D3DFMT_INDEX16,
D3DPOOL_MANAGED,
&IB,
0);
//设置顶点缓存
Vertex *vertices;
VB->Lock(0,0,(void**)&vertices,0);
vertices[0]=Vertex(-1.0f,-1.0f,-1.0f);
vertices[1]=Vertex(-1.0f, 1.0f,-1.0f);
vertices[2]=Vertex(1.0f,1.0f,-1.0f);
vertices[3]=Vertex(1.0f,-1.0f,-1.0f);
vertices[4]=Vertex(-1.0f,-1.0f,1.0f);
vertices[5]=Vertex(-1.0f,1.0f,1.0f);
vertices[6]=Vertex(1.0f,1.0f,-1.0f);
vertices[7]=Vertex(1.0f,-1.0f,1.0f);
VB->Unlock();
//设置索引缓存
WORD *indices=0;
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,0.0f,-5.0f);
D3DXVECTOR3 target(0.0f,0.0f,0.0f);
D3DXVECTOR3 up(0.0f,1.0f,0.0f);
D3DXMATRIX V;
D3DXMatrixLookAtLH(&V,&position,&target,&up);
device->SetTransform(D3DTS_VIEW,&V);
D3DXMATRIX proj;
D3DXMatrixPerspectiveFovLH(
&proj,
D3DX_PI*0.5f,
(float)WINDOW_WIDTH/(float)WINDOW_HEIGHT,
1.0f,
1000.0f);
device->SetTransform(D3DTS_PROJECTION,&proj);
device->SetRenderState(D3DRS_FILLMODE,D3DFILL_WIREFRAME); //设置绘制状态
return true;
}