GLW
Computer Graphics Structures library
Window.h
1 /*
2  Copyright (C) Skeletex Research, s.r.o. - All Rights Reserved
3  Unauthorized copying of this file, via any medium is strictly prohibited
4  Proprietary and confidential
5 */
6 #ifndef GLW_WINDOW_H
7 #define GLW_WINDOW_H
8 #include <string>
9 #include <set>
10 #include <array>
11 #include <unordered_map>
12 #include <memory>
13 
14 #include <glm/glm.hpp>
15 
16 #include <GLW/WindowEventObserver.h>
17 
18 struct GLFWwindow;
19 
20 namespace glw
21 {
22 
24  class GLW_API Window : public WindowEventObserver
25  {
26  public:
27 
28  static constexpr uint8_t max_observer_priority = 15u;
29 
32  {
34  glm::uvec2 resolution = glm::uvec2(640, 480);
36  glm::uvec2 min_resolution = glm::uvec2(0);
38  glm::uvec2 max_resolution = glm::uvec2(0);
40  bool maximized = false;
42  std::string caption = "GLW Window";
44  int8_t aa_samples = 8;
46  bool visible = true;
48  bool try_share_contexts = true;
50  bool resizable = true;
52  glm::ivec2 position = glm::ivec2(100);
54  bool fullscreen = false;
56  bool hidden_cursor = false;
57  };
58 
59  explicit Window();
60  explicit Window(const InitParameters &params);
61  ~Window();
66  void ObserverRegister(WindowEventObserver *object, const uint8_t priority = glw::Window::max_observer_priority);
68  void ObserverRemove(WindowEventObserver *object);
69 
70  [[nodiscard]] bool IsVisible() const;
71  [[nodiscard]] bool IsMaximized() const;
72  [[nodiscard]] bool IsIconified() const;
73  [[nodiscard]] bool IsFullscreen();
74 
75  [[nodiscard]] bool IsInitialized();
76  [[nodiscard]] bool ShouldClose();
77  void SetShouldClose();
78 
79  [[nodiscard]] bool IsContextShared();
80 
82  [[nodiscard]] GLFWwindow *GetWindowPtr();
83 
85  [[nodiscard]] glm::vec2 GetMousePosition() const;
87  [[nodiscard]] glm::ivec2 GetPosition() const;
89  [[nodiscard]] glm::uvec2 GetResolution() const;
91  [[nodiscard]] uint32_t GetWidth() const;
93  [[nodiscard]] uint32_t GetHeight() const;
106  [[nodiscard]] glm::vec2 GetContentScaling() const;
107 
109  [[nodiscard]] glm::uvec2 GetUnscaledResolution() const;
110 
111  void SetWindowedMode(int width, int height, int posx = 0, int posy = 0);
112  void SetFullscreenMode();
113 
114  void PollEvents();
115  void SwapBuffers();
116 
117  void SetContextConditional();
118  void UnsetContextConditional();
119 
120  protected:
125  bool Initialize(const InitParameters &init);
126 
127  private:
128  std::array < std::set<WindowEventObserver *>, glw::Window::max_observer_priority + 1 > obs_priority_lists_;
129  std::unordered_map<WindowEventObserver *, std::set<WindowEventObserver *>*> obs_iterator_for_ptr_;
130 
131  GLFWwindow *window_ = nullptr;
132  GLFWwindow *shared_window_ = nullptr;
133 
134  glm::uvec2 resolution_;
135  glm::ivec2 position_;
136  glm::vec2 mouse_position_;
137 
138  bool is_initialized_ = false;
139 
140  bool is_fullscreen_ = false;
141 
142  void InitializeMembers(const InitParameters &init);
143  void RegisterCallbacks();
144 
145  static void CursorPosCallback(GLFWwindow *window, double xpos, double ypos);
146  static void MouseButtonCallback(GLFWwindow *window, int button, int action, int mods);
147  static void ScrollCallback(GLFWwindow *window, double xoffset, double yoffset);
148  static void CharCallback(GLFWwindow *window, unsigned int codepoint);
149  static void KeyCallback(GLFWwindow *window, int key, int scancode, int action, int mods);
150  static void TouchCallback(GLFWwindow *window, unsigned int num_of_contacts, unsigned int *id, double *xpos, double *ypos, double *cwidth, double *cheight);
151  static void TouchGestBeginCallback(GLFWwindow *window);
152  static void TouchGestEndCallback(GLFWwindow *window);
153  static void TouchGestZoomCallback(GLFWwindow *window, double distance, double xpos, double ypos);
154  static void TouchGestPanCallback(GLFWwindow *window, double distance, double xpos, double ypos);
155  static void FramebufferResizeCallback(GLFWwindow *window, int xpos, int ypos);
156  static void WindowMoveCallback(GLFWwindow *window, int xpos, int ypos);
157  static void WindowRefreshCallback(GLFWwindow *window);
158  static void WindowResizeCallback(GLFWwindow *window, int xpos, int ypos);
159  static void WindowFocusCallback(GLFWwindow *window, int is_focused);
160  static void WindowCloseCallback(GLFWwindow *window);
161  static void WindowIconifyCallback(GLFWwindow *window, int is_iconified);
162  static void FilesDropCallback(GLFWwindow *window, int32_t file_count, const char **files);
163  static void WindowContentScaleCallback(GLFWwindow *window, float xscale, float yscale);
164  };
165 
166  class GLW_API HiddenWindow : public WindowEventObserver
167  {
168  public:
169  HiddenWindow();
170  void SwapBuffers();
171  protected:
172  std::unique_ptr<Window> window_;
173  };
174 
175 }
176 #endif /* !GLW_WINDOW_H */
OpenGL wrapper library namespace. Provides simplified interface for using OpenGL features.
Definition: API.h:17
Any class derived from WindowEventObserver can receive Window events.
Definition: WindowEventObserver.h:14
Parameters used to initialize Window.
Definition: Window.h:31
Implements GLFW functions in object style programming.
Definition: Window.h:24