mirror of
https://github.com/glfw/glfw.git
synced 2024-11-26 03:52:01 +00:00
Improve init and termination with counting
Counts additional calls to glfwInit and waits with termination until the same amount of calls to glfwTerminate is done. For normal users nothing should change. For libraries/plug-ins they can now call glfwInit and glfwTerminate additional times as long as the calls are balanced. This avoids the problem that they would terminate GLFW completely when the main application is still using it.
This commit is contained in:
parent
c18851f52e
commit
6ea62735e7
@ -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
|
||||
|
@ -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.
|
||||
*
|
||||
|
14
src/init.c
14
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();
|
||||
}
|
||||
|
@ -760,7 +760,7 @@ struct _GLFWplatform
|
||||
//
|
||||
struct _GLFWlibrary
|
||||
{
|
||||
GLFWbool initialized;
|
||||
int initialized;
|
||||
GLFWallocator allocator;
|
||||
|
||||
_GLFWplatform platform;
|
||||
|
Loading…
Reference in New Issue
Block a user