Sign in to follow this  
CodeReaver

Windowed to fullscreen and back:

Recommended Posts

CodeReaver    131
In my MsgProc I have this:
case WM_SIZE:
	Cleanup();
	if (!InitD3D(hWnd))
		MessageBox(NULL, "InitD3D failed", "ERROR", MB_ICONERROR | MB_OK);
	if (!InitVB())
		MessageBox(NULL, "InitVB failed", "ERROR", MB_ICONERROR | MB_OK);
	break;
case WM_KEYDOWN:
	switch (wParam) {
		case VK_F2:
			ToggleFullScreen(hWnd);
			break;
	}
break;


The toggle full screen function is this:
void ToggleFullScreen(HWND hWnd) {
	
	ShowCursor(fullscreen);
	fullscreen = !fullscreen;
	if (fullscreen) SetWindowLong(hWnd, GWL_style, WS_POPUP | WS_VISIBLE);
	else		SetWindowLong(hWnd, GWL_style, WS_OVERLAPPEDWINDOW | WS_VISIBLE);
	SetWindowPos(hWnd, HWND_NOTOPMOST, 100, 100, 500, 500, SWP_FRAMECHANGED);
	return;
}

The Cleanup function is this:
void Cleanup() {

	if (g_pVB != NULL)			g_pVB->Release();
	if (g_pD3DDevice != NULL)		g_pD3DDevice->Release();
	if (g_pD3D != NULL)			g_pD3D->Release();
}

I've bought "Beginning Direct3D Game Programming" a couple of days ago and it says something about needing to recreate/reset the device before resizing the window for some reason. Im my program I have the backbuffer size always the same size as the window or the size of the system metrics, so would it still be a problem. The vertex buffer, device and D3D object are the only things I created, so that's all I release. The SetWindowPos seems to trigger the WM_SIZE message before it completes, so I was wondering if this is working in the right order or not or whether it would make a difference the way I calculate the backbuffer sizes. Here is what I did in the InitD3D function:
bool InitD3D (HWND hWnd) {

	if((g_pD3D = Direct3DCreate9(D3D_SDK_VERSION)) == NULL) return false;

	ZeroMemory(&g_pD3Dpp, sizeof(g_pD3Dpp));

	if(!fullscreen)
	{
		g_pD3Dpp.Windowed = TRUE;
		g_pD3Dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
		//RECT windowRect;
		//GetClientRect(GetDesktopWindow(), &windowRect);
		g_pD3Dpp.BackBufferWidth        = GetSystemMetrics(SM_CXSCREEN);
		g_pD3Dpp.BackBufferHeight       = GetSystemMetrics(SM_CYSCREEN);
		g_pD3Dpp.BackBufferFormat = D3DFMT_UNKNOWN;
	}
	else
	{
		g_pD3Dpp.Windowed               = FALSE;
		g_pD3Dpp.SwapEffect             = D3DSWAPEFFECT_DISCARD;
		g_pD3Dpp.BackBufferWidth        = GetSystemMetrics(SM_CXSCREEN);
		g_pD3Dpp.BackBufferHeight       = GetSystemMetrics(SM_CYSCREEN);
		g_pD3Dpp.BackBufferFormat       = D3DFMT_A8R8G8B8;
	}

	if (FAILED(
		g_pD3D->CreateDevice(
			D3DADAPTER_DEFAULT,
			D3DDEVTYPE_HAL,
			hWnd,
			D3DCREATE_SOFTWARE_VERTEXPROCESSING,
		    &g_pD3Dpp,
			&g_pD3DDevice
		)
	)) return false;

    g_pD3DDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
    g_pD3DDevice->SetRenderState(D3DRS_LIGHTING, FALSE);


	D3DXMATRIXA16 matProj;
	float aspect = 1.0f;
	D3DDEVICE_CREATION_PARAMETERS cParams;
	RECT windowRect;

	g_pD3DDevice->GetCreationParameters(&cParams);
	GetClientRect(cParams.hFocusWindow, &windowRect);
	aspect = ((float) windowRect.right-windowRect.left) / ((float)windowRect.bottom-windowRect.top);
	D3DXMatrixPerspectiveFovLH(&matProj, D3DX_PI/4, aspect, 1.0f, 100.0f);
	g_pD3DDevice->SetTransform(D3DTS_PROJECTION, &matProj);

	return true;
}

Also, would I only need to set the perspective once if that's staying the same until the screen is resized or should I move that back into the SetUpMatrices function where it was to begin with? Another thing I had an issue with was at one point, I had the windowed mode getting the back buffer sizes from the windowRect and it was making the program crash when I didn't use a proper size like the GetSystemMetrics. I thought that was only an issue in fullscreen mode. I changed some other (seemingly unrelated) part of the program and it seemed to fix it, but I don't know what was causing it in the first place. I don't have the updated code with me at the moment and I don't have the internet at home. [Edited by - Muhammad Haggag on November 25, 2005 8:55:44 AM]

Share this post


Link to post
Share on other sites
CodeReaver    131
I did do a search already, but I didn't find any examples of letting the WM_SIZE message handle the release and rebuild of the resources for a change to full screen mode. There might have been threads on simply changing the size of the window or changing modes seperarly, but I don't think there were any that do both at once by allowing the SetWindowPos to cause the WM_SIZE and the WM_SIZE to cause the recreation of resources in the way I did.

(Is there a way to just search the current forum and not the whole message boards? Because that would help.)

Share this post


Link to post
Share on other sites
Muhammad Haggag    1358
Quote:
Original post by CodeReaver
I did do a search already, but I didn't find any examples of letting the WM_SIZE message handle the release and rebuild of the resources for a change to full screen mode. There might have been threads on simply changing the size of the window or changing modes seperarly, but I don't think there were any that do both at once by allowing the SetWindowPos to cause the WM_SIZE and the WM_SIZE to cause the recreation of resources in the way I did.

(Is there a way to just search the current forum and not the whole message boards? Because that would help.)

Oh, sorry for not making myself clear. I was talking about the absence of source tags from your post, not the nature of the question.

The original forum search software has been disabled for a long time now, and without it you can't search an individual forum. Enabling it is on the staff's ToDo list.

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