HOW to switch resolution in games using DirectX

Started by
17 comments, last by roy84 13 years, 9 months ago
hello guys, my problem here is : how to switch resolution in games? for example, when i am in the game, and then i want to change the resolution from 1024 * 768 to 800 *600 or such? then what should i do? my own thought is :reset the D3DPRESENT_PARAMETERS and reload the scene object?
thanks in advance.
Advertisement
It takes a total reset, you have to reload everything to change the resolution, unless you sneakily make the game a window that stretches to the size of the desktop, then you could avoid it.
Quote:Original post by rouncED
It takes a total reset, you have to reload everything to change the resolution, unless you sneakily make the game a window that stretches to the size of the desktop, then you could avoid it.
No, you only need to reload resources in D3DPOOL_DEFAULT. But yes, you call IDirect3DDevice9::Reset() with a new resolution in the present parameters.
Quote:Original post by Evil Steve
No, you only need to reload resources in D3DPOOL_DEFAULT. But yes, you call IDirect3DDevice9::Reset() with a new resolution in the present parameters.

thanks,but when should i Reset the D3DPRESENT_PARAMETERS? reset in my program often fails.
void ChangeResolution(int width, int height){	D3DDISPLAYMODE d3ddm;	if(FAILED(g_pD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &d3ddm)))	{		return ;	}	D3DPRESENT_PARAMETERS d3dpp; 	d3dpp.Windowed = true;	d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;	d3dpp.BackBufferHeight = height;	d3dpp.BackBufferWidth = width;	d3dpp.BackBufferFormat = d3ddm.Format;	d3dpp.FullScreen_RefreshRateInHz = 0;        HRESULT ret = g_pD3DDevice->Reset(&d3dpp);	if(SUCCEEDED(ret))	{		MessageBox(NULL,"g_pD3DDevice","warning",MB_OK);	}	else	{		if (ret == D3DERR_DEVICELOST)		{			MessageBox(NULL,"D3DERR_DEVICELOST","warning",MB_OK);		}		else if(ret == D3DERR_DRIVERINTERNALERROR)		{			MessageBox(NULL,"D3DERR_DRIVERINTERNALERROR","warning",MB_OK);		}		else if(ret == D3DERR_OUTOFVIDEOMEMORY)		{			MessageBox(NULL,"D3DERR_OUTOFVIDEOMEMORY","warning",MB_OK);		}	}}

Quote:
you only need to reload resources in D3DPOOL_DEFAULT ?
does it mean that all non-managed resources should be reloaded,or just resources in D3DPOOL_DEFAULT?

also before calling Reset, do i need to release the former resources in D3DPOOL_DEFAULT, or even the non-managed resources?
thank Evil Steve and rounceED's reply.
Quote:Original post by roy84
thanks,but when should i Reset the D3DPRESENT_PARAMETERS? reset in my program often fails.
Whenever you like. What error does it give when it fails?

Quote:Original post by roy84
Quote:you only need to reload resources in D3DPOOL_DEFAULT ?
does it mean that all non-managed resources should be reloaded,or just resources in D3DPOOL_DEFAULT?

also before calling Reset, do i need to release the former resources in D3DPOOL_DEFAULT, or even the non-managed resources?
thank Evil Steve and rounceED's reply.
You need to Release() resources in D3DPOOL_DEFAULT before calling Reset() (And call OnLostDevice() for any D3DX classes you have that have such a function), Reset(), and then re-create those default pool resources (And call OnResetDevice for any D3DX classes). See Lost Devices (Direct3D 9), in particular "Responding to a Lost Device". Resetting the device to change resolution is very similar to responding to a lost device (As if the device was in D3DERR_DEVICENOTRESET) state:
Quote:If the device can be restored, the application prepares the device by destroying all video-memory resources and any swap chains. Then, the application calls the IDirect3DDevice9::Reset method. Reset is the only method that has an effect when a device is lost, and is the only method by which an application can change the device from a lost to an operational state. Reset will fail unless the application releases all resources that are allocated in D3DPOOL_DEFAULT, including those created by the IDirect3DDevice9::CreateRenderTarget and IDirect3DDevice9::CreateDepthStencilSurface methods.
Quote:Original post by Evil Steve
Whenever you like. What error does it give when it fails?

it returns D3DERR_INVALIDCALL.
Quote:Original post by roy84
Quote:Original post by Evil Steve
Whenever you like. What error does it give when it fails?

it returns D3DERR_INVALIDCALL.
That usually means that there's still default pool resources allocated. Do the Debug Runtimes give any more information?
Quote:That usually means that there's still default pool resources allocated. Do the Debug Runtimes give any more information?

i have found the bug, it was a silly mistake i have made, sorry for that. i have not ZeroMemory-ed the D3DPRESENT_PARAMETERS structure, part of its memeber have not been assigned. so...
and my code is in version DirectX 8. so the DEBUG RUNTIMES doesn't bring any useful message to me.
now my code is:
void ChangeResolution(int width, int height){	D3DDISPLAYMODE d3ddm;	if(FAILED(g_pD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &d3ddm)))	{		return ;	}	D3DPRESENT_PARAMETERS d3dpp;         ZeroMemory( &d3dpp, sizeof(d3dpp) );// the very line is appended later.	d3dpp.Windowed = true;	d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;	d3dpp.BackBufferHeight = height;	d3dpp.BackBufferWidth = width;	d3dpp.BackBufferFormat = d3ddm.Format;	d3dpp.FullScreen_RefreshRateInHz = 0;        // first release the D3DPOOL_DEFAULT resources        Safe_Release(g_pVertexBuffer);// release D3DPOOL_DEFAULT resources.        // second reset the device.        HRESULT ret = g_pD3DDevice->Reset(&d3dpp);	if(SUCCEEDED(ret))	{		MessageBox(NULL,"g_pD3DDevice","warning",MB_OK);	}	else	{		if (ret == D3DERR_DEVICELOST)		{			MessageBox(NULL,"D3DERR_DEVICELOST","warning",MB_OK);		}		else if(ret == D3DERR_DRIVERINTERNALERROR)		{			MessageBox(NULL,"D3DERR_DRIVERINTERNALERROR","warning",MB_OK);		}		else if(ret == D3DERR_OUTOFVIDEOMEMORY)		{			MessageBox(NULL,"D3DERR_OUTOFVIDEOMEMORY","warning",MB_OK);		}	}} // here is the control part.  case WM_KEYDOWN:	switch (wParam)	{	case VK_F1:	ChangeResolution(1024,768);	if(SUCCEEDED(InitialiseVertexBuffer()))// re-create the sources.	{		MessageBox(NULL,"dddddddd","warning",MB_OK);	}		break;	}

thanks a lot for Evil's kind help,you are really a great master.thank you again.
Does the code work now, or are you still having problems?
Quote:Original post by Evil Steve
Does the code work now, or are you still having problems?


yup, it works now ,today i have made a simple model, which can show how to switch resolution in a D3D Application, and tomorrow i will apply the principle in the game.
thank you for kindness, Evil, you are so great. thank you so much.

This topic is closed to new replies.

Advertisement