|
照着微软的例子写了个directx11的程序,但当窗体改变大小的时候,会严重闪烁,不知道为何,下面是源码,请大家指教
#include "main.h"
//这个就是普通的头文件,包含了一些direct和windows用到的文件,没什么特别的,就不发了。
HINSTANCE hInst;
TCHAR *szWindowClass = _T("TEST");
TCHAR *szTitle = _T("test");
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
IDXGISwapChain* pSwapChain;
ID3D11RenderTargetView* pRenderTargetView;
ID3D11Device* pDevice;
ID3D11DeviceContext* pImmediateContext;
bool InitializeD3D(HWND hWnd, int fullscreen)
{
DXGI_SWAP_CHAIN_DESC sd;
ZeroMemory( &sd, sizeof( sd ) );
sd.BufferCount = 1;
sd.BufferDesc.Width = 800;
sd.BufferDesc.Height = 600;
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;
D3D_FEATURE_LEVEL FeatureLevelsRequested = D3D_FEATURE_LEVEL_10_0;
UINT numLevelsRequested = 1;
D3D_FEATURE_LEVEL FeatureLevelsSupported;
HRESULT hr = D3D11CreateDeviceAndSwapChain(
NULL,
D3D_DRIVER_TYPE_HARDWARE ,
NULL, //GetModuleHandle(NULL),
NULL,
&FeatureLevelsRequested,
1,
D3D11_SDK_VERSION,
&sd,
&pSwapChain,
&pDevice,
&FeatureLevelsSupported,
&pImmediateContext );
ID3D11Texture2D *pBackBuffer;
pSwapChain->GetBuffer(0,__uuidof(ID3D11Texture2D), ( LPVOID* )&pBackBuffer );
pDevice->CreateRenderTargetView( pBackBuffer,
NULL, &pRenderTargetView );
pBackBuffer->Release();
pImmediateContext->OMSetRenderTargets( 1, &pRenderTargetView, NULL );
D3D11_VIEWPORT vp;
vp.Width = 800;
vp.Height = 600;
vp.MaxDepth = 1.0f;
vp.MinDepth = 0.0f;
vp.TopLeftX = 0;
vp.TopLeftY = 0;
pImmediateContext->RSSetViewports( 1, &vp );
float ClearColor[4] = { 0.0f, 0.125f, 0.6f, 1.0f };
pImmediateContext->ClearRenderTargetView( pRenderTargetView,
ClearColor);
pSwapChain-> resent( 0, 0);
return true;
}
void RenderScene()
{
float ClearColor[4] = { 0.0f, 0.125f, 0.6f, 1.0f };
pImmediateContext->ClearRenderTargetView( pRenderTargetView,
ClearColor);
pSwapChain->Present( 0, 0);
}
void Shutdown()
{
}
int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
MSG msg;
MyRegisterClass(hInstance);
// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int) msg.wParam;
}
ATOM MyRegisterClass(HINSTANCE hInstance)
{
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 = 0;
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName =0;
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = 0;
return RegisterClassEx(&wcex);
}
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
hInst = GetModuleHandle(NULL);//hInstance; // Store instance handle in our global variable
hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, 800, 600, NULL, NULL, hInstance, NULL);
if (!hWnd)
{
return FALSE;
}
InitializeD3D(hWnd,0);
//SetTimer(hWnd,3,20,NULL);
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
LRESULT res;
switch (message)
{
case WM_DISPLAYCHANGE:
InvalidateRect(hWnd, NULL, FALSE);
break;
case WM_SIZE:
{
if(pSwapChain)
{
pRenderTargetView->Release();
pSwapChain->ResizeBuffers(1,LOWORD(lParam),HIWORD(lParam),DXGI_FORMAT_R8G8B8A8_UNORM,0);
ID3D11Texture2D *pBackBuffer;
pSwapChain->GetBuffer(0,__uuidof(ID3D11Texture2D), ( LPVOID* )&pBackBuffer );
pDevice->CreateRenderTargetView( pBackBuffer, NULL, &pRenderTargetView );
pSwapChain->Release();
pImmediateContext->OMSetRenderTargets( 1, &pRenderTargetView, NULL );
D3D11_VIEWPORT vp;
vp.Width = LOWORD(lParam);
vp.Height = HIWORD(lParam);
vp.MaxDepth = 1.0f;
vp.MinDepth = 0.0f;
vp.TopLeftX = 0;
vp.TopLeftY = 0;
pImmediateContext->RSSetViewports( 1, &vp );
break;
}
else
{
return DefWindowProc(hWnd, message, wParam, lParam);
}
}
case WM_PAINT:
BeginPaint(hWnd,&ps);
RenderScene();
EndPaint(hWnd,&ps);
break;
case WM_TIMER:
InvalidateRect(hWnd,NULL,TRUE);
break;
case WM_DESTROY:
Shutdown();
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
|
|