mirror of
https://github.com/glfw/glfw.git
synced 2024-11-29 13:32: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 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
|
if @ref glfwTerminate had been called. The library only needs to be initialized
|
||||||
once and additional calls to an already initialized library will return
|
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
|
Once the library has been successfully initialized, it should be terminated
|
||||||
before the application exits. Modern systems are very good at freeing resources
|
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();
|
glfwTerminate();
|
||||||
@endcode
|
@endcode
|
||||||
|
|
||||||
This will destroy any remaining window, monitor and cursor objects, restore any
|
This needs to be called once for every time @ref glfwInit was called successfully.
|
||||||
modified gamma ramps, re-enable the screensaver if it had been disabled and free
|
On last necessary call this will destroy any remaining window, monitor and cursor
|
||||||
any other resources allocated by GLFW.
|
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
|
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
|
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.
|
* succeeds, you should call @ref glfwTerminate before the application exits.
|
||||||
*
|
*
|
||||||
* Additional calls to this function after successful initialization but before
|
* 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
|
* The @ref GLFW_PLATFORM init hint controls which platforms are considered during
|
||||||
* initialization. This also depends on which platforms the library was compiled to
|
* 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.
|
* you will be able to use most GLFW functions.
|
||||||
*
|
*
|
||||||
* If GLFW has been successfully initialized, this function should be called
|
* If GLFW has been successfully initialized, this function should be called
|
||||||
* before the application exits. If initialization fails, there is no need to
|
* once for every successful call to @ref glfwInit before the application
|
||||||
* call this function, as it is called by @ref glfwInit before it returns
|
* exits. If initialization fails, there is no need to call this function, as
|
||||||
* failure.
|
* it is called by @ref glfwInit before it returns failure.
|
||||||
*
|
*
|
||||||
* This function has no effect if GLFW is not initialized.
|
* 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
|
// 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
|
// These are outside of _glfw so they can be used before initialization and
|
||||||
// after termination without special handling when _glfw is cleared to zero
|
// after termination without special handling when _glfw is cleared to zero
|
||||||
@ -119,7 +119,7 @@ static void terminate(void)
|
|||||||
_glfw.platform.terminateJoysticks();
|
_glfw.platform.terminateJoysticks();
|
||||||
_glfw.platform.terminate();
|
_glfw.platform.terminate();
|
||||||
|
|
||||||
_glfw.initialized = GLFW_FALSE;
|
_glfw.initialized = 0;
|
||||||
|
|
||||||
while (_glfw.errorListHead)
|
while (_glfw.errorListHead)
|
||||||
{
|
{
|
||||||
@ -406,7 +406,10 @@ void _glfwInputError(int code, const char* format, ...)
|
|||||||
GLFWAPI int glfwInit(void)
|
GLFWAPI int glfwInit(void)
|
||||||
{
|
{
|
||||||
if (_glfw.initialized)
|
if (_glfw.initialized)
|
||||||
|
{
|
||||||
|
_glfw.initialized++;
|
||||||
return GLFW_TRUE;
|
return GLFW_TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
memset(&_glfw, 0, sizeof(_glfw));
|
memset(&_glfw, 0, sizeof(_glfw));
|
||||||
_glfw.hints.init = _glfwInitHints;
|
_glfw.hints.init = _glfwInitHints;
|
||||||
@ -443,7 +446,7 @@ GLFWAPI int glfwInit(void)
|
|||||||
_glfwPlatformInitTimer();
|
_glfwPlatformInitTimer();
|
||||||
_glfw.timer.offset = _glfwPlatformGetTimerValue();
|
_glfw.timer.offset = _glfwPlatformGetTimerValue();
|
||||||
|
|
||||||
_glfw.initialized = GLFW_TRUE;
|
_glfw.initialized = 1;
|
||||||
|
|
||||||
glfwDefaultWindowHints();
|
glfwDefaultWindowHints();
|
||||||
return GLFW_TRUE;
|
return GLFW_TRUE;
|
||||||
@ -453,6 +456,11 @@ GLFWAPI void glfwTerminate(void)
|
|||||||
{
|
{
|
||||||
if (!_glfw.initialized)
|
if (!_glfw.initialized)
|
||||||
return;
|
return;
|
||||||
|
if (_glfw.initialized > 1)
|
||||||
|
{
|
||||||
|
_glfw.initialized--;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
terminate();
|
terminate();
|
||||||
}
|
}
|
||||||
|
@ -760,7 +760,7 @@ struct _GLFWplatform
|
|||||||
//
|
//
|
||||||
struct _GLFWlibrary
|
struct _GLFWlibrary
|
||||||
{
|
{
|
||||||
GLFWbool initialized;
|
int initialized;
|
||||||
GLFWallocator allocator;
|
GLFWallocator allocator;
|
||||||
|
|
||||||
_GLFWplatform platform;
|
_GLFWplatform platform;
|
||||||
|
Loading…
Reference in New Issue
Block a user