Platformer in OpenGL
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

372 lines
14 KiB

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