Document VR ownership and GLFW platform hooks

This commit is contained in:
2026-06-16 07:53:27 +02:00
parent 73c13f8cde
commit 34e2747867
5 changed files with 65 additions and 13 deletions

View File

@@ -30,6 +30,7 @@
#include <iomanip>
#include <ctime>
#include <sstream>
#include <memory>
#include <stop_token>
#define WM_USER_CLOSE (WM_USER + 1)
@@ -519,18 +520,17 @@ void init_vk_map()
std::mutex hmd_render_mutex;
std::condition_variable hmd_render_cv;
Vive* vive = nullptr;
std::unique_ptr<Vive> vive;
bool win32_vr_start()
{
if (sandboxed)
return false;
vive = new Vive;
vive = std::make_unique<Vive>();
vive->on_draw = [](const glm::mat4& proj, const glm::mat4& view, const glm::mat4& pose) { App::I->vr_draw(proj, view, pose); };
if (!vive->Initialize())
{
delete vive;
vive = nullptr;
vive.reset();
LOG("VR: failed to initialize vive");
return false;
}
@@ -615,8 +615,7 @@ void win32_vr_stop()
hmd_renderer.join();
}
vive->Terminate();
delete vive;
vive = nullptr;
vive.reset();
}
}

View File

@@ -94,6 +94,33 @@ public:
#endif
}
#if defined(__LINUX__) || defined(__WEB__)
struct RetainedLegacyGlfwWindowHooks final {
std::function<void()> acquire_render_context;
std::function<void()> present_render_context;
std::function<void()> request_app_close;
};
[[nodiscard]] RetainedLegacyGlfwWindowHooks& active_legacy_glfw_window_hooks()
{
static RetainedLegacyGlfwWindowHooks hooks = [] {
RetainedLegacyGlfwWindowHooks retained;
GLFWwindow* const window = App::I->glfw_window;
retained.acquire_render_context = [window] {
glfwMakeContextCurrent(window);
};
retained.present_render_context = [window] {
glfwSwapBuffers(window);
};
retained.request_app_close = [window] {
glfwSetWindowShouldClose(window, GLFW_TRUE);
};
return retained;
}();
return hooks;
}
#endif
#if defined(__IOS__) || defined(__OSX__)
[[nodiscard]] NSMutableArray<NSString*>* apple_file_types_array(const std::vector<std::string>& file_types)
{
@@ -412,7 +439,7 @@ public:
#elif __ANDROID__
android_async_lock();
#elif __LINUX__ || __WEB__
glfwMakeContextCurrent(App::I->glfw_window);
active_legacy_glfw_window_hooks().acquire_render_context();
#endif
}
@@ -432,7 +459,7 @@ public:
#elif __ANDROID__
android_async_swap();
#elif __LINUX__ || __WEB__
glfwSwapBuffers(App::I->glfw_window);
active_legacy_glfw_window_hooks().present_render_context();
#endif
}
@@ -743,7 +770,7 @@ public:
#ifdef __OSX__
active_apple_document_platform_services().request_app_close();
#elif __LINUX__
glfwSetWindowShouldClose(App::I->glfw_window, GLFW_TRUE);
active_legacy_glfw_window_hooks().request_app_close();
#endif
}