]> git.tdb.fi Git - libs/game.git/commitdiff
Rework virtual reality initialization
authorMikko Rasa <tdb@tdb.fi>
Wed, 12 Feb 2025 16:48:12 +0000 (18:48 +0200)
committerMikko Rasa <tdb@tdb.fi>
Wed, 12 Feb 2025 16:50:38 +0000 (18:50 +0200)
VR mode must now be requested when creating the Application object,
because it may need to interact with graphics initialization.

source/gameview/application.h

index 83f96f19c674ce03398db51801d53a6faee5540a..2ac7570695ae8d462e6a0da9c666c0bb4683b154 100644 (file)
@@ -17,33 +17,41 @@ template<typename T, typename R>
 class Application: public Msp::RegisteredApplication<T>
 {
 public:
+       enum Features
+       {
+               NO_FEATURES = 0,
+               VIRTUAL_REALITY = 1
+       };
+
        using ResourcesType = R;
 
 protected:
        Graphics::Display display;
        Graphics::Window window;
+       std::unique_ptr<VR::System> vr_system;
        GL::Device gl_device;
        ResourcesType resources;
        Game::Director director;
        GL::WindowView gl_view;
-       std::unique_ptr<VR::System> vr_system;
        Presenter presenter;
        PlayerInput player_input;
 
 public:
-       Application();
+       Application(Features = NO_FEATURES);
+
+protected:
+       GL::DeviceOptions create_gl_device_options();
 
        int main() override;
 protected:
        void tick() override;
-
-       bool try_enable_vr();
 };
 
 template<typename T, typename R>
-Application<T, R>::Application():
+Application<T, R>::Application(Features features):
        window(display, 1920, 1080),
-       gl_device(window),
+       vr_system(features==VIRTUAL_REALITY ? VR::System::create_autodetect() : nullptr),
+       gl_device(window, create_gl_device_options()),
        director(resources),
        gl_view(window),
        presenter(director, gl_view),
@@ -51,6 +59,21 @@ Application<T, R>::Application():
 {
        window.set_keyboard_autorepeat(false);
        window.signal_close.connect(sigc::bind(sigc::mem_fun(this, &Application::exit), 0));
+
+       if(vr_system)
+       {
+               player_input.enable_vr(*vr_system);
+               presenter.enable_vr(*vr_system);
+       }
+}
+
+template<typename T, typename R>
+GL::DeviceOptions Application<T, R>::create_gl_device_options()
+{
+       GL::DeviceOptions opts = GL::Device::create_default_options();
+       if(vr_system)
+               vr_system->fill_gl_device_options(opts);
+       return opts;
 }
 
 template<typename T, typename R>
@@ -69,18 +92,6 @@ void Application<T, R>::tick()
        director.tick();
 }
 
-template<typename T, typename R>
-bool Application<T, R>::try_enable_vr()
-{
-       vr_system = VR::System::create_autodetect();
-       if(vr_system)
-       {
-               player_input.enable_vr(*vr_system);
-               presenter.enable_vr(*vr_system);
-       }
-       return static_cast<bool>(vr_system);
-}
-
 } // namespace Msp::GameView
 
 #endif