~sircmpwn/xrgears

ref: cedf85662a4c7c3d29611ad41a7238f26b74aaed xrgears/vitamin-k/system/vikApplication.hpp -rw-r--r-- 5.5 KiB
cedf8566 — Lubosz Sarnecki formatting. 4 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/*
 * vitamin-k
 *
 * Copyright 2016 Sascha Willems - www.saschawillems.de
 * Copyright 2017-2018 Collabora Ltd.
 *
 * Authors: Lubosz Sarnecki <lubosz.sarnecki@collabora.com>
 * SPDX-License-Identifier: MIT
 */

#pragma once

#include <sys/stat.h>

#include <vulkan/vulkan.h>

#define GLM_FORCE_RADIANS
#define GLM_FORCE_DEPTH_ZERO_TO_ONE
#define GLM_ENABLE_EXPERIMENTAL
#include <glm/glm.hpp>

#include <iostream>
#include <chrono>
#include <string>
#include <array>
#include <vector>

#include "vikSettings.hpp"

#include "../window/vikWindowXCB.hpp"
#include "../window/vikWindowWaylandXDG.hpp"
#include "../window/vikWindowWaylandShell.hpp"
#include "../window/vikWindowKMS.hpp"
#include "../window/vikWindowKhrDisplay.hpp"

#include "../render/vikTools.hpp"
#include "../render/vikInitializers.hpp"

#include "../scene/vikCamera.hpp"

#include "../render/vikRendererTextOverlay.hpp"

#include "../render/vikTimer.hpp"


namespace vik {
class Application {
 public:
  Settings settings;
  Window *window;
  bool quit = false;

  RendererTextOverlay *renderer;
  Camera *camera = nullptr;

  bool viewUpdated = false;

  std::string name = "vitamin-k Example";


  Application(int argc, char *argv[]) {
    if (!settings.parse_args(argc, argv))
      vik_log_f("Invalid arguments.");
    init_window();
    renderer = new RendererTextOverlay(&settings, window);

    auto set_window_resize_cb = [this]() { resize(); };
    renderer->set_window_resize_cb(set_window_resize_cb);

    auto enabled_features_cb = [this]() { get_enabled_features(); };
    renderer->set_enabled_features_cb(enabled_features_cb);

    auto frame_start_cb = [this]() {
      check_view_update();
    };
    renderer->set_frame_start_cb(frame_start_cb);

    auto frame_end_cb = [this](float frame_time) {
      update_camera(frame_time);
    };
    renderer->set_frame_end_cb(frame_end_cb);

    auto render_cb = [this]() {
      render();
    };
    renderer->set_render_cb(render_cb);

    if (camera != nullptr)
      camera->set_view_updated_cb([this]() { viewUpdated = true; });

    auto keyboard_key_cb = [this](Input::Key key, bool state) {
      switch (key) {
        case Input::Key::P:
          if (state)
            renderer->timer.toggle_animation_pause();
          break;
        case Input::Key::F1:
          if (state && settings.enable_text_overlay)
            renderer->textOverlay->visible = !renderer->textOverlay->visible;
          break;
        case Input::Key::ESCAPE:
          quit = true;
          break;
        default:
          break;
      }

      if (state)
        key_pressed(key);

      camera->keyboard_key_cb(key, state);
    };

    window->set_pointer_motion_cb(
          [this](double x, double y) {
      camera->pointer_motion_cb(x,y);
    });
    window->set_pointer_button_cb(
          [this](Input::MouseButton button, bool state) {
      camera->pointer_button_cb(button, state);
    });
    window->set_pointer_axis_cb(
          [this](Input::MouseScrollAxis axis, double value) {
      camera->pointer_axis_cb(axis, value);
    });
    window->set_keyboard_key_cb(keyboard_key_cb);

    auto quit_cb = [this]() { quit = true; };
    window->set_quit_cb(quit_cb);
  }

  ~Application()  {
    delete renderer;
  }

  int init_window_from_settings() {
    switch (settings.type) {
      case Settings::KMS:
        window = new WindowKMS(&settings);
        break;
      case Settings::XCB:
        window = new WindowXCB(&settings);
        break;
      case Settings::WAYLAND_XDG:
        window = new WindowWaylandXDG(&settings);
        break;
      case Settings::WAYLAND_SHELL:
        window = new WindowWaylandShell(&settings);
        break;
      case Settings::KHR_DISPLAY:
        window = new WindowKhrDisplay(&settings);
      default:
        return -1;
    }
    return 0;
  }

  void init_window_auto() {
    settings.type = Settings::WAYLAND_XDG;
    if (init_window_from_settings() == -1) {
      vik_log_e("failed to initialize wayland, falling back to xcb");
      delete(window);
      settings.type = Settings::XCB;
      if (init_window_from_settings() == -1) {
        vik_log_e("failed to initialize xcb, falling back to kms");
        delete(window);
        settings.type = Settings::KMS;
        init_window_from_settings();
      }
    }
  }

  void init_window() {
    if (settings.type == Settings::AUTO)
      init_window_auto();
    else if (init_window_from_settings() == -1)
      vik_log_f("failed to initialize %s", window->name.c_str());
  }

  virtual void render() = 0;
  virtual void view_changed_cb() {}
  virtual void key_pressed(Input::Key) {}
  virtual void build_command_buffers() {}
  virtual void get_enabled_features() {}
  virtual void update_text_overlay(vik::TextOverlay *overlay) {}

  void check_view_update() {
    if (viewUpdated) {
      viewUpdated = false;
      view_changed_cb();
    }
  }

  virtual void init() {
    renderer->init(name);

    std::function<void(vik::TextOverlay *overlay)>
      text_overlay_update_cb = [this](vik::TextOverlay *overlay) {
      update_text_overlay(overlay);
    };

    renderer->textOverlay->update_cb = text_overlay_update_cb;

  }

  void loop() {
    while (!quit)
      renderer->render();
    renderer->wait_idle();
  }

  void update_camera(float frame_time) {
    camera->update_movement(frame_time);
    if (camera->moving())
      viewUpdated = true;
  }

  void resize() {
    build_command_buffers();

    renderer->wait_idle();

    camera->update_aspect_ratio(renderer->get_aspect_ratio());

    view_changed_cb();
  }
};
}  // namespace vik