intro.dox 17 KB

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