   Direct3D fullscreen crashes
  Hello,I've been creating a framework for Direct3D (DirectX9) so I don't have to initialize directx and a window every time i want to create a project.I've read (and finished reading the book Introduction to 3D Game Programming with DirectX 9.0c: A Shader Approach by Frank D Luna. So I know the stuff about Direct3D, I just can't manage to solve this problem.Basicly, what my program now does is create a window, initialize Direct3D and clear the backbuffer every frame (and then present it).When I press space, the program should go into fullscreen mode.My device gets created by these parameters:md3dPP.BackBufferWidth = 0; md3dPP.BackBufferHeight = 0;md3dPP.BackBufferFormat = D3DFMT_UNKNOWN;md3dPP.BackBufferCount = 1;md3dPP.MultiSampleType = D3DMULTISAMPLE_NONE;md3dPP.MultiSampleQuality = 0;md3dPP.SwapEffect = D3DSWAPEFFECT_DISCARD; md3dPP.hDeviceWindow = mhWnd;md3dPP.Windowed = true;md3dPP.EnableAutoDepthStencil = true; md3dPP.AutoDepthStencilFormat = D3DFMT_D24S8;md3dPP.Flags = 0;md3dPP.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;md3dPP.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;And when the fullscreen function is called, it changes the BackBufferWidth to 1280, backbufferheight to 1024, the format to D3DFMT_X8R8G8B8 and windowed to false.Then it calls te reset function (IDirect3DDevice9::Reset) with the &md3dPP passed to it.That reset call makes the program crash. It is compiled and ran under debug.This function has been enclosed by HR(X) so the error I get is D3DERR_INVALIDCALLI'm using a HD4870 graphics card and have my drivers installed (not sure they're the latest) but i can play new games on with it, so it's probably not my graphics card support.And my OS is windows 7 64bit.If more code is needed, i'll gladly post, i wanted to prevent the topic getting too long.Thanks in advance,Tigran   
  Are you sure the resolution you're requesting is supported?Also, try running the app using the debug D3D runtime (the debug output may very well tell you exactly what the problem is).   ;  I'm sure the format is supported, because I ran a demo application out of the book's source code, and it worked with the same format as what I use now.BTW, there also is this code:SetWindowLong(mhWnd, GWL_style, WS_POPUP);SetWindowPos(mhWnd, HWND_TOP, 0, 0, width, height, SWP_NOZORDER | SWP_SHOWWINDOW);when it goes to fullscreen andSetWindowLong(mhWnd, GWL_style, WS_OVERLAPPEDWINDOW);SetWindowPos(mhWnd, HWND_TOP, 100, 100, mWidth, mHeight, SWP_NOZORDER | SWP_SHOWWINDOW);Commenting out the first SetWindowPos call makes the program behave in a different way.I can switch to fullscreen, then back but if I then try going to fullscreen again, it crashes again.Ohyeah, when i run it under release then instead of the crash, by screen starts flickering black and white.BTW, the code above does work in the demo program of the book.   ;  You might not be resetting your devices. For instance if you are using D3DXFont then you would need to call that objects onResetDevice() or onLostDevice() method.That could cause a crash when the screen is re-sized.   ;  I don't have any of those devices.I kindda fixed it by calling the SetWindowLong SetWindowPos functions after the reset call. But it should've worked the other way aswell.I feel like i'm bypassing something, some kind of hack.Mostly, I just want to understand why it didn't work the prevous way.Anyway, what is the BB code of putting code in a message?   ;  Here are my opinions.1) BackbufferWidth and Height shouldn't be zero for a full screen app.Width and Height of the new swap chain's back buffers, in pixels. If Windowed is FALSE (the presentation is full-screen), this value must equal the width and height of the desktop. If Windowed is TRUE and either BackBufferWidth or BackBufferHeight is zero, the corresponding dimension of the client area of the hDeviceWindow (or the focus window, if hDeviceWindow is NULL) is taken.2) Windowed shoudl be set to FALSE. you tried to create a full screen app.http://msdn.microsoft.com/en-us/library/bb172588(v=VS.85).aspxHere is a sameple code for your reference. Error checking omitted.    DWORD    behaviorFlags = D3DCREATE_HARDWARE_VERTEXPROCESSING;  if (g_bFullScreenApp)  {hrResult = g_pD3D->GetAdapterDisplayMode(i, &desktopMode); g_pPresentParams[ushUsedParas].BackBufferWidth = desktopMode.Width; g_pPresentParams[ushUsedParas].BackBufferHeight = desktopMode.Height; g_pPresentParams[ushUsedParas].FullScreen_RefreshRateInHz = desktopMode.RefreshRate;  }  else  { GetClientRect(g_hWnd[ushUsedParas], &rcClient); g_pPresentParams[ushUsedParas].BackBufferWidth = rcClient.right - rcClient.left; g_pPresentParams[ushUsedParas].BackBufferHeight = rcClient.bottom - rcClient.top; g_pPresentParams[ushUsedParas].FullScreen_RefreshRateInHz = 0;  }  g_pPresentParams[ushUsedParas].BackBufferFormat = desktopMode.Format;  g_pPresentParams[ushUsedParas].BackBufferCount = 1;    g_pPresentParams[ushUsedParas].MultiSampleType = D3DMULTISAMPLE_NONE;    g_pPresentParams[ushUsedParas].MultiSampleQuality = 0;  g_pPresentParams[ushUsedParas].SwapEffect = D3DSWAPEFFECT_FLIP;  g_pPresentParams[ushUsedParas].hDeviceWindow = g_hWnd[ushUsedParas];    g_pPresentParams[ushUsedParas].Windowed = (g_bFullScreenApp == FALSE);  g_pPresentParams[ushUsedParas].EnableAutoDepthStencil = FALSE;  g_pPresentParams[ushUsedParas].AutoDepthStencilFormat = D3DFMT_D16;    g_pPresentParams[ushUsedParas].Flags = 0;  g_pPresentParams[ushUsedParas].PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;    hrResult = g_pD3D->CreateDevice(g_Device.Adapters[0].Ordinal,  D3DDEVTYPE_HAL, g_hWnd[0],  behaviorFlags,  g_pPresentParams, &g_pd3dDevice);If you still encounter some problem, please also provide code for full screen window creation.   ;  Thanks for your reply Jimmy Chen,Creating a fullscreen application (by filling the parameters the way you explained) did work. But I wanted to create an application that starts as a windowed application (hence the backbuffer width and height are 0 etc.) and wanted to switch to fullscreen mode by pressing a button. Without recreating the device, just changing a few parameters and resetting the device like done in the demo program I used to learn how to do this.You can download the demo program at http://www.d3dcoder.net/Data/Book2/PartII.zipFor example the source code of Chapter 4 (which is the shortest)There it does use everything the way I did as far as I know.BTW, at the demo, he leaves the FullScreen_RefreshRateInHz member at 0 (even for fullscreen)   