mirror of
https://github.com/glfw/glfw.git
synced 2025-04-22 16:42:51 +00:00
Improved usercontext test with threading
This commit is contained in:
parent
58aad6c136
commit
e2f9340a8c
@ -28,7 +28,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(usercontext usercontext.c ${TINYCTHREAD} ${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})
|
||||||
|
@ -1,3 +1,35 @@
|
|||||||
|
//========================================================================
|
||||||
|
// User context test
|
||||||
|
// Copyright (c) Camilla Löwy <elmindreda@glfw.org>
|
||||||
|
//
|
||||||
|
// This software is provided 'as-is', without any express or implied
|
||||||
|
// warranty. In no event will the authors be held liable for any damages
|
||||||
|
// arising from the use of this software.
|
||||||
|
//
|
||||||
|
// Permission is granted to anyone to use this software for any purpose,
|
||||||
|
// including commercial applications, and to alter it and redistribute it
|
||||||
|
// freely, subject to the following restrictions:
|
||||||
|
//
|
||||||
|
// 1. The origin of this software must not be misrepresented; you must not
|
||||||
|
// claim that you wrote the original software. If you use this software
|
||||||
|
// in a product, an acknowledgment in the product documentation would
|
||||||
|
// be appreciated but is not required.
|
||||||
|
//
|
||||||
|
// 2. Altered source versions must be plainly marked as such, and must not
|
||||||
|
// be misrepresented as being the original software.
|
||||||
|
//
|
||||||
|
// 3. This notice may not be removed or altered from any source
|
||||||
|
// distribution.
|
||||||
|
//
|
||||||
|
//========================================================================
|
||||||
|
//
|
||||||
|
// This test is intended to verify whether the OpenGL user context part of
|
||||||
|
// the GLFW API is able to be used from multiple threads
|
||||||
|
//
|
||||||
|
//========================================================================
|
||||||
|
|
||||||
|
#include "tinycthread.h"
|
||||||
|
|
||||||
#define GLAD_GL_IMPLEMENTATION
|
#define GLAD_GL_IMPLEMENTATION
|
||||||
#include <glad/gl.h>
|
#include <glad/gl.h>
|
||||||
#define GLFW_INCLUDE_NONE
|
#define GLFW_INCLUDE_NONE
|
||||||
@ -9,10 +41,44 @@ static void error_callback(int error, const char* description)
|
|||||||
fprintf(stderr, "Error: %s\n", description);
|
fprintf(stderr, "Error: %s\n", description);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int thread_main(void* data)
|
||||||
|
{
|
||||||
|
GLFWusercontext* usercontext = (GLFWusercontext*)data;
|
||||||
|
|
||||||
|
/* set the user context current */
|
||||||
|
glfwMakeUserContextCurrent(usercontext);
|
||||||
|
|
||||||
|
if (glfwGetCurrentContext() != NULL)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "Current glfw window context not NULL after glfwMakeUserContextCurrent\n");
|
||||||
|
glfwTerminate();
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
if (glfwGetCurrentUserContext() != usercontext)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "Current user context not correct after glfwMakeUserContextCurrent\n");
|
||||||
|
glfwTerminate();
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* set the user context to NULL */
|
||||||
|
glfwMakeUserContextCurrent(NULL);
|
||||||
|
if (glfwGetCurrentUserContext() != NULL)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "Current user context not NULL after glfwMakeContextCurrent\n");
|
||||||
|
glfwTerminate();
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
int main(void)
|
int main(void)
|
||||||
{
|
{
|
||||||
GLFWwindow* window;
|
GLFWwindow* window;
|
||||||
GLFWusercontext* usercontext;
|
GLFWusercontext* usercontext;
|
||||||
|
thrd_t thread_id;
|
||||||
|
int result, count;
|
||||||
|
|
||||||
glfwSetErrorCallback(error_callback);
|
glfwSetErrorCallback(error_callback);
|
||||||
|
|
||||||
@ -41,7 +107,6 @@ int main(void)
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/* set the user context current */
|
/* set the user context current */
|
||||||
glfwMakeUserContextCurrent(usercontext);
|
glfwMakeUserContextCurrent(usercontext);
|
||||||
|
|
||||||
@ -76,9 +141,19 @@ int main(void)
|
|||||||
|
|
||||||
glClearColor( 0.4f, 0.3f, 0.4f, 1.0f );
|
glClearColor( 0.4f, 0.3f, 0.4f, 1.0f );
|
||||||
|
|
||||||
|
// Launch a thread which should create and use the usercontext
|
||||||
|
if (thrd_create(&thread_id, thread_main, usercontext ) !=
|
||||||
|
thrd_success)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "Failed to create secondary thread\n");
|
||||||
|
|
||||||
/* Loop until the user closes the window */
|
glfwTerminate();
|
||||||
while (!glfwWindowShouldClose(window))
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Loop 60 times or until the user closes the window */
|
||||||
|
count = 0;
|
||||||
|
while (!glfwWindowShouldClose(window) && count++ < 60)
|
||||||
{
|
{
|
||||||
/* Render here */
|
/* Render here */
|
||||||
glClear(GL_COLOR_BUFFER_BIT);
|
glClear(GL_COLOR_BUFFER_BIT);
|
||||||
@ -90,6 +165,35 @@ int main(void)
|
|||||||
glfwPollEvents();
|
glfwPollEvents();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
thrd_join(thread_id, &result);
|
||||||
|
|
||||||
|
/* One more test now the thread has joined */
|
||||||
|
|
||||||
|
/* set the user context current */
|
||||||
|
glfwMakeUserContextCurrent(usercontext);
|
||||||
|
|
||||||
|
if (glfwGetCurrentContext() != NULL)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "Current glfw window context not NULL after glfwMakeUserContextCurrent\n");
|
||||||
|
glfwTerminate();
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
if (glfwGetCurrentUserContext() != usercontext)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "Current user context not correct after glfwMakeUserContextCurrent\n");
|
||||||
|
glfwTerminate();
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* set the user context to NULL */
|
||||||
|
glfwMakeUserContextCurrent(NULL);
|
||||||
|
if (glfwGetCurrentUserContext() != NULL)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "Current user context not NULL after glfwMakeContextCurrent\n");
|
||||||
|
glfwTerminate();
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
glfwDestroyUserContext(usercontext);
|
glfwDestroyUserContext(usercontext);
|
||||||
glfwTerminate();
|
glfwTerminate();
|
||||||
return 0;
|
return 0;
|
||||||
|
Loading…
Reference in New Issue
Block a user