Commit 7fcb11f4 authored by Doug Binks's avatar Doug Binks Committed by Degrande Samuel
Browse files

User Contexts API with Win32 implementation.

parent 0a49ef0a
...@@ -1156,6 +1156,18 @@ typedef struct GLFWmonitor GLFWmonitor; ...@@ -1156,6 +1156,18 @@ typedef struct GLFWmonitor GLFWmonitor;
*/ */
typedef struct GLFWwindow GLFWwindow; typedef struct GLFWwindow GLFWwindow;
/*! @brief Opaque user OpenGL context object.
*
* Opaque user OpenGL context object.
*
* @see @ref user_gl_context
*
* @since Added in version 3.3.
*
* @ingroup window
*/
typedef struct GLFWusercontext GLFWusercontext;
/*! @brief Opaque cursor object. /*! @brief Opaque cursor object.
* *
* Opaque cursor object. * Opaque cursor object.
...@@ -5620,6 +5632,21 @@ GLFWAPI int glfwExtensionSupported(const char* extension); ...@@ -5620,6 +5632,21 @@ GLFWAPI int glfwExtensionSupported(const char* extension);
*/ */
GLFWAPI GLFWglproc glfwGetProcAddress(const char* procname); GLFWAPI GLFWglproc glfwGetProcAddress(const char* procname);
/*! @brief Create a new OpenGL user context for a window
*
*/
GLFWAPI GLFWusercontext* glfwCreateUserContext(GLFWwindow* window);
/*! @brief Delete an OpenGL user context
*
*/
GLFWAPI void glfwDestroyUserContext(GLFWusercontext* context);
/*! @brief Make an OpenGL user context
*
*/
GLFWAPI void glfwMakeUserContextCurrent(GLFWusercontext* context);
/*! @brief Returns whether the Vulkan loader and an ICD have been found. /*! @brief Returns whether the Vulkan loader and an ICD have been found.
* *
* This function returns whether the Vulkan loader and any minimally functional * This function returns whether the Vulkan loader and any minimally functional
......
...@@ -758,3 +758,26 @@ GLFWAPI GLFWglproc glfwGetProcAddress(const char* procname) ...@@ -758,3 +758,26 @@ GLFWAPI GLFWglproc glfwGetProcAddress(const char* procname)
return window->context.getProcAddress(procname); return window->context.getProcAddress(procname);
} }
GLFWAPI GLFWusercontext* glfwCreateUserContext(GLFWwindow* handle)
{
_GLFWusercontext* context;
_GLFWwindow* window = (_GLFWwindow*)handle;
context = _glfwPlatformCreateUserContext(window);
return (GLFWusercontext*)context;
}
GLFWAPI void glfwDestroyUserContext(GLFWusercontext* handle)
{
_GLFWusercontext* context = (_GLFWusercontext*)handle;
_glfwPlatformDestroyUserContext(context);
}
GLFWAPI void glfwMakeUserContextCurrent(GLFWusercontext* handle)
{
_GLFWusercontext* context = (_GLFWusercontext*)handle;
_glfwPlatformMakeUserContextCurrent(context);
}
...@@ -75,6 +75,7 @@ typedef struct _GLFWmapping _GLFWmapping; ...@@ -75,6 +75,7 @@ typedef struct _GLFWmapping _GLFWmapping;
typedef struct _GLFWjoystick _GLFWjoystick; typedef struct _GLFWjoystick _GLFWjoystick;
typedef struct _GLFWtls _GLFWtls; typedef struct _GLFWtls _GLFWtls;
typedef struct _GLFWmutex _GLFWmutex; typedef struct _GLFWmutex _GLFWmutex;
typedef struct _GLFWusercontext _GLFWusercontext;
typedef void (* _GLFWmakecontextcurrentfun)(_GLFWwindow*); typedef void (* _GLFWmakecontextcurrentfun)(_GLFWwindow*);
typedef void (* _GLFWswapbuffersfun)(_GLFWwindow*); typedef void (* _GLFWswapbuffersfun)(_GLFWwindow*);
...@@ -678,6 +679,11 @@ void _glfwPlatformWaitEvents(void); ...@@ -678,6 +679,11 @@ void _glfwPlatformWaitEvents(void);
void _glfwPlatformWaitEventsTimeout(double timeout); void _glfwPlatformWaitEventsTimeout(double timeout);
void _glfwPlatformPostEmptyEvent(void); void _glfwPlatformPostEmptyEvent(void);
_GLFWusercontext* _glfwPlatformCreateUserContext(_GLFWwindow* window);
void _glfwPlatformDestroyUserContext(_GLFWusercontext* context);
void _glfwPlatformMakeUserContextCurrent(_GLFWusercontext* context);
void _glfwPlatformGetRequiredInstanceExtensions(char** extensions); void _glfwPlatformGetRequiredInstanceExtensions(char** extensions);
int _glfwPlatformGetPhysicalDevicePresentationSupport(VkInstance instance, int _glfwPlatformGetPhysicalDevicePresentationSupport(VkInstance instance,
VkPhysicalDevice device, VkPhysicalDevice device,
......
...@@ -776,6 +776,62 @@ GLFWbool _glfwCreateContextWGL(_GLFWwindow* window, ...@@ -776,6 +776,62 @@ GLFWbool _glfwCreateContextWGL(_GLFWwindow* window,
#undef setAttrib #undef setAttrib
//////////////////////////////////////////////////////////////////////////
////// GLFW platform API //////
//////////////////////////////////////////////////////////////////////////
_GLFWusercontext* _glfwPlatformCreateUserContext(_GLFWwindow* window)
{
_GLFWusercontext* context;
context = calloc(1, sizeof(_GLFWusercontext));
context->handle = wglCreateContext(window->context.wgl.dc);
context->window = window;
if (!context->handle)
{
_glfwInputErrorWin32(GLFW_VERSION_UNAVAILABLE,
"WGL: Failed to create user OpenGL context");
free(context);
return GLFW_FALSE;
}
if (!wglShareLists(window->context.wgl.handle,context->handle))
{
_glfwInputErrorWin32(GLFW_PLATFORM_ERROR,
"WGL: Failed to enable sharing with window OpenGL context and user context");
free(context);
return GLFW_FALSE;
}
return context;
}
void _glfwPlatformDestroyUserContext(_GLFWusercontext* context)
{
wglDeleteContext(context->handle);
free(context);
}
void _glfwPlatformMakeUserContextCurrent(_GLFWusercontext* context)
{
if(context)
{
if(!wglMakeCurrent(context->window->context.wgl.dc,context->handle))
{
_glfwInputErrorWin32(GLFW_PLATFORM_ERROR,
"WGL: Failed to set current user context");
}
}
else
{
if (!wglMakeCurrent(NULL, NULL))
{
_glfwInputErrorWin32(GLFW_PLATFORM_ERROR,
"WGL: Failed to clear current context");
}
}
}
////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////
////// GLFW native API ////// ////// GLFW native API //////
////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////
......
...@@ -155,6 +155,11 @@ typedef struct _GLFWlibraryWGL ...@@ -155,6 +155,11 @@ typedef struct _GLFWlibraryWGL
} _GLFWlibraryWGL; } _GLFWlibraryWGL;
typedef struct _GLFWusercontext
{
_GLFWwindow* window;
HGLRC handle;
} _GLFWusercontext;
GLFWbool _glfwInitWGL(void); GLFWbool _glfwInitWGL(void);
void _glfwTerminateWGL(void); void _glfwTerminateWGL(void);
......
...@@ -39,6 +39,7 @@ add_executable(iconify iconify.c ${GETOPT} ${GLAD_GL}) ...@@ -39,6 +39,7 @@ add_executable(iconify iconify.c ${GETOPT} ${GLAD_GL})
add_executable(monitors monitors.c ${GETOPT} ${GLAD_GL}) add_executable(monitors monitors.c ${GETOPT} ${GLAD_GL})
add_executable(reopen reopen.c ${GLAD_GL}) add_executable(reopen reopen.c ${GLAD_GL})
add_executable(cursor cursor.c ${GLAD_GL}) add_executable(cursor cursor.c ${GLAD_GL})
add_executable(usercontext usercontext.c ${GLAD_GL})
add_executable(empty WIN32 MACOSX_BUNDLE empty.c ${TINYCTHREAD} ${GLAD_GL}) add_executable(empty WIN32 MACOSX_BUNDLE empty.c ${TINYCTHREAD} ${GLAD_GL})
add_executable(gamma WIN32 MACOSX_BUNDLE gamma.c ${GLAD_GL}) add_executable(gamma WIN32 MACOSX_BUNDLE gamma.c ${GLAD_GL})
...@@ -63,7 +64,7 @@ endif() ...@@ -63,7 +64,7 @@ endif()
set(GUI_ONLY_BINARIES empty gamma icon inputlag joysticks opacity tearing set(GUI_ONLY_BINARIES empty gamma icon inputlag joysticks opacity tearing
threads timeout title triangle-vulkan windows) threads timeout title triangle-vulkan windows)
set(CONSOLE_BINARIES clipboard events msaa glfwinfo iconify monitors reopen set(CONSOLE_BINARIES clipboard events msaa glfwinfo iconify monitors reopen
cursor) cursor usercontext)
set_target_properties(${GUI_ONLY_BINARIES} ${CONSOLE_BINARIES} PROPERTIES set_target_properties(${GUI_ONLY_BINARIES} ${CONSOLE_BINARIES} PROPERTIES
FOLDER "GLFW3/Tests") FOLDER "GLFW3/Tests")
......
#include <glad/gl.h>
#include <GLFW/glfw3.h>
int main(void)
{
GLFWwindow* window;
GLFWusercontext* usercontext;
/* Initialize the library */
if (!glfwInit())
return -1;
/* Create a windowed mode window and its OpenGL context */
window = glfwCreateWindow(640, 480, "User Context", NULL, NULL);
if (!window)
{
glfwTerminate();
return -1;
}
/* Make the window's context current */
glfwMakeContextCurrent(window);
gladLoadGL(glfwGetProcAddress);
/* make a new context */
usercontext = glfwCreateUserContext(window);
if (!usercontext)
{
glfwTerminate();
return -1;
}
/* set the user context current */
glfwMakeContextCurrent(NULL);
glfwMakeUserContextCurrent(usercontext);
/* set the window context current */
glfwMakeUserContextCurrent(NULL);
glfwMakeContextCurrent(window);
glClearColor( 0.4f, 0.3f, 0.4f, 0.0f );
/* Loop until the user closes the window */
while (!glfwWindowShouldClose(window))
{
/* Render here */
glClear(GL_COLOR_BUFFER_BIT);
/* Swap front and back buffers */
glfwSwapBuffers(window);
/* Poll for and process events */
glfwPollEvents();
}
glfwDestroyUserContext(usercontext);
glfwTerminate();
return 0;
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment