intro.dox 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. /*!
  2. @page intro_guide Introduction to the API
  3. @tableofcontents
  4. This guide introduces the basic concepts of GLFW and describes initialization,
  5. error handling and API guarantees and limitations. For a broad but shallow
  6. tutorial, see @ref quick_guide instead. For details on a specific function in
  7. this category, see the @ref init.
  8. There are also guides for the other areas of GLFW.
  9. - @ref window_guide
  10. - @ref context_guide
  11. - @ref vulkan_guide
  12. - @ref monitor_guide
  13. - @ref input_guide
  14. @section intro_init Initialization and termination
  15. Before most GLFW functions may be called, the library must be initialized.
  16. This initialization checks what features are available on the machine,
  17. enumerates monitors and joysticks, initializes the timer and performs any
  18. required platform-specific initialization.
  19. Only the following functions may be called before the library has been
  20. successfully initialized, and only from the main thread.
  21. - @ref glfwGetVersion
  22. - @ref glfwGetVersionString
  23. - @ref glfwGetError
  24. - @ref glfwSetErrorCallback
  25. - @ref glfwInitHint
  26. - @ref glfwInit
  27. - @ref glfwTerminate
  28. Calling any other function before successful initialization will cause a @ref
  29. GLFW_NOT_INITIALIZED error.
  30. @subsection intro_init_init Initializing GLFW
  31. The library is initialized with @ref glfwInit, which returns `GLFW_FALSE` if an
  32. error occurred.
  33. @code
  34. if (!glfwInit())
  35. {
  36. // Handle initialization failure
  37. }
  38. @endcode
  39. If any part of initialization fails, any parts that succeeded are terminated as
  40. if @ref glfwTerminate had been called. The library only needs to be initialized
  41. once and additional calls to an already initialized library will return
  42. `GLFW_TRUE` immediately.
  43. Once the library has been successfully initialized, it should be terminated
  44. before the application exits. Modern systems are very good at freeing resources
  45. allocated by programs that exit, but GLFW sometimes has to change global system
  46. settings and these might not be restored without termination.
  47. @subsection init_hints Initialization hints
  48. Initialization hints are set before @ref glfwInit and affect how the library
  49. behaves until termination. Hints are set with @ref glfwInitHint.
  50. @code
  51. glfwInitHint(GLFW_JOYSTICK_HAT_BUTTONS, GLFW_FALSE);
  52. @endcode
  53. The values you set hints to are never reset by GLFW, but they only take effect
  54. during initialization. Once GLFW has been initialized, any values you set will
  55. be ignored until the library is terminated and initialized again.
  56. Some hints are platform specific. These may be set on any platform but they
  57. will only affect their specific platform. Other platforms will ignore them.
  58. Setting these hints requires no platform specific headers or functions.
  59. @subsubsection init_hints_shared Shared init hints
  60. @anchor GLFW_JOYSTICK_HAT_BUTTONS
  61. __GLFW_JOYSTICK_HAT_BUTTONS__ specifies whether to also expose joystick hats as
  62. buttons, for compatibility with earlier versions of GLFW that did not have @ref
  63. glfwGetJoystickHats. Possible values are `GLFW_TRUE` and `GLFW_FALSE`.
  64. @subsubsection init_hints_osx macOS specific init hints
  65. @anchor GLFW_COCOA_CHDIR_RESOURCES_hint
  66. __GLFW_COCOA_CHDIR_RESOURCES__ specifies whether to set the current directory to
  67. the application to the `Contents/Resources` subdirectory of the application's
  68. bundle, if present. Set this with @ref glfwInitHint.
  69. @anchor GLFW_COCOA_MENUBAR_hint
  70. __GLFW_COCOA_MENUBAR__ specifies whether to create a basic menu bar, either from
  71. a nib or manually, when the first window is created, which is when AppKit is
  72. initialized. Set this with @ref glfwInitHint.
  73. @subsubsection init_hints_wayland Wayland specific init hints
  74. @anchor GLFW_WAYLAND_LIBDECOR_hint
  75. __GLFW_WAYLAND_LIBDECOR__ specifies whether to use
  76. [libdecor](https://gitlab.freedesktop.org/libdecor/libdecor) for window
  77. decorations where available. Possible values are `GLFW_WAYLAND_PREFER_LIBDECOR`
  78. and `GLFW_WAYLAND_DISABLE_LIBDECOR`. This is ignored on other platforms.
  79. @note This init hint was added in 3.3.9 and is not present in earlier patch releases. It
  80. is safe to attempt to set this hint on earlier versions of GLFW 3.3 but it will emit
  81. a harmless @ref GLFW_INVALID_ENUM error. If you need to avoid causing any errors, you can
  82. check the library version first with @ref glfwGetVersion.
  83. @note To set this hint while also building against earlier versions of GLFW 3.3, you can
  84. use the numerical constants directly.
  85. @note @code
  86. int minor, patch;
  87. glfwGetVersion(NULL, &minor, &patch);
  88. if (minor > 3 || (minor == 3 && patch >= 9))
  89. glfwInitHint(0x00053001 /*GLFW_WAYLAND_LIBDECOR*/, 0x00038002 /*GLFW_WAYLAND_DISABLE_LIBDECOR*/);
  90. @endcode
  91. @subsubsection init_hints_values Supported and default values
  92. Initialization hint | Default value | Supported values
  93. ------------------------------- | ------------- | ----------------
  94. @ref GLFW_JOYSTICK_HAT_BUTTONS | `GLFW_TRUE` | `GLFW_TRUE` or `GLFW_FALSE`
  95. @ref GLFW_COCOA_CHDIR_RESOURCES | `GLFW_TRUE` | `GLFW_TRUE` or `GLFW_FALSE`
  96. @ref GLFW_COCOA_MENUBAR | `GLFW_TRUE` | `GLFW_TRUE` or `GLFW_FALSE`
  97. @ref GLFW_WAYLAND_LIBDECOR | `GLFW_WAYLAND_PREFER_LIBDECOR` | `GLFW_WAYLAND_PREFER_LIBDECOR` or `GLFW_WAYLAND_DISABLE_LIBDECOR`
  98. @subsection intro_init_terminate Terminating GLFW
  99. Before your application exits, you should terminate the GLFW library if it has
  100. been initialized. This is done with @ref glfwTerminate.
  101. @code
  102. glfwTerminate();
  103. @endcode
  104. This will destroy any remaining window, monitor and cursor objects, restore any
  105. modified gamma ramps, re-enable the screensaver if it had been disabled and free
  106. any other resources allocated by GLFW.
  107. Once the library is terminated, it is as if it had never been initialized, therefore
  108. you will need to initialize it again before being able to use GLFW. If the
  109. library was not initialized or had already been terminated, it returns
  110. immediately.
  111. @section error_handling Error handling
  112. Some GLFW functions have return values that indicate an error, but this is often
  113. not very helpful when trying to figure out what happened or why it occurred.
  114. Other functions have no return value reserved for errors, so error notification
  115. needs a separate channel. Finally, far from all GLFW functions have return
  116. values.
  117. The last [error code](@ref errors) for the calling thread can be queried at any
  118. time with @ref glfwGetError.
  119. @code
  120. int code = glfwGetError(NULL);
  121. if (code != GLFW_NO_ERROR)
  122. handle_error(code);
  123. @endcode
  124. If no error has occurred since the last call, @ref GLFW_NO_ERROR (zero) is
  125. returned. The error is cleared before the function returns.
  126. The error code indicates the general category of the error. Some error codes,
  127. such as @ref GLFW_NOT_INITIALIZED has only a single meaning, whereas others like
  128. @ref GLFW_PLATFORM_ERROR are used for many different errors.
  129. GLFW often has more information about an error than its general category. You
  130. can retrieve a UTF-8 encoded human-readable description along with the error
  131. code. If no error has occurred since the last call, the description is set to
  132. `NULL`.
  133. @code
  134. const char* description;
  135. int code = glfwGetError(&description);
  136. if (description)
  137. display_error_message(code, description);
  138. @endcode
  139. The retrieved description string is only valid until the next error occurs.
  140. This means you must make a copy of it if you want to keep it.
  141. You can also set an error callback, which will be called each time an error
  142. occurs. It is set with @ref glfwSetErrorCallback.
  143. @code
  144. glfwSetErrorCallback(error_callback);
  145. @endcode
  146. The error callback receives the same error code and human-readable description
  147. returned by @ref glfwGetError.
  148. @code
  149. void error_callback(int code, const char* description)
  150. {
  151. display_error_message(code, description);
  152. }
  153. @endcode
  154. The error callback is called after the error is stored, so calling @ref
  155. glfwGetError from within the error callback returns the same values as the
  156. callback argument.
  157. The description string passed to the callback is only valid until the error
  158. callback returns. This means you must make a copy of it if you want to keep it.
  159. __Reported errors are never fatal.__ As long as GLFW was successfully
  160. initialized, it will remain initialized and in a safe state until terminated
  161. regardless of how many errors occur. If an error occurs during initialization
  162. that causes @ref glfwInit to fail, any part of the library that was initialized
  163. will be safely terminated.
  164. Do not rely on a currently invalid call to generate a specific error, as in the
  165. future that same call may generate a different error or become valid.
  166. @section coordinate_systems Coordinate systems
  167. GLFW has two primary coordinate systems: the _virtual screen_ and the window
  168. _content area_ or _content area_. Both use the same unit: _virtual screen
  169. coordinates_, or just _screen coordinates_, which don't necessarily correspond
  170. to pixels.
  171. <img src="spaces.svg" width="90%" />
  172. Both the virtual screen and the content area coordinate systems have the X-axis
  173. pointing to the right and the Y-axis pointing down.
  174. Window and monitor positions are specified as the position of the upper-left
  175. corners of their content areas relative to the virtual screen, while cursor
  176. positions are specified relative to a window's content area.
  177. Because the origin of the window's content area coordinate system is also the
  178. point from which the window position is specified, you can translate content
  179. area coordinates to the virtual screen by adding the window position. The
  180. window frame, when present, extends out from the content area but does not
  181. affect the window position.
  182. Almost all positions and sizes in GLFW are measured in screen coordinates
  183. relative to one of the two origins above. This includes cursor positions,
  184. window positions and sizes, window frame sizes, monitor positions and video mode
  185. resolutions.
  186. Two exceptions are the [monitor physical size](@ref monitor_size), which is
  187. measured in millimetres, and [framebuffer size](@ref window_fbsize), which is
  188. measured in pixels.
  189. Pixels and screen coordinates may map 1:1 on your machine, but they won't on
  190. every other machine, for example on a Mac with a Retina display. The ratio
  191. between screen coordinates and pixels may also change at run-time depending on
  192. which monitor the window is currently considered to be on.
  193. @section guarantees_limitations Guarantees and limitations
  194. This section describes the conditions under which GLFW can be expected to
  195. function, barring bugs in the operating system or drivers. Use of GLFW outside
  196. these limits may work on some platforms, or on some machines, or some of the
  197. time, or on some versions of GLFW, but it may break at any time and this will
  198. not be considered a bug.
  199. @subsection lifetime Pointer lifetimes
  200. GLFW will never free any pointer you provide to it, and you must never free any
  201. pointer it provides to you.
  202. Many GLFW functions return pointers to dynamically allocated structures, strings
  203. or arrays, and some callbacks are provided with strings or arrays. These are
  204. always managed by GLFW and should never be freed by the application. The
  205. lifetime of these pointers is documented for each GLFW function and callback.
  206. If you need to keep this data, you must copy it before its lifetime expires.
  207. Many GLFW functions accept pointers to structures or strings allocated by the
  208. application. These are never freed by GLFW and are always the responsibility of
  209. the application. If GLFW needs to keep the data in these structures or strings,
  210. it is copied before the function returns.
  211. Pointer lifetimes are guaranteed not to be shortened in future minor or patch
  212. releases.
  213. @subsection reentrancy Reentrancy
  214. GLFW event processing and object destruction are not reentrant. This means that
  215. the following functions must not be called from any callback function:
  216. - @ref glfwDestroyWindow
  217. - @ref glfwDestroyCursor
  218. - @ref glfwPollEvents
  219. - @ref glfwWaitEvents
  220. - @ref glfwWaitEventsTimeout
  221. - @ref glfwTerminate
  222. These functions may be made reentrant in future minor or patch releases, but
  223. functions not on this list will not be made non-reentrant.
  224. @subsection thread_safety Thread safety
  225. Most GLFW functions must only be called from the main thread (the thread that
  226. calls main), but some may be called from any thread once the library has been
  227. initialized. Before initialization the whole library is thread-unsafe.
  228. The reference documentation for every GLFW function states whether it is limited
  229. to the main thread.
  230. Initialization, termination, event processing and the creation and
  231. destruction of windows, cursors and OpenGL and OpenGL ES contexts are all
  232. restricted to the main thread due to limitations of one or several platforms.
  233. Because event processing must be performed on the main thread, all callbacks
  234. except for the error callback will only be called on that thread. The error
  235. callback may be called on any thread, as any GLFW function may generate errors.
  236. The error code and description may be queried from any thread.
  237. - @ref glfwGetError
  238. Empty events may be posted from any thread.
  239. - @ref glfwPostEmptyEvent
  240. The window user pointer and close flag may be read and written from any thread,
  241. but this is not synchronized by GLFW.
  242. - @ref glfwGetWindowUserPointer
  243. - @ref glfwSetWindowUserPointer
  244. - @ref glfwWindowShouldClose
  245. - @ref glfwSetWindowShouldClose
  246. These functions for working with OpenGL and OpenGL ES contexts may be called
  247. from any thread, but the window object is not synchronized by GLFW.
  248. - @ref glfwMakeContextCurrent
  249. - @ref glfwGetCurrentContext
  250. - @ref glfwSwapBuffers
  251. - @ref glfwSwapInterval
  252. - @ref glfwExtensionSupported
  253. - @ref glfwGetProcAddress
  254. The raw timer functions may be called from any thread.
  255. - @ref glfwGetTimerFrequency
  256. - @ref glfwGetTimerValue
  257. The regular timer may be used from any thread, but reading and writing the timer
  258. offset is not synchronized by GLFW.
  259. - @ref glfwGetTime
  260. - @ref glfwSetTime
  261. Library version information may be queried from any thread.
  262. - @ref glfwGetVersion
  263. - @ref glfwGetVersionString
  264. All Vulkan related functions may be called from any thread.
  265. - @ref glfwVulkanSupported
  266. - @ref glfwGetRequiredInstanceExtensions
  267. - @ref glfwGetInstanceProcAddress
  268. - @ref glfwGetPhysicalDevicePresentationSupport
  269. - @ref glfwCreateWindowSurface
  270. GLFW uses synchronization objects internally only to manage the per-thread
  271. context and error states. Additional synchronization is left to the
  272. application.
  273. Functions that may currently be called from any thread will always remain so,
  274. but functions that are currently limited to the main thread may be updated to
  275. allow calls from any thread in future releases.
  276. @subsection compatibility Version compatibility
  277. GLFW uses [Semantic Versioning](https://semver.org/). This guarantees source
  278. and binary backward compatibility with earlier minor versions of the API. This
  279. means that you can drop in a newer version of the library and existing programs
  280. will continue to compile and existing binaries will continue to run.
  281. Once a function or constant has been added, the signature of that function or
  282. value of that constant will remain unchanged until the next major version of
  283. GLFW. No compatibility of any kind is guaranteed between major versions.
  284. Undocumented behavior, i.e. behavior that is not described in the documentation,
  285. may change at any time until it is documented.
  286. If the reference documentation and the implementation differ, the reference
  287. documentation will almost always take precedence and the implementation will be
  288. fixed in the next release. The reference documentation will also take
  289. precedence over anything stated in a guide.
  290. @subsection event_order Event order
  291. The order of arrival of related events is not guaranteed to be consistent
  292. across platforms. The exception is synthetic key and mouse button release
  293. events, which are always delivered after the window defocus event.
  294. @section intro_version Version management
  295. GLFW provides mechanisms for identifying what version of GLFW your application
  296. was compiled against as well as what version it is currently running against.
  297. If you are loading GLFW dynamically (not just linking dynamically), you can use
  298. this to verify that the library binary is compatible with your application.
  299. @subsection intro_version_compile Compile-time version
  300. The compile-time version of GLFW is provided by the GLFW header with the
  301. `GLFW_VERSION_MAJOR`, `GLFW_VERSION_MINOR` and `GLFW_VERSION_REVISION` macros.
  302. @code
  303. printf("Compiled against GLFW %i.%i.%i\n",
  304. GLFW_VERSION_MAJOR,
  305. GLFW_VERSION_MINOR,
  306. GLFW_VERSION_REVISION);
  307. @endcode
  308. @subsection intro_version_runtime Run-time version
  309. The run-time version can be retrieved with @ref glfwGetVersion, a function that
  310. may be called regardless of whether GLFW is initialized.
  311. @code
  312. int major, minor, revision;
  313. glfwGetVersion(&major, &minor, &revision);
  314. printf("Running against GLFW %i.%i.%i\n", major, minor, revision);
  315. @endcode
  316. @subsection intro_version_string Version string
  317. GLFW 3 also provides a compile-time generated version string that describes the
  318. version, platform, compiler and any platform-specific compile-time options.
  319. This is primarily intended for submitting bug reports, to allow developers to
  320. see which code paths are enabled in a binary.
  321. The version string is returned by @ref glfwGetVersionString, a function that may
  322. be called regardless of whether GLFW is initialized.
  323. __Do not use the version string__ to parse the GLFW library version. The @ref
  324. glfwGetVersion function already provides the version of the running library
  325. binary.
  326. The format of the string is as follows:
  327. - The version of GLFW
  328. - The name of the window system API
  329. - The name of the context creation API
  330. - Any additional options or APIs
  331. For example, when compiling GLFW 3.3.9 with MinGW for Windows, may result in
  332. a version string like this:
  333. @code
  334. 3.3.9 Win32 WGL EGL OSMesa MinGW
  335. @endcode
  336. */