DirectX 10 Draw Text

Credits:
UC Assorted
GD
aB
More.

Code:
#define _CRT_SECURE_NO_WARNINGS

#include <windows.h>
#include <stdlib.h>
#include <string.h>
#include <tchar.h>
#include <d3d10.h> 
#include <d3dx10.h>

#pragma comment(lib, "D3D10.lib")
#pragma comment(lib, "d3dx10d.lib")

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
VOID WINAPI D3D_Thread( VOID );

static TCHAR szWindowClass[]		= _T("DX10");
static TCHAR szTitle[]						= _T("DX10 Font");

HINSTANCE hInst							= NULL;
HWND hWnd									= NULL;
IDXGISwapChain* pSwapChain      = NULL;
ID3D10Device* pDevice					= NULL; 
ID3D10Texture2D *pBackBuffer		= NULL;
ID3DX10Font* Font						= NULL;

ID3D10RenderTargetView* pRenderTargetView      = NULL;

D3D10_VIEWPORT vp						= { 0, 0, 500, 400, 0, 1 };
RECT FontRect								= { 35, 50, 0, 0 };

D3DXCOLOR FontColor( 1.0f, 0.0f, 0.0f, 1.0f );

D3DX10_FONT_DESC FontDesc;

INT WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, 
	LPSTR lpCmdLine, 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					= LoadIcon(hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
    wcex.hCursor					= LoadCursor(NULL, IDC_ARROW);
    wcex.hbrBackground		= (HBRUSH)(COLOR_WINDOW+1);
    wcex.lpszMenuName		= NULL;
    wcex.lpszClassName		= szWindowClass;
    wcex.hIconSm				= LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_APPLICATION));

    RegisterClassEx( &wcex );

    hInst = hInstance;

    hWnd = CreateWindow(
        szWindowClass,
        szTitle,
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT,
        500, 400,
        NULL,
        NULL,
        hInstance,
        NULL
    );

    ShowWindow( hWnd, nCmdShow );

    UpdateWindow( hWnd );

	CreateThread( NULL, NULL, (LPTHREAD_START_ROUTINE)
		D3D_Thread, NULL, NULL, NULL );

    MSG msg;

    while( GetMessage( &msg, NULL, 0, 0 ) )
    {
        TranslateMessage( &msg );
        DispatchMessage( &msg );
    }

    return (INT)msg.wParam;
}

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

    return 0;
}

VOID WINAPI D3D_Thread( VOID )
{
	DXGI_SWAP_CHAIN_DESC swapChainDesc;

	ZeroMemory( &swapChainDesc, sizeof(swapChainDesc) );

	swapChainDesc.BufferCount					= 1;
	swapChainDesc.BufferDesc.Width			= 500;
	swapChainDesc.BufferDesc.Height			= 400;
	swapChainDesc.BufferDesc.Format			= DXGI_FORMAT_R8G8B8A8_UNORM;
	swapChainDesc.BufferUsage					= DXGI_USAGE_RENDER_TARGET_OUTPUT;
	swapChainDesc.OutputWindow				= hWnd;
	swapChainDesc.SampleDesc.Count			= 1;
	swapChainDesc.SampleDesc.Quality		= 0;
	swapChainDesc.Windowed						= TRUE;

	if( FAILED( D3D10CreateDeviceAndSwapChain( NULL, D3D10_DRIVER_TYPE_HARDWARE, 
			NULL, 0, D3D10_SDK_VERSION, &swapChainDesc, 
				&pSwapChain, &pDevice ) ) )
	{
		if( FAILED( D3D10CreateDeviceAndSwapChain( NULL, D3D10_DRIVER_TYPE_REFERENCE, 
			NULL, 0, D3D10_SDK_VERSION, &swapChainDesc, 
					&pSwapChain, &pDevice ) ) )
		{
			MessageBoxA( hWnd, "D3D10CreateDeviceAndSwapChain", "Error", MB_OK );
			return;
		}
	}

	if( FAILED( pSwapChain->GetBuffer( 0, __uuidof( ID3D10Texture2D ), 
		 (LPVOID*)&pBackBuffer ) ) )
	{
		MessageBoxA( hWnd, "GetBuffer", "Error", MB_OK );
		return;
	}

	if(FAILED( pDevice->CreateRenderTargetView( pBackBuffer, 
		NULL, &pRenderTargetView )))
	{
		MessageBoxA( hWnd, "CreateRenderTargetView", "Error", MB_OK );
		return;
	}

	pBackBuffer->Release( );

	pDevice->OMSetRenderTargets( 1, &pRenderTargetView, NULL );

	pDevice->RSSetViewports( 1, &vp );

	FontDesc.Height					= 20;
	FontDesc.Width					= 0;
	FontDesc.Weight				= 0;
	FontDesc.MipLevels			= 1;
	FontDesc.Italic					= false;
	FontDesc.CharSet				= OUT_DEFAULT_PRECIS;
	FontDesc.Quality				= DEFAULT_QUALITY;
	FontDesc.PitchAndFamily	= DEFAULT_PITCH | FF_DONTCARE;

	strcpy( FontDesc.FaceName, "Cambria" );

	D3DX10CreateFontIndirect( pDevice, &FontDesc, &Font );

	while( 1 )
	{
		pDevice->ClearRenderTargetView( pRenderTargetView, D3DXVECTOR4( 0, 0, 0, 1 ) );

		if( Font ) Font->DrawTextA( 0, "TEXT", -1, &FontRect, DT_NOCLIP, FontColor );

		pSwapChain->Present( 0, 0 );

		Sleep( 10 );
	}
}