📄 main.cpp
字号:
//声明调用头文件,以便调用其中声明的函数
#include "d3d8.h"
//一个指向IDirect3D8接口的指针
LPDIRECT3D8 g_pD3D = NULL;
//一个指向Direct3DDevice设备接口的指针
LPDIRECT3DDEVICE8 g_pD3DDevice = NULL;
//初始化函数,返回一个long型(HRESULT)变量
HRESULT InitialiseD3D(HWND hWnd)
{
//First of all, create the main D3D object. If it is created successfully we
//创建一个D3D对象,对象创建成功返回一个指向该IDirect3D8接口的指针,否则返回一个long型常量表示对象创建失败
//should get a pointer to an IDirect3D8 interface.
g_pD3D = Direct3DCreate8(D3D_SDK_VERSION);
if(g_pD3D == NULL)
{
//E_FAIL表示一个长整型数0x80004005L
return E_FAIL;
}
//Get the current display mode
//声明一个用于保存显示模式的结构体
/*****************************
typedef struct _D3DDISPLAYMODE
{
UINT Width;
UINT Height;
UINT RefreshRate;
D3DFORMAT Format;
} D3DDISPLAYMODE;
********************************/
D3DDISPLAYMODE d3ddm;
//获取适配器显示模式保存在d3ddm中,失败返回E_FAIL
if(FAILED(g_pD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &d3ddm)))
{
return E_FAIL;
}
//创建一个结构体用于保存当前显示设置
//Create a structure to hold the settings for our device
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory(&d3dpp, sizeof(d3dpp));
//填充结构体
//Fill the structure.
//设置程序运行在当前窗口,设置背景缓冲区格式
//We want our program to be windowed, and set the back buffer to a format
//匹配当前显示模式
//that matches our current display mode
d3dpp.Windowed = true;
d3dpp.SwapEffect = D3DSWAPEFFECT_COPY_VSYNC;
d3dpp.BackBufferFormat = d3ddm.Format;
//创建Direct3D设备对象。
//Create a Direct3D device.
if(FAILED(g_pD3D->CreateDevice( D3DADAPTER_DEFAULT,//指向默认的适配器
D3DDEVTYPE_HAL, //希望创建的设备类型,如无法得到指定的设备类型创建对象将失败
hWnd, //设备句柄
D3DCREATE_SOFTWARE_VERTEXPROCESSING, //指定顶点处理方式,由软件控制或由硬件控制
&d3dpp, //指向当前设置的对象指针
&g_pD3DDevice))) //指向该方法创建的设备对象指针
{
return E_FAIL;
}
return S_OK;
}
//渲染方法
void Render()
{
if(g_pD3DDevice == NULL)
{
return;
}
//将背景缓冲清理成绿色背景
//Clear the backbuffer to a green color
g_pD3DDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 255, 0), 1.0f, 0);
//开始绘制场景
//Begin the scene
g_pD3DDevice->BeginScene();
//此处添加需要渲染的game对象
//Rendering of our game objects will go here
//结束场景绘制
//End the scene
g_pD3DDevice->EndScene();
//将后缓冲设置为前缓冲(当前显示桢)
//Filp the back and front buffers so that whatever has been rendered on the back buffer
//will now be visible on screen (front buffer).
g_pD3DDevice->Present(NULL, NULL, NULL, NULL);
}
//清理设备资源和D3D对象
void CleanUp()
{
if(g_pD3DDevice != NULL)
{
//释放设备资源
g_pD3DDevice->Release();
g_pD3DDevice = NULL;
}
if(g_pD3D != NULL)
{
//释放D3D对象资源
g_pD3D->Release();
g_pD3D = NULL;
}
}
//游戏场景循环
void GameLoop()
{
//开始场景循环
//Enter the game loop
MSG msg;
BOOL fMessage;
PeekMessage(&msg, NULL, 0U, 0U, PM_NOREMOVE);
while(msg.message != WM_QUIT)
{
fMessage = PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE);
if(fMessage)
{
//消息处理
//Process message
//解释消息
TranslateMessage(&msg);
//派遣消息
DispatchMessage(&msg);
}
else
{
//No message to process, so render the current scene
//无消息处理则渲染当前场景
Render();
}
}
}
//windows消息处理函数
//The windows message handler
LRESULT WINAPI WinProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
//通过消息循环接收消息并判断处理
switch(msg)
{ //窗口销毁消息
case WM_DESTROY:
//用于线程的终止请求。
PostQuitMessage(0);
return 0;
break;
case WM_KEYUP:
switch (wParam)
{
case VK_ESCAPE:
//User has pressed the escape key, so quit
//当用户按下escape键,退出程序。
DestroyWindow(hWnd);
return 0;
break;
}
break;
}
return DefWindowProc(hWnd, msg, wParam, lParam);
}
//应用程序入口点
//Application entry point
INT WINAPI WinMain(HINSTANCE hInst, HINSTANCE, LPSTR, INT)
{
//注册windows窗体类
//Register the window class
WNDCLASSEX wc = { sizeof(WNDCLASSEX),
CS_CLASSDC,
WinProc,
0L,
0L,
GetModuleHandle(NULL),
NULL,
NULL,
NULL,
NULL,
"DX Project 1",
NULL
};
RegisterClassEx(&wc);
//创建一个windows窗体对象
//Create the application's window
HWND hWnd = CreateWindow( "DX Project 1",
"www.andypike.com: Tutorial 1",
WS_OVERLAPPEDWINDOW,
50,
50,
500,
500,
GetDesktopWindow(),
NULL,
wc.hInstance,
NULL );
//初始化Direct3D对象
//Initialize Direct3D
if(SUCCEEDED(InitialiseD3D(hWnd)))
{
//显示之前创建的窗体
//Show our window
ShowWindow(hWnd, SW_SHOWDEFAULT);
UpdateWindow(hWnd);
//开始游戏循环
//Start game running: Enter the game loop
GameLoop();
}
CleanUp();
UnregisterClass("DX Project 1", wc.hInstance);
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -