-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGLFWScr.cpp
92 lines (69 loc) · 2.18 KB
/
GLFWScr.cpp
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
#include <iostream>
#include <thread>
#include <cstdlib>
#include "GL_includes.h"
#include "screen/GLFWScr.h"
namespace engine {
GLFWScr::GLFWScr(int width, int height) throw(const char *)
{
glewExperimental = true;
if(!glfwInit()) { throw("Failed to Initialize GLFW!"); }
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
glfwWindowHint(GLFW_DEPTH_BITS, 32);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
_win = glfwCreateWindow(width, height, "", NULL, NULL);
if(!_win) { glfwTerminate(); throw("Failed to Open GLFW Window!"); }
glfwMakeContextCurrent( _win);
glfwSetWindowUserPointer(_win, this);
if(glewInit() != GLEW_OK) { throw( "Failed to Initialize GLEW!"); }
// To clear error produced by glewExperimental bug.
while(GL_NO_ERROR != glGetError());
glfwSetInputMode(_win, GLFW_STICKY_KEYS, GL_TRUE);
glEnable( GL_DEPTH_TEST);
glDepthMask( GL_TRUE);
glDepthFunc( GL_LEQUAL);
}
GLFWScr::~GLFWScr()
{
glfwTerminate();
}
void
GLFWScr::set_title(const char *title) const
{
glfwSetWindowTitle(_win, title);
}
void
GLFWScr::display_link(RenderEngine<GLFWScr> *engine) const
{
double last = glfwGetTime(),
acc,
now,
delta;
static const double TICK_STEP = 1.0/60;
do {
engine->thrd_req();
engine->render( );
swap();
now = glfwGetTime();
delta = now - last;
while(delta >= 0) {
engine->tick( std::min(delta, TICK_STEP) );
delta -= TICK_STEP;
}
last = glfwGetTime();
engine->thrd_rel();
}
while( glfwGetKey(_win, GLFW_KEY_ESCAPE) != GLFW_PRESS &&
!glfwWindowShouldClose(_win)
);
}
void
GLFWScr::swap() const
{
glfwSwapBuffers(_win);
glfwPollEvents();
}
}; // namespace engine