First DX11 App

本来打算接着学DX9的

不过后来觉得虽然DX11没有教程吧,但是有文档,虽然我英语不好,但是看看文档还是差不多的

就干脆转战到DX11上面了……

又是从0开始……

废话不多说,第一个DX11的APP代码

跟DX Sample的差不多

/*
--------------------------------------
第一个DirectX11程序
写于12年3月23日
--------------------------------------
*/

#include <d3d11.h>
#include <d3dx11.h>
#include <stdio.h>

#pragma comment(lib, "d3d11.lib")
#pragma comment(lib, "d3dx11.lib")

//======================================
// 全局变量
//======================================

HWND g_hWnd;

D3D_DRIVER_TYPE g_driverType = D3D_DRIVER_TYPE_HARDWARE;
D3D_FEATURE_LEVEL g_featureLevel = D3D_FEATURE_LEVEL_11_0;

ID3D11Device *g_pD3dDevice = NULL;
ID3D11DeviceContext *g_pImContext = NULL;
IDXGISwapChain *g_pSwapChain = NULL;
ID3D11RenderTargetView *g_pRenderTargetView = NULL;

LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);

void InitWindow(HINSTANCE hInstance, int nCmdShow)
{

WNDCLASSEX wcex;
wcex.cbSize = sizeof( WNDCLASSEX );
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = NULL;
wcex.hCursor = LoadCursor( NULL, IDC_ARROW );
wcex.hbrBackground = ( HBRUSH )( COLOR_WINDOW + 1 );
wcex.lpszMenuName = NULL;
wcex.lpszClassName = L"TutorialWindowClass";
wcex.hIconSm = NULL;
if( !RegisterClassEx( &wcex ) )
{
MessageBeep(1);
}

// Create window
RECT rc = { 0, 0, 640, 480 };
AdjustWindowRect( &rc, WS_OVERLAPPEDWINDOW, FALSE );
g_hWnd = CreateWindow( L"TutorialWindowClass", L"First DirectX11 App", WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, rc.right - rc.left, rc.bottom - rc.top, NULL, NULL, hInstance,
NULL );

ShowWindow( g_hWnd, nCmdShow );
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch(uMsg)
{
case WM_DESTROY:
PostQuitMessage(0);
return 0;
default:
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
}

void InitDevice()
{
RECT rt;
GetClientRect(g_hWnd, &rt);
UINT width = rt.right - rt.left;
UINT height = rt.bottom - rt.top;

UINT createDeviceFlags = 0;//创建设备标记,可以用来debug之类

D3D_FEATURE_LEVEL featureLevels[] =
{
D3D_FEATURE_LEVEL_11_0,
//D3D_FEATURE_LEVEL_10_1,
//D3D_FEATURE_LEVEL_10_0,
};
//D3D_FEATURE_LEVEL *featureLevels = {D3D_FEATURE_LEVEL_11_0};

//初始化DXGI_SWAP_CHAIN_DESC结构体
DXGI_SWAP_CHAIN_DESC sd;
ZeroMemory(&sd, sizeof(sd));//注意这里很重要,下面一些部分的初始化对部分成员没有归0
sd.BufferCount = 1;//缓冲区个数
sd.BufferDesc.Width = width;
sd.BufferDesc.Height = height;
sd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
sd.BufferDesc.RefreshRate.Numerator = 60;
sd.BufferDesc.RefreshRate.Denominator = 1;
sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;//缓冲的用途
sd.OutputWindow = g_hWnd;//输出的窗口句柄
sd.SampleDesc.Count = 1;
sd.SampleDesc.Quality = 0;
sd.Windowed = true;


HRESULT hr = D3D11CreateDeviceAndSwapChain( NULL, g_driverType, NULL, createDeviceFlags, featureLevels
, 1, D3D11_SDK_VERSION, &sd, &g_pSwapChain, &g_pD3dDevice, &g_featureLevel, &g_pImContext);

if(FAILED(hr))
{
MessageBox(NULL, L"Create Wrong", L"Wrong", 0);
return;
}

ID3D11Texture2D *pBackBuffer = NULL;
hr = g_pSwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (void **)&pBackBuffer);
if(FAILED(hr))
{
printf("GetBuffer Wrong\n");
return;
}
hr = g_pD3dDevice->CreateRenderTargetView(pBackBuffer, NULL, &g_pRenderTargetView);
if(FAILED(hr))
{
printf("CreateRenderTargetView Wrong\n");
return;
}

pBackBuffer->Release();

g_pImContext->OMSetRenderTargets( 1, &g_pRenderTargetView, NULL);

D3D11_VIEWPORT vp;
vp.Height = height;
vp.Width = width;
vp.MinDepth = 0.0f;
vp.MaxDepth = 1.0f;
vp.TopLeftX = 0;
vp.TopLeftY = 0;

g_pImContext->RSSetViewports(1, &vp);
}


void Render()
{
float ClearColor[4] = {0.0f, 0.0f, 0.0f, 1.0f};
g_pImContext->ClearRenderTargetView(g_pRenderTargetView, ClearColor);
g_pSwapChain->Present(0, 0);
}

void Clear()
{
if(g_pImContext)
g_pImContext->Release();
if(g_pRenderTargetView)
g_pRenderTargetView->Release();
if(g_pSwapChain)
g_pSwapChain->Release();
if(g_pD3dDevice)
g_pD3dDevice->Release();
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
InitWindow(hInstance, nShowCmd);
InitDevice();

MSG msg;
ZeroMemory(&msg, sizeof(MSG));

while(msg.message != WM_QUIT)
{
if(PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{
Render();
}
}
}



posted on 2012-03-23 19:09  z光束star  阅读(1236)  评论(1)    收藏  举报

导航