SimpleGL  1.1.0
A framework for platform independent rendering
glfw_window_gl.cpp
Go to the documentation of this file.
1 
9 
10 #include <stdexcept>
11 
12 #include <GL/glew.h>
13 
14 #include <GLFW/glfw3.h>
15 
17 
18 using namespace std;
19 
20 namespace sgl {
21 
22  WindowGL::WindowGL(int width, int height, std::string title, bool vsync) : Window() {
23  size.x = width;
24  size.y = height;
25  this->title = title;
26 
27  /* Reset to default window hints */
28  glfwDefaultWindowHints();
29 
30  /* Not resizeable */
31  glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE);
32 
33  /* Setting window hints to get an OpenGL 3.2+ core profile context */
34  glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
35  glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
36  glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
37  glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GLFW_TRUE);
38 
39  /* Creating the GLFW window */
40  handle = glfwCreateWindow(width, height, title.c_str(), NULL, NULL);
41  if (!handle) {
42  throw runtime_error("Could not create window!");
43  }
44 
45  /* Make the OpenGL context current for the calling thread */
46  glfwMakeContextCurrent(handle);
47 
48  /* Initialize GLEW */
49  glewExperimental = GL_TRUE;
50  if (glewInit() != GLEW_OK) {
51  throw runtime_error("Could not init GLEW for the current GL context!");
52  }
53 
54  /* Center window to screen */
55  centerWindow();
56 
57  /* Set vertical synchronization if requested */
58  setVSync(vsync);
59 
60  /* Set default callbacks */
61  KeyCallback* keyCallback = new KeyCallback();
62  setKeyCallback(keyCallback);
63  MouseButtonCallback* mouseButtonCallback = new MouseButtonCallback();
64  setMouseButtonCallback(mouseButtonCallback);
65  }
66 
67  void WindowGL::setVSync(bool vsync) {
68  if (vsync) {
69  glfwSwapInterval(1);
70  } else {
71  glfwSwapInterval(0);
72  }
73  }
74 
76  glfwSwapBuffers(handle);
77  }
78 }
void setKeyCallback(KeyCallback *callback)
Set the key callback.
Definition: glfw_window.cpp:89
This class provides a basic GLFW window.
Definition: glfw_window.hpp:29
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.
GLFWwindow * handle
The handle for the GLFW window.
Definition: glfw_window.hpp:33
std::string title
The title for the GLFW window.
Definition: glfw_window.hpp:39
void swapBuffers() override
Swaps the framebuffers of this window.
void setMouseButtonCallback(MouseButtonCallback *callback)
Set the mouse button callback.
Definition: glfw_window.cpp:94
void centerWindow()
Centers the window on the primary screen.
Definition: glfw_window.cpp:84
glm::ivec2 size
The size of the drawing area.
Definition: glfw_window.hpp:36
void setVSync(bool vsync)
Enables or disables vertical synchronization.