学习一门开发语言的第一件事情当然是搭建一个可运行的环境,然后运行一个可成功执行的案例。
本人在学习DirectX的第一个工程的时候,参考了雨凇MoMo的一篇文章(https://www.xuanyusong.com/archives/802),但是看了之后,还是不知道如何才能运行起来。
然后,又参考了一篇文章(https://blog.****.net/chenwu_843402773/article/details/8077935)。
通过参考第二篇文章中的一些内容,最终把第一篇文章中的案例运行了起来,在此做一下记录,具体原理,相信随着学习 Direct 的逐渐升入,会理解的,现在不做过多说明,哈哈哈哈哈哈。。。
#include<d3d9.h> #define WINDOW_CLASS "UGPDX" //宏定义窗口类名称
#define WINDOW_NAME "MyFirstDirectProject" //宏定义窗口名称 LRESULT WINAPI MsgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
LPDIRECT3D9 g_pD3D = NULL;
LPDIRECT3DDEVICE9 g_pDevice = NULL;
HRESULT InitD3D(HWND hwnd);
void Cleanup();
void Render(); int WINAPI WinMain(HINSTANCE hInst, HINSTANCE prevhInst, LPSTR cmdLine, int show)
{
WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, MsgProc, 0L, 0L,
GetModuleHandle(NULL), NULL, NULL, NULL, NULL,
WINDOW_CLASS, NULL };
RegisterClassEx(&wc); HWND hWnd = CreateWindow(WINDOW_CLASS, WINDOW_NAME, WS_OVERLAPPEDWINDOW,
, , , , GetDesktopWindow(), NULL,
wc.hInstance, NULL); if (InitD3D(hWnd) == S_OK)
{
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
{
Render();
}
}
} UnregisterClass(WINDOW_CLASS, wc.hInstance); return ;
}
LRESULT WINAPI MsgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_DESTROY:
PostQuitMessage();
return ;
break; case WM_KEYUP:
if (wParam == VK_ESCAPE) PostQuitMessage();
break;
} return DefWindowProc(hWnd, msg, wParam, lParam);
} HRESULT InitD3D(HWND hwnd)
{
if (NULL == (g_pD3D = Direct3DCreate9(D3D_SDK_VERSION)))
{
return E_FAIL;
} D3DPRESENT_PARAMETERS d3dpparams;
ZeroMemory(&d3dpparams, sizeof(d3dpparams));
d3dpparams.Windowed = true;
d3dpparams.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpparams.BackBufferFormat = D3DFMT_UNKNOWN; if (FAILED(g_pD3D->CreateDevice(D3DADAPTER_DEFAULT,
D3DDEVTYPE_HAL,
hwnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpparams,
&g_pDevice)))
{
return E_FAIL;
} return S_OK;
} void Cleanup()
{
if (NULL != g_pDevice)
{
g_pDevice->Release();
}
if (NULL != g_pD3D)
{
g_pD3D->Release();
}
} void Render()
{
if (NULL == g_pDevice)
{
return;
} g_pDevice->Clear(, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(, , ), 1.0f, ); if (SUCCEEDED(g_pDevice->BeginScene()))
{
g_pDevice->EndScene();
} g_pDevice->Present(NULL, NULL, NULL, NULL);
}
运行结果: