   directX 9 app nearly crashing with other progs running
  hi all,well i''ve been using the information and tutorials at codesampler.com to learn directX 9 and i have a basic initialization app up and running.Now the problem im having is that when i run my app, or the first D3D9_initialization tutorial from codesampler.com they run okay, but then if i click on another open program (like my compiler, vc++ or wordpad for example) then my computer starts chugging along really really really slow. When i finally get focus back on the directX app everything runs fine again.This only happens with my app, and the first initialisation tutorial (which i used to help make my app), all the other tutorials run fine all the time.Has anyone else ever had this or similar problems with directX? or does anyone think they mite know something that is causing this?Im running winXP, AMD 1900xp, geForce3 ti 200, 256mb RAM if that helps.cheers 
  I only took a glimpse, but i see that the tutorials always render full speed ahead, active or not. This is alright if your window is the active one, but once the user works with another app/window, your 3d app will suck up all cpu power for itself.You''ll need to run a different loop part when your app is not active. Using WM_ACTIVATE you can see whether your app is active or not. Set a bool there according to your activation state. In the message loop if you''re not active use getmessage instead of peekmessage. And only render during WM_PAINT calls. Or at least put a Sleep inside that loop then. ;  thanks for the reply Endurion,i''ve been playing around with the dx9_initialization code from codesampler.com''s tutorials, and trying to play around with the WM_ACTIVE and WM_PAINT calls, i have added a bool like u suggested to track weather the app is active or not, and added the following case to WndProc() case WM_ACTIVATE:{if (LOWORD(wParam) == WA_INACTIVE)active = false;elseactive = true;}break;so if i have it right, this should now be keeping track of my ''active'' status.I then changed the message loop in win main to the following: while( uMsg.message != WM_QUIT ){if (active = true){if( PeekMessage( &uMsg, NULL, 0, 0, PM_REMOVE ) ){ TranslateMessage( &uMsg );DispatchMessage( &uMsg );}elserender();}else{if( GetMessage( &uMsg, NULL, 0, 0 ) ){ TranslateMessage( &uMsg );DispatchMessage( &uMsg );}}}now if i understand this code it is saying that if the app is inactive then wait for a message to arrive before processing it, otherwise just keep processing.What im having trouble understanding now is the WM_PAINT call, and where i put my render() calls. If i add a WM_PAINT case in WndProc() and put my render call in there then the program compiles but dies with fatal error upon running it.I currently have the render call in the message loop above, but it doesnt seem to get called unless i completely remove the WM_PAINT call from my WndProc() the window is just a black screen, where it should be a green screen that has been cleared by directx.when the render function isnt called i dont get the problem mentioned in my first post anymore, only when the render funtion is called.So i guess im askin for a bit of help understanding the WM_PAINT call a little better please. ;  Windows is probably sending your application a WM_PAINT message after  you've created your window, but before  you've initialized DirectX.  So what is happening is that you're calling your render() function before you have any DirectX stuff set up.  Which equals bad.  One option is to make sure that you set your DX pointers to NULL before window creation, and then in your render function, check to see that everything is not NULL before rendering; otherwise, skip rendering that frame.The other option is to use code something like this for your loop: while( uMsg.message != WM_QUIT ){  while ( PeekMessage( &uMsg, NULL, 0, 0, PM_REMOVE ) )  {    TranslateMessage( &uMsg );    DispatchMessage( &uMsg );  }  render();  if (active == false)  {    Sleep(4); //Could be anything from 0 to 16;  Experiment to find a good value  }}You'll always be peeking and rendering quickly, but if you're not active, then Sleep() is called every frame, letting other programs do stuff.  Also, notice that the PeekMessage() and all is in a while loop.  This'll save you headaches later on, 'cause sometimes you'll get more than one Windows message per frame, and thus you want to handle all messages you currently have queued at the moment before actually rendering the next frame.[Edit - Doh!  I was sleeping when I wasactive, not when I wasn'tactive.  Silly me.  Although a Sleep(0) even when you areactive, and a longer Sleep() when you aren't isn't necessarily a bad idea, depending on the program.] [edited by - Agony on February 12, 2004 11:03:32 AM];  In WM_PAINT you don''t need to call the full render, just call Present (only if your device is ok, and not lost of course). Also remember to "return 0" after presenting to tell windows that the HWND is already updated.Depending on where your movement code is now your app will only present the last rendered frame again. ;quote:Original post by Endurion  In WM_PAINT you don''t need to call the full render, just call Present (only if your device is ok, and not lost of course). Also remember to "return 0" after presenting to tell windows that the HWND is already updated.Depending on where your movement code is now your app will only present the last rendered frame again.   If your app uses the DISCARD method, you''ll end up Presenting garbage. Stay Casual,KenDrunken Hyena;quote:If your app uses the DISCARD method, you''ll end up Presenting garbage.does this mean i should keep my Present call within my render function?I must say as i''ve been working thru tutorials and examining directX source code i havent really seen the WM_PAINT call used much, if at all. Is there a reason it is left out?And Agony, i put a sleep call in and that has stopped the app from chugging really slow when another program gets focus. The only time it still chugs slow is if i minimise my app by clicking its icon on the taskbar. if i minimise it this way is my app sending a different message than WM_ACTIVATE? coz if this is the case then it is no longer setting the active flag properly and therfore its not calling the sleep function.I''ve gone back to using and if statement with my PeekMessage call because i cannot quit the app when using a while loop.And one last thing i cant work out (i know im asking a lot of questions...sorry, hehe) how come its only that one example that i need no play around with WM_ACTIVATE and stuff? coz the others all dont do it, either do examples from other sites, and they dont have any calls to WM_ACTIVATE or similar that i can see of.thanks everyone ;  I took the d3dapp-framework from the sdk, which includes that WM_PAINT, and i never had any problems with it. I also do use discard.But now, since you mention it, it''s strange, it ought to return garbage (just looked, i do call render, when i''m inactive too, just not that often, and since it''s between processed message i will have an image to be presented). ;  In the tight loop most D3D samples use the CPU will be busy 100%, causing other apps to almost stall.I just put a sleep(1) in my loop, now it uses only 20% CPU and other apps work just fine. If you are doing demo''s and samples to see how something works you don''t need 800 fps anyway. 