SimpleGL  1.1.0
A framework for platform independent rendering
backend_gl.cpp
Go to the documentation of this file.
1 
9 
10 #include <cstdlib>
11 
12 #include <GLFW/glfw3.h>
13 
14 #include <SimpleGL/logging.hpp>
15 
16 using namespace std;
17 
18 namespace sgl {
20 
21  GLVersion::GLVersion() : major{1}, minor{0}
22  {
23  /* Nothing to do here */
24  }
25 
26  GLVersion::GLVersion(int major, int minor) : major{major}, minor{minor}
27  {
28  /* Nothing to do here */
29  }
30 
31  bool GLVersion::operator<(const GLVersion& other) {
32  return (major < other.major) || ((major == other.major) && (minor < other.minor));
33  }
34 
35  bool GLVersion::operator<=(const GLVersion& other) {
36  return (*this < other) || (*this == other);
37  }
38 
39  bool GLVersion::operator==(const GLVersion& other) {
40  return (major == other.major) && (minor == other.minor);
41  }
42 
43  bool GLVersion::operator!=(const GLVersion& other) {
44  return !(*this == other);
45  }
46 
47  bool GLVersion::operator>=(const GLVersion& other) {
48  return (*this > other) || (*this == other);
49  }
50 
51  bool GLVersion::operator>(const GLVersion& other) {
52  return (major > other.major) || ((major == other.major) && (minor > other.minor));
53  }
54 
55  void initGL() {
56  /* Create dummy context for checking extensions */
57  Logger::logInfo("Creating dummy GL context...");
58  glfwDefaultWindowHints();
59  glfwWindowHint(GLFW_VISIBLE, GL_FALSE);
60  GLFWwindow* context = glfwCreateWindow(1, 1, "", NULL, NULL);
61  if (!context) {
62  Logger::logError("Failed to create a GL context!");
63  glfwTerminate();
64  exit(EXIT_FAILURE);
65  }
66  glfwMakeContextCurrent(context);
67 
68  /* Initialize GLEW */
69  Logger::logInfo("Initializing GLEW...");
70  glewExperimental = GL_TRUE;
71  if (glewInit() != GLEW_OK) {
72  Logger::logError("Could not init GLEW!");
73  exit(EXIT_FAILURE);
74  }
75 
76  Logger::logInfo("Checking OpenGL version...");
77  /* OpenGL 3.2 */
78  if (GLEW_VERSION_3_2) {
79  Logger::logInfo("OpenGL 3.2 is supported!");
80  GLVersion version(3, 2);
81  } else {
82  Logger::logError("OpenGL 3.2 is not supported, you may want to update your graphics driver.");
83  exit(EXIT_FAILURE);
84  }
85 
86  Logger::logInfo("Checking extensions...");
87  /* ARB_vertex_buffer_object */
88  if (GLEW_ARB_vertex_buffer_object) {
89  Logger::logInfo("Vertex Buffer Objects are supported!");
90  } else {
91  Logger::logError("Vertex Buffer Objects aren't supported!");
92  exit(EXIT_FAILURE);
93  }
94 
95  /* ARB_map_buffer_range */
96  if (GLEW_ARB_map_buffer_range) {
97  Logger::logInfo("Mapping buffer ranges are supported!");
98  } else {
99  Logger::logError("Mapping buffer ranges aren't supported!");
100  exit(EXIT_FAILURE);
101  }
102 
103  /* ARB_vertex_array_object */
104  if (GLEW_ARB_vertex_array_object) {
105  Logger::logInfo("Vertex Array Objects are supported!");
106  } else {
107  Logger::logError("Vertex Array Objects aren't supported!");
108  exit(EXIT_FAILURE);
109  }
110 
111  /* ARB_shader_objects */
112  if (GLEW_ARB_shader_objects) {
113  Logger::logInfo("Shader Objects are supported!");
114  } else {
115  Logger::logError("Shader Objects aren't supported!");
116  exit(EXIT_FAILURE);
117  }
118 
119  /* ARB_vertex_shader */
120  if (GLEW_ARB_vertex_shader) {
121  Logger::logInfo("Vertex Shaders are supported!");
122  } else {
123  Logger::logError("Vertex Shaders aren't supported!");
124  exit(EXIT_FAILURE);
125  }
126 
127  /* ARB_fragment_shader */
128  if (GLEW_ARB_fragment_shader) {
129  Logger::logInfo("Fragment Shaders are supported!");
130  } else {
131  Logger::logError("Fragment Shaders aren't supported!");
132  exit(EXIT_FAILURE);
133  }
134 
135  /* ARB_framebuffer_object */
136  if (GLEW_ARB_framebuffer_object) {
137  Logger::logInfo("Framebuffers are supported!");
138  } else {
139  Logger::logError("Framebuffers aren't supported!");
140  exit(EXIT_FAILURE);
141  }
142 
143  /* Destroy dummy context */
144  glfwDestroyWindow(context);
145  }
146 }
static void logInfo(std::string message)
Logs an information string to cout.
Definition: logging.cpp:20
static void logError(std::string message)
Logs an error string to cerr.
Definition: logging.cpp:24
const int major
Constant for the major version.
Definition: backend_gl.hpp:25
const int minor
Constant for the minor version.
Definition: backend_gl.hpp:28
bool operator<(const GLVersion &other)
Overloaded less than operator.
Definition: backend_gl.cpp:31
Generic namespace for the SimpleGL framework.
Definition: application.hpp:18
bool operator>=(const GLVersion &other)
Overloaded greater than or equal to operator.
Definition: backend_gl.cpp:47
bool operator!=(const GLVersion &other)
Overloaded unequal to operator.
Definition: backend_gl.cpp:43
bool operator==(const GLVersion &other)
Overloaded equal to operator.
Definition: backend_gl.cpp:39
GLVersion()
Default constructor.
Definition: backend_gl.cpp:21
bool operator>(const GLVersion &other)
Overloaded greater than operator.
Definition: backend_gl.cpp:51
bool operator<=(const GLVersion &other)
Overloaded less than or equal to operator.
Definition: backend_gl.cpp:35
SIMPLEGL_GL_EXPORT GLVersion contextVersion
Stores the OpenGL version structure.
Definition: backend_gl.cpp:19
Structure for saving the OpenGL version.
Definition: backend_gl.hpp:22
SIMPLEGL_GL_EXPORT void initGL()
Initializes the OpenGL backend, this will also check available extensions and should be called after ...
Definition: backend_gl.cpp:55