Sign in to follow this  
Skute

Full CPU Usage (50% on dual core)

Recommended Posts

Skute    134
Hi, I've just been looking at the new Direct3D 10 tutorials (from the DX SDK) and I've noticed that they run at a full 100% on one core. The message loop uses the same one described in: http://www.mvps.org/directx/articles/writing_the_game_loop.htm Once you switch to another window you can't switch back. Is there a better loop? One that allows other applications to function too? (Heres the code from the tutorials)
// Main message loop
    MSG msg = {0};
    while( WM_QUIT != msg.message )
    {
        if( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) )
        {
            TranslateMessage( &msg );
            DispatchMessage( &msg );
        }
        else
        {
            Render();  
        }
    }

Share this post


Link to post
Share on other sites
Coaster Kev    269
With that type of message loop, you're always going to use 100%. In a fullscreen game, that's acceptable behavior.

In a windowed application, you might want to throw a Sleep(0) at end of your while loop. This will cause your application to give control back to the OS so it can let other applications do that thing they do.

Of course, you can also rearrange the message loop to handle window messages differently. I believe the Forum FAQ has information on how to best do this.

EDIT: Here's the link I was talking about. And you're handling it the practical way already.

Share this post


Link to post
Share on other sites
Skute    134
I think I may have just worked out why, but I'd appreciate it if someone could just confirm it for me.

My laptop has a GeForce Go 7950 - I'm guessing as it's not the 8xxx series it must be running in software mode? That's why it's running so slowly?

Share this post


Link to post
Share on other sites
Ezbez    1164
In the future, [google], please! This question gets asked very often.

Edit: Okay, you have some additional queries here too. However, I can't answer these without some guesswork. Your 100%-50% CPU for Signle-Dual core is, though, an extremely common question.

Share this post


Link to post
Share on other sites
Driv3MeFar    1080
Quote:
Original post by Skute
My laptop has a GeForce Go 7950 - I'm guessing as it's not the 8xxx series it must be running in software mode? That's why it's running so slowly?


Using 100% CPU does not imply that it's running slowly. Your message loop will constantly be running, looking for new messages and either dispatching them or rendering if there aren't any. That means that it's always doing some, hence you are using 100% of the CPU. In a game, this is generally acceptable, as nothing else really needs CPU time when you are playing a game. If it really is a concern (which it shouldn't be), throw in a Sleep() at the end of the loop, as others have suggested.

As for switching windows while your game is running, make sure you handle lost devices.

Share this post


Link to post
Share on other sites
sirob    1181
Quote:
Original post by Driv3MeFar
As for switching windows while your game is running, make sure you handle lost devices.

Direct3D 10 doesn't not have lost devices.

As for the OP, if you own an NVidia 7xxx series card, you cannot run D3D10 in hardware. The tutorials for D3D10 all default to the reference rasterizer (a slow software implementation).

Under the Reference Device, you application is running so slow, that it can't correctly handle getting focus back. You might have to wait several seconds between clicking on the window, and it getting focus. When running in hardware, that render loop is much more responsive.

Regardless, most game render loops are designed to loop as quickly as possible, and thus always take 100% CPU. You can use different techniques if you need it to work correctly in the background, the easiest being forcing VSync on your device, which would limit the framerate generated to the monitor's refresh rate.

Hope this helps.

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