Monday, November 28, 2011

DirextX -Begin D3D

The DirectX Begin


#include windows.h
#include windowsx.h
#include d3d11.h
#include d3d10.h
#include d3d11.h
#include d3dx10.h
#include d3dx11.h
#include windows.h

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

IDXGISwapChain *swapchain;
ID3D11Device *dev;
ID3D11DeviceContext *devcon;

void InitD3D(HWND hWnd);
void CleanD3D(void);

LRESULT CALLBACK WindowProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lPram);


IDXGISwapChain: The swap chain is the series of buffers which take turns being rendered on.

ID3D11Device:This variable is a pointer to a device.

ID3D11DeviceContext:A device context is similar to a device, but it is responsible for managing the GPU and the rendering pipeline (the device mostly handles video memory).

Initialize D3D


void InitD3D(HWND hWnd)
{
DXGI_SWAP_CHAIN_DESC scd;

ZeroMemory(&scd,sizeof(DXGI_SWAP_CHAIN_DESC));

//Swap chain struct
scd.BufferCount = 1;//BackBuffer
scd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;//32bit-buffer
scd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; //how swap chain is to be used
scd.OutputWindow = hWnd;//window to be use
scd.SampleDesc.Count = 2;//How many multisample  anti-aliased
scd.Windowed = TRUE;//Screen Full or not
//scd.Flags  = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH;//use alt + enter   DXGI_SWAP_CHAIN_FLAG_NONPREROTATED moniter rotation
D3D11CreateDeviceAndSwapChain(NULL,D3D_DRIVER_TYPE_HARDWARE,NULL,NULL,NULL,NULL,D3D11_SDK_VERSION,&scd,&swapchain,&dev,NULL,&devcon);
}



UnLoad3D


void Clean3D()
{
swapchain -> Release();
dev ->Release();
devcon->Release();
}



Program Main



int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
{
HWND hWnd;
WNDCLASSEX wc;

ZeroMemory(&wc,sizeof(WNDCLASSEX));

wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.hCursor = LoadCursor(NULL,IDC_ARROW);
wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
wc.lpszClassName = "WindowClass";

RegisterClassEx(&wc);

RECT wr = {0,0,1280,720};
AdjustWindowRect(&wr,WS_OVERLAPPEDWINDOW,FALSE);

hWnd  = CreateWindowEx(NULL,"WindowClass","RealDirectX",WS_OVERLAPPEDWINDOW,300,300,wr.right - wr.left,wr.bottom - wr.top,NULL,NULL,hInstance,NULL);

ShowWindow(hWnd,nCmdShow);

InitD3D(hWnd);

MSG msg;

while(TRUE)
{
if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
{
TranslateMessage(&msg);

DispatchMessage(&msg);

if(msg.message == WM_QUIT)
break;
}
else
{

}
}

Clean3D();
return msg.wParam;
}



Window Program


LRESULT CALLBACK WindowProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam)
{
switch(message)
{
case WM_DESTROY:
{
PostQuitMessage(0);
return 0;
}break;
}

return DefWindowProc(hWnd,message,wParam,lParam);
}


1scd.Flags:Three property
                 1.DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH
                 2.DXGI_SWAP_CHAIN_FLAG_NONPREROTATED
                 3.DXGI_SWAP_CHAIN_FLAG_GDI_COMPATIBLE
2.scd.SampleDesc.Count: Max Number is 4 Min Number is 1 ,Setting Multisample anti-aliased

----------------------- Using Back Buffer with Deep Blue

1.Setting RenderTarget
2.Setting ViewPort
3.RenderFrame

In header i add

ID3D11RenderTargetView *backbuffer;
void RenderFrame(void);


 InitD3D(HWND hWnd)
   Add Render Target &ViewPort

void InitD3D(HWND hWnd)
{
     //........

//Setting Render Target

ID3D11Texture2D *pBackBuffer;
swapchain->GetBuffer(0,__uuidof(ID3D11Texture2D),(LPVOID*)&pBackBuffer);

dev->CreateRenderTargetView(pBackBuffer,NULL,&backbuffer);
pBackBuffer->Release();
devcon->OMSetRenderTargets(1,&backbuffer,NULL);



//Setting ViewPort
D3D11_VIEWPORT viewport;
ZeroMemory(&viewport,sizeof(D3D11_VIEWPORT));
viewport.MaxDepth = 1.0f;
viewport.MinDepth = 0.0f;
viewport.TopLeftX = 0;
viewport.TopLeftY = 0;
viewport.Width = 1280;
viewport.Height = 720;

devcon->RSSetViewports(1,&viewport);

}



void RenderFrame(void)
{
//Clear the back buffer in deep blue
devcon->ClearRenderTargetView(backbuffer,D3DXCOLOR(0.0f,0.2f,0.4f,1.0f));

//swtich the back buffer and front buffer
swapchain->Present(0,0);
}



In game Logical Add

RenderFrame And done

No comments:

Post a Comment