0

In my DirectX program, I have an algorithm for resizing the render target so it fits the screen when changing to full screen mode (vertically on 4:3 and horizontally in 16:9, creating a letterbox effect). It works fine on full screen, but every time I return to windowed mode, the window somehow gets bigger and bigger.

The app uses this values for width and height; they don't change during the program's execution:

int winwidth=1280;
int winheight=720;
int screenwidth=GetSystemMetrics(SM_CXSCREEN);
int screenheight=GetSystemMetrics(SM_CYSCREEN);

Here's where I toggle the screen mode on ALT + ENTER at the message loop:

case WM_SYSKEYUP:
    switch(wParam){
    case VK_RETURN:
        if(GetKeyState(VK_LMENU)<0){
            if(!god->settings.fullscreen){
                god->settings.fullscreen=true;
                d2d->resize(hwnd,god->settings);
                d2d->swapchain->SetFullscreenState(true, NULL);
                god->saveSettings();
            }
            else{
                god->settings.fullscreen=false;
                d2d->resize(hwnd,god->settings);
                d2d->swapchain->SetFullscreenState(false, NULL);
                god->saveSettings();
            }
        }
        break;
    }
    break;

This is the resizing method that I used:

void D2DHandle::resize(HWND hwnd,GameSettings settings){
    DXGI_MODE_DESC modedesc;
    DXGI_RATIONAL rat;
    rat.Denominator = 1;
    rat.Numerator = 30;
    modedesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
    if(settings.fullscreen){
        if(settings.aspect43){
            modedesc.Width=(int)((settings.screenheight)*(4.f/3.f));
            modedesc.Height=settings.screenheight;
        }
        else{
            modedesc.Width=settings.screenwidth;
            modedesc.Height=(int)((settings.screenwidth)*(9.f/16.f));
        }
    }
    else{
        modedesc.Width=settings.winwidth;
        modedesc.Height=settings.winheight;
    }
    modedesc.RefreshRate = rat;
    modedesc.Scaling=DXGI_MODE_SCALING_CENTERED;
    modedesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_PROGRESSIVE;
    swapchain->ResizeTarget(&modedesc);
    if(settings.fullscreen){
        if(settings.aspect43){
            swapchain->ResizeBuffers(0,(int)((settings.screenheight)*(4.f/3.f)),settings.screenheight,DXGI_FORMAT_R8G8B8A8_UNORM,0);
        }
        else{
            swapchain->ResizeBuffers(0,settings.screenwidth,(int)((settings.screenwidth)*(9.f/16.f)),DXGI_FORMAT_R8G8B8A8_UNORM,0);
        }
    }
    else{
        swapchain->ResizeBuffers(0,settings.winwidth,settings.winheight,DXGI_FORMAT_R8G8B8A8_UNORM,0);
    }
    swapchain->GetBuffer(0,IID_PPV_ARGS(surface.ReleaseAndGetAddressOf()));
    D2D1_RENDER_TARGET_PROPERTIES rtp;
    rtp.dpiX=0.0f;
    rtp.dpiY=0.0f;
    rtp.minLevel=D2D1_FEATURE_LEVEL_10;
    rtp.pixelFormat=PixelFormat(DXGI_FORMAT_R8G8B8A8_UNORM,D2D1_ALPHA_MODE_PREMULTIPLIED);
    rtp.type=D2D1_RENDER_TARGET_TYPE_HARDWARE;
    rtp.usage=D2D1_RENDER_TARGET_USAGE_NONE;
    d2dfactory->CreateDxgiSurfaceRenderTarget(surface.Get(),rtp,target.ReleaseAndGetAddressOf());
}
  • 1
    You are probably not accounting for the window's non-client area. But we cannot see how you calculate `winheight` and `winwidth`, nor how you are resizing the actual window. – IInspectable Dec 15 '19 at 08:50
  • You are passing a `HWND` into `resize`, but your code doesn't show, how you're using it. Since you're having issues resizing that window, you need to show the code that does. Please provide a [mcve]. Presumably, you can drop all the D2D code; it doesn't appear to be immediately relevant. – IInspectable Dec 15 '19 at 10:54

0 Answers0