地形系统,在游戏中肯定少不了滴。终于要开始学习这个东东啦!地形看起来非常神奇,绵延的山峦,横看成岭侧成峰,远近高低各不同,这么复杂的地形到底我们到底要怎么写呢?说实话不学这个之前我真的完全想不到地形的实现方法。
/************************************************************************/ /*封装三维地形的类 */ /************************************************************************/ #ifndef __TERRAIN_H_ #define __TERRAIN_H_ #include <vector> #include <string> #pragma once struct TERRAINVERTEX//地形的顶点格式 { float _x, _y, _z; float _u, _v; TERRAINVERTEX(float x, float y, float z, float u, float v) : _x(x), _y(y),_z(z),_u(u),_v(v){} static const DWORD FVF = D3DFVF_XYZ | D3DFVF_TEX1; }; class CTerrain { private: LPDIRECT3DDEVICE9 m_pDevice; //设备指针 LPDIRECT3DTEXTURE9 m_pTexture; //纹理坐标 LPDIRECT3DINDEXBUFFER9 m_pIndexBuffer; //索引缓存 LPDIRECT3DVERTEXBUFFER9 m_pVertexBuffer; //顶点缓存 std::vector<float> m_vHeightInfo; //存放高度信息 int m_nCellsPerRow; //每行的单元数 int m_nCellsPerCol; //每列的单元数 int m_nVertsPerRow; //每行的顶点数 int m_nVertesPerCol; //每列的顶点数 int m_nNumVertices; //总共顶点数 float m_fTerrainWidth; //地形的宽度 float m_fTerrainDepth; //地形的深度 float m_fCellSpacing; //单元格的间距 float m_fHeightScale; //高度缩放系数 public: CTerrain(IDirect3DDevice9 *pDv); virtual ~CTerrain(void); //从文件中加载高度图以及纹理 bool LoadTerrainFromFile(std::string RawFilename, std::string TextureFilename); //地形初始化函数(行数,列数,间距,高度比例) bool InitTerrain(int nRows, int nCols, float fSpace, float fScale); //绘制地形函数 bool RenderTerrain(D3DXMATRIX* pMatWorld, bool bDrawFrame = false); }; #endif
#include "stdafx.h" #include "Terrain.h" #include <fstream> CTerrain::CTerrain(IDirect3DDevice9 *pDv) { //各种初始化 m_pDevice = pDv; m_pTexture = NULL; m_pIndexBuffer = NULL; m_pVertexBuffer = NULL; m_nCellsPerRow = 0; m_nCellsPerCol = 0; m_nVertsPerRow = 0; m_nVertesPerCol = 0; m_nNumVertices = 0; m_fTerrainWidth = 0.0f; m_fTerrainDepth = 0.0f; m_fCellSpacing = 0.0f; m_fHeightScale = 0.0f; } CTerrain::~CTerrain(void) { } bool CTerrain::LoadTerrainFromFile(std::string RawFilename, std::string TextureFilename) { //打开文件 using namespace std; ifstream inFile; inFile.open(RawFilename, std::ios::binary); //移动到文件尾 inFile.seekg(0, std::ios::end); std::vector<BYTE> inData(inFile.tellg()); inFile.seekg(std::ios::beg); inFile.read((char*)&inData[0], inData.size()); inFile.close(); m_vHeightInfo.resize(inData.size()); for (int i = 0; i < inData.size(); i++) { m_vHeightInfo[i] = inData[i]; } //加载地形纹理 if (FAILED(D3DXCreateTextureFromFile(m_pDevice, TextureFilename.c_str() ,&m_pTexture))) return false; return true; } bool CTerrain::InitTerrain(int nRows, int nCols, float fSpace, float fScale) { m_nCellsPerRow = nRows; m_nCellsPerCol = nCols; m_fCellSpacing = fSpace; m_fHeightScale = fScale; m_fTerrainWidth = m_fCellSpacing * m_nCellsPerRow; m_fTerrainDepth = m_fCellSpacing * m_nCellsPerCol; m_nVertsPerRow = m_nCellsPerRow + 1; m_nVertesPerCol = m_nCellsPerCol + 1; m_nNumVertices = m_nVertsPerRow * m_nVertesPerCol; //将高度与高度系数相乘,得到范围更广的高度 for (int i = 0; i < m_vHeightInfo.size(); i++) { m_vHeightInfo[i] *= m_fHeightScale; } //处理顶点 //创建 if (FAILED(m_pDevice->CreateVertexBuffer(m_nNumVertices * sizeof(TERRAINVERTEX), D3DUSAGE_WRITEONLY, TERRAINVERTEX::FVF, D3DPOOL_DEFAULT, &m_pVertexBuffer, 0))) return false; //加锁 TERRAINVERTEX *pVertices = NULL; m_pVertexBuffer->Lock(0, 0, (void**)&pVertices, 0); //访问,赋值 float fStartX = -m_fTerrainWidth / 2.0f; //起点的X坐标 float fEndX = m_fTerrainWidth / 2.0f; //终点的X坐标 float fStartZ = m_fTerrainDepth / 2.0f; //起点的Z坐标 float fEndZ = -m_fTerrainDepth / 2.0f; //终点的Z坐标 float fCoordU = 3.0f / (float)m_nCellsPerRow;//指定纹理的横坐标 float fCoordV = 3.0f / (float)m_nCellsPerCol;//指定纹理的纵坐标 int nIndex = 0, i = 0, j = 0; for (float z = fStartZ; z > fEndZ; z -= m_fCellSpacing, i++) { j = 0; for (float x = fStartX; x < fEndX; x += m_fCellSpacing, j++) { nIndex = i * m_nCellsPerRow + j; //指定顶点在顶点缓存的位置 pVertices[nIndex] = TERRAINVERTEX(x, m_vHeightInfo[nIndex], z, j * fCoordU, i * fCoordV);//将高度值以及纹理坐标信息赋值 nIndex++; } } //解锁 m_pVertexBuffer->Unlock(); //处理索引 //创建索引 if(FAILED(m_pDevice->CreateIndexBuffer(m_nNumVertices * 6 * sizeof(WORD), D3DUSAGE_WRITEONLY, D3DFMT_INDEX16, D3DPOOL_MANAGED, &m_pIndexBuffer, 0))) return false; //加锁 WORD* pIndices = NULL; m_pIndexBuffer->Lock(0, 0, (void**)&pIndices, 0); //赋值 nIndex = 0; for (int row = 0; row < m_nCellsPerRow - 1; row++)//行遍历 { for (int col = 0; col < m_nCellsPerCol - 1; col++)//列遍历 { //三角形ABC三个顶点 pIndices[nIndex] = row * m_nCellsPerRow + col;//A pIndices[nIndex + 1] = row * m_nCellsPerRow + col + 1;//B pIndices[nIndex + 2] = (row + 1) * m_nCellsPerRow + col;//C //三角形CBD三个顶点 pIndices[nIndex + 3] = (row + 1) * m_nCellsPerRow + col;//C pIndices[nIndex + 4] = row * m_nCellsPerRow + col + 1;//B pIndices[nIndex + 5] = (row + 1) * m_nCellsPerRow + col + 1;//D nIndex += 6; } } //解锁 m_pIndexBuffer->Unlock(); return true; } bool CTerrain::RenderTerrain(D3DXMATRIX *pMatWorld, bool bRenderFrame) { m_pDevice->SetStreamSource(0, m_pVertexBuffer, 0, sizeof(TERRAINVERTEX)); m_pDevice->SetFVF(TERRAINVERTEX::FVF); m_pDevice->SetIndices(m_pIndexBuffer); m_pDevice->SetTexture(0, m_pTexture); m_pDevice->SetRenderState(D3DRS_LIGHTING, false); m_pDevice->SetTransform(D3DTS_WORLD, pMatWorld); m_pDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, m_nNumVertices, 0, m_nNumVertices * 2); m_pDevice->SetRenderState(D3DRS_LIGHTING, true); m_pDevice->SetTexture(0, 0); return true; }
<span style="font-family: Arial, Helvetica, sans-serif; font-size: 12px;">CTerrain* g_pTerrian = NULL; //地形类指针</span>
<span style="font-family: Arial, Helvetica, sans-serif; font-size: 12px;"> </span>
//创建地形 void CreateTerrain() { g_pTerrian = new CTerrain(g_pDevice); g_pTerrian->LoadTerrainFromFile(TEXT("heighmap.raw"), TEXT("terraintexture.jpg")); g_pTerrian->InitTerrain(200, 200, 30.0f, 3.0f); }
//绘制地形 g_pTerrian->RenderTerrain(&matWorld4, false);
(露馅了...)
好了,下面是main函数.cpp文件,一个比较完整的demo:
// D3DDemo.cpp : 定义应用程序的入口点。 // #include "stdafx.h" #include "D3DDemo.h" #include "DirectInput.h" #include "Camera.h" #include "Terrain.h" #include "Mesh.h" #define MAX_LOADSTRING 100 // 全局变量: HINSTANCE hInst; // 当前实例 TCHAR szTitle[MAX_LOADSTRING]; // 标题栏文本 TCHAR szWindowClass[MAX_LOADSTRING]; // 主窗口类名 // 此代码模块中包含的函数的前向声明: HWND g_hWnd; ATOM MyRegisterClass(HINSTANCE hInstance); BOOL InitInstance(HINSTANCE, int); LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); //---------改造3D窗口需要的内容------------ LPDIRECT3D9 g_pD3D = NULL; //D3D接口指针 LPDIRECT3DDEVICE9 g_pDevice = NULL; //D3D设备指针 CDirectInput* g_pDirectInput = NULL; //控制指针 CCamera* g_pCamera = NULL; //摄像机指针 CTerrain* g_pTerrian = NULL; //地形类指针 CMesh* g_pMesh1 = NULL; //网格对象指针1 CMesh* g_pMesh2 = NULL; //网格对象指针2 CMesh* g_pMesh3 = NULL; //网格对象指针3 LPDIRECT3DTEXTURE9 g_pGroundTexture = NULL; //地面纹理 D3DXMATRIX g_matWorld; //世界矩阵 //------------绘制图形步骤1.定义灵活顶点格式 #define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_TEX1)//坐标为经过变换的屏幕坐标,顶点的颜色 //------------绘制图形步骤2.根据上面定义的顶点格式,创建一个顶点的结构体 struct stVertex { float _x, _y, _z; //位置坐标 float _u, _v; //纹理坐标 stVertex(float x, float y, float z, float u, float v) : _x(x), _y(y), _z(z), _u(u), _v(v){} stVertex(){} }; //----------绘制图形步骤3.声明一个顶点缓冲区指针&一个索引缓冲区指针 LPDIRECT3DVERTEXBUFFER9 g_pVB = NULL; LPDIRECT3DINDEXBUFFER9 g_pIB = NULL; //初始化顶点缓冲区 void initVB() { //----------绘制图形步骤4.定义一个结构体数组用来给每个顶点赋值 //数组中存储当前程序中顶点的数据 stVertex vertex[4]; // 正面顶点数据 vertex[0] = stVertex(-500.0f, 0.0f, -500.0f, 0.0f, 1.0f); vertex[1] = stVertex(-500.0f, 0.0f, 500.0f, 0.0f, 0.0f); vertex[2] = stVertex( 500.0f, 0.0f, -500.0f, 1.0f, 1.0f); vertex[3] = stVertex( 500.0f, 0.0f, 500.0f, 1.0f, 0.0f); //----------绘制图形步骤5.为定点缓冲区分配内存,并将数组中的顶点值拷贝到顶点缓冲区中 //通过设备指针来创建顶点缓冲区,用来存储顶点数据 g_pDevice->CreateVertexBuffer( sizeof(vertex), //顶点缓冲区大小 D3DUSAGE_WRITEONLY, //顶点缓冲区作用 D3DFVF_CUSTOMVERTEX, //通知系统顶点格式 D3DPOOL_MANAGED, //顶点缓冲区存储位置,此处表示由系统处理 &g_pVB, //返回顶点缓冲区指针 NULL //系统保留参数,NULL ); void* pVertices = NULL; //锁定顶点缓冲区,向其中拷贝数据 g_pVB->Lock( 0, //锁定的偏移量 sizeof(vertex), //锁定的大小 &pVertices, //锁定之后存储空间 0 //锁定的标识,0 ); //将数组中的内容拷贝到缓冲区中 memcpy(pVertices, vertex, sizeof(vertex)); //解锁 g_pVB->Unlock(); //创建纹理 D3DXCreateTextureFromFile(g_pDevice, TEXT("texture.png"), &g_pGroundTexture); } void onCreatD3D() { g_pD3D = Direct3DCreate9(D3D_SDK_VERSION); if (!g_pD3D) return; //检测硬件设备能力的方法 /*D3DCAPS9 caps; ZeroMemory(&caps, sizeof(caps)); g_pD3D->GetDeviceCaps(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &caps);*/ //获得相关信息,屏幕大小,像素点属性 D3DDISPLAYMODE d3ddm; ZeroMemory(&d3ddm, sizeof(d3ddm)); g_pD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &d3ddm); //设置全屏模式 D3DPRESENT_PARAMETERS d3dpp; ZeroMemory(&d3dpp, sizeof(d3dpp)); /*d3dpp.Windowed = false; d3dpp.BackBufferWidth = d3ddm.Width; d3dpp.BackBufferHeight = d3ddm.Height;*/ d3dpp.Windowed = true; d3dpp.BackBufferFormat = d3ddm.Format; d3dpp.BackBufferCount = 1; d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;//交换后原缓冲区数据丢弃 //是否开启自动深度模板缓冲 d3dpp.EnableAutoDepthStencil = true; //当前自动深度模板缓冲的格式 d3dpp.AutoDepthStencilFormat = D3DFMT_D16;//每个像素点有16位的存储空间,存储离摄像机的距离 g_pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, g_hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &g_pDevice); if (!g_pDevice) return; //设置渲染状态,设置启用深度值 g_pDevice->SetRenderState(D3DRS_ZENABLE, true); //设置渲染状态,关闭灯光 g_pDevice->SetRenderState(D3DRS_LIGHTING, false); //设置渲染状态,裁剪模式 g_pDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE); //g_pDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE) ; } void CreateMesh() { g_pMesh1 = new CMesh(g_pDevice); g_pMesh1->CreateMesh("miki.X"); g_pMesh2 = new CMesh(g_pDevice); g_pMesh2->CreateMesh("Demon.X"); g_pMesh3 = new CMesh(g_pDevice); g_pMesh3->CreateMesh("dragon.X"); } void CreateCamera() { g_pCamera = new CCamera(g_pDevice); g_pCamera->SetCameraPosition(&D3DXVECTOR3(0.0f, 500.0f, -500.0f)); g_pCamera->SetTargetPosition(&D3DXVECTOR3(0.0f, 500.0f, 0.0f)); g_pCamera->SetViewMatrix(); g_pCamera->SetProjectionMartix(); } void CreateTerrain() { g_pTerrian = new CTerrain(g_pDevice); g_pTerrian->LoadTerrainFromFile(TEXT("heighmap.raw"), TEXT("terraintexture.jpg")); g_pTerrian->InitTerrain(200, 200, 30.0f, 3.0f); } void onInit() { //初始化D3D onCreatD3D(); //初始化顶点缓冲区 initVB(); //创建Mesh模型 CreateMesh(); //创建摄像机 CreateCamera(); //创建地形 CreateTerrain(); } void onDestroy() { SAFE_DELETE(g_pDirectInput); SAFE_DELETE(g_pCamera); SAFE_DELETE(g_pTerrian); SAFE_RELEASE(g_pDevice); } void onLogic(float fElapsedTime) { //使用DirectInput类读取数据 g_pDirectInput->GetInput(); // 沿摄像机各分量移动视角 if (g_pDirectInput->IsKeyDown(DIK_A)) g_pCamera->MoveAlongRightVec(-10.0f); if (g_pDirectInput->IsKeyDown(DIK_D)) g_pCamera->MoveAlongRightVec( 10.0f); if (g_pDirectInput->IsKeyDown(DIK_W)) g_pCamera->MoveAlongLookVec( 10.0f); if (g_pDirectInput->IsKeyDown(DIK_S)) g_pCamera->MoveAlongLookVec(-10.0f); if (g_pDirectInput->IsKeyDown(DIK_I)) g_pCamera->MoveAlongUpVec( 10.0f); if (g_pDirectInput->IsKeyDown(DIK_K)) g_pCamera->MoveAlongUpVec(-10.0f); //沿摄像机各分量旋转视角 if (g_pDirectInput->IsKeyDown(DIK_LEFT)) g_pCamera->RotationUpVec(-0.003f); if (g_pDirectInput->IsKeyDown(DIK_RIGHT)) g_pCamera->RotationUpVec( 0.003f); if (g_pDirectInput->IsKeyDown(DIK_UP)) g_pCamera->RotationRightVec(-0.003f); if (g_pDirectInput->IsKeyDown(DIK_DOWN)) g_pCamera->RotationRightVec( 0.003f); if (g_pDirectInput->IsKeyDown(DIK_J)) g_pCamera->RotationLookVec(-0.001f); if (g_pDirectInput->IsKeyDown(DIK_L)) g_pCamera->RotationLookVec( 0.001f); //鼠标控制右向量和上向量的旋转 //g_pCamera->RotationUpVec(g_pDirectInput->MouseDX()* 0.001f); //g_pCamera->RotationRightVec(g_pDirectInput->MouseDY() * 0.001f); //鼠标滚轮控制观察点收缩操作 static FLOAT fPosZ=0.0f; fPosZ += g_pDirectInput->MouseDZ()*3.0f; //计算并设置取景变换矩阵 D3DXMATRIX matView; g_pCamera->CalculateViewMatrix(&matView); g_pDevice->SetTransform(D3DTS_VIEW, &matView); //把正确的世界变换矩阵存到g_matWorld中 D3DXMatrixTranslation(&g_matWorld, 0.0f, 0.0f, fPosZ); } void Transform() { //WorldTransform:世界变换 D3DXMATRIXA16 matWorld; D3DXMATRIXA16 matScaling; //生成缩放矩阵 D3DXMatrixScaling(&matScaling, 0.8f, 0.8f, 0.8f); //生成绕Y轴旋转矩阵,存储于矩阵中 D3DXMatrixRotationY( &matWorld, //输出矩阵 10.0f //角度 ); matWorld = matScaling * matWorld; g_pDevice->SetTransform(D3DTS_WORLD, &matWorld); //ViewTransform:取景变换 D3DXVECTOR3 vEyePt(0.0f, 0.0f, -500.0f); //摄像机世界坐标 D3DXVECTOR3 vLookatPt(0.0f, 0.0f, 0.0f); //观察点世界坐标 D3DXVECTOR3 vUpVec(0.0f, 1.0f, 0.0f); //摄像机的上向量,通常为(0.0f, 1.0f, 0.0f) D3DXMATRIXA16 matView; //View变换的矩阵 //根据上面的结果计算出矩阵,存入矩阵中 D3DXMatrixLookAtLH(&matView, &vEyePt, &vLookatPt, &vUpVec); //进行取景变换 g_pDevice->SetTransform(D3DTS_VIEW, &matView); //ProjectionTransform:投影变换 D3DXMATRIXA16 matProj; //投影变换矩阵 //生成投影变换矩阵,存入上面的矩阵中 D3DXMatrixPerspectiveFovLH( &matProj, //输出结果矩阵 D3DX_PI / 4, //视域角度,一般为PI/4 1.0f, //显示屏的长宽比 1.0f, //视截体中近截面距离摄像机的位置 1000.0f //视截体中远截面距离摄像机的位置 ); //进行投影变换 g_pDevice->SetTransform(D3DTS_PROJECTION, &matProj); } void onRender(float fElasedTime) { //前两个参数是0和NULL时,清空整个游戏窗口的内容(清的是后台) //第三个是清除的对象:前面表示清除颜色缓冲区,后面表示清除深度缓冲区,D3DCLEAR_STENCIL清空模板缓冲区 g_pDevice->Clear(0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0,100,100), 1.0f, 0); g_pDevice->BeginScene(); //Transform(); //////----------绘制图形步骤6.设置数据源,设置灵活顶点格式,绘制图元 // //设置数据流来源 //g_pDevice->SetStreamSource( // 0, //数据流管道号(0-15) // g_pVB, //数据来源 // 0, //数据流偏移量 // sizeof(stVertex) //每个数据的字节数大小 // ); // //通知系统数据格式,以便解析数据 //g_pDevice->SetFVF(D3DFVF_CUSTOMVERTEX); //D3DXMATRIX matWorld; //D3DXMatrixTranslation(&matWorld, 0.0f, 0.0f, 200.0f); //g_pDevice->SetRenderState(D3DRS_LIGHTING, false); //g_pDevice->SetTransform(D3DTS_WORLD, &matWorld); //g_pDevice->SetTexture(0, g_pGroundTexture); ////绘制图元 //g_pDevice->DrawPrimitive( // D3DPT_TRIANGLESTRIP, //三角形列 // 0, //起始点编号 // 2 //图元数量 // ); D3DXMATRIX matWorld1, matWorld2, matWorld3, matWorld4; D3DXMatrixTranslation(&matWorld1, 0.0f, 600.0f, 0.0f); D3DXMatrixTranslation(&matWorld2, 500.0f, 500.0f, 0.0f); D3DXMatrixTranslation(&matWorld3, 0.0f, 500.0f, 0.0f); D3DXMatrixTranslation(&matWorld4, 0.0f, 0.0f, 0.0f); g_pMesh1->DrawMesh(matWorld1); g_pMesh2->DrawMesh(matWorld2); g_pMesh3->DrawMesh(matWorld3); g_pTerrian->RenderTerrain(&matWorld4, false); g_pDevice->EndScene(); g_pDevice->Present(NULL, NULL, NULL, NULL); } int APIENTRY _tWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPTSTR lpCmdLine, _In_ int nCmdShow) { UNREFERENCED_PARAMETER(hPrevInstance); UNREFERENCED_PARAMETER(lpCmdLine); // TODO: 在此放置代码。 MSG msg; HACCEL hAccelTable; // 初始化全局字符串 LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING); LoadString(hInstance, IDC_D3DDEMO, szWindowClass, MAX_LOADSTRING); MyRegisterClass(hInstance); // 执行应用程序初始化: if (!InitInstance (hInstance, nCmdShow)) { return FALSE; } hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_D3DDEMO)); ZeroMemory(&msg, sizeof(msg)); while (msg.message != WM_QUIT) { if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) { TranslateMessage(&msg); DispatchMessage(&msg); } else { static DWORD dwTime = timeGetTime(); DWORD dwCurrentTime = timeGetTime(); DWORD dwElapsedTime = dwCurrentTime - dwTime; float fElapsedTime = dwElapsedTime * 0.001f; //------------渲染和逻辑部分代码---------- onLogic(fElapsedTime); onRender(fElapsedTime); //----------------------------------------- if (dwElapsedTime < 1000 / 60) { Sleep(1000/ 60 - dwElapsedTime); } dwTime = dwCurrentTime; } } onDestroy(); return (int) msg.wParam; } // // 函数: MyRegisterClass() // // 目的: 注册窗口类。 // ATOM MyRegisterClass(HINSTANCE hInstance) { WNDCLASSEX wcex; wcex.cbSize = sizeof(WNDCLASSEX); wcex.style = CS_HREDRAW | CS_VREDRAW; wcex.lpfnWndProc = WndProc; wcex.cbClsExtra = 0; wcex.cbWndExtra = 0; wcex.hInstance = hInstance; wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_D3DDEMO)); wcex.hCursor = LoadCursor(NULL, IDC_ARROW); wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); wcex.lpszMenuName = MAKEINTRESOURCE(IDC_D3DDEMO); wcex.lpszClassName = szWindowClass; wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL)); return RegisterClassEx(&wcex); } // // 函数: InitInstance(HINSTANCE, int) // // 目的: 保存实例句柄并创建主窗口 // // 注释: // // 在此函数中,我们在全局变量中保存实例句柄并 // 创建和显示主程序窗口。 // BOOL InitInstance(HINSTANCE hInstance, int nCmdShow) { hInst = hInstance; // 将实例句柄存储在全局变量中 g_hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL); if (!g_hWnd) { return FALSE; } //初始化DirectInput类 g_pDirectInput = new CDirectInput(); g_pDirectInput->Init(g_hWnd, hInst, DISCL_FOREGROUND|DISCL_NONEXCLUSIVE, DISCL_FOREGROUND|DISCL_NONEXCLUSIVE); SetMenu(g_hWnd, NULL); ShowWindow(g_hWnd, nCmdShow); UpdateWindow(g_hWnd); onInit(); return TRUE; } // // 函数: WndProc(HWND, UINT, WPARAM, LPARAM) // // 目的: 处理主窗口的消息。 // // WM_COMMAND - 处理应用程序菜单 // WM_PAINT - 绘制主窗口 // WM_DESTROY - 发送退出消息并返回 // // LRESULT CALLBACK WndProc(HWND g_hWnd, UINT message, WPARAM wParam, LPARAM lParam) { switch (message) { case WM_KEYDOWN: if (wParam == VK_ESCAPE) PostQuitMessage(0); break; case WM_CLOSE: DestroyWindow(g_hWnd); break; case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(g_hWnd, message, wParam, lParam); } return 0; }