Sign in to follow this  
johnny_no1_boy

Toggling windowed/fullscreen DirectX C++

Recommended Posts

Hello.I have a directx application that works on windowed mode and I wan't to make an option (like a button) to go in fullscreen and from fullscreen in windowed mode.How can I toggle from windowed in fullscreen and from fullscreen mode in windowed mode.Thanks...

Share this post


Link to post
Share on other sites
You need to Reset() the device, after modifying the Windowed member of the present parameters.

For any code or anything, we'll need to know what language you're using and what version of D3D (8, 9, 10, etc).

Share this post


Link to post
Share on other sites
I'm using Microsoft Visual C++ 2005 Express Edition + Platform SDK + DirectX 9 April 2007. Please give me some code...

[Edited by - johnny_no1_boy on August 2, 2007 11:51:23 AM]

Share this post


Link to post
Share on other sites
From my engine:

void PGraphics::SetFullscreen(bool bFullscreen)
{
if(!bFullscreen == m_thePresentParams.Windowed)
return;

m_thePresentParams.Windowed = !bFullscreen;
DoReset();
}

bool PGraphics::DoReset()
{
// Notify objects if needed
if(!m_bDeviceLost)
OnDeviceLost();

// Reset device
HRESULT hResult = m_pDevice->Reset(&m_thePresentParams);
if(FAILED(hResult))
{
std::wstringstream str;
str << L"Failed to reset D3D device. Error: ";
str << DXGetErrorString(hResult);
PApp::Get().SetError(str.str());
ELog::Get().ErrorFormat(L"RENDER : %s\n", PApp::Get().GetError().c_str());
PApp::Get().Exit();
Assert(false, "IDirect3DDevice9::Reset() failed");
return false;
}

// Update window style
if(m_thePresentParams.Windowed)
{
SetWindowLong(m_thePresentParams.hDeviceWindow, GWL_STYLE, WS_OVERLAPPEDWINDOW);
SetWindowPos(m_thePresentParams.hDeviceWindow, HWND_TOP, 0, 0, 640, 480,
SWP_FRAMECHANGED | SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW);
}
else
{
SetWindowLong(m_thePresentParams.hDeviceWindow, GWL_STYLE, WS_POPUPWINDOW);
SetWindowPos(m_thePresentParams.hDeviceWindow, HWND_TOP, 0, 0, 0, 0,
SWP_FRAMECHANGED | SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW);
}

// Reset render states, etc
PostReset();

// Reset objects
bool bFailed = false;
for(size_t i=0; i<m_vResettableObjects.size(); ++i)
{
if(!m_vResettableObjects[i]->OnReset())
bFailed = true;
}

return !bFailed;
}



Basically, you need to clean stuff up like state blocks, swap chains and D3DX objects (See the SDK docs for Reset() for a list of exactly what) before resetting, reset the device, then restore any objects that were cleaned up.

The window style stuff there is because you want a popup window (no caption or border) for fullscreen apps, and an overlapped (standard looking) window if you're windowed.

m_thePresentParams in my code is the same structure used to create the device.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this