Search code examples
c++directxdirectx-9

Changing resolution in DirectX9


I'm finding it hard to find good sources of information for explanations of DirectX API although I've been looking at the DirectX Documentation for a while.

I'm trying to create a method for a wrapper class for DX9 that changes the resolution during runtime. I've managed to handle this with DirectDraw but I find no information regarding DX9 even though it should be more common.

All I found was a reference to SetDisplayMode, but neither my Direct Object or my DirectX Device has this method.

I'm using DirectX 9.

Example method:

void SetResolution(int width, int height, int depth)
{
   // I have access to DirectX device, object and the window HWND in this class
};

...do I change the HWND window size, or do I handle this in DirectX? I know how to change resolution in a Windows application but no clue how to do it in DX9.


Solution

  • Try adjusting your D3DPRESENT_PARAMETERS the way you normally would. Something like:

    // D3DPRESENT_PARAMETERS is already defined as md3dPP
    RECT R ={0, 0, 640, 480}
    AdjustwindowRect(&R, WS_OVERLAPPEDWINDOW, false);
    md3dPP.BackBufferFormat = D3DFMT_UNKNOWN;
    md3dPP.BackBufferWidth = 640;
    md3dPP.BackBufferHeight = 480;
    md3dPP.Windowed = true;
    

    If you wish, this might also be a good time to change the window style, such as:

    SetWindowLongPtr(yourWindowHandle, GWL_STYLE, WS_OVERLAPPEDWINDOW)
    
    // must use SetWindowPos for SetWindowLongPtr changes to take effect
    SetWindowPos(yourWindowHandle, HWND_TOP, 100, 100, R.right, R.bottom, SWP_NOZORDER |     SWP_SHOWWINDOW);
    

    Alternatively, if you're using fullscreen:

    md3dPP.BackBufferFormat = D3DFMT_X8R8G8B8;
    md3dPP.BackBufferWidth = 640;
    md3dPP.BackBufferHeight = 480;
    md3dPP.Windowed = true;
    
    SetWindowLongPtr(yourWindowHandle, GWL_STYLE, WS_POPUP)
    
    // must use SetWindowPos for SetWindowLongPtr changes to take effect
    SetWindowPos(yourWindowHandle, HWND_TOP, 0, 0, 640, 480, SWP_NOZORDER | SWP_SHOWWINDOW);
    

    Either way, follow this up by calling IDirect3DDevice9::Reset. Just make sure you re-initialize your resources when resetting.

    So if you defined something like: IDirect3DDevice9 *gd3dDevice;

    Use:

    gd3dDevice->Reset(&md3dPP);