HOW to switch resolution in games using DirectX

Started by
17 comments, last by roy84 13 years, 9 months ago
actually, i am still facing a problem when i apply the principle today.for example, my current resolution is 800*600,when i switch the resolution to 1024*768, the render window size is still 800*600, leaving parts of the new window black, i am wondering what happens.
Advertisement
Quote:Original post by roy84
actually, i am still facing a problem when i apply the principle today.for example, my current resolution is 800*600,when i switch the resolution to 1024*768, the render window size is still 800*600, leaving parts of the new window black, i am wondering what happens.
In Windowed mode, the resolution in your presentation parameters is just the size of the backbuffer, you also need to resize the window using something like SetWindowPos.
Quote:Original post by Evil Steve
In Windowed mode, the resolution in your presentation parameters is just the size of the backbuffer, you also need to resize the window using something like SetWindowPos.

i have updated the code now
void ChangeResolution(int width, int height){D3DDISPLAYMODE d3ddm;if(FAILED(g_pD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &d3ddm))){return ;}D3DPRESENT_PARAMETERS d3dpp; ZeroMemory( &d3dpp, sizeof(d3dpp) );d3dpp.Windowed = true;d3dpp.SwapEffect = /*D3DSWAPEFFECT_DISCARD*/D3DSWAPEFFECT_COPY_VSYNC;d3dpp.BackBufferHeight = height;d3dpp.BackBufferWidth = width;d3dpp.BackBufferFormat = d3ddm.Format;d3dpp.FullScreen_RefreshRateInHz = 0;// here because they are managed pool resources, so releasing resources can be avoided.HRESULT ret = g_pD3DDevice->Reset(&d3dpp);if(SUCCEEDED(ret)){}SetWindowPos (g_hWnd, HWND_TOP, 0, 0, width, height, SWP_SHOWWINDOW);}

and the control part, when i press F7, it will change the resolution to 1024*768
case VK_F7:	ChangeResolution(1024,768);// also here re-creating resources is not needed.	D3DVIEWPORT8 m_ViewPortMain;// change the viewport to a new one.	m_ViewPortMain.X = 0;	m_ViewPortMain.Y = 0;	m_ViewPortMain.Width = 1024;	m_ViewPortMain.Height = 768;	m_ViewPortMain.MinZ = 0;	m_ViewPortMain.MaxZ = 1;	m_pd3dDevice->SetViewport(&m_ViewPortMain);

and now the window size is 1027*768, but the render window still is 800*600.
so can anyone be helpful? i still can not figure out exacly what the problem is : i have reconfigured the viewport, what should i do next? refresh the backbuffer and even reset the perspective matrix? any help will be greatly thankful.
You don't specify the window handle in your D3DPRESENT_PARAMETERS. Try setting the hDeviceWindow member to g_hWnd.
Quote:Original post by Evil Steve
You don't specify the window handle in your D3DPRESENT_PARAMETERS. Try setting the hDeviceWindow member to g_hWnd.

thank you for you timely reply, i have followed your instruction,but the problem still exists. when i press F7, the new window extends to 1024 *768,however, the render window size doesn't change a bit, that is,800*600. furthermore, in the former 800 *600 render window, everything is fine, except the parts exceeding the render window,because they are filled with no texture or disordered textures.
thanks again.
Are any of the parameters to Present() non-NULL in your code? If not, can we see your complete code (in [ source ] [ /source ] tags)?
okay.here comes the code:
the main part.void  CD3DApplication::ChangeResolution(int width, int height){		D3DDISPLAYMODE d3ddm;		if(FAILED(m_pD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &d3ddm)))			{				return ;			}		D3DPRESENT_PARAMETERS d3dpp; 		ZeroMemory( &d3dpp, sizeof(d3dpp) );		d3dpp.Windowed = true;		d3dpp.SwapEffect = D3DSWAPEFFECT_COPY_VSYNC;		d3dpp.BackBufferHeight = height;		d3dpp.BackBufferWidth = width;		d3dpp.BackBufferFormat = d3ddm.Format;		d3dpp.FullScreen_RefreshRateInHz = 0;		d3dpp.hDeviceWindow = g_hWnd;		HRESULT ret = m_pd3dDevice->Reset(&d3dpp);		if(SUCCEEDED(ret))		{		}		HRESULT result = m_pd3dDevice->TestCooperativeLevel();		if (result == D3D_OK)		{		}		SetWindowPos (g_hWnd, HWND_TOP, 0, 0, width, height, SWP_SHOWWINDOW);}


and the control part.
LRESULT CD3DApplication::MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam,                                  LPARAM lParam ){    HRESULT hr;    switch( uMsg )    {		case WM_KEYDOWN:			switch (wParam)				{				case VK_F7:					ChangeResolution(1024,768);					D3DVIEWPORT8 m_ViewPortMain;					m_ViewPortMain.X = 0;					m_ViewPortMain.Y = 0;					m_ViewPortMain.Width = 1024;					m_ViewPortMain.Height = 768;					m_ViewPortMain.MinZ = 0.0f;					m_ViewPortMain.MaxZ = 1.0f;					m_pd3dDevice->SetViewport(&m_ViewPortMain);                      // i have reset the camera project matrix and clear the device.                                                    FLOAT fAspect = ((FLOAT)m_ViewPortMain.Width) / m_ViewPortMain.Height;					m_pCamera->SetProjParams( g_fCamerFOV, fAspect,g_fNearPlane, g_fFarPlane);											RI->SetTransform( D3DTS_PROJECTION, (FLOAT*)&m_pCamera->GetProjMatrix() );					RI->SetViewport(&m_ViewPortMain);					RI->Clear( 0L, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER,0, 1.0f, 0L );					break;				}			break;    }    return DefWindowProc( hWnd, uMsg, wParam, lParam );}

and g_hWnd is created in:
HRESULT CD3DApplication::Create( HINSTANCE hInstance ){// any unrelated codes are omitted   // Create the render window        g_hWnd = m_hWnd = CreateWindow( _T("D3D Window"), m_strWindowTitle, m_dwWindowstyle,                               CW_USEDEFAULT, CW_USEDEFAULT,							   //m_dwCreationWidth, m_dwCreationHeight,                               (rc.right-rc.left), (rc.bottom-rc.top), 							   0L,                               NULL/*hMenu*/,                               hInstance, 0L );}

and the render part.
//-----------------------------------------------------------------------------// Name: Render3DEnvironment()// Desc: Draws the scene.//-----------------------------------------------------------------------------HRESULT CD3DApplication::Render3DEnvironment(){    // any unrelated codes are omitted    // Show the frame on the primary surface.    m_pd3dDevice->Present( NULL, NULL, NULL, NULL );}

Evil, they are the wholly codes that related to the switching-to-resolution function.
ANY HINTS?

[Edited by - roy84 on July 9, 2010 5:14:47 AM]
hello, everyone. i have found the bug behind it: i have not properly placed the code
           D3DVIEWPORT8 m_ViewPortMain;	   m_ViewPortMain.X = 0;	   m_ViewPortMain.Y = 0;	   m_ViewPortMain.Width = 1024;	   m_ViewPortMain.Height = 768;	   m_ViewPortMain.MinZ = 0.0f;	   m_ViewPortMain.MaxZ = 1.0f;	   m_pd3dDevice->SetViewport(&m_ViewPortMain);                      // i have reset the camera project matrix and clear the device.                       FLOAT fAspect = ((FLOAT)m_ViewPortMain.Width) / m_ViewPortMain.Height;	   m_pCamera->SetProjParams( g_fCamerFOV, fAspect,g_fNearPlane, g_fFarPlane);							   RI->SetTransform( D3DTS_PROJECTION, (FLOAT*)&m_pCamera->GetProjMatrix() );	   RI->SetViewport(&m_ViewPortMain);

these codes should be placed in the main render loop, and be called frequently.
i can not place them in MsgProc, in that way, it can only be called once.
so when i move them there, it can function normally.
and when i press F7, the D3DPRESENT_PARAMETERS is reset, the device is reset,a global flag is set to indicate there will be some modifications on the viewport and project matrix as well,in the main render loop, when the global flag is set, then it will regularly set the viewport and the project matrix.
thanks for everyone' kind help, especially Evil's. thanks again.

This topic is closed to new replies.

Advertisement