?? main.cpp
字號:
#include <d3d8.h>
//一個指向DIRECT3D8對象的指針
LPDIRECT3D8 g_pD3D = NULL;
//一個指向DIRECT3DDEVICE8設備對象的指針
LPDIRECT3DDEVICE8 g_pD3DDevice = NULL;
// Buffer to hold vertices
//聲明一個用于保存頂點的緩沖區
LPDIRECT3DVERTEXBUFFER8 g_pVertexBuffer = NULL;
struct CUSTOMVERTEX
{
// The transformed position for the vertex.
// 頂點坐標的變換
FLOAT x, y, z, rhw;
// The vertex colour.
//頂點顏色
DWORD colour;
};
#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZRHW|D3DFVF_DIFFUSE)
#define SafeRelease(pObject) if(pObject != NULL) {pObject->Release(); pObject=NULL;}
//初始化D3D對象
HRESULT InitialiseD3D(HWND hWnd)
{
//First of all, create the main D3D object. If it is created successfully we
//should get a pointer to an IDirect3D8 interface.
//構造一個D3D對象
g_pD3D = Direct3DCreate8(D3D_SDK_VERSION);
if(g_pD3D == NULL)
{
return E_FAIL;
}
//Get the current display mode
//得到現實模式
D3DDISPLAYMODE d3ddm;
if(FAILED(g_pD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &d3ddm)))
{
return E_FAIL;
}
//Create a structure to hold the settings for our device
//申明一個D3DPRESENT_PARAMETERS結構體用來保存當前顯示設備設置
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;
//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;
}
//初始化頂點緩沖區
HRESULT InitialiseVertexBuffer()
{
VOID* pVertices;
//保存三角形各個頂點以及它們的顏色
//Store each point of the triangle together with it's colour
CUSTOMVERTEX cvVertices[] =
{
{250.0f, 100.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(255, 0, 0),}, //Vertex 1 - Red (250, 100)
{400.0f, 350.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(0, 255, 0),}, //Vertex 2 - Green (400, 350)
{100.0f, 350.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(0, 0, 255),}, //Vertex 3 - Blue (100, 350)
{400.0f, 100.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(255, 0, 0),},
{400.0f, 350.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(0, 255, 0),},
{100.0f, 350.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(0, 0, 255),},
};
//從設備創建頂點緩沖
//Create the vertex buffer from our device
if(FAILED(g_pD3DDevice->CreateVertexBuffer(6 * sizeof(CUSTOMVERTEX),//
0, D3DFVF_CUSTOMVERTEX,
D3DPOOL_DEFAULT, &g_pVertexBuffer)))
{
return E_FAIL;
}
//得到指向頂點緩沖的指針并鎖定頂點緩沖
//Get a pointer to the vertex buffer vertices and lock the vertex buffer
if(FAILED(g_pVertexBuffer->Lock(0, sizeof(cvVertices), (BYTE**)&pVertices, 0)))
{
return E_FAIL;
}
//拷貝保存在cvVertices中的值到頂點緩沖中
//Copy our stored vertices values into the vertex buffer
memcpy(pVertices, cvVertices, sizeof(cvVertices));
//解除頂點緩沖的鎖定
//Unlock the vertex buffer
g_pVertexBuffer->Unlock();
return S_OK;
}
void Render()
{
if(g_pD3DDevice == NULL)
{
return;
}
//清空背景緩沖為黑色
//Clear the backbuffer to black
g_pD3DDevice->Clear( 0,
NULL,
D3DCLEAR_TARGET,
D3DCOLOR_XRGB(0, 0, 0),
1.0f,
0
);
//Begin the scene
//開始場景渲染
g_pD3DDevice->BeginScene();
//渲染三角形
//Rendering our triangle
//邦定頂點緩沖到設備數據流中
g_pD3DDevice->SetStreamSource( 0, //指定數據流,范圍從0-Streams-1
g_pVertexBuffer, //指向IDirect3DVertexBuffer8接口返回對象的指針,描述所綁定到的數據流對象
sizeof(CUSTOMVERTEX));//頂點緩沖大小
//設置頂點陰影模式
g_pD3DDevice->SetVertexShader(D3DFVF_CUSTOMVERTEX);
//頂點緩存渲染頂點
g_pD3DDevice->DrawPrimitive( D3DPT_TRIANGLELIST,
0,
2
);
//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);
}
void CleanUp()
{
SafeRelease(g_pVertexBuffer);
SafeRelease(g_pD3DDevice);
SafeRelease(g_pD3D);
}
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();
}
}
}
//The windows message handler
//windows消息處理函數
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鍵時銷毀hWnd指向的窗體
DestroyWindow(hWnd);
return 0;
break;
}
break;
}
//默認的系統消息處理函數
return DefWindowProc(hWnd, msg, wParam, lParam);
}
//Application entry point
INT WINAPI WinMain(HINSTANCE hInst, HINSTANCE, LPSTR, INT)
{
//Register the window class
//用于注冊windows窗體類的設置
WNDCLASSEX wc = {sizeof(WNDCLASSEX), CS_CLASSDC, WinProc, 0L, 0L,
GetModuleHandle(NULL), NULL, NULL, NULL, NULL,
"DX Project 2", NULL};
RegisterClassEx(&wc);
//Set the mouse pointer to an arrow
//設置鼠標指針
HWND hWnd = CreateWindow("DX Project 2", "www.andypike.com: Tutorial 2",
WS_OVERLAPPEDWINDOW, 50, 50, 500, 500,
GetDesktopWindow(), NULL, wc.hInstance, NULL);
//Initialize Direct3D
//初始化Direct3D對象
if(SUCCEEDED(InitialiseD3D(hWnd)))
{
//Show our window
//顯示窗體
ShowWindow(hWnd, SW_SHOWDEFAULT);
UpdateWindow(hWnd);
//刷新窗體顯示
if(SUCCEEDED(InitialiseVertexBuffer()))
{
//Start game running: Enter the game loop
//進入游戲循環
GameLoop();
}
}
//清理資源
CleanUp();
//取消窗體類注冊
UnregisterClass("DX Project 2", wc.hInstance);
return 0;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -