intro.dox 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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_values Supported and default values
  74. Initialization hint | Default value | Supported values
  75. ------------------------------- | ------------- | ----------------
  76. @ref GLFW_JOYSTICK_HAT_BUTTONS | `GLFW_TRUE` | `GLFW_TRUE` or `GLFW_FALSE`
  77. @ref GLFW_COCOA_CHDIR_RESOURCES | `GLFW_TRUE` | `GLFW_TRUE` or `GLFW_FALSE`
  78. @ref GLFW_COCOA_MENUBAR | `GLFW_TRUE` | `GLFW_TRUE` or `GLFW_FALSE`
  79. @subsection intro_init_terminate Terminating GLFW
  80. Before your application exits, you should terminate the GLFW library if it has
  81. been initialized. This is done with @ref glfwTerminate.
  82. @code
  83. glfwTerminate();
  84. @endcode
  85. This will destroy any remaining window, monitor and cursor objects, restore any
  86. modified gamma ramps, re-enable the screensaver if it had been disabled and free
  87. any other resources allocated by GLFW.
  88. Once the library is terminated, it is as if it had never been initialized and
  89. you will need to initialize it again before being able to use GLFW. If the
  90. library was not initialized or had already been terminated, it return
  91. immediately.
  92. @section error_handling Error handling
  93. Some GLFW functions have return values that indicate an error, but this is often
  94. not very helpful when trying to figure out what happened or why it occurred.
  95. Other functions have no return value reserved for errors, so error notification
  96. needs a separate channel. Finally, far from all GLFW functions have return
  97. values.
  98. The last [error code](@ref errors) for the calling thread can be queried at any
  99. time with @ref glfwGetError.
  100. @code
  101. int code = glfwGetError(NULL);
  102. if (code != GLFW_NO_ERROR)
  103. handle_error(code);
  104. @endcode
  105. If no error has occurred since the last call, @ref GLFW_NO_ERROR (zero) is
  106. returned. The error is cleared before the function returns.
  107. The error code indicates the general category of the error. Some error codes,
  108. such as @ref GLFW_NOT_INITIALIZED has only a single meaning, whereas others like
  109. @ref GLFW_PLATFORM_ERROR are used for many different errors.
  110. GLFW often has more information about an error than its general category. You
  111. can retrieve a UTF-8 encoded human-readable description along with the error
  112. code. If no error has occurred since the last call, the description is set to
  113. `NULL`.
  114. @code
  115. const char* description;
  116. int code = glfwGetError(&description);
  117. if (description)
  118. display_error_message(code, description);
  119. @endcode
  120. The retrieved description string is only valid until the next error occurs.
  121. This means you must make a copy of it if you want to keep it.
  122. You can also set an error callback, which will be called each time an error
  123. occurs. It is set with @ref glfwSetErrorCallback.
  124. @code
  125. glfwSetErrorCallback(error_callback);
  126. @endcode
  127. The error callback receives the same error code and human-readable description
  128. returned by @ref glfwGetError.
  129. @code
  130. void error_callback(int code, const char* description)
  131. {
  132. display_error_message(code, description);
  133. }
  134. @endcode
  135. The error callback is called after the error is stored, so calling @ref
  136. glfwGetError from within the error callback returns the same values as the
  137. callback argument.
  138. The description string passed to the callback is only valid until the error
  139. callback returns. This means you must make a copy of it if you want to keep it.
  140. __Reported errors are never fatal.__ As long as GLFW was successfully
  141. initialized, it will remain initialized and in a safe state until terminated
  142. regardless of how many errors occur. If an error occurs during initialization
  143. that causes @ref glfwInit to fail, any part of the library that was initialized
  144. will be safely terminated.
  145. Do not rely on a currently invalid call to generate a specific error, as in the
  146. future that same call may generate a different error or become valid.
  147. @section coordinate_systems Coordinate systems
  148. GLFW has two primary coordinate systems: the _virtual screen_ and the window
  149. _content area_ or _content area_. Both use the same unit: _virtual screen
  150. coordinates_, or just _screen coordinates_, which don't necessarily correspond
  151. to pixels.
  152. <img src="spaces.svg" width="90%" />
  153. Both the virtual screen and the content area coordinate systems have the X-axis
  154. pointing to the right and the Y-axis pointing down.
  155. Window and monitor positions are specified as the position of the upper-left
  156. corners of their content areas relative to the virtual screen, while cursor
  157. positions are specified relative to a window's content area.
  158. Because the origin of the window's content area coordinate system is also the
  159. point from which the window position is specified, you can translate content
  160. area coordinates to the virtual screen by adding the window position. The
  161. window frame, when present, extends out from the content area but does not
  162. affect the window position.
  163. Almost all positions and sizes in GLFW are measured in screen coordinates
  164. relative to one of the two origins above. This includes cursor positions,
  165. window positions and sizes, window frame sizes, monitor positions and video mode
  166. resolutions.
  167. Two exceptions are the [monitor physical size](@ref monitor_size), which is
  168. measured in millimetres, and [framebuffer size](@ref window_fbsize), which is
  169. measured in pixels.
  170. Pixels and screen coordinates may map 1:1 on your machine, but they won't on
  171. every other machine, for example on a Mac with a Retina display. The ratio
  172. between screen coordinates and pixels may also change at run-time depending on
  173. which monitor the window is currently considered to be on.
  174. @section guarantees_limitations Guarantees and limitations
  175. This section describes the conditions under which GLFW can be expected to
  176. function, barring bugs in the operating system or drivers. Use of GLFW outside
  177. of these limits may work on some platforms, or on some machines, or some of the
  178. time, or on some versions of GLFW, but it may break at any time and this will
  179. not be considered a bug.
  180. @subsection lifetime Pointer lifetimes
  181. GLFW will never free any pointer you provide to it and you must never free any
  182. pointer it provides to you.
  183. Many GLFW functions return pointers to dynamically allocated structures, strings
  184. or arrays, and some callbacks are provided with strings or arrays. These are
  185. always managed by GLFW and should never be freed by the application. The
  186. lifetime of these pointers is documented for each GLFW function and callback.
  187. If you need to keep this data, you must copy it before its lifetime expires.
  188. Many GLFW functions accept pointers to structures or strings allocated by the
  189. application. These are never freed by GLFW and are always the responsibility of
  190. the application. If GLFW needs to keep the data in these structures or strings,
  191. it is copied before the function returns.
  192. Pointer lifetimes are guaranteed not to be shortened in future minor or patch
  193. releases.
  194. @subsection reentrancy Reentrancy
  195. GLFW event processing and object destruction are not reentrant. This means that
  196. the following functions must not be called from any callback function:
  197. - @ref glfwDestroyWindow
  198. - @ref glfwDestroyCursor
  199. - @ref glfwPollEvents
  200. - @ref glfwWaitEvents
  201. - @ref glfwWaitEventsTimeout
  202. - @ref glfwTerminate
  203. These functions may be made reentrant in future minor or patch releases, but
  204. functions not on this list will not be made non-reentrant.
  205. @subsection thread_safety Thread safety
  206. Most GLFW functions must only be called from the main thread (the thread that
  207. calls main), but some may be called from any thread once the library has been
  208. initialized. Before initialization the whole library is thread-unsafe.
  209. The reference documentation for every GLFW function states whether it is limited
  210. to the main thread.
  211. Initialization, termination, event processing and the creation and
  212. destruction of windows, cursors and OpenGL and OpenGL ES contexts are all
  213. restricted to the main thread due to limitations of one or several platforms.
  214. Because event processing must be performed on the main thread, all callbacks
  215. except for the error callback will only be called on that thread. The error
  216. callback may be called on any thread, as any GLFW function may generate errors.
  217. The error code and description may be queried from any thread.
  218. - @ref glfwGetError
  219. Empty events may be posted from any thread.
  220. - @ref glfwPostEmptyEvent
  221. The window user pointer and close flag may be read and written from any thread,
  222. but this is not synchronized by GLFW.
  223. - @ref glfwGetWindowUserPointer
  224. - @ref glfwSetWindowUserPointer
  225. - @ref glfwWindowShouldClose
  226. - @ref glfwSetWindowShouldClose
  227. These functions for working with OpenGL and OpenGL ES contexts may be called
  228. from any thread, but the window object is not synchronized by GLFW.
  229. - @ref glfwMakeContextCurrent
  230. - @ref glfwGetCurrentContext
  231. - @ref glfwSwapBuffers
  232. - @ref glfwSwapInterval
  233. - @ref glfwExtensionSupported
  234. - @ref glfwGetProcAddress
  235. The raw timer functions may be called from any thread.
  236. - @ref glfwGetTimerFrequency
  237. - @ref glfwGetTimerValue
  238. The regular timer may be used from any thread, but reading and writing the timer
  239. offset is not synchronized by GLFW.
  240. - @ref glfwGetTime
  241. - @ref glfwSetTime
  242. Library version information may be queried from any thread.
  243. - @ref glfwGetVersion
  244. - @ref glfwGetVersionString
  245. All Vulkan related functions may be called from any thread.
  246. - @ref glfwVulkanSupported
  247. - @ref glfwGetRequiredInstanceExtensions
  248. - @ref glfwGetInstanceProcAddress
  249. - @ref glfwGetPhysicalDevicePresentationSupport
  250. - @ref glfwCreateWindowSurface
  251. GLFW uses synchronization objects internally only to manage the per-thread
  252. context and error states. Additional synchronization is left to the
  253. application.
  254. Functions that may currently be called from any thread will always remain so,
  255. but functions that are currently limited to the main thread may be updated to
  256. allow calls from any thread in future releases.
  257. @subsection compatibility Version compatibility
  258. GLFW uses [Semantic Versioning](https://semver.org/). This guarantees source
  259. and binary backward compatibility with earlier minor versions of the API. This
  260. means that you can drop in a newer version of the library and existing programs
  261. will continue to compile and existing binaries will continue to run.
  262. Once a function or constant has been added, the signature of that function or
  263. value of that constant will remain unchanged until the next major version of
  264. GLFW. No compatibility of any kind is guaranteed between major versions.
  265. Undocumented behavior, i.e. behavior that is not described in the documentation,
  266. may change at any time until it is documented.
  267. If the reference documentation and the implementation differ, the reference
  268. documentation will almost always take precedence and the implementation will be
  269. fixed in the next release. The reference documentation will also take
  270. precedence over anything stated in a guide.
  271. @subsection event_order Event order
  272. The order of arrival of related events is not guaranteed to be consistent
  273. across platforms. The exception is synthetic key and mouse button release
  274. events, which are always delivered after the window defocus event.
  275. @section intro_version Version management
  276. GLFW provides mechanisms for identifying what version of GLFW your application
  277. was compiled against as well as what version it is currently running against.
  278. If you are loading GLFW dynamically (not just linking dynamically), you can use
  279. this to verify that the library binary is compatible with your application.
  280. @subsection intro_version_compile Compile-time version
  281. The compile-time version of GLFW is provided by the GLFW header with the
  282. `GLFW_VERSION_MAJOR`, `GLFW_VERSION_MINOR` and `GLFW_VERSION_REVISION` macros.
  283. @code
  284. printf("Compiled against GLFW %i.%i.%i\n",
  285. GLFW_VERSION_MAJOR,
  286. GLFW_VERSION_MINOR,
  287. GLFW_VERSION_REVISION);
  288. @endcode
  289. @subsection intro_version_runtime Run-time version
  290. The run-time version can be retrieved with @ref glfwGetVersion, a function that
  291. may be called regardless of whether GLFW is initialized.
  292. @code
  293. int major, minor, revision;
  294. glfwGetVersion(&major, &minor, &revision);
  295. printf("Running against GLFW %i.%i.%i\n", major, minor, revision);
  296. @endcode
  297. @subsection intro_version_string Version string
  298. GLFW 3 also provides a compile-time generated version string that describes the
  299. version, platform, compiler and any platform-specific compile-time options.
  300. This is primarily intended for submitting bug reports, to allow developers to
  301. see which code paths are enabled in a binary.
  302. The version string is returned by @ref glfwGetVersionString, a function that may
  303. be called regardless of whether GLFW is initialized.
  304. __Do not use the version string__ to parse the GLFW library version. The @ref
  305. glfwGetVersion function already provides the version of the running library
  306. binary.
  307. The format of the string is as follows:
  308. - The version of GLFW
  309. - The name of the window system API
  310. - The name of the context creation API
  311. - Any additional options or APIs
  312. For example, when compiling GLFW 3.0 with MinGW using the Win32 and WGL
  313. back ends, the version string may look something like this:
  314. @code
  315. 3.0.0 Win32 WGL MinGW
  316. @endcode
  317. */