diff --git a/docs/intro.dox b/docs/intro.dox index 79348323..a59c61df 100644 --- a/docs/intro.dox +++ b/docs/intro.dox @@ -58,7 +58,8 @@ if (!glfwInit()) If any part of initialization fails, any parts that succeeded are terminated as if @ref glfwTerminate had been called. The library only needs to be initialized once and additional calls to an already initialized library will return -`GLFW_TRUE` immediately. +`GLFW_TRUE` immediately. For each additional call to this function an +additional call to @ref glfwTerminate will need to be done later. Once the library has been successfully initialized, it should be terminated before the application exits. Modern systems are very good at freeing resources @@ -265,9 +266,10 @@ been initialized. This is done with @ref glfwTerminate. glfwTerminate(); @endcode -This will destroy any remaining window, monitor and cursor objects, restore any -modified gamma ramps, re-enable the screensaver if it had been disabled and free -any other resources allocated by GLFW. +This needs to be called once for every time @ref glfwInit was called successfully. +On last necessary call this will destroy any remaining window, monitor and cursor +objects, restore any modified gamma ramps, re-enable the screensaver if it had +been disabled and free any other resources allocated by GLFW. Once the library is terminated, it is as if it had never been initialized, therefore you will need to initialize it again before being able to use GLFW. If the diff --git a/include/GLFW/glfw3.h b/include/GLFW/glfw3.h index 26875465..b2c799ca 100644 --- a/include/GLFW/glfw3.h +++ b/include/GLFW/glfw3.h @@ -2109,7 +2109,9 @@ typedef struct GLFWallocator * succeeds, you should call @ref glfwTerminate before the application exits. * * Additional calls to this function after successful initialization but before - * termination will return `GLFW_TRUE` immediately. + * termination will return `GLFW_TRUE` immediately. For each additional call + * to this function an additional call to @ref glfwTerminate will need to be + * done later. * * The @ref GLFW_PLATFORM init hint controls which platforms are considered during * initialization. This also depends on which platforms the library was compiled to @@ -2159,9 +2161,9 @@ GLFWAPI int glfwInit(void); * you will be able to use most GLFW functions. * * If GLFW has been successfully initialized, this function should be called - * before the application exits. If initialization fails, there is no need to - * call this function, as it is called by @ref glfwInit before it returns - * failure. + * once for every successful call to @ref glfwInit before the application + * exits. If initialization fails, there is no need to call this function, as + * it is called by @ref glfwInit before it returns failure. * * This function has no effect if GLFW is not initialized. * diff --git a/src/init.c b/src/init.c index d07a492e..afdf5418 100644 --- a/src/init.c +++ b/src/init.c @@ -41,7 +41,7 @@ // This contains all mutable state shared between compilation units of GLFW // -_GLFWlibrary _glfw = { GLFW_FALSE }; +_GLFWlibrary _glfw = { 0 }; // These are outside of _glfw so they can be used before initialization and // after termination without special handling when _glfw is cleared to zero @@ -119,7 +119,7 @@ static void terminate(void) _glfw.platform.terminateJoysticks(); _glfw.platform.terminate(); - _glfw.initialized = GLFW_FALSE; + _glfw.initialized = 0; while (_glfw.errorListHead) { @@ -406,7 +406,10 @@ void _glfwInputError(int code, const char* format, ...) GLFWAPI int glfwInit(void) { if (_glfw.initialized) + { + _glfw.initialized++; return GLFW_TRUE; + } memset(&_glfw, 0, sizeof(_glfw)); _glfw.hints.init = _glfwInitHints; @@ -443,7 +446,7 @@ GLFWAPI int glfwInit(void) _glfwPlatformInitTimer(); _glfw.timer.offset = _glfwPlatformGetTimerValue(); - _glfw.initialized = GLFW_TRUE; + _glfw.initialized = 1; glfwDefaultWindowHints(); return GLFW_TRUE; @@ -453,6 +456,11 @@ GLFWAPI void glfwTerminate(void) { if (!_glfw.initialized) return; + if (_glfw.initialized > 1) + { + _glfw.initialized--; + return; + } terminate(); } diff --git a/src/internal.h b/src/internal.h index 5aa22f59..91ece68b 100644 --- a/src/internal.h +++ b/src/internal.h @@ -760,7 +760,7 @@ struct _GLFWplatform // struct _GLFWlibrary { - GLFWbool initialized; + int initialized; GLFWallocator allocator; _GLFWplatform platform;