   Help! When i close my d3d program the screen flashes green!
  Hi guys,Im making a simple program with direct3d9, and when i close the program, the screen flashes green for a split second :(Iv read up on ToyMaker that this means that the backbuffer is not being cleared properly, but i dont see why this would be the case.The main part of WinMain() is:D3DInit(hWnd);//initialise direct3dbigShip.Load("Meshes\\LandShark.x");//load the mesh to display //set up the cameracamera.LookAt(D3DXVECTOR3(0.0f, 0.0f, 0.0f));camera.Up(D3DXVECTOR3(0.0f, 1.0f, 0.0f));camera.FOV(D3DXToRadian(45));camera.AspectRatio((FLOAT)SCREEN_WIDTH / (FLOAT)SCREEN_HEIGHT);camera.NearPlane(1.0f);camera.FarPlane(1000.0f); //set up cursorCursorPos(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2);ShowCursor(false); MSG msg; //main loopwhile(TRUE){if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)){if (msg.message == WM_QUIT)break; TranslateMessage(&msg);DispatchMessage(&msg);} Render();//render the scene //quit when user presses the ESC keyif(KeyDown(VK_ESCAPE))PostMessage(hWnd, WM_DESTROY, 0, 0);} //clean up d3d stuffD3DClean(); return msg.wParam;the render function://clear back and z bufferd3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);d3ddev->Clear(0, NULL, D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(255, 255, 255), 1.0f, 0); //begin scened3ddev->BeginScene(); //update the cameracamera.Update(); //draw the meshbigShip.Draw(); //end and display the scened3ddev->EndScene(); d3ddev->Present(NULL, NULL, NULL, NULL);d3d cleanup://in the header://for safely deleting and releasing things#define SAFE_RELEASE(p){if(p) {(p)->Release(); (p)=NULL;}}#define SAFE_RELEASE_ARRAY(p) {if(p) {(p)->Release(); (p)=NULL;}}#define SAFE_DELETE(p) {if(p) {delete(p); (p)=NULL;}}#define SAFE_DELETE_ARRAY(p) {if(p) {delete[](p); (p)=NULL;}} //loop through a vector of StaticMeshes (a class containing a mesh, array of materials, //and an array of textures, which for every instance which loads, adds a pointer of itself//to this vectorfor(vector<StaticMesh*>::iterator it = vStaticMeshes.begin();it != vStaticMeshes.end();++it){SAFE_RELEASE((*it)->Mesh);//release the meshSAFE_DELETE_ARRAY((*it)->Material);//delete[] the materialsfor(int i = 0;i < (int)(*it)->MaterialCount;i++){//for all the materialsSAFE_RELEASE_ARRAY((*it)->Texture);//release the textures}SAFE_DELETE_ARRAY((*it)->Texture);//delete[] the texture array} SAFE_RELEASE(z_buffer);//release the z-bufferSAFE_RELEASE(d3ddev);//release the d3d deviceSAFE_RELEASE(d3d);//release the d3d objectI am releasing or deleting everything, and am clearing the backbuffer like in any example, so i am very confused as to why this is happening.I debugged the program, and the output of the d3d part is this:Direct3D9: :====> ENTER: DLLMAIN(00c0e6e0): Process Attach: 000015bc, tid=00000e80Direct3D9: :====> EXIT: DLLMAIN(00c0e6e0): Process Attach: 000015bcDirect3D9: (INFO) :Direct3D9 Debug Runtime selected.Direct3D9: (WARN) :driver set D3DDEVCAPS_TEXTURENONLOCALVIDMEM w/o DDCAPS2_NONLOCALVIDMEM:turning off D3DDEVCAPS_TEXTURENONLOCALVIDMEMDirect3D9: (WARN) :driver set D3DDEVCAPS_TEXTURENONLOCALVIDMEM w/o DDCAPS2_NONLOCALVIDMEM:turning off D3DDEVCAPS_TEXTURENONLOCALVIDMEMDirect3D9: (WARN) :driver set D3DDEVCAPS_TEXTURENONLOCALVIDMEM w/o DDCAPS2_NONLOCALVIDMEM:turning off D3DDEVCAPS_TEXTURENONLOCALVIDMEMDirect3D9: (INFO) :======================= Hal SWVP device selected Direct3D9: (INFO) :HalDevice Driver style 6 Direct3D9: :BackBufferCount not specified, considered default 1 Direct3D9: :Subclassing window 000208bcDirect3D9: :StartExclusiveModeDirect3D9: (INFO) :Using P4 PSGP D3D9 Helper: Warning: Default value for D3DRS_POINTSIZE_MAX is 2.19902e+012f, not 0.0078125f.  This is ok.Direct3D9: (WARN) :Ignoring redundant SetRenderState - 137 Direct3D9: (WARN) :Ignoring redundant SetRenderState - 7 Direct3D9: :Window 000608be is on top of us!!Direct3D9: :Window 000508c6 is on top of us!!Direct3D9: :Window 000608be is on top of us!!Direct3D9: :Window 000508c6 is on top of us!!Direct3D9: :Window 000608be is on top of us!!Direct3D9: :Window 000508c6 is on top of us!!Direct3D9: :Window 000608be is on top of us!!Direct3D9: :Window 000508c6 is on top of us!!Direct3D9: :WM_SYSKEYUP: wParam=00000009 lParam=a00f0001Direct3D9: :Hot key pressed, switching away from appDirect3D9: :Window 000608be is on top of us!!Direct3D9: :Window 000508c6 is on top of us!!Direct3D9: :WM_ACTIVATEAPP: BEGIN Deactivating app pid=000015bc, tid=00000e80Direct3D9: :DoneExclusiveModeDirect3D9: :INACTIVE: 000015bc: Restoring original mode (1024x768x22x60) at adapter index 0Direct3D9: :*** Active state changingDirect3D9: :WM_ACTIVATEAPP: DONE Deactivating app pid=000015bc, tid=00000e80D3D9 Helper: IDirect3DDevice9::Present failed: D3DERR_DEVICELOSTDirect3D9: :WM_SYSKEYUP: wParam=00000012 lParam=c0380001D3D9 Helper: IDirect3DDevice9::Present failed: D3DERR_DEVICELOSTD3D9 Helper: IDirect3DDevice9::Present failed: D3DERR_DEVICELOSTDirect3D9: :WM_SYSKEYUP: wParam=00000012 lParam=c0000001D3D9 Helper: IDirect3DDevice9::Present failed: D3DERR_DEVICELOSTDirect3D9: :WM_ACTIVATEAPP: BEGIN Deactivating app pid=000015bc, tid=00000e80Direct3D9: :*** Already deactivatedDirect3D9: :WM_ACTIVATEAPP: DONE Deactivating app pid=000015bc, tid=00000e80D3D9 Helper: IDirect3DDevice9::Present failed: D3DERR_DEVICELOSTD3D9 Helper: IDirect3DDevice9::Present failed: D3DERR_DEVICELOSTafter that it keeps saying that it lost the device, but that is because i alt tabbed to get to the vc++ window.Does anyone have any ideas about how i can fix this?Thanks in advance,Nazdreg   
  Urgh. This is why I hate the site you got that code from (Which I'm not going to mention because it's a swear word as far as I'm concerned, but for the record it's not toymaker.info, that's fine). There's loads of things wrong with that code:1. You only process 1 message per frame, you need to process all messages, then render2. WM_DESTROY is a notification, it's invalid for you to send it as a message to anything, and undefined behaviour to try (Most likely, you'll get a memory or handle leak from that PostMessage() call).3. You shouldn't clear the target and z-buffer seperately, do it in one call (When you clear stuff off your desk, do you clear paper first then pens/pencils, or do you clear everything in one go?)4. You never check the return value of Present() - there's 3 possible important return values, and you ignore them all.As for the actual problem; are you creating a fullscreen device? Do you know about Lost Devices?   ;  ok..1&2: could you please give an example of a better main loop then?all the examples i have seen (including ones from toymaker.info)show the loop i am using.3: ok cool, i didnt know that you could do that.4: what? do you mean d3ddev->Present(NULL, NULL, NULL, NULL);?   because i am not checking the return value..yes, i am creating a fullscreen device, and yes, i know about lost devices, but thats not the problem, i just havnt included the code for hanlding that yet   ;  here is my code to initialise d3d if it helpsd3d = Direct3DCreate9(D3D_SDK_VERSION); D3DPRESENT_PARAMETERS d3dpp; ZeroMemory(&d3dpp, sizeof(d3dpp));d3dpp.Windowed = FALSE;d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;d3dpp.hDeviceWindow = hWnd;d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;d3dpp.BackBufferWidth = SCREEN_WIDTH;d3dpp.BackBufferHeight = SCREEN_HEIGHT;d3dpp.EnableAutoDepthStencil = TRUE;d3dpp.AutoDepthStencilFormat = D3DFMT_D16; if(FAILED(d3d->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,hWnd,D3DCREATE_SOFTWARE_VERTEXPROCESSING,&d3dpp,&d3ddev)))assert(!"Direct3D device not created!"); d3ddev->SetRenderState(D3DRS_LIGHTING, FALSE);d3ddev->SetRenderState(D3DRS_ZENABLE, TRUE);;Quote:Original post by Nazdreg1&2: could you please give an example of a better main loop then?all the examples i have seen (including ones from toymaker.info)show the loop i am using.http://members.gamedev.net/jhoxley/directx/DirectXForumFAQ.htm#D3D_24There's not full code there, but you can still find out what's wrong in your message loop. Pay attention to 6-b.Btw, to your problem - if you don't use the debug DX runtimes, is the problem still there?   ;Quote:Original post by Nazdregok..1&2: could you please give an example of a better main loop then?all the examples i have seen (including ones from toymaker.info)show the loop i am using.Instead of:while(TRUE){if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)){if (msg.message == WM_QUIT)break; TranslateMessage(&msg);DispatchMessage(&msg);} Render();//render the scene// ...You should use:bool bQuit = false;while(!bQuit){while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)){if (msg.message == WM_QUIT)bQuit=true; TranslateMessage(&msg);DispatchMessage(&msg);} Render();//render the scene// ...Quote:Original post by Nazdreg3: ok cool, i didnt know that you could do that.Yup, you can bitwise OR the D3DCLEAR_* flags together.Quote:Original post by Nazdreg4: what? do you mean d3ddev->Present(NULL, NULL, NULL, NULL);?   because i am not checking the return value..pThat's my point [smile]Possible values it can return, that you need to know about:D3D_OK: No errorD3DERR_DEVICELOST: The device is lost (E.g. fullscreen window not in focus), so you cannot do any rendering.D3DERR_DEVICENOTRESET: The device was previously lost, and needs Reset() (E.g. because your fullscreen app is back in focus now)Any other FAILED() return value you can probably assume is a critical failure and you should exit ASAP (E.g. D3DERR_DRIVERINTERNALERROR).Quote:Original post by Nazdregyes, i am creating a fullscreen device, and yes, i know about lost devices, but thats not the problem, i just havnt included the code for hanlding that yetThe message D3D9 Helper: IDirect3DDevice9::Present failed: D3DERR_DEVICELOST implies that you're not handling lost devices properly (If your device is lost, you shouldn't even try to render until the device is ready to be reset). That may be related the the screen flashing green for one frame - the green flash implies that on the last frame before shutdown you're calling Present() without having called Clear() since the last call to Present().;  ok, thanks, but it looks like the only extra thing it is doing is checking for and handling lost devices, which i should and will add, but i didnt think that would cause a backbuffer error. (i do check for other windows messages, i do it in a WindoProc())and no, when i set d3d to use retail version, the problem goes away, but i expected that, because toymaker.info syas that if the backbuffer is not cleared properly then the debug runtime clears it to green and purple alternatively   ;Quote:Original post by Evil SteveThat's my pointPossible values it can return, that you need to know about:D3D_OK: No errorD3DERR_DEVICELOST: The device is lost (E.g. fullscreen window not in focus), so you cannot do any rendering.D3DERR_DEVICENOTRESET: The device was previously lost, and needs Reset() (E.g. because your fullscreen app is back in focus now)Any other FAILED() return value you can probably assume is a critical failure and you should exit ASAP (E.g. D3DERR_DRIVERINTERNALERROR).oh right, i misreadQuote:Original post by Evil SteveThat may be related the the screen flashing green for one frame - the green flash implies that on the last frame before shutdown you're calling Present() without having called Clear() since the last call to Present().how can that be?at the start of the render function, i call clear(), and at the end, i call present().;Quote:Original post by NazdregQuote:Original post by Evil SteveThat may be related the the screen flashing green for one frame - the green flash implies that on the last frame before shutdown you're calling Present() without having called Clear() since the last call to Present().how can that be?at the start of the render function, i call clear(), and at the end, i call present().From the code you've posted, I can't see any reason that would happen, but I'd still try to fix the "D3D9 Helper: IDirect3DDevice9::Present failed: D3DERR_DEVICELOST" warnings, and see if that's related to the cause.Does this green flash only happen at shutdown, or does it happen when you alt+tab too?   ;  yes, it flashes when i alt-tab.I guess ill put in the lost device handling then, thanks   