Tutorial 1: Creating a window and a Direct3D 10 Device
From DirectXWiki
This is the complete code to show a first window with a Direct3D device. This code is similar to the SDK Tutorials, but this one is complete! Just copy and paste the code in a .cpp file of Visual Studio and build it. Then run it. If you get a linker error you have to add d3d10.lib in your project properties and/or set project paths to the DirectX SDK. See this How to setup a Visual Studio Project for DirectX for detailed info.
Contents |
[edit] Code
[edit] C++
#include "windows.h" #include "d3d10.h"
bool g_bContinue = true;
//Besides the main function, there must be a message processing function
LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
switch( msg )
{
case WM_DESTROY:
PostQuitMessage( 0 );
g_bContinue = false;
return 0;
}
return DefWindowProc( hWnd, msg, wParam, lParam );
}
//The entry point of a windows application is the WinMain function
INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT )
{
//Create a window class.
WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, MsgProc, 0L, 0L,
GetModuleHandle(NULL), NULL, NULL, NULL, NULL,
"Direct3D Window", NULL };
//Register the window class.
RegisterClassEx( &wc );
//Create the application's window.
HWND hWnd = CreateWindow( "Direct3D Window", "DirectX Wiki - D3D10 Tutorial 1",
WS_OVERLAPPEDWINDOW, 100, 100, 400, 400,
GetDesktopWindow(), NULL, wc.hInstance, NULL );
ShowWindow(hWnd,SW_SHOW);
//Create the Direct3D Device and Swap Chain
DXGI_SWAP_CHAIN_DESC sd;
ZeroMemory( &sd, sizeof(sd) );
sd.BufferCount = 1;
sd.BufferDesc.Width = 640;
sd.BufferDesc.Height = 480;
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 = hWnd;
sd.SampleDesc.Count = 1;
sd.SampleDesc.Quality = 0;
sd.Windowed = TRUE;
IDXGISwapChain* pSwapChain = NULL;
ID3D10Device* pd3dDevice = NULL;
if( FAILED( D3D10CreateDeviceAndSwapChain( NULL,
D3D10_DRIVER_TYPE_REFERENCE,
NULL,
0,
D3D10_SDK_VERSION,
&sd,
&pSwapChain,
&pd3dDevice ) ) )
{
return FALSE;
}
// Create a render target view(from Backbuffer)
ID3D10Texture2D *pBackBuffer = NULL;
ID3D10RenderTargetView *pRenderTargetView = NULL;
if( FAILED( pSwapChain->GetBuffer( 0, __uuidof( ID3D10Texture2D ), (LPVOID*)&pBackBuffer ) ) )
return FALSE;
HRESULT hr = pd3dDevice->CreateRenderTargetView( pBackBuffer, NULL, &pRenderTargetView );
pBackBuffer->Release();
if( FAILED( hr ) )
return FALSE;
pd3dDevice->OMSetRenderTargets( 1, &pRenderTargetView, NULL );
//In D3D10 this must be done, because there is no default Viewport
D3D10_VIEWPORT vp;
vp.Width = 640;
vp.Height = 480;
vp.MinDepth = 0.0f;
vp.MaxDepth = 1.0f;
vp.TopLeftX = 0;
vp.TopLeftY = 0;
pd3dDevice->RSSetViewports( 1, &vp );
MSG msg;
while( g_bContinue )
{
//Clear render region with blue
float ClearColor[4] = { 0.0f, 0.0f, 1.0f, 1.0f }; //red,green,blue,alpha
pd3dDevice->ClearRenderTargetView( pRenderTargetView, ClearColor );
pSwapChain->Present( 0, 0 );
// A window has to handle its messages.
TranslateMessage( &msg );
DispatchMessage( &msg );
PeekMessage(&msg, 0, 0, 0, PM_REMOVE);
}
//Do not forget to clean up here
pSwapChain->Release();
pSwapChain = NULL;
pRenderTargetView->Release();
pRenderTargetView = NULL;
pd3dDevice->Release();
pd3dDevice = NULL;
return 0; }
[edit] Resources
This application and code can be downloaded here: VS Project and Code.
This application can also be found and downloaded in the Direct3D 10 Samples section.
[edit] Navigation
Direct3D 10 Tutorial Selection Page Direct3D_10_Tutorials
Next Tutorial: Tutorial_2:_Using_a_vertexbuffer_with_an_Effect_D3D10
