Window.cpp
Go to the documentation of this file.
1 #include "quori_face/Window.hpp"
3 
4 #include <GL/gl3w.h>
5 #include <GLFW/glfw3.h>
6 #include <stdexcept>
7 #include <atomic>
8 
9 #include "trace.hpp"
10 
11 using namespace quori_face;
12 
13 
14 
16 {
17  throw std::runtime_error("GLFW Error");
18 }
19 
20 Window::Window(const std::uint32_t width, const std::uint32_t height, const std::string &name)
21 {
22  window_ = QUORI_FACE_TRACE(glfwCreateWindow(width, height, name.c_str(), nullptr, nullptr));
23  if (!window_) throwGlfwError();
24 
25  glfwSetInputMode(window_, GLFW_CURSOR, GLFW_CURSOR_HIDDEN);
26 
27  execute([] {
29  });
30 }
31 
32 Window::Window(const std::uint32_t width, const std::uint32_t height, const std::string &name, const Monitor::ConstPtr &monitor)
33 {
34  window_ = QUORI_FACE_TRACE(glfwCreateWindow(width, height, name.c_str(), monitor->getHandle(), nullptr));
35  if (!window_) throwGlfwError();
36 
37  glfwSetInputMode(window_, GLFW_CURSOR, GLFW_CURSOR_HIDDEN);
38 
39 
40  execute([] {
42  });
43 }
44 
46 {
47  if (!window_) return;
48  QUORI_FACE_TRACE(glfwDestroyWindow(window_));
49 }
50 
52 {
53  WindowManager::ref().detach(shared_from_this());
54  close_impl();
55 }
56 
57 bool Window::isClosed() const
58 {
59  return window_ == nullptr;
60 }
61 
63 {
64  QUORI_FACE_TRACE(glfwPollEvents());
65 }
66 
67 std::uint32_t Window::getWidth() const
68 {
69  if (!window_) throw std::runtime_error("Window is closed");
70 
71  int width = 0;
72  QUORI_FACE_TRACE(glfwGetWindowSize(window_, &width, nullptr));
73  return width;
74 }
75 
76 std::uint32_t Window::getHeight() const
77 {
78  if (!window_) throw std::runtime_error("Window is closed");
79 
80  int height = 0;
81  QUORI_FACE_TRACE(glfwGetWindowSize(window_, nullptr, &height));
82  return height;
83 }
84 
85 GLFWwindow *Window::getHandle()
86 {
87  return window_;
88 }
89 
90 const GLFWwindow *Window::getHandle() const
91 {
92  return window_;
93 }
94 
96 {
97  QUORI_FACE_TRACE(glfwMakeContextCurrent(window_));
98 }
99 
101 {
102  QUORI_FACE_TRACE(glfwMakeContextCurrent(nullptr));
103 }
104 
106 {
107  QUORI_FACE_TRACE(glfwSwapBuffers(window_));
108 }
109 
110 void Window::execute(const std::function<void ()> &f)
111 {
112  if (!window_) throw std::runtime_error("Window is closed");
113 
114  bind();
115  f();
116  unbind();
117 }
118 
119 void Window::draw(const std::function<void ()> &f)
120 {
121  execute([this, &f]() {
122  f();
123  swapBuffers();
124  });
125 }
126 
128 {
129  if (!window_) return;
130  QUORI_FACE_TRACE(glfwDestroyWindow(window_));
131  window_ = nullptr;
132 }
quori_face::WindowManager::ref
static WindowManager & ref()
Definition: WindowManager.cpp:95
throwGlfwError
void throwGlfwError()
Definition: Window.cpp:15
width
GLint GLsizei width
Definition: glcorearb.h:251
quori_face::WindowManager::detach
void detach(const Window::Ptr &window)
Definition: WindowManager.cpp:49
quori_face::Window::draw
void draw(const std::function< void()> &f)
Definition: Window.cpp:119
quori_face::Window::window_
GLFWwindow * window_
Definition: Window.hpp:86
string
GLsizei const GLchar *const * string
Definition: glcorearb.h:790
quori_face::Window::getHeight
std::uint32_t getHeight() const
Definition: Window.cpp:76
quori_face::Window::close_impl
void close_impl()
Definition: Window.cpp:127
Window.hpp
trace.hpp
quori_face::Window::isClosed
bool isClosed() const
Definition: Window.cpp:57
quori_face::Window::unbind
void unbind()
Definition: Window.cpp:100
height
GLint GLsizei GLsizei height
Definition: glcorearb.h:251
quori_face
Definition: Cache.hpp:9
gl3w.h
quori_face::Window::execute
void execute(const std::function< void()> &f)
Definition: Window.cpp:110
name
GLuint const GLchar * name
Definition: glcorearb.h:762
quori_face::Window::getWidth
std::uint32_t getWidth() const
Definition: Window.cpp:67
WindowManager.hpp
quori_face::Window::swapBuffers
void swapBuffers()
Definition: Window.cpp:105
QUORI_FACE_TRACE
#define QUORI_FACE_TRACE(stmt)
Definition: quori_face/trace.hpp:16
quori_face::Window::bind
void bind()
Definition: Window.cpp:95
quori_face::Window::pollEvents
static void pollEvents()
Definition: Window.cpp:62
quori_face::Window::~Window
~Window()
Definition: Window.cpp:45
gl3wInit
int gl3wInit(void)
Definition: gl3w.c:145
f
GLdouble f
Definition: glcorearb.h:291
quori_face::Window::close
void close()
Definition: Window.cpp:51
quori_face::Monitor::ConstPtr
std::shared_ptr< const Monitor > ConstPtr
Definition: Monitor.hpp:20
quori_face::Window::Window
Window(const std::uint32_t width, const std::uint32_t height, const std::string &name)
Definition: Window.cpp:20
quori_face::Window::getHandle
GLFWwindow * getHandle()
Definition: Window.cpp:85


quori_face
Author(s):
autogenerated on Wed Mar 2 2022 00:53:20