I've been working on this for a couple days now. I'm really looking for the best way to render. Nothing too complicated but something that is fast, reliable, and powerful. I've seen many websites and about each one shows a different way. I've probably gone through about 5 different ways so far. But I'm really looking for a good, reliable one and I don't think I have found it yet.

I'm looking for a way that if the game is minimized, then no rendering will take place. Such that, the CPU usage is less than 5%. Also, a lot of people say handle the message to check when the game is minimized and then sleep every 50 ms until the game is back up. That just seems so messy to me. I'm looking for a way that will not Render or anything like that when the program is minimized, but when it is maximized, the device will reset.

I'm looking for a way that will process messages very fast (i suppose with peekmessage combined with a while loop) and a way that will allow the program to exit once the message is WM_QUIT. Perhaps the outer loop being a while loop checking peekmessage for WM_QUIT? I'm just not sure. I guess I'm just looking for a perfect loop. I know there probably isn't one, but there has to be at least some good ones.

Most of the examples I find on the web, the CPU usage is 50% when the game is minimized, the device doesn't properly get reset when the program is maximized again, etc etc.

I fiqure I should use TestCooperativeLevel() but whatever I try always never works (processing while game is minimized, device not getting reset properly, etc.)

Really, I'm just looking for idea on creating a GOOD render loop. One suitable for a large game. Thanks for any help :)

Dani AI

Generated

raised the right concerns: an efficient D3D9 loop must (1) stop burning CPU when the app is not visible and (2) handle device loss/reset cleanly. 's idea of not rendering while minimized is a valid direction, and is right to look at examples — but a small, reliable pattern ties those pieces together and avoids busy waits or fragile Reset attempts.

Use a two-mode loop: active (visible) and idle (not visible). In active mode process all pending messages, update time-based game logic, and render every frame. In idle mode block on the OS so the thread sleeps (use GetMessage or MsgWaitForMultipleObjects with a sensible timeout) instead of spinning with Sleep. That keeps CPU near zero while still waking promptly for input or window events. A short example of the switch pattern:

while(running)
{
  // drain messages
  while(PeekMessage(...)) { DispatchMessage(...); if (quit) goto exit; }

  if (!appIsActive) {
    // block until a message or timeout (poll device state occasionally)
    MsgWaitForMultipleObjects(0, NULL, FALSE, 100, QS_ALLINPUT);
    continue;
  }

  // update by elapsed time; render (with device-loss handling inside)
}

Device-loss handling must live inside the render/update paths and follow D3D9 rules: query TestCooperativeLevel, stop rendering if the device is lost, release D3DPOOL_DEFAULT resources before Reset, call Reset when the device is ready, then recreate those resources. Do not call Reset repeatedly in a tight loop; poll at intervals while blocked in idle mode. See Microsoft’s D3D9 guidance for exact return codes and steps: and IDirect3DDevice9::TestCooperativeLevel.

Troubleshooting tips: avoid Sleep-based throttles for normal visibility changes, keep volatile resources in D3DPOOL_DEFAULT so you can release/recreate them, and cap frame rate or present interval to reduce CPU/GPU when active. This pattern is simple, robust, and scales from small demos to larger D3D9 engines.

Recommended Answers

All 2 Replies

Member Avatar for Member #46692

Read the tutorials, NeHe etc, there's so many...

Dont have any code atm .... But you could look to catch the window's minimize message and skip your render & update parts of your game. Then catch the restore message, reset/reload any resources you need (images, timmer, models ect).

... that's how I used to do it. I use OpenGL now .... really clean & simple code and no resource losses....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.