Skip to content

Commit

Permalink
fix minimized cpu usage error
Browse files Browse the repository at this point in the history
  • Loading branch information
Plutoberth committed Feb 11, 2021
1 parent 17c5688 commit fa9a06d
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
1 change: 1 addition & 0 deletions Client/GUI_Impls/CrossPlatformGUI.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ constexpr auto GUI_MAX_MESSAGES = 10;
constexpr auto GUI_HEIGHT = 350;
constexpr auto GUI_WIDTH = 540;
constexpr auto FPS = 60;
constexpr auto MS_PER_FRAME = 1000 / FPS;
constexpr auto FONT_SIZE = 15.0f;
const auto WINDOW_COLOR = ImVec4(0.45f, 0.55f, 0.60f, 1.00f);

Expand Down
16 changes: 14 additions & 2 deletions Client/GUI_Impls/WindowsGUI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ void EnterGUIMainLoop(BluetoothWrapper bt)

CrossPlatformGUI gui(std::move(bt));

UINT presentFlags = 0;

// Main loop
MSG msg = { 0 };
while (msg.message != WM_QUIT)
Expand All @@ -58,6 +60,7 @@ void EnterGUIMainLoop(BluetoothWrapper bt)
// - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application.
// - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application.
// Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags.

if (::PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE))
{
::TranslateMessage(&msg);
Expand All @@ -79,8 +82,16 @@ void EnterGUIMainLoop(BluetoothWrapper bt)
g_pd3dDeviceContext->OMSetRenderTargets(1, &g_mainRenderTargetView, NULL);
g_pd3dDeviceContext->ClearRenderTargetView(g_mainRenderTargetView, (float*)&WINDOW_COLOR);
ImGui_ImplDX11_RenderDrawData(ImGui::GetDrawData());

g_pSwapChain->Present(1, 0); // Present with vsync

//We need this because Present doesn't delay when the app is minimized.
if (g_pSwapChain->Present(1, presentFlags) == DXGI_STATUS_OCCLUDED) {
presentFlags = DXGI_PRESENT_TEST;
//Artificial VSYNC when the app is minimized
Sleep(MS_PER_FRAME);
}
else {
presentFlags = 0;
}
}

// Cleanup
Expand All @@ -96,6 +107,7 @@ void EnterGUIMainLoop(BluetoothWrapper bt)
void DisplayErrorMessagebox(const std::string& message)
{
MessageBoxA(0, message.c_str(), "Sony Headphones Client | Unrecoverable Error", MB_OK | MB_ICONSTOP);
exit(GetLastError());
}

namespace WindowsGUIInternal
Expand Down

0 comments on commit fa9a06d

Please sign in to comment.