Add glfwGetError

Related to #970.

If you have opinions on the design or implementation of this function,
please come join us in #970 before it is frozen for release.
This commit is contained in:
Camilla Löwy
2017-05-01 19:20:57 +02:00
parent 16ddfafeaa
commit 6350641f0a
8 changed files with 122 additions and 19 deletions
+35 -3
View File
@@ -43,6 +43,7 @@ _GLFWlibrary _glfw = { GLFW_FALSE };
// These are outside of _glfw so they can be used before initialization and
// after termination
//
static int _glfwErrorCode;
static GLFWerrorfun _glfwErrorCallback;
static _GLFWinitconfig _glfwInitHints =
{
@@ -113,7 +114,11 @@ static void terminate(void)
_glfwTerminateVulkan();
_glfwPlatformTerminate();
if (_glfwPlatformIsValidTls(&_glfw.error))
_glfwErrorCode = (int) (intptr_t) _glfwPlatformGetTls(&_glfw.error);
_glfwPlatformDestroyTls(&_glfw.context);
_glfwPlatformDestroyTls(&_glfw.error);
memset(&_glfw, 0, sizeof(_glfw));
}
@@ -125,6 +130,11 @@ static void terminate(void)
void _glfwInputError(int error, const char* format, ...)
{
if (_glfw.initialized)
_glfwPlatformSetTls(&_glfw.error, (void*) (intptr_t) error);
else
_glfwErrorCode = error;
if (_glfwErrorCallback)
{
char buffer[8192];
@@ -164,15 +174,19 @@ GLFWAPI int glfwInit(void)
memset(&_glfw, 0, sizeof(_glfw));
_glfw.hints.init = _glfwInitHints;
if (!_glfwPlatformCreateTls(&_glfw.context))
return GLFW_FALSE;
if (!_glfwPlatformInit())
{
terminate();
return GLFW_FALSE;
}
if (!_glfwPlatformCreateTls(&_glfw.error))
return GLFW_FALSE;
if (!_glfwPlatformCreateTls(&_glfw.context))
return GLFW_FALSE;
_glfwPlatformSetTls(&_glfw.error, (void*) (intptr_t) _glfwErrorCode);
_glfw.initialized = GLFW_TRUE;
_glfw.timer.offset = _glfwPlatformGetTimerValue();
@@ -223,6 +237,24 @@ GLFWAPI const char* glfwGetVersionString(void)
return _glfwPlatformGetVersionString();
}
GLFWAPI int glfwGetError(void)
{
int error;
if (_glfw.initialized)
{
error = (int) (intptr_t) _glfwPlatformGetTls(&_glfw.error);
_glfwPlatformSetTls(&_glfw.error, (intptr_t) GLFW_NO_ERROR);
}
else
{
error = _glfwErrorCode;
_glfwErrorCode = GLFW_NO_ERROR;
}
return error;
}
GLFWAPI GLFWerrorfun glfwSetErrorCallback(GLFWerrorfun cbfun)
{
_GLFW_SWAP_POINTERS(_glfwErrorCallback, cbfun);
+2
View File
@@ -517,6 +517,7 @@ struct _GLFWlibrary
_GLFWjoystick joysticks[GLFW_JOYSTICK_LAST + 1];
_GLFWtls error;
_GLFWtls context;
struct {
@@ -653,6 +654,7 @@ GLFWbool _glfwPlatformCreateTls(_GLFWtls* tls);
void _glfwPlatformDestroyTls(_GLFWtls* tls);
void* _glfwPlatformGetTls(_GLFWtls* tls);
void _glfwPlatformSetTls(_GLFWtls* tls, void* value);
GLFWbool _glfwPlatformIsValidTls(_GLFWtls* tls);
/*! @} */
+5 -1
View File
@@ -52,7 +52,6 @@ GLFWbool _glfwPlatformCreateTls(_GLFWtls* tls)
void _glfwPlatformDestroyTls(_GLFWtls* tls)
{
assert(tls->posix.allocated == GLFW_TRUE);
if (tls->posix.allocated)
pthread_key_delete(tls->posix.key);
memset(tls, 0, sizeof(_GLFWtls));
@@ -70,3 +69,8 @@ void _glfwPlatformSetTls(_GLFWtls* tls, void* value)
pthread_setspecific(tls->posix.key, value);
}
GLFWbool _glfwPlatformIsValidTls(_GLFWtls* tls)
{
return tls->posix.allocated;
}
+5 -1
View File
@@ -52,7 +52,6 @@ GLFWbool _glfwPlatformCreateTls(_GLFWtls* tls)
void _glfwPlatformDestroyTls(_GLFWtls* tls)
{
assert(tls->win32.allocated == GLFW_TRUE);
if (tls->win32.allocated)
TlsFree(tls->win32.index);
memset(tls, 0, sizeof(_GLFWtls));
@@ -70,3 +69,8 @@ void _glfwPlatformSetTls(_GLFWtls* tls, void* value)
TlsSetValue(tls->win32.index, value);
}
GLFWbool _glfwPlatformIsValidTls(_GLFWtls* tls)
{
return tls->win32.allocated;
}