游戏开发论坛

 找回密码
 立即注册
搜索
查看: 2514|回复: 2

关于directx11,如何处理wm_size?

[复制链接]

1

主题

2

帖子

0

积分

新手上路

Rank: 1

积分
0
发表于 2010-5-27 23:59:00 | 显示全部楼层 |阅读模式
照着微软的例子写了个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-&gtresent( 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;
}


5

主题

686

帖子

697

积分

高级会员

Rank: 4

积分
697
QQ
发表于 2010-5-31 08:13:00 | 显示全部楼层

Re:关于directx11,如何处理wm_size?

拦截WM_ERASEBKGND和WM_PAINT消息,遇到这两个就直接return 0
否则背景刷子会跟DXGI打架,造成闪烁

1

主题

2

帖子

0

积分

新手上路

Rank: 1

积分
0
 楼主| 发表于 2010-5-31 16:28:00 | 显示全部楼层

Re:关于directx11,如何处理wm_size?

解决了,非常感谢jk20012001
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

作品发布|文章投稿|广告合作|关于本站|游戏开发论坛 ( 闽ICP备17032699号-3 )

GMT+8, 2025-6-9 11:08

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表