• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*!
2
3@page intro_guide Introduction to the API
4
5@tableofcontents
6
7This guide introduces the basic concepts of GLFW and describes initialization,
8error handling and API guarantees and limitations.  For a broad but shallow
9tutorial, see @ref quick_guide instead.  For details on a specific function in
10this category, see the @ref init.
11
12There are also guides for the other areas of GLFW.
13
14 - @ref window_guide
15 - @ref context_guide
16 - @ref vulkan_guide
17 - @ref monitor_guide
18 - @ref input_guide
19
20
21@section intro_init Initialization and termination
22
23Before most GLFW functions may be called, the library must be initialized.
24This initialization checks what features are available on the machine,
25enumerates monitors and joysticks, initializes the timer and performs any
26required platform-specific initialization.
27
28Only the following functions may be called before the library has been
29successfully initialized, and only from the main thread.
30
31 - @ref glfwGetVersion
32 - @ref glfwGetVersionString
33 - @ref glfwSetErrorCallback
34 - @ref glfwInit
35 - @ref glfwTerminate
36
37Calling any other function before successful initialization will cause a @ref
38GLFW_NOT_INITIALIZED error.
39
40
41@subsection intro_init_init Initializing GLFW
42
43The library is initialized with @ref glfwInit, which returns `GLFW_FALSE` if an
44error occurred.
45
46@code
47if (!glfwInit())
48{
49    // Handle initialization failure
50}
51@endcode
52
53If any part of initialization fails, any parts that succeeded are terminated as
54if @ref glfwTerminate had been called.  The library only needs to be initialized
55once and additional calls to an already initialized library will simply return
56`GLFW_TRUE` immediately.
57
58Once the library has been successfully initialized, it should be terminated
59before the application exits.  Modern systems are very good at freeing resources
60allocated by programs that simply exit, but GLFW sometimes has to change global
61system settings and these might not be restored without termination.
62
63
64@subsection intro_init_terminate Terminating GLFW
65
66Before your application exits, you should terminate the GLFW library if it has
67been initialized.  This is done with @ref glfwTerminate.
68
69@code
70glfwTerminate();
71@endcode
72
73This will destroy any remaining window, monitor and cursor objects, restore any
74modified gamma ramps, re-enable the screensaver if it had been disabled and free
75any other resources allocated by GLFW.
76
77Once the library is terminated, it is as if it had never been initialized and
78you will need to initialize it again before being able to use GLFW.  If the
79library was not initialized or had already been terminated, it return
80immediately.
81
82
83@section error_handling Error handling
84
85Some GLFW functions have return values that indicate an error, but this is often
86not very helpful when trying to figure out _why_ the error occurred.  Some
87functions also return otherwise valid values on error.  Finally, far from all
88GLFW functions have return values.
89
90This is where the error callback comes in.  This callback is called whenever an
91error occurs.  It is set with @ref glfwSetErrorCallback, a function that may be
92called regardless of whether GLFW is initialized.
93
94@code
95glfwSetErrorCallback(error_callback);
96@endcode
97
98The error callback receives a human-readable description of the error and (when
99possible) its cause.  The description encoded as UTF-8.  The callback is also
100provided with an [error code](@ref errors).
101
102@code
103void error_callback(int error, const char* description)
104{
105    puts(description);
106}
107@endcode
108
109The error code indicates the general category of the error.  Some error codes,
110such as @ref GLFW_NOT_INITIALIZED has only a single meaning, whereas others like
111@ref GLFW_PLATFORM_ERROR are used for many different errors.
112
113The description string is only valid until the error callback returns, as it may
114have been generated specifically for that error.  This lets GLFW provide much
115more specific error descriptions but means you must make a copy if you want to
116keep the description string.
117
118@note Relying on erroneous behavior is not forward compatible.  In other words,
119do not rely on a currently invalid call to generate a specific error, as that
120same call may in future versions generate a different error or become valid.
121
122
123@section coordinate_systems Coordinate systems
124
125GLFW has two primary coordinate systems: the _virtual screen_ and the window
126_client area_ or _content area_.  Both use the same unit: _virtual screen
127coordinates_, or just _screen coordinates_, which don't necessarily correspond
128to pixels.
129
130<img src="spaces.svg" width="90%" />
131
132Both the virtual screen and the client area coordinate systems have the X-axis
133pointing to the right and the Y-axis pointing down.
134
135Window and monitor positions are specified as the position of the upper-left
136corners of their content areas relative to the virtual screen, while cursor
137positions are specified relative to a window's client area.
138
139Because the origin of the window's client area coordinate system is also the
140point from which the window position is specified, you can translate client area
141coordinates to the virtual screen by adding the window position.  The window
142frame, when present, extends out from the client area but does not affect the
143window position.
144
145Almost all positions and sizes in GLFW are measured in screen coordinates
146relative to one of the two origins above.  This includes cursor positions,
147window positions and sizes, window frame sizes, monitor positions and video mode
148resolutions.
149
150Two exceptions are the [monitor physical size](@ref monitor_size), which is
151measured in millimetres, and [framebuffer size](@ref window_fbsize), which is
152measured in pixels.
153
154Pixels and screen coordinates may map 1:1 on your machine, but they won't on
155every other machine, for example on a Mac with a Retina display.  The ratio
156between screen coordinates and pixels may also change at run-time depending on
157which monitor the window is currently considered to be on.
158
159
160@section guarantees_limitations Guarantees and limitations
161
162This section describes the conditions under which GLFW can be expected to
163function, barring bugs in the operating system or drivers.  Use of GLFW outside
164of these limits may work on some platforms, or on some machines, or some of the
165time, or on some versions of GLFW, but it may break at any time and this will
166not be considered a bug.
167
168
169@subsection lifetime Pointer lifetimes
170
171GLFW will never free any pointer you provide to it and you must never free any
172pointer it provides to you.
173
174Many GLFW functions return pointers to dynamically allocated structures, strings
175or arrays, and some callbacks are provided with strings or arrays.  These are
176always managed by GLFW and should never be freed by the application.  The
177lifetime of these pointers is documented for each GLFW function and callback.
178If you need to keep this data, you must copy it before its lifetime expires.
179
180Many GLFW functions accept pointers to structures or strings allocated by the
181application.  These are never freed by GLFW and are always the responsibility of
182the application.  If GLFW needs to keep the data in these structures or strings,
183it is copied before the function returns.
184
185Pointer lifetimes are guaranteed not to be shortened in future minor or patch
186releases.
187
188
189@subsection reentrancy Reentrancy
190
191GLFW event processing and object creation and destruction are not reentrant.
192This means that the following functions must not be called from any callback
193function:
194
195 - @ref glfwCreateWindow
196 - @ref glfwDestroyWindow
197 - @ref glfwCreateCursor
198 - @ref glfwCreateStandardCursor
199 - @ref glfwDestroyCursor
200 - @ref glfwPollEvents
201 - @ref glfwWaitEvents
202 - @ref glfwWaitEventsTimeout
203 - @ref glfwTerminate
204
205These functions may be made reentrant in future minor or patch releases, but
206functions not on this list will not be made non-reentrant.
207
208
209@subsection thread_safety Thread safety
210
211Most GLFW functions must only be called from the main thread, but some may be
212called from any thread.  However, no GLFW function may be called from any thread
213but the main thread until GLFW has been successfully initialized, including
214functions that may called before initialization.
215
216The reference documentation for every GLFW function states whether it is limited
217to the main thread.
218
219Initialization and termination, event processing and the creation and
220destruction of windows, contexts and cursors are all limited to the main thread
221due to limitations of one or several platforms.
222
223Because event processing must be performed on the main thread, all callbacks
224except for the error callback will only be called on that thread.  The error
225callback may be called on any thread, as any GLFW function may generate errors.
226
227The posting of empty events may be done from any thread.  The window user
228pointer and close flag may also be accessed and modified from any thread, but
229this is not synchronized by GLFW.  The following window related functions may
230be called from any thread:
231
232 - @ref glfwPostEmptyEvent
233 - @ref glfwGetWindowUserPointer
234 - @ref glfwSetWindowUserPointer
235 - @ref glfwWindowShouldClose
236 - @ref glfwSetWindowShouldClose
237
238Rendering may be done on any thread.  The following context related functions
239may be called from any thread:
240
241 - @ref glfwMakeContextCurrent
242 - @ref glfwGetCurrentContext
243 - @ref glfwSwapBuffers
244 - @ref glfwSwapInterval
245 - @ref glfwExtensionSupported
246 - @ref glfwGetProcAddress
247
248The raw timer may be queried from any thread.  The following raw timer related
249functions may be called from any thread:
250
251 - @ref glfwGetTimerFrequency
252 - @ref glfwGetTimerValue
253
254The regular timer may be used from any thread, but the reading and writing of
255the timer offset is not synchronized by GLFW.  The following timer related
256functions may be called from any thread:
257
258 - @ref glfwGetTime
259 - @ref glfwSetTime
260
261Library version information may be queried from any thread.  The following
262version related functions may be called from any thread:
263
264 - @ref glfwGetVersion
265 - @ref glfwGetVersionString
266
267Vulkan objects may be created and information queried from any thread.  The
268following Vulkan related functions may be called from any thread:
269
270 - @ref glfwVulkanSupported
271 - @ref glfwGetRequiredInstanceExtensions
272 - @ref glfwGetInstanceProcAddress
273 - @ref glfwGetPhysicalDevicePresentationSupport
274 - @ref glfwCreateWindowSurface
275
276GLFW uses no synchronization objects internally except for thread-local storage
277to keep track of the current context for each thread.  Synchronization is left
278to the application.
279
280Functions that may currently be called from any thread will always remain so,
281but functions that are currently limited to the main thread may be updated to
282allow calls from any thread in future releases.
283
284
285@subsection compatibility Version compatibility
286
287GLFW guarantees source and binary backward compatibility with earlier minor
288versions of the API.  This means that you can drop in a newer version of the
289library and existing programs will continue to compile and existing binaries
290will continue to run.
291
292Once a function or constant has been added, the signature of that function or
293value of that constant will remain unchanged until the next major version of
294GLFW.  No compatibility of any kind is guaranteed between major versions.
295
296Undocumented behavior, i.e. behavior that is not described in the documentation,
297may change at any time until it is documented.
298
299If the reference documentation and the implementation differ, the reference
300documentation is correct and the implementation will be fixed in the next
301release.
302
303
304@subsection event_order Event order
305
306The order of arrival of related events is not guaranteed to be consistent
307across platforms.  The exception is synthetic key and mouse button release
308events, which are always delivered after the window defocus event.
309
310
311@section intro_version Version management
312
313GLFW provides mechanisms for identifying what version of GLFW your application
314was compiled against as well as what version it is currently running against.
315If you are loading GLFW dynamically (not just linking dynamically), you can use
316this to verify that the library binary is compatible with your application.
317
318
319@subsection intro_version_compile Compile-time version
320
321The compile-time version of GLFW is provided by the GLFW header with the
322`GLFW_VERSION_MAJOR`, `GLFW_VERSION_MINOR` and `GLFW_VERSION_REVISION` macros.
323
324@code
325printf("Compiled against GLFW %i.%i.%i\n",
326       GLFW_VERSION_MAJOR,
327       GLFW_VERSION_MINOR,
328       GLFW_VERSION_REVISION);
329@endcode
330
331
332@subsection intro_version_runtime Run-time version
333
334The run-time version can be retrieved with @ref glfwGetVersion, a function that
335may be called regardless of whether GLFW is initialized.
336
337@code
338int major, minor, revision;
339glfwGetVersion(&major, &minor, &revision);
340
341printf("Running against GLFW %i.%i.%i\n", major, minor, revision);
342@endcode
343
344
345@subsection intro_version_string Version string
346
347GLFW 3 also provides a compile-time generated version string that describes the
348version, platform, compiler and any platform-specific compile-time options.
349This is primarily intended for submitting bug reports, to allow developers to
350see which code paths are enabled in a binary.
351
352The version string is returned by @ref glfwGetVersionString, a function that may
353be called regardless of whether GLFW is initialized.
354
355__Do not use the version string__ to parse the GLFW library version.  The @ref
356glfwGetVersion function already provides the version of the running library
357binary.
358
359The format of the string is as follows:
360 - The version of GLFW
361 - The name of the window system API
362 - The name of the context creation API
363 - Any additional options or APIs
364
365For example, when compiling GLFW 3.0 with MinGW using the Win32 and WGL
366back ends, the version string may look something like this:
367
368@code
3693.0.0 Win32 WGL MinGW
370@endcode
371
372*/
373