一切从零开始,一切皆有可能。
浅墨,90后,《逐梦之旅》深入浅出,堪比大师。
1.安装 DXSDK_June10.exe 或更新版本。
2.运行 vs2012,新建 VC Win32 空项目 x01.Game.Main。
3.在项目属性 Directory 中添加 SDK 中的 Include 和 Lib\X86。在链接 Input 中添加相关依赖项如:d3d9.lib;d3dx9.lib; 等。
4.添加源文件 Main.cpp,内容如下:
// Main.cpp (2013.11.18 by x01)
//
// 1.安装 DirectX SDK 后,需在项目属性 Directory 中分别导入 SDK 中的 Include 和 Lib\X86。
// 2.在链接 Input 中添加相关依赖项: d3d9.lib; d3dx9.lib; #include <Windows.h>
#include <d3d9.h> #define RELEASE(p) if ((p) != NULL) { (p)->Release(); (p) = NULL; } const int ScreenWidth = ;
const int ScreenHeight = ; LPDIRECT3D9 g_D3D = NULL;
LPDIRECT3DDEVICE9 g_Device = NULL; LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
bool InitD3D(HWND hwnd, bool isFullScreen);
void GameRender();
void GameClear(); int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrev, LPSTR cmdLine, int cmdShow)
{
WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, WndProc, , , hInstance, NULL, NULL, NULL, NULL,
L"x01GameMainClass", NULL };
RegisterClassEx(&wc); HWND hwnd = CreateWindow(L"x01GameMainClass", L"x01 Game", WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, ScreenWidth, ScreenHeight, NULL, NULL, hInstance, NULL); UnregisterClass(wc.lpszClassName, wc.hInstance); if (!InitD3D(hwnd, false))
{
return ;
} ShowWindow(hwnd, SW_SHOWDEFAULT);
UpdateWindow(hwnd); MSG msg;
ZeroMemory(&msg, sizeof(msg)); while (msg.message != WM_QUIT)
{
if (PeekMessage(&msg, NULL, , , PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{
GameRender();
}
} GameClear(); return ;
} LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_DESTROY:
PostQuitMessage();
break;
case WM_KEYUP:
if (wParam == VK_ESCAPE)
{
PostQuitMessage();
}
break;
default:
break;
} return DefWindowProc(hwnd, message, wParam, lParam);
} bool InitD3D(HWND hwnd, bool isFullScreen)
{
g_D3D = Direct3DCreate9(D3D_SDK_VERSION);
if (g_D3D == NULL)
{
MessageBox(hwnd, L"Create D3d failed.", L"Error", MB_OK);
return false;
} D3DDISPLAYMODE displayMode;
if (FAILED(g_D3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &displayMode)))
{
MessageBox(hwnd, L"Get Adapter Display Mode Failed.", L"Error", MB_OK); return false;
} D3DPRESENT_PARAMETERS pp;
ZeroMemory(&pp, sizeof(pp)); if (isFullScreen)
{
pp.Windowed = false;
pp.BackBufferWidth = ScreenWidth;
pp.BackBufferHeight = ScreenHeight;
}
else
{
pp.Windowed = true;
} pp.SwapEffect = D3DSWAPEFFECT_DISCARD;
pp.BackBufferFormat = displayMode.Format; if (FAILED(g_D3D->CreateDevice(
D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd, D3DCREATE_HARDWARE_VERTEXPROCESSING,&pp,&g_Device) ))
{
MessageBox(hwnd, L"Create Device Failed.", L"Error", MB_OK); return false;
} return true;
} void GameRender()
{
g_Device->Clear(, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(,,), 1.0f, );
g_Device->BeginScene();
g_Device->EndScene();
g_Device->Present(NULL, NULL, NULL, NULL);
} void GameClear()
{
RELEASE(g_Device);
RELEASE(g_D3D);
}
Main.cp
5.按 F5 调试运行,效果图如下:
该程序是对浅墨的高度模仿,但这并不妨碍我们学习。博客不仅是为了分享,更是对自己的一种总结。
以上项目可作为模板保存。什么时候需要新建项目,可将该模板简单 复制 重命名 即可。