1
0
Fork 0
mirror of https://github.com/gwm17/glfw.git synced 2024-11-30 05:58:52 -05:00
glfw/docs/intro.dox

465 lines
17 KiB
Plaintext
Raw Normal View History

2014-04-23 07:30:11 -04:00
/*!
@page intro_guide Introduction to the API
2014-04-23 07:30:11 -04:00
@tableofcontents
2016-08-08 19:34:56 -04:00
This guide introduces the basic concepts of GLFW and describes initialization,
2014-09-18 09:03:29 -04:00
error handling and API guarantees and limitations. For a broad but shallow
2016-08-11 13:04:21 -04:00
tutorial, see @ref quick_guide instead. For details on a specific function in
this category, see the @ref init.
2014-04-23 07:30:11 -04:00
There are also guides for the other areas of GLFW.
- @ref window_guide
- @ref context_guide
- @ref vulkan_guide
- @ref monitor_guide
- @ref input_guide
2014-04-23 07:30:11 -04:00
@section intro_init Initialization and termination
Before most GLFW functions may be called, the library must be initialized.
This initialization checks what features are available on the machine,
enumerates monitors and joysticks, initializes the timer and performs any
required platform-specific initialization.
Only the following functions may be called before the library has been
2014-09-18 09:03:29 -04:00
successfully initialized, and only from the main thread.
2014-04-23 07:30:11 -04:00
- @ref glfwGetVersion
- @ref glfwGetVersionString
- @ref glfwGetError
2014-04-23 07:30:11 -04:00
- @ref glfwSetErrorCallback
- @ref glfwInitHint
- @ref glfwInitHintString
2014-04-23 07:30:11 -04:00
- @ref glfwInit
- @ref glfwTerminate
2016-08-08 19:34:56 -04:00
Calling any other function before successful initialization will cause a @ref
GLFW_NOT_INITIALIZED error.
2014-04-23 07:30:11 -04:00
@subsection intro_init_init Initializing GLFW
2015-08-23 13:30:04 -04:00
The library is initialized with @ref glfwInit, which returns `GLFW_FALSE` if an
2015-01-17 19:55:25 -05:00
error occurred.
2014-04-23 07:30:11 -04:00
@code
if (!glfwInit())
{
// Handle initialization failure
}
@endcode
2016-08-08 19:34:56 -04:00
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 simply return
2015-08-23 13:30:04 -04:00
`GLFW_TRUE` immediately.
2014-04-23 07:30:11 -04:00
Once the library has been successfully initialized, it should be terminated
2016-08-08 19:34:56 -04:00
before the application exits. Modern systems are very good at freeing resources
allocated by programs that simply exit, but GLFW sometimes has to change global
system settings and these might not be restored without termination.
2014-04-23 07:30:11 -04:00
@subsection init_hints Initialization hints
Initialization hints are set before @ref glfwInit and affect how the library
behaves until termination. Integer type hints are set with @ref glfwInitHint
and string type hints with @ref glfwInitHintString.
@code
glfwInitHint(GLFW_JOYSTICK_HAT_BUTTONS, GLFW_FALSE);
@endcode
The values you set hints to are never reset by GLFW, but they only take effect
during initialization. Once GLFW has been initialized, any values you set will
be ignored until the library is terminated and initialized again.
Some hints are platform specific. These may be set on any platform but they
will only affect their specific platform. Other platforms will simply ignore
them. Setting these hints requires no platform specific headers or functions.
@anchor GLFW_JOYSTICK_HAT_BUTTONS
__GLFW_JOYSTICK_HAT_BUTTONS__ specifies whether to also expose joystick hats as
buttons, for compatibility with earlier versions of GLFW that did not have @ref
glfwGetJoystickHats. Set this with @ref glfwInitHint.
2017-06-11 12:04:17 -04:00
@subsubsection init_hints_osx macOS specific init hints
@anchor GLFW_COCOA_CHDIR_RESOURCES
__GLFW_COCOA_CHDIR_RESOURCES__ specifies whether to set the current directory to
the application to the `Contents/Resources` subdirectory of the application's
bundle, if present. Set this with @ref glfwInitHint.
@anchor GLFW_COCOA_MENUBAR
__GLFW_COCOA_MENUBAR__ specifies whether to create a basic menu bar, either from
a nib or manually, when the first window is created, which is when AppKit is
initialized. Set this with @ref glfwInitHint.
@subsubsection init_hints_x11 X11 specific init hints
@anchor GLFW_X11_WM_CLASS_NAME
@anchor GLFW_X11_WM_CLASS_CLASS
__GLFW_X11_WM_CLASS_NAME__ and __GLFW_X11_WM_CLASS_CLASS__ specifies the desired
ASCII encoded name and class parts of the ICCCM `WM_CLASS` hint for all windows.
Set this with @ref glfwInitHintString.
@subsubsection init_hints_values Supported and default values
2017-08-03 19:00:36 -04:00
Initialization hint | Default value | Supported values
------------------------------- | ------------- | ----------------
@ref GLFW_JOYSTICK_HAT_BUTTONS | `GLFW_TRUE` | `GLFW_TRUE` or `GLFW_FALSE`
@ref GLFW_COCOA_CHDIR_RESOURCES | `GLFW_TRUE` | `GLFW_TRUE` or `GLFW_FALSE`
@ref GLFW_COCOA_MENUBAR | `GLFW_TRUE` | `GLFW_TRUE` or `GLFW_FALSE`
@ref GLFW_X11_WM_CLASS_NAME | `""` | An ASCII encoded `WM_CLASS` name
@ref GLFW_X11_WM_CLASS_CLASS | `""` | An ASCII encoded `WM_CLASS` class
2014-04-23 07:30:11 -04:00
@subsection intro_init_terminate Terminating GLFW
Before your application exits, you should terminate the GLFW library if it has
been initialized. This is done with @ref glfwTerminate.
@code
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
2016-08-08 19:34:56 -04:00
any other resources allocated by GLFW.
2014-04-23 07:30:11 -04:00
Once the library is terminated, it is as if it had never been initialized and
you will need to initialize it again before being able to use GLFW. If the
2014-10-06 20:15:36 -04:00
library was not initialized or had already been terminated, it return
immediately.
2014-04-23 07:30:11 -04:00
2014-09-18 09:03:29 -04:00
@section error_handling Error handling
2014-04-23 07:30:11 -04:00
Some GLFW functions have return values that indicate an error, but this is often
not very helpful when trying to figure out what happened or why it occurred.
Other functions have no return value reserved for errors, so error notification
needs a separate channel. Finally, far from all GLFW functions have return
values.
2014-04-23 07:30:11 -04:00
The last [error code](@ref errors) for the calling thread can be queried at any
time with @ref glfwGetError.
@code
int code = glfwGetError(NULL);
if (code != GLFW_NO_ERROR)
handle_error(code);
@endcode
2017-06-11 12:04:17 -04:00
If no error has occurred since the last call, @ref GLFW_NO_ERROR (zero) is
returned. The error is cleared before the function returns.
The error code indicates the general category of the error. Some error codes,
such as @ref GLFW_NOT_INITIALIZED has only a single meaning, whereas others like
@ref GLFW_PLATFORM_ERROR are used for many different errors.
GLFW often has more information about an error than its general category. You
can retrieve a UTF-8 encoded human-readable description along with the error
code. If no error has occurred since the last call, the description is set to
`NULL`.
@code
const char* description;
int code = glfwGetError(&description);
if (description)
display_error_message(code, description);
@endcode
The retrieved description string is only valid until the next error occurs.
This means you must make a copy of it if you want to keep it.
You can also set an error callback, which will be called each time an error
occurs. It is set with @ref glfwSetErrorCallback.
2014-04-23 07:30:11 -04:00
@code
glfwSetErrorCallback(error_callback);
@endcode
The error callback receives the same error code and human-readable description
returned by @ref glfwGetError.
2014-04-23 07:30:11 -04:00
@code
void error_callback(int code, const char* description)
2014-04-23 07:30:11 -04:00
{
display_error_message(code, description);
2014-04-23 07:30:11 -04:00
}
@endcode
The error callback is called after the error is stored, so calling @ref
glfwGetError from within the error callback returns the same values as the
callback argument.
2014-04-23 07:30:11 -04:00
The description string passed to the callback is only valid until the error
callback returns. This means you must make a copy of it if you want to keep it.
__Reported errors are never fatal.__ As long as GLFW was successfully
initialized, it will remain initialized and in a safe state until terminated
regardless of how many errors occur. If an error occurs during initialization
that causes @ref glfwInit to fail, any part of the library that was initialized
will be safely terminated.
2017-02-13 09:36:06 -05:00
Do not rely on a currently invalid call to generate a specific error, as in the
future that same call may generate a different error or become valid.
2014-09-18 09:03:29 -04:00
@section coordinate_systems Coordinate systems
GLFW has two primary coordinate systems: the _virtual screen_ and the window
2014-10-07 17:37:59 -04:00
_client area_ or _content area_. Both use the same unit: _virtual screen
coordinates_, or just _screen coordinates_, which don't necessarily correspond
to pixels.
2014-09-18 09:03:29 -04:00
<img src="spaces.svg" width="90%" />
2014-10-07 17:37:59 -04:00
Both the virtual screen and the client area coordinate systems have the X-axis
pointing to the right and the Y-axis pointing down.
2014-09-18 09:03:29 -04:00
2014-10-07 17:37:59 -04:00
Window and monitor positions are specified as the position of the upper-left
corners of their content areas relative to the virtual screen, while cursor
positions are specified relative to a window's client area.
Because the origin of the window's client area coordinate system is also the
point from which the window position is specified, you can translate client area
coordinates to the virtual screen by adding the window position. The window
frame, when present, extends out from the client area but does not affect the
window position.
2014-09-18 09:03:29 -04:00
Almost all positions and sizes in GLFW are measured in screen coordinates
relative to one of the two origins above. This includes cursor positions,
window positions and sizes, window frame sizes, monitor positions and video mode
resolutions.
Two exceptions are the [monitor physical size](@ref monitor_size), which is
measured in millimetres, and [framebuffer size](@ref window_fbsize), which is
measured in pixels.
Pixels and screen coordinates may map 1:1 on your machine, but they won't on
every other machine, for example on a Mac with a Retina display. The ratio
between screen coordinates and pixels may also change at run-time depending on
2014-10-07 17:37:59 -04:00
which monitor the window is currently considered to be on.
2014-09-18 09:03:29 -04:00
@section guarantees_limitations Guarantees and limitations
This section describes the conditions under which GLFW can be expected to
2015-01-11 12:25:54 -05:00
function, barring bugs in the operating system or drivers. Use of GLFW outside
of these limits may work on some platforms, or on some machines, or some of the
time, or on some versions of GLFW, but it may break at any time and this will
not be considered a bug.
2014-09-18 09:03:29 -04:00
@subsection lifetime Pointer lifetimes
GLFW will never free any pointer you provide to it and you must never free any
pointer it provides to you.
Many GLFW functions return pointers to dynamically allocated structures, strings
or arrays, and some callbacks are provided with strings or arrays. These are
always managed by GLFW and should never be freed by the application. The
lifetime of these pointers is documented for each GLFW function and callback.
If you need to keep this data, you must copy it before its lifetime expires.
Many GLFW functions accept pointers to structures or strings allocated by the
application. These are never freed by GLFW and are always the responsibility of
the application. If GLFW needs to keep the data in these structures or strings,
2015-01-11 12:25:54 -05:00
it is copied before the function returns.
2014-09-18 09:03:29 -04:00
2015-01-11 12:25:54 -05:00
Pointer lifetimes are guaranteed not to be shortened in future minor or patch
releases.
2014-09-18 09:03:29 -04:00
@subsection reentrancy Reentrancy
2017-05-02 10:58:02 -04:00
GLFW event processing and object destruction are not reentrant. This means that
the following functions must not be called from any callback function:
2014-09-18 09:03:29 -04:00
- @ref glfwDestroyWindow
- @ref glfwDestroyCursor
- @ref glfwPollEvents
- @ref glfwWaitEvents
2016-03-29 05:26:37 -04:00
- @ref glfwWaitEventsTimeout
2015-01-01 12:41:22 -05:00
- @ref glfwTerminate
2014-09-18 09:03:29 -04:00
2015-01-11 12:25:54 -05:00
These functions may be made reentrant in future minor or patch releases, but
functions not on this list will not be made non-reentrant.
2014-09-18 09:03:29 -04:00
@subsection thread_safety Thread safety
Most GLFW functions must only be called from the main thread, but some may be
called from any thread. However, no GLFW function may be called from any thread
but the main thread until GLFW has been successfully initialized, including
functions that may called before initialization.
2015-01-11 12:25:54 -05:00
The reference documentation for every GLFW function states whether it is limited
to the main thread.
2014-09-18 09:03:29 -04:00
2015-06-15 14:37:57 -04:00
Initialization and termination, event processing and the creation and
destruction of windows, contexts and cursors are all limited to the main thread
due to limitations of one or several platforms.
2014-09-18 09:03:29 -04:00
Because event processing must be performed on the main thread, all callbacks
except for the error callback will only be called on that thread. The error
callback may be called on any thread, as any GLFW function may generate errors.
The posting of empty events may be done from any thread. The window user
pointer and close flag may also be accessed and modified from any thread, but
this is not synchronized by GLFW. The following window related functions may
be called from any thread:
- @ref glfwPostEmptyEvent
- @ref glfwGetWindowUserPointer
- @ref glfwSetWindowUserPointer
- @ref glfwWindowShouldClose
- @ref glfwSetWindowShouldClose
Rendering may be done on any thread. The following context related functions
may be called from any thread:
- @ref glfwMakeContextCurrent
- @ref glfwGetCurrentContext
- @ref glfwSwapBuffers
- @ref glfwSwapInterval
- @ref glfwExtensionSupported
- @ref glfwGetProcAddress
2016-03-29 05:26:37 -04:00
The raw timer may be queried from any thread. The following raw timer related
functions may be called from any thread:
- @ref glfwGetTimerFrequency
- @ref glfwGetTimerValue
The regular timer may be used from any thread, but the reading and writing of
the timer offset is not synchronized by GLFW. The following timer related
functions may be called from any thread:
2014-09-18 09:03:29 -04:00
- @ref glfwGetTime
- @ref glfwSetTime
2014-09-18 09:03:29 -04:00
2015-01-11 12:25:54 -05:00
Library version information may be queried from any thread. The following
version related functions may be called from any thread:
- @ref glfwGetVersion
- @ref glfwGetVersionString
2014-09-18 09:03:29 -04:00
Vulkan objects may be created and information queried from any thread. The
following Vulkan related functions may be called from any thread:
- @ref glfwVulkanSupported
- @ref glfwGetRequiredInstanceExtensions
- @ref glfwGetInstanceProcAddress
- @ref glfwGetPhysicalDevicePresentationSupport
- @ref glfwCreateWindowSurface
2014-09-18 09:03:29 -04:00
GLFW uses no synchronization objects internally except for thread-local storage
to keep track of the current context for each thread. Synchronization is left
to the application.
Functions that may currently be called from any thread will always remain so,
2016-03-08 09:29:03 -05:00
but functions that are currently limited to the main thread may be updated to
allow calls from any thread in future releases.
2014-09-18 09:03:29 -04:00
@subsection compatibility Version compatibility
2016-08-08 19:34:56 -04:00
GLFW guarantees source and binary backward compatibility with earlier minor
versions of the API. This means that you can drop in a newer version of the
library and existing programs will continue to compile and existing binaries
will continue to run.
2014-09-18 09:03:29 -04:00
Once a function or constant has been added, the signature of that function or
value of that constant will remain unchanged until the next major version of
GLFW. No compatibility of any kind is guaranteed between major versions.
2015-01-11 12:25:54 -05:00
Undocumented behavior, i.e. behavior that is not described in the documentation,
may change at any time until it is documented.
2014-09-18 09:03:29 -04:00
If the reference documentation and the implementation differ, the reference
2017-08-03 19:00:36 -04:00
documentation will almost always take precedence and the implementation will be
fixed in the next release. The reference documentation will also take
precedence over anything stated in a guide.
2014-09-18 09:03:29 -04:00
@subsection event_order Event order
The order of arrival of related events is not guaranteed to be consistent
across platforms. The exception is synthetic key and mouse button release
events, which are always delivered after the window defocus event.
2014-04-23 07:30:11 -04:00
@section intro_version Version management
GLFW provides mechanisms for identifying what version of GLFW your application
2014-09-18 09:03:29 -04:00
was compiled against as well as what version it is currently running against.
If you are loading GLFW dynamically (not just linking dynamically), you can use
this to verify that the library binary is compatible with your application.
2014-04-23 07:30:11 -04:00
@subsection intro_version_compile Compile-time version
The compile-time version of GLFW is provided by the GLFW header with the
`GLFW_VERSION_MAJOR`, `GLFW_VERSION_MINOR` and `GLFW_VERSION_REVISION` macros.
2014-09-18 09:03:29 -04:00
@code
printf("Compiled against GLFW %i.%i.%i\n",
GLFW_VERSION_MAJOR,
GLFW_VERSION_MINOR,
GLFW_VERSION_REVISION);
@endcode
2014-04-23 07:30:11 -04:00
@subsection intro_version_runtime Run-time version
The run-time version can be retrieved with @ref glfwGetVersion, a function that
2014-09-18 09:03:29 -04:00
may be called regardless of whether GLFW is initialized.
2014-04-23 07:30:11 -04:00
@code
int major, minor, revision;
glfwGetVersion(&major, &minor, &revision);
2014-09-18 09:03:29 -04:00
printf("Running against GLFW %i.%i.%i\n", major, minor, revision);
2014-04-23 07:30:11 -04:00
@endcode
@subsection intro_version_string Version string
GLFW 3 also provides a compile-time generated version string that describes the
version, platform, compiler and any platform-specific compile-time options.
This is primarily intended for submitting bug reports, to allow developers to
see which code paths are enabled in a binary.
The version string is returned by @ref glfwGetVersionString, a function that may
2014-09-18 09:03:29 -04:00
be called regardless of whether GLFW is initialized.
2014-04-23 07:30:11 -04:00
__Do not use the version string__ to parse the GLFW library version. The @ref
2015-01-11 12:25:54 -05:00
glfwGetVersion function already provides the version of the running library
binary.
2014-04-23 07:30:11 -04:00
The format of the string is as follows:
- The version of GLFW
- The name of the window system API
- The name of the context creation API
- Any additional options or APIs
For example, when compiling GLFW 3.0 with MinGW using the Win32 and WGL
back ends, the version string may look something like this:
2014-09-18 09:03:29 -04:00
@code
3.0.0 Win32 WGL MinGW
@endcode
2014-04-23 07:30:11 -04:00
*/