簡體   English   中英

使用基於 GetMessage 的鼠標停止時,GetCursorPos 不起作用 Window

[英]GetCursorPos doesn't work when mouse stops with GetMessage-based Window

我正在制作一個基於 UI 的 DirectX 11 應用程序。 在我發現PeekMessage()使用高 CPU 使用率(當 window 最小化時它使用量增加三倍)之前我沒有問題,所以我決定使用GetMessage() ,但現在輸入很奇怪而且我不知道如何解釋一下,當鼠標停止時,它不會給我 cursor position。

這是給我 cursor position 的 function:

Vector2f* InputSystem::GetMousePosition(Window* window, bool normalized) {
    POINT p;
    GetCursorPos(&p);
    RECT r;
    GetClientRect(window->hWnd, &r);
    ScreenToClient(window->hWnd, &p);
    lastMousePos = Vector2i(p.x - (r.right - r.left) / 2.0f, p.y - (r.bottom - r.top) / 2.0f);
    lastMousePos.y *= -1.0f;
    lastMousePos *= 2.0f;
    if (normalized) { lastMousePos /= Vector2f(window->GetClientSize().x, window->GetClientSize().y); }
    return &lastMousePos;
}

和循環:

while (mainWindow.IsRunning()) {
    mainWindow.Broadcast();
    mainWindow.PhaseUpdate();
    mainWindow.Loop();
}

Window::Broadcast()

void Window::Broadcast() {
    if (GetMessageA(&msg, hWnd, NULL, NULL)) {
        TranslateMessage(&msg);
        DispatchMessageA(&msg);
    }
}

GetMessage()阻塞調用線程,直到消息到達。 PeekMessage()不會阻塞。 您需要在游戲循環中使用PeekMessage()類的東西,而不是GetMessage() 盡管你聲稱,使用PeekMessage()不會增加你的 CPU 負載,除非你濫用它。 例如,與其在每個循環迭代中只調用一次,不如嘗試在第二個循環中調用它,直到消息隊列耗盡,例如:

void Window::Broadcast() {
    while (PeekMessageA(&msg, hWnd, 0, 0, PM_REMOVE)) {
        TranslateMessage(&msg);
        DispatchMessageA(&msg);
    }
}

另請參閱過濾 window 消息的危險

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM