/*
directx9学习笔记之二:初始化D3D,划线 */#include "resource.h"//保护本地资源,程序图标
//保护d3d头文件和库文件#include <d3d9.h>#include <d3dx9.h>#pragma comment(lib, "d3d9.lib")
#pragma comment(lib, "d3dx9.lib")//定义窗体类名称、窗体标题、窗体宽度和高度#define WINDOW_CLASS "Agame"#define WINDOW_NAME "我的第一个游戏"#define WINDOW_WIDTH 800#define WINDOW_HEIGHT 600#define WINDOW_TYPE WS_OVERLAPPEDWINDOW&~WS_MAXIMIZEBOX//层叠窗口、禁用最大化按钮// 定义函数LRESULT WINAPI MsgProc(HWND hWnd, UINT msg, WPARAM wp, LPARAM lp);int WINAPI WinMain(HINSTANCE hInst, HINSTANCE prevhInst,LPSTR cmdLine, int show);bool InitializeD3D(HWND hWnd, bool fullscreen);bool InitializeObjects();void RenderScene();void ReleaseD3D(); // 定义d3d接口、设备LPDIRECT3D9 g_D3D = NULL;LPDIRECT3DDEVICE9 g_D3DDevice = NULL;// 定义定点缓冲
LPDIRECT3DVERTEXBUFFER9 g_VertexBuffer = NULL; // 定义顶点结构struct stD3DVertex{ float x, y, z, rhw; unsigned long color;};// 定义顶点格式标识符:x、y、z、深度、漫反射颜色
#define D3DFVF_VERTEX (D3DFVF_XYZRHW | D3DFVF_DIFFUSE)//消息处理函数
LRESULT WINAPI MsgProc(HWND hWnd, UINT msg, WPARAM wp, LPARAM lp){ switch(msg) { case WM_DESTROY: PostQuitMessage(0); ReleaseD3D(); return 0; break;case WM_KEYUP:
if(wp == VK_ESCAPE) PostQuitMessage(0); break; }return DefWindowProc(hWnd, msg, wp, lp);
}//winmain函数
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE prevhInst, LPSTR cmdLine, int show){ // 定义windows类,并赋值 WNDCLASSEX wcex;wcex.cbSize=sizeof(wcex);
wcex.style=CS_CLASSDC; wcex.lpfnWndProc=MsgProc; wcex.cbClsExtra=0; wcex.cbWndExtra=0; wcex.hInstance=hInst; wcex.hIcon=LoadIcon(hInst,MAKEINTRESOURCE(IDI_APP)); wcex.hCursor=LoadCursor(NULL,IDC_ARROW); wcex.hbrBackground=NULL;//(HBRUSH)(COLOR_WINDOW+1); wcex.lpszMenuName=NULL; wcex.lpszClassName=WINDOW_CLASS; wcex.hIconSm=LoadIcon(hInst,MAKEINTRESOURCE(IDI_APP)); RegisterClassEx(&wcex);//注册windows类// 创建窗口
HWND hWnd = CreateWindow(WINDOW_CLASS, WINDOW_NAME,WINDOW_TYPE, (GetSystemMetrics(SM_CXSCREEN)-800)/2,(GetSystemMetrics(SM_CYSCREEN)-600)/2,WINDOW_WIDTH, WINDOW_HEIGHT,NULL, NULL,hInst, NULL);// 如果初始化d3d成功,则显示窗口
if(InitializeD3D(hWnd, false)) { // 显示并更新窗口 ShowWindow(hWnd, SW_SHOWDEFAULT); UpdateWindow(hWnd);//进入消息循环
MSG msg; ZeroMemory(&msg, sizeof(msg));while(msg.message != WM_QUIT)
{ if(PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE)) { TranslateMessage(&msg); DispatchMessage(&msg); } else RenderScene(); } }// 释放d3d
ReleaseD3D();// 取消windows类注册
UnregisterClass(WINDOW_CLASS, wcex.hInstance); return 0;}//初始化d3d
bool InitializeD3D(HWND hWnd, bool fullscreen){ D3DDISPLAYMODE displayMode;// 创建d3d接口
g_D3D = Direct3DCreate9(D3D_SDK_VERSION); if(g_D3D == NULL) return false; // Get the desktop display mode. if(FAILED(g_D3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT,&displayMode))) return false; //D3DADAPTER_DEFAULT桌面窗口显示模式// 设置显示参数
D3DPRESENT_PARAMETERS d3dpp; ZeroMemory(&d3dpp, sizeof(d3dpp)); if(fullscreen) { d3dpp.Windowed = FALSE;//是否窗口显示 } else { d3dpp.Windowed = TRUE;//是否窗口显示 d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;//窗口切换效果 d3dpp.BackBufferFormat = displayMode.Format;//后台缓冲格式 } d3dpp.BackBufferWidth = WINDOW_WIDTH;//后台缓冲窗口宽度、高度 d3dpp.BackBufferHeight = WINDOW_HEIGHT; // 创建d3d设备---D3DADAPTER_DEFAULT主显卡 if(FAILED(g_D3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_HARDWARE_VERTEXPROCESSING, &d3dpp, &g_D3DDevice))) return false; // 划线 if(!InitializeObjects()) return false;return true;
}//初始化划线参数
bool InitializeObjects(){ unsigned long col = D3DCOLOR_XRGB(255, 255, 255);// 创建顶点数组
//顶点数组:x坐标、y坐标、z坐标、深度、颜色 stD3DVertex objData[] = { { 0.0f, 0.0f, 0.0f, 1.0f, col}, { 800.0f, 600.0f, 0.0f, 1.0f, col}, { 0.0f, 600.0f, 0.0f, 1.0f, col}, { 800.0f, 0.0f, 0.0f, 1.0f, col}, };// 创建顶点缓冲
if(FAILED(g_D3DDevice->CreateVertexBuffer(sizeof(objData), 0, D3DFVF_VERTEX, D3DPOOL_DEFAULT, &g_VertexBuffer, NULL))) return false;// 填充顶点缓冲
void *ptr;if(FAILED(g_VertexBuffer->Lock(0, sizeof(objData),
(void**)&ptr, 0))) return false;memcpy(ptr, objData, sizeof(objData));
g_VertexBuffer->Unlock();
return true;
}//渲染函数
void RenderScene(){ //清除d3d设备缓存. g_D3DDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,0), 1.0f, 0);// 开始渲染
g_D3DDevice->BeginScene();// 将顶点缓存设置为渲染流
g_D3DDevice->SetStreamSource(0, g_VertexBuffer, 0,sizeof(stD3DVertex)); g_D3DDevice->SetFVF(D3DFVF_VERTEX);//设置顶点渲染格式 g_D3DDevice->DrawPrimitive(D3DPT_LINELIST, 0, 2);//绘制要渲染的图元// 结束渲染
g_D3DDevice->EndScene();// Display the scene.
g_D3DDevice->Present(NULL, NULL, NULL, NULL);//在屏幕上显示渲染结果} void ReleaseD3D(){ if(g_D3DDevice != NULL) g_D3DDevice->Release(); if(g_D3D != NULL) g_D3D->Release(); if(g_VertexBuffer != NULL) g_VertexBuffer->Release();g_D3DDevice = NULL;
g_D3D = NULL; g_VertexBuffer = NULL;}