2
0

quick.dox 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. /*!
  2. @page quick_guide Getting started
  3. @tableofcontents
  4. This guide takes you through writing a simple application using GLFW 3. The
  5. application will create a window and OpenGL context, render a rotating triangle
  6. and exit when the user closes the window or presses _Escape_. This guide will
  7. introduce a few of the most commonly used functions, but there are many more.
  8. This guide assumes no experience with earlier versions of GLFW. If you
  9. have used GLFW 2 in the past, read @ref moving_guide, as some functions
  10. behave differently in GLFW 3.
  11. @section quick_steps Step by step
  12. @subsection quick_include Including the GLFW header
  13. In the source files of your application where you use GLFW, you need to include
  14. its header file.
  15. @code
  16. #include <GLFW/glfw3.h>
  17. @endcode
  18. This header provides all the constants, types and function prototypes of the
  19. GLFW API.
  20. By default it also includes the OpenGL header from your development environment.
  21. On some platforms this header only supports older versions of OpenGL. The most
  22. extreme case is Windows, where it typically only supports OpenGL 1.2.
  23. Most programs will instead use an
  24. [extension loader library](@ref context_glext_auto) and include its header.
  25. This example uses files generated by [glad](https://gen.glad.sh/). The GLFW
  26. header can detect most such headers if they are included first and will then not
  27. include the one from your development environment.
  28. @code
  29. #include <glad/gl.h>
  30. #include <GLFW/glfw3.h>
  31. @endcode
  32. To make sure there will be no header conflicts, you can define @ref
  33. GLFW_INCLUDE_NONE before the GLFW header to explicitly disable inclusion of the
  34. development environment header. This also allows the two headers to be included
  35. in any order.
  36. @code
  37. #define GLFW_INCLUDE_NONE
  38. #include <GLFW/glfw3.h>
  39. #include <glad/gl.h>
  40. @endcode
  41. @subsection quick_init_term Initializing and terminating GLFW
  42. Before you can use most GLFW functions, the library must be initialized. On
  43. successful initialization, `GLFW_TRUE` is returned. If an error occurred,
  44. `GLFW_FALSE` is returned.
  45. @code
  46. if (!glfwInit())
  47. {
  48. // Initialization failed
  49. }
  50. @endcode
  51. Note that `GLFW_TRUE` and `GLFW_FALSE` are and will always be one and zero.
  52. When you are done using GLFW, typically just before the application exits, you
  53. need to terminate GLFW.
  54. @code
  55. glfwTerminate();
  56. @endcode
  57. This destroys any remaining windows and releases any other resources allocated by
  58. GLFW. After this call, you must initialize GLFW again before using any GLFW
  59. functions that require it.
  60. @subsection quick_capture_error Setting an error callback
  61. Most events are reported through callbacks, whether it's a key being pressed,
  62. a GLFW window being moved, or an error occurring. Callbacks are C functions (or
  63. C++ static methods) that are called by GLFW with arguments describing the event.
  64. In case a GLFW function fails, an error is reported to the GLFW error callback.
  65. You can receive these reports with an error callback. This function must have
  66. the signature below but may do anything permitted in other callbacks.
  67. @code
  68. void error_callback(int error, const char* description)
  69. {
  70. fprintf(stderr, "Error: %s\n", description);
  71. }
  72. @endcode
  73. Callback functions must be set, so GLFW knows to call them. The function to set
  74. the error callback is one of the few GLFW functions that may be called before
  75. initialization, which lets you be notified of errors both during and after
  76. initialization.
  77. @code
  78. glfwSetErrorCallback(error_callback);
  79. @endcode
  80. @subsection quick_create_window Creating a window and context
  81. The window and its OpenGL context are created with a single call to @ref
  82. glfwCreateWindow, which returns a handle to the created combined window and
  83. context object
  84. @code
  85. GLFWwindow* window = glfwCreateWindow(640, 480, "My Title", NULL, NULL);
  86. if (!window)
  87. {
  88. // Window or OpenGL context creation failed
  89. }
  90. @endcode
  91. This creates a 640 by 480 windowed mode window with an OpenGL context. If
  92. window or OpenGL context creation fails, `NULL` will be returned. You should
  93. always check the return value. While window creation rarely fails, context
  94. creation depends on properly installed drivers and may fail even on machines
  95. with the necessary hardware.
  96. By default, the OpenGL context GLFW creates may have any version. You can
  97. require a minimum OpenGL version by setting the `GLFW_CONTEXT_VERSION_MAJOR` and
  98. `GLFW_CONTEXT_VERSION_MINOR` hints _before_ creation. If the required minimum
  99. version is not supported on the machine, context (and window) creation fails.
  100. @code
  101. glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2);
  102. glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
  103. GLFWwindow* window = glfwCreateWindow(640, 480, "My Title", NULL, NULL);
  104. if (!window)
  105. {
  106. // Window or context creation failed
  107. }
  108. @endcode
  109. When a window and context is no longer needed, destroy it.
  110. @code
  111. glfwDestroyWindow(window);
  112. @endcode
  113. Once this function is called, no more events will be delivered for that window
  114. and its handle becomes invalid.
  115. @subsection quick_context_current Making the OpenGL context current
  116. Before you can use the OpenGL API, you must have a current OpenGL context.
  117. @code
  118. glfwMakeContextCurrent(window);
  119. @endcode
  120. The context will remain current until you make another context current or until
  121. the window owning the current context is destroyed.
  122. If you are using an [extension loader library](@ref context_glext_auto) to
  123. access modern OpenGL then this is when to initialize it, as the loader needs
  124. a current context to load from. This example uses
  125. [glad](https://github.com/Dav1dde/glad), but the same rule applies to all such
  126. libraries.
  127. @code
  128. gladLoadGL(glfwGetProcAddress);
  129. @endcode
  130. @subsection quick_window_close Checking the window close flag
  131. Each window has a flag indicating whether the window should be closed.
  132. When the user attempts to close the window, either by pressing the close widget
  133. in the title bar or using a key combination like Alt+F4, this flag is set to 1.
  134. Note that __the window isn't actually closed__, so you are expected to monitor
  135. this flag and either destroy the window or give some kind of feedback to the
  136. user.
  137. @code
  138. while (!glfwWindowShouldClose(window))
  139. {
  140. // Keep running
  141. }
  142. @endcode
  143. You can be notified when the user is attempting to close the window by setting
  144. a close callback with @ref glfwSetWindowCloseCallback. The callback will be
  145. called immediately after the close flag has been set.
  146. You can also set it yourself with @ref glfwSetWindowShouldClose. This can be
  147. useful if you want to interpret other kinds of input as closing the window, like
  148. for example pressing the _Escape_ key.
  149. @subsection quick_key_input Receiving input events
  150. Each window has a large number of callbacks that can be set to receive all the
  151. various kinds of events. To receive key press and release events, create a key
  152. callback function.
  153. @code
  154. static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
  155. {
  156. if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
  157. glfwSetWindowShouldClose(window, GLFW_TRUE);
  158. }
  159. @endcode
  160. The key callback, like other window related callbacks, are set per-window.
  161. @code
  162. glfwSetKeyCallback(window, key_callback);
  163. @endcode
  164. In order for event callbacks to be called when events occur, you need to process
  165. events as described below.
  166. @subsection quick_render Rendering with OpenGL
  167. Once you have a current OpenGL context, you can use OpenGL normally. In this
  168. tutorial, a multicolored rotating triangle will be rendered. The framebuffer
  169. size needs to be retrieved for `glViewport`.
  170. @code
  171. int width, height;
  172. glfwGetFramebufferSize(window, &width, &height);
  173. glViewport(0, 0, width, height);
  174. @endcode
  175. You can also set a framebuffer size callback using @ref
  176. glfwSetFramebufferSizeCallback and be notified when the size changes.
  177. The details of how to render with OpenGL is outside the scope of this tutorial,
  178. but there are many excellent resources for learning modern OpenGL. Here are
  179. a few of them:
  180. - [Anton's OpenGL 4 Tutorials](https://antongerdelan.net/opengl/)
  181. - [Learn OpenGL](https://learnopengl.com/)
  182. - [Open.GL](https://open.gl/)
  183. These all happen to use GLFW, but OpenGL itself works the same whatever API you
  184. use to create the window and context.
  185. @subsection quick_timer Reading the timer
  186. To create smooth animation, a time source is needed. GLFW provides a timer that
  187. returns the number of seconds since initialization. The time source used is the
  188. most accurate on each platform and generally has micro- or nanosecond
  189. resolution.
  190. @code
  191. double time = glfwGetTime();
  192. @endcode
  193. @subsection quick_swap_buffers Swapping buffers
  194. GLFW windows by default use double buffering. That means that each window has
  195. two rendering buffers; a front buffer and a back buffer. The front buffer is
  196. the one being displayed and the back buffer the one you render to.
  197. When the entire frame has been rendered, the buffers need to be swapped with one
  198. another, so the back buffer becomes the front buffer and vice versa.
  199. @code
  200. glfwSwapBuffers(window);
  201. @endcode
  202. The swap interval indicates how many frames to wait until swapping the buffers,
  203. commonly known as _vsync_. By default, the swap interval is zero, meaning
  204. buffer swapping will occur immediately. On fast machines, many of those frames
  205. will never be seen, as the screen is still only updated typically 60-75 times
  206. per second, so this wastes a lot of CPU and GPU cycles.
  207. Also, because the buffers will be swapped in the middle the screen update,
  208. leading to [screen tearing](https://en.wikipedia.org/wiki/Screen_tearing).
  209. For these reasons, applications will typically want to set the swap interval to
  210. one. It can be set to higher values, but this is usually not recommended,
  211. because of the input latency it leads to.
  212. @code
  213. glfwSwapInterval(1);
  214. @endcode
  215. This function acts on the current context and will fail unless a context is
  216. current.
  217. @subsection quick_process_events Processing events
  218. GLFW needs to communicate regularly with the window system both in order to
  219. receive events and to show that the application hasn't locked up. Event
  220. processing must be done regularly while you have visible windows and is normally
  221. done each frame after buffer swapping.
  222. There are two methods for processing pending events; polling and waiting. This
  223. example will use event polling, which processes only those events that have
  224. already been received and then returns immediately.
  225. @code
  226. glfwPollEvents();
  227. @endcode
  228. This is the best choice when rendering continually, like most games do. If
  229. instead you only need to update your rendering once you have received new input,
  230. @ref glfwWaitEvents is a better choice. It waits until at least one event has
  231. been received, putting the thread to sleep in the meantime, and then processes
  232. all received events. This saves a great deal of CPU cycles and is useful for,
  233. for example, many kinds of editing tools.
  234. @section quick_example Putting it together
  235. Now that you know how to initialize GLFW, create a window and poll for
  236. keyboard input, it's possible to create a simple program.
  237. This program creates a 640 by 480 windowed mode window and starts a loop that
  238. clears the screen, renders a triangle and processes events until the user either
  239. presses _Escape_ or closes the window.
  240. @snippet simple.c code
  241. The program above can be found in the
  242. [source package](https://www.glfw.org/download.html) as `examples/simple.c`
  243. and is compiled along with all other examples when you build GLFW. If you
  244. built GLFW from the source package then you already have this as `simple.exe` on
  245. Windows, `simple` on Linux or `simple.app` on macOS.
  246. This tutorial used only a few of the many functions GLFW provides. There are
  247. guides for each of the areas covered by GLFW. Each guide will introduce all the
  248. functions for that category.
  249. - @ref intro_guide
  250. - @ref window_guide
  251. - @ref context_guide
  252. - @ref monitor_guide
  253. - @ref input_guide
  254. You can access reference documentation for any GLFW function by clicking it and
  255. the reference for each function links to related functions and guide sections.
  256. The tutorial ends here. Once you have written a program that uses GLFW, you
  257. will need to compile and link it. How to do that depends on the development
  258. environment you are using and is best explained by the documentation for that
  259. environment. To learn about the details that are specific to GLFW, see
  260. @ref build_guide.
  261. */