1#ifdef HEADLESS_HAS_GLFW
2# include <Headless/OpenGLContext/GlfwOpenGLContext.hpp>
4# include <GLFW/glfw3.h>
6# include <glbinding-aux/ValidVersions.h>
7# include <glbinding/glbinding.h>
8# include <globjects/globjects.h>
15using namespace glbinding;
17static void error(
int errnum,
const char* errmsg ) {
18 std::cerr <<
"GlfwOpenGLContext::GLFW error -- "
22GlfwOpenGLContext::GlfwOpenGLContext(
const glbinding::Version& glVersion,
24 OpenGLContext( glVersion, size ) {
27 glfwSetErrorCallback( error );
28 glfwDefaultWindowHints();
29 glfwWindowHint( GLFW_VISIBLE,
false );
30 glfwWindowHint( GLFW_CONTEXT_VERSION_MAJOR, glVersion.majorVersion() );
31 glfwWindowHint( GLFW_CONTEXT_VERSION_MINOR, glVersion.minorVersion() );
32 glfwWindowHint( GLFW_OPENGL_FORWARD_COMPAT,
true );
33 glfwWindowHint( GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE );
35 glfwCreateWindow( size[0], size[1],
"Radium CommandLine Context",
nullptr,
nullptr );
37 const char* description;
38 int code = glfwGetError( &description );
39 if ( code == GLFW_VERSION_UNAVAILABLE ) {
42 glfwWindowHint( GLFW_CONTEXT_VERSION_MAJOR, 4 );
43 glfwWindowHint( GLFW_CONTEXT_VERSION_MINOR, 1 );
45 glfwCreateWindow( size[0], size[1],
"Radium CommandLine Context",
nullptr,
nullptr );
46 code = glfwGetError( &description );
48 if ( code != GLFW_NO_ERROR ) {
50 error( code, description );
57 glfwMakeContextCurrent( m_glfwContext );
58 globjects::init( [](
const char* name ) {
return glfwGetProcAddress( name ); } );
59 glfwSetWindowUserPointer( m_glfwContext,
this );
60 auto resizeCB = []( GLFWwindow* window,
int width,
int height ) {
61 auto context =
static_cast<GlfwOpenGLContext*
>( glfwGetWindowUserPointer( window ) );
62 context->resizeFrameBuffer( width, height );
64 glfwSetFramebufferSizeCallback( m_glfwContext, resizeCB );
65 auto keyCB = []( GLFWwindow* window,
int key,
int scancode,
int action,
int mods ) {
66 auto context =
static_cast<GlfwOpenGLContext*
>( glfwGetWindowUserPointer( window ) );
67 context->keyboardEventCallback( key, scancode, action, mods );
69 glfwSetKeyCallback( m_glfwContext, keyCB );
71 auto mouseCB = []( GLFWwindow* window,
int button,
int action,
int mods ) {
72 auto context =
static_cast<GlfwOpenGLContext*
>( glfwGetWindowUserPointer( window ) );
75 glfwGetWindowContentScale( window, &xscale, &yscale );
78 glfwGetCursorPos( window, &xpos, &ypos );
79 context->mouseEventCallback( button, action, mods,
int( xpos ),
int( ypos ) );
81 glfwSetMouseButtonCallback( m_glfwContext, mouseCB );
83 auto scrollCB = []( GLFWwindow* window,
double xoffset,
double yoffset ) {
84 auto context =
static_cast<GlfwOpenGLContext*
>( glfwGetWindowUserPointer( window ) );
86 glfwGetWindowContentScale( window, &xscale, &yscale );
87 context->scrollEventCallback(
int( xoffset ),
int( yoffset ) );
89 glfwSetScrollCallback( m_glfwContext, scrollCB );
91 auto mouseMoveCB = []( GLFWwindow* window,
double xpos,
double ypos ) {
92 auto context =
static_cast<GlfwOpenGLContext*
>( glfwGetWindowUserPointer( window ) );
93 context->mouseMoveEventCallback(
int( xpos ),
int( ypos ) );
95 glfwSetCursorPosCallback( m_glfwContext, mouseMoveCB );
98GlfwOpenGLContext::~GlfwOpenGLContext() {
101void GlfwOpenGLContext::makeCurrent()
const {
102 if ( m_glfwContext ) { glfwMakeContextCurrent( m_glfwContext ); }
105void GlfwOpenGLContext::doneCurrent()
const {
106 if ( m_glfwContext ) { glfwMakeContextCurrent(
nullptr ); }
109bool GlfwOpenGLContext::isValid()
const {
110 return m_glfwContext !=
nullptr;
114 auto generalInfo = OpenGLContext::getInfo();
116 infoText << generalInfo <<
"Context provider : GLFW\n";
117 return infoText.
str();
120void GlfwOpenGLContext::show( EventMode mode,
float delay ) {
123 glfwShowWindow( m_glfwContext );
126void GlfwOpenGLContext::hide() {
127 glfwHideWindow( m_glfwContext );
131 glfwSetWindowSize( m_glfwContext, size[0], size[1] );
134bool GlfwOpenGLContext::processEvents() {
136 case EventMode::POLL:
139 case EventMode::WAIT:
142 case EventMode::TIMEOUT:
143 glfwWaitEventsTimeout( m_delay );
152void GlfwOpenGLContext::renderLoop(
std::function<
void(
float )> render ) {
153 double prevFrameDate = glfwGetTime();
157 glfwGetFramebufferSize( m_glfwContext, &width, &height );
158 glViewport( 0, 0, width, height );
160 while ( !glfwWindowShouldClose( m_glfwContext ) ) {
161 curFrameDate = glfwGetTime();
162 render( curFrameDate - prevFrameDate );
163 prevFrameDate = curFrameDate;
165 glfwSwapBuffers( m_glfwContext );
hepler function to manage enum as underlying types in VariableSet