4

I intend to create a small application with 2 windows, a normal window with controls and a 3D window, rendered with DirectX. For 3D window PeekMessage() is recommended because it doesn't wait after checking the messages but for normal windows (no 3D rendering) the GetMessage() function is used to avoid processor senseless overuse. So, when the 3D window is active (WM_ACTIVE message received) I want to use PeekMessage() and when the normal window is active I want to use GetMessage().

The main loop would look like this:

NormalWindowActive = false;
Window3DActive = false;
MSG msg;

while (TRUE) {
    if (PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE)) {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    if (NormalWindowActive) {
        if (GetMessage(&msg, NULL, 0, 0)) {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    } else {
        RenderWindow();
    }
}

In the messages handler of these windows I have the WM_ACTIVATE message:

HWND NormalWindow, Window3D; // These windows are global vars

LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
    // ...
    case WM_ACTIVATE:
        if (wParam!= WA_INACTIVE) {
            if (hWnd == NormalWindow) {
                NormalWindowActive = true;
                Window3DActive = false;
            } else {
                Window3DActive = true;
                NormalWindowActive = false;
            }
        }
    break;
    // ...
}

What I expect from the TaskManager is to show the application busy (~50% processor use) when the 3D window is active and not so busy when the normal window is active (~5% processor use). I only see 50% processor use when both windows have lost focus but I see 0-5% processor use when any of them is active. I believe that there should be a difference, so I am not sure if this is really working (or even possible). The 3D window is rendered and the normal window responds to events, too, but the processor use confuses me. I just don't want this to affect the FPS of the 3D window.

Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
ali
  • 10,927
  • 20
  • 89
  • 138
  • Instead of trying to deduce what's happening from task manager, why not simply put in a counter and count how many times you're using GetMessage vs. PeekMessage? Then you can test it a while and break in with a debugger and examine the counters. – Carey Gregory Apr 05 '13 at 15:45
  • I'll do that right now. Thanks – ali Apr 05 '13 at 15:46
  • Add some tracing statements with OutputDebugString(), this is hard to debug with breakpoints. – Hans Passant Apr 05 '13 at 15:50
  • Well, in like 3 seconds of application running, the counter (inside the main while) has led to 136. I suppose this is a god sign – ali Apr 05 '13 at 15:51
  • The case should be `WM_ACTIVATE` (rather than `WM_ACTIVE`). Is that just a typo here? Also, do both windows share this window proc? – Adrian McCarthy Apr 05 '13 at 16:59
  • WM_ACTIVATE, sorry. Yes, both windows share the same WndProc – ali Apr 05 '13 at 18:00
  • 1
    Does your `RenderWindow` function use vertical synchronization? That would limit the rate at which your message loop runs. – arx Apr 06 '13 at 17:08
  • Try using a while loop for PeekMessage. – Ashish Negi Feb 07 '14 at 12:06
  • Have you fixed this? Perhaps you could show where NormalWindow gets set? And how you know its the proper value? – Dan Feb 17 '14 at 23:31

1 Answers1

0

Instead of checking if the normal window is active, you should check whether the 3D window is inactive. You can also use WaitMessage instead of GetMessage because it saves you from duplicating code.

For example:

if (Window3DActive)
    RenderWindow();
else
    WaitMessage();

Your problem seems to be that when the normal window is inactive, the message loop doesn't wait even though the 3D window is inactive.

If you decide to use WaitMessage, you should also loop on PeekMessage because there might be more than one message in the queue and WaitMessage doesn't return until there a new message arrives.

quantum
  • 3,672
  • 29
  • 51