SimpleGL  1.1.0
A framework for platform independent rendering
glfw_window.cpp
Go to the documentation of this file.
1 
9 
10 #include <thread>
11 
13 #include "SimpleGL/application.hpp"
14 
15 using namespace std;
16 using namespace glm;
17 
18 namespace sgl {
19 
20  Window::Window() {
21  /* Nothing to do here */
22  }
23 
24  Window::~Window(void) {
25  glfwDestroyWindow(handle);
26  }
27 
28  GLFWwindow* Window::getHandle() {
29  return handle;
30  }
31 
32  int Window::getWidth() {
33  return size.x;
34  }
35 
36  void Window::setWidth(int width) {
37  if (width > 0) {
38  size.x = width;
39  glfwSetWindowSize(handle, width, size.y);
40  centerWindow();
41  }
42  }
43 
44  int Window::getHeight() {
45  return size.y;
46  }
47 
48  void Window::setHeight(int height) {
49  if (height > 0) {
50  size.y = height;
51  glfwSetWindowSize(handle, size.x, height);
52  centerWindow();
53  }
54  }
55 
56  glm::ivec2 Window::getSize() {
57  return size;
58  }
59 
60  void Window::setSize(glm::ivec2 size) {
61  if (size.x > 0 && size.y > 0) {
62  this->size = size;
63  glfwSetWindowSize(handle, size.x, size.y);
64  centerWindow();
65  }
66  }
67 
68  std::string Window::getTitle() {
69  return title;
70  }
71 
72  void Window::setTitle(std::string title) {
73  this->title = title;
74  glfwSetWindowTitle(handle, title.c_str());
75  }
76 
77  void Window::showInfoTitle() {
78  string fps = to_string(TimeUtil::getFPS());
79  string ups = to_string(TimeUtil::getUPS());
80  string infoTitle = title + " - FPS: " + fps + " | UPS: " + ups;
81  glfwSetWindowTitle(handle, infoTitle.c_str());
82  }
83 
84  void Window::centerWindow() {
85  const GLFWvidmode* vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor());
86  glfwSetWindowPos(handle, (vidmode->width - size.x) / 2, (vidmode->height - size.y) / 2);
87  }
88 
89  void Window::setKeyCallback(KeyCallback* callback) {
90  KeyCallback::setCurrent(callback);
91  glfwSetKeyCallback(handle, KeyCallback::dispatch);
92  }
93 
94  void Window::setMouseButtonCallback(MouseButtonCallback* callback) {
95  MouseButtonCallback::setCurrent(callback);
96  glfwSetMouseButtonCallback(handle, MouseButtonCallback::dispatch);
97  }
98 
99  void Window::update() {
100  swapBuffers();
101  GLFW::pollEvents();
102  }
103 
104  bool Window::shouldClose() {
105  return glfwWindowShouldClose(handle) == GLFW_TRUE;
106  }
107 
108  void Window::sync(int fps) {
109  /* Execute only if fps is greater than 0 */
110  if (fps > 0) {
111  /* Calculate target time in seconds */
112  double now = TimeUtil::getTime();
113  double lastLoopTime = TimeUtil::getLastLoopTime();
114  double targetTime = 1.0 / double(fps);
115 
116  /* Yield and sleep for 1 µs until the target time matches */
117  while (now - lastLoopTime < targetTime) {
118  this_thread::yield();
119  this_thread::sleep_for(chrono::nanoseconds(1000));
120 
121  now = TimeUtil::getTime();
122  }
123  }
124  }
125 
126  glm::dvec2 Window::getCursorPos() {
127  dvec2 position;
128  glfwGetCursorPos(handle, &position.x, &position.y);
129  return position;
130  }
131 
132  double Window::getCursorX() {
133  return getCursorPos().x;
134  }
135 
136  double Window::getCursorY() {
137  return getCursorPos().y;
138  }
139 }
Generic namespace for the SimpleGL framework.
Definition: application.hpp:18
This class wraps a GLFW mouse button callback.
This class wraps a GLFW key callback.