intro.dox 17 KB

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