头文件:
#include <windows.h>
#include <d3d11.h>
#include <d3dx11.h>
#include <d3dcompiler.h>
#include <xnamath.h>
#include "resource.h"
结构体声明:
// Structures
struct SimpleVertex
{
D3DXVECTOR3 Pos;
};
全局变量声明:
// Global Variables
HINSTANCE g_hInst = NULL; //实例句柄
HWND g_hWnd = NULL; //窗口句柄
D3D_DRIVER_TYPE g_driverType = D3D_DRIVER_TYPE_NULL; //驱动类型
D3D_FEATURE_LEVEL g_featureLevel = D3D_FEATURE_LEVEL_11_0; //特征等级
ID3D11Device* g_pd3dDevice = NULL; //设备接口指针
ID3D11DeviceContext* g_pImmediateContext = NULL; //设备上下文接口指针
IDXGISwapChain* g_pSwapChain = NULL; //交换链接口指针
ID3D11RenderTargetView* g_pRenderTargetView = NULL; //渲染目标视图接口指针
ID3D11VertexShader* g_pVertexShader = NULL; //顶点着色器接口指针
ID3D11PixelShader* g_pPixelShader = NULL; //像素着色器接口指针
ID3D11InputLayout* g_pVertexLayout = NULL; //输入布局接口指针
ID3D11Buffer* g_pVertexBuffer = NULL; //缓冲区接口指针
注:
D3D10_DRIVER_TYPE的MSDN资料:
https://msdn.microsoft.com/pt-br/library/windows/apps/bb205042.aspx
函数声明:
// Forward declarations
HRESULT InitWindow( HINSTANCE hInstance, int nCmdShow ); //初始化窗口函数
HRESULT InitDevice(); //初始化设备函数
void CleanupDevice(); //清除设备函数
LRESULT CALLBACK WndProc( HWND, UINT, WPARAM, LPARAM ); //窗口过程函数
void Render(); //渲染函数
程序的入口点函数:
// Entry point to the program. Initializes everything and goes into a message processing loop. Idle time is used to render the scene.
int WINAPI wWinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nCmdShow )
{
UNREFERENCED_PARAMETER( hPrevInstance );
UNREFERENCED_PARAMETER( lpCmdLine );
//初始化窗口
if( FAILED( InitWindow( hInstance, nCmdShow ) ) )
return 0;
//初始化设备
if( FAILED( InitDevice() ) )
{
//初始化失败,清除设备
CleanupDevice();
return 0;
}
//消息主循环
// Main message loop
//消息结构体MSG
MSG msg = {
0};
//当没有接收到退出程序消息时,继续循环
while( WM_QUIT != msg.message )
{
//获取消息
//如果有消息,则对消息进行转译、分发
if( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
//如果没有消息,则进行渲染
else
{
Render();
}
}
//清除设备
CleanupDevice();
return ( int )msg.wParam;
}
初始化窗口函数:
// 该函数用于注册窗口类以及创建窗口
// Register class and create win