-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGLFWController.c++
105 lines (91 loc) · 2.59 KB
/
GLFWController.c++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// GLFWController.c++
#include "GLFWController.h"
#include "ModelView.h"
GLFWController::GLFWController() : theWindow(nullptr), model(nullptr)
{
glfwInit();
// Before any OpenGL graphics calls can be made, we must create an
// OpenGL Rendering Context (RC). There is one RC per window.
createWindowAndRC("Hello, OpenGL");
}
GLFWController::~GLFWController()
{
if (theWindow != nullptr)
glfwDestroyWindow(theWindow);
}
void GLFWController::addModel(ModelView* m)
{
// for now, we just assume we only have one model:
model = m;
}
bool GLFWController::checkForErrors(std::ostream& os, const std::string& context)
// CLASS METHOD
{
bool hadError = false;
GLenum e = glGetError();
while (e != GL_NO_ERROR)
{
os << "CheckForErrors (context: " << context
<< "): " << ((int)e) << std::endl;
e = glGetError();
hadError = true;
}
return hadError;
}
void GLFWController::createWindowAndRC(const std::string& windowTitle)
{
// The following calls enforce use of only non-deprecated functionality.
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
int minor = 8; // Start AT LEAST one greater than where you really want to start
while ((theWindow == nullptr) && (minor > 0))
{
minor--;
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, minor);
theWindow = glfwCreateWindow(400, 400, windowTitle.c_str(), nullptr, nullptr);
}
if (theWindow == nullptr)
{
std::cerr << "\n**** COULD NOT CREATE A 4.x RENDERING CONTEXT ****\n\n";
return;
}
glfwMakeContextCurrent(theWindow);
}
void GLFWController::handleDisplay()
{
// clear the frame buffer
glClear(GL_COLOR_BUFFER_BIT);
if (model != nullptr)
model->render();
glfwSwapBuffers(theWindow);
checkForErrors(std::cout, "GLFWController::handleDisplay");
}
void GLFWController::reportVersions(std::ostream& os) const
{
const char* glVer = reinterpret_cast<const char*>(glGetString(GL_VERSION));
const char* glslVer = reinterpret_cast<const char*>
(glGetString(GL_SHADING_LANGUAGE_VERSION));
// glGetString can return nullptr if no rendering context has been created
os << "VERSIONS: GL: ";
if (glVer == nullptr)
os << "nullptr (has RC been created?)\n";
else
os << glVer << '\n';
os << " GLSL: ";
if (glslVer == nullptr)
os << "nullptr (has RC been created?)\n";
else
os << glslVer << '\n';
os << " GLFW: " << glfwGetVersionString() << '\n';
}
void GLFWController::run()
{
while (!glfwWindowShouldClose(theWindow))
{
glfwWaitEvents();
handleDisplay();
}
glfwDestroyWindow(theWindow);
theWindow = nullptr;
}