#include <windows.h>
#include <d3d11.h>
HWND hwnd;
HINSTANCE hInstance;
D3D_FEATURE_LEVEL pFeatureLevel;
IDXGISwapChain *ppSwapChain;
ID3D11Device *ppDevice;
ID3D11DeviceContext *ppImmediateContext;
ID3D11RenderTargetView *pRenderTargetView;
void InitWindow( HINSTANCE hInstance, int nCmdShow );
void InitDevice();
void Render();
LRESULT CALLBACK WndProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam );
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow )
{
InitWindow( hInstance, nCmdShow );
InitDevice();
MSG msg = {0};
while( WM_QUIT != msg.message )
{
if( PeekMessage( &msg, hwnd, 0, 0, PM_REMOVE ) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
else
Render();
}
}
void InitWindow( HINSTANCE hInstance, int nCmdShow )
{
WNDCLASS wnd;
wnd.cbClsExtra = 0;
wnd.cbWndExtra = 0;
wnd.hbrBackground = (HBRUSH)COLOR_WINDOW;
wnd.hCursor = LoadCursor( 0, IDC_ARROW );
wnd.hIcon = LoadIcon( 0, IDI_APPLICATION );
wnd.hInstance = hInstance;
wnd.lpfnWndProc = WndProc;
wnd.lpszClassName = "Default";
wnd.lpszMenuName = NULL;
wnd.style = CS_VREDRAW | CS_HREDRAW;
RegisterClass( &wnd );
hwnd = CreateWindow( "Default", "Test", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 400, 400, 0, 0, hInstance, 0 );
ShowWindow( hwnd, nCmdShow );
}
void InitDevice()
{
// first fill out swap chain desc
DXGI_SWAP_CHAIN_DESC sd;
sd.BufferCount = 1;
sd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
sd.BufferDesc.Width = 400;
sd.BufferDesc.Height = 400;
sd.BufferDesc.RefreshRate.Numerator = 60;
sd.BufferDesc.RefreshRate.Denominator = 1;
sd.BufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED;
sd.BufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED;
sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
sd.Flags = NULL;
sd.OutputWindow = hwnd;
sd.SampleDesc.Count = 1;
sd.SampleDesc.Quality = 0;
sd.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
sd.Windowed = true;
// now create device and swap chain
D3D_DRIVER_TYPE DriverType = D3D_DRIVER_TYPE_HARDWARE;
D3D_FEATURE_LEVEL pFeatureLevels = D3D_FEATURE_LEVEL_11_0;
if( !D3D11CreateDeviceAndSwapChain( NULL, DriverType, NULL, NULL, &pFeatureLevels, 1, D3D11_SDK_VERSION, &sd, &ppSwapChain, &ppDevice, &pFeatureLevel, &ppImmediateContext ))
MessageBox( 0, "OOps", "Oops!", 0 );
// now handle the render target views for buffers
ID3D11Texture2D *pBackBuffer;
ppSwapChain->GetBuffer( 0, __uuidof( ID3D11Texture2D ), reinterpret_cast<void**>(&pBackBuffer) );
MessageBox( 0, "GetBuffer success!", "Success!", 0 );
ppDevice->CreateRenderTargetView( pBackBuffer, NULL, &pRenderTargetView );
D3D11_VIEWPORT vp;
vp.Width = 400;
vp.Height = 400;
vp.MaxDepth = 1.0f;
vp.MinDepth = 0.0f;
vp.TopLeftX = 0;
vp.TopLeftY = 0;
ppImmediateContext->RSSetViewports( 1, &vp );
}
void Render()
{
// Just clear the backbuffer
float ClearColor[4] = { 0.0f, 0.125f, 0.3f, 1.0f }; //red,green,blue,alpha
ppImmediateContext->ClearRenderTargetView( pRenderTargetView, ClearColor );
ppSwapChain->Present( 0, 0 );
}
LRESULT CALLBACK WndProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
PAINTSTRUCT ps;
HDC hdc;
switch( msg )
{
case WM_CREATE:
return 0;
case WM_KEYDOWN:
switch( wParam )
{
case VK_LEFT:
SetWindowText( hwnd, "pressed left arrow key!" );
return 0;
case VK_RIGHT:
SetWindowText( hwnd, "pressed right arrow key!" );
return 0;
case VK_DOWN:
SetWindowText( hwnd, "pressed down arrow key!" );
return 0;
case VK_UP:
SetWindowText( hwnd, "pressed up arrow key!" );
return 0;
case VK_ESCAPE:
PostQuitMessage( 0 );
return 0;
}
case WM_KEYUP:
{
SetWindowText( hwnd, "Test" );
return 0;
}
case WM_DESTROY:
{
PostQuitMessage( 0 );
return 0;
}
}
return DefWindowProc( hwnd, msg, wParam, lParam );
}
Says I have an access violation when I execute ppSwapChain->GetBuffer.
Any ideas?