diff --git a/src/main.cpp b/src/main.cpp index b11163f..4a78984 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -35,6 +35,7 @@ std::map vkey_map; std::thread hmd_renderer; std::thread renderer; int running = -1; +int vr_running = 0; std::mutex render_mutex; std::condition_variable render_cv; @@ -475,10 +476,11 @@ bool win32_vr_start() BT_SetTerminate(); LOG("start hmd render thread"); App::I.has_vr = true; + vr_running = true; const float target_tick_rate = 90; unsigned long t0 = GetTickCount(); - while (running == 1 && vive->Valid()) + while (vr_running && running == 1 && vive->Valid()) { std::unique_lock lock(hmd_render_mutex); unsigned long t1 = GetTickCount(); @@ -487,7 +489,7 @@ bool win32_vr_start() vive->Update(); App::I.vr_active = vive->m_active; - if (vive->m_active) + if (vr_running && vive->m_active) { async_lock(); vive->Draw(); @@ -500,9 +502,12 @@ bool win32_vr_start() } App::I.vr_active = false; App::I.has_vr = false; - async_lock(); - vive->Terminate(); - async_unlock(); + vr_running = false; + if (async_lock_try()) + { + vive->Terminate(); + async_unlock(); + } LOG("hmd renderer terminated"); }); return true; @@ -512,6 +517,9 @@ void win32_vr_stop() { if (vive) { + vr_running = false; + if (hmd_renderer.joinable()) + hmd_renderer.join(); vive->Terminate(); delete vive; vive = nullptr;