From e21e0b80154afe4a62480efca61a07d4c2f2b0ef Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Mon, 3 Oct 2016 17:00:11 +0300 Subject: [PATCH] Add a system creation function with automatic backend detection --- source/openvr/openvrsystem.cpp | 5 +++++ source/openvr/openvrsystem.h | 2 ++ source/system.cpp | 26 ++++++++++++++++++++++++++ source/system.h | 1 + 4 files changed, 34 insertions(+) diff --git a/source/openvr/openvrsystem.cpp b/source/openvr/openvrsystem.cpp index e71ba33..c137400 100644 --- a/source/openvr/openvrsystem.cpp +++ b/source/openvr/openvrsystem.cpp @@ -47,6 +47,11 @@ OpenVRSystem::~OpenVRSystem() vr::VR_Shutdown(); } +bool OpenVRSystem::is_maybe_available() +{ + return vr::VR_IsHmdPresent(); +} + void OpenVRSystem::configure_view(StereoView &view) const { vr::IVRSystem *vr_sys = vr::VRSystem(); diff --git a/source/openvr/openvrsystem.h b/source/openvr/openvrsystem.h index 3e401ed..4988659 100644 --- a/source/openvr/openvrsystem.h +++ b/source/openvr/openvrsystem.h @@ -20,6 +20,8 @@ public: OpenVRSystem(); ~OpenVRSystem(); + static bool is_maybe_available(); + virtual void configure_window(Graphics::Window &) const { } virtual void configure_view(StereoView &) const; virtual void set_absolute_tracking(bool); diff --git a/source/system.cpp b/source/system.cpp index 99a6d48..c835456 100644 --- a/source/system.cpp +++ b/source/system.cpp @@ -25,5 +25,31 @@ System *System::create(const string &type) throw invalid_argument(format("system '%s' not supported", type)); } +System *System::create_autodetect() +{ +#ifdef WITH_OPENVR + if(OpenVRSystem::is_maybe_available()) + { + try + { + return new OpenVRSystem; + } + catch(const runtime_error &) + { } + } +#endif + +#ifdef WITH_LIBOVR + try + { + return new LibOVRSystem; + } + catch(const runtime_error &) + { } +#endif + + return 0; +} + } // namespace VR } // namespace Msp diff --git a/source/system.h b/source/system.h index 46c65d9..f7e28b5 100644 --- a/source/system.h +++ b/source/system.h @@ -20,6 +20,7 @@ public: virtual ~System() { } static System *create(const std::string &); + static System *create_autodetect(); virtual void configure_window(Graphics::Window &) const = 0; virtual void configure_view(StereoView &) const = 0; -- 2.45.2