quick.dox 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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. The window handle is passed to all window related functions and is provided to
  110. along to all window related callbacks, so they can tell which window received
  111. the event.
  112. When a window and context is no longer needed, destroy it.
  113. @code
  114. glfwDestroyWindow(window);
  115. @endcode
  116. Once this function is called, no more events will be delivered for that window
  117. and its handle becomes invalid.
  118. @subsection quick_context_current Making the OpenGL context current
  119. Before you can use the OpenGL API, you must have a current OpenGL context.
  120. @code
  121. glfwMakeContextCurrent(window);
  122. @endcode
  123. The context will remain current until you make another context current or until
  124. the window owning the current context is destroyed.
  125. If you are using an [extension loader library](@ref context_glext_auto) to
  126. access modern OpenGL then this is when to initialize it, as the loader needs
  127. a current context to load from. This example uses
  128. [glad](https://github.com/Dav1dde/glad), but the same rule applies to all such
  129. libraries.
  130. @code
  131. gladLoadGL(glfwGetProcAddress);
  132. @endcode
  133. @subsection quick_window_close Checking the window close flag
  134. Each window has a flag indicating whether the window should be closed.
  135. When the user attempts to close the window, either by pressing the close widget
  136. in the title bar or using a key combination like Alt+F4, this flag is set to 1.
  137. Note that __the window isn't actually closed__, so you are expected to monitor
  138. this flag and either destroy the window or give some kind of feedback to the
  139. user.
  140. @code
  141. while (!glfwWindowShouldClose(window))
  142. {
  143. // Keep running
  144. }
  145. @endcode
  146. You can be notified when the user is attempting to close the window by setting
  147. a close callback with @ref glfwSetWindowCloseCallback. The callback will be
  148. called immediately after the close flag has been set.
  149. You can also set it yourself with @ref glfwSetWindowShouldClose. This can be
  150. useful if you want to interpret other kinds of input as closing the window, like
  151. for example pressing the _Escape_ key.
  152. @subsection quick_key_input Receiving input events
  153. Each window has a large number of callbacks that can be set to receive all the
  154. various kinds of events. To receive key press and release events, create a key
  155. callback function.
  156. @code
  157. static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
  158. {
  159. if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
  160. glfwSetWindowShouldClose(window, GLFW_TRUE);
  161. }
  162. @endcode
  163. The key callback, like other window related callbacks, are set per-window.
  164. @code
  165. glfwSetKeyCallback(window, key_callback);
  166. @endcode
  167. In order for event callbacks to be called when events occur, you need to process
  168. events as described below.
  169. @subsection quick_render Rendering with OpenGL
  170. Once you have a current OpenGL context, you can use OpenGL normally. In this
  171. tutorial, a multi-colored rotating triangle will be rendered. The framebuffer
  172. size needs to be retrieved for `glViewport`.
  173. @code
  174. int width, height;
  175. glfwGetFramebufferSize(window, &width, &height);
  176. glViewport(0, 0, width, height);
  177. @endcode
  178. You can also set a framebuffer size callback using @ref
  179. glfwSetFramebufferSizeCallback and be notified when the size changes.
  180. The details of how to render with OpenGL is outside the scope of this tutorial,
  181. but there are many excellent resources for learning modern OpenGL. Here are
  182. a few of them:
  183. - [Anton's OpenGL 4 Tutorials](https://antongerdelan.net/opengl/)
  184. - [Learn OpenGL](https://learnopengl.com/)
  185. - [Open.GL](https://open.gl/)
  186. These all happen to use GLFW, but OpenGL itself works the same whatever API you
  187. use to create the window and context.
  188. @subsection quick_timer Reading the timer
  189. To create smooth animation, a time source is needed. GLFW provides a timer that
  190. returns the number of seconds since initialization. The time source used is the
  191. most accurate on each platform and generally has micro- or nanosecond
  192. resolution.
  193. @code
  194. double time = glfwGetTime();
  195. @endcode
  196. @subsection quick_swap_buffers Swapping buffers
  197. GLFW windows by default use double buffering. That means that each window has
  198. two rendering buffers; a front buffer and a back buffer. The front buffer is
  199. the one being displayed and the back buffer the one you render to.
  200. When the entire frame has been rendered, the buffers need to be swapped with one
  201. another, so the back buffer becomes the front buffer and vice versa.
  202. @code
  203. glfwSwapBuffers(window);
  204. @endcode
  205. The swap interval indicates how many frames to wait until swapping the buffers,
  206. commonly known as _vsync_. By default, the swap interval is zero, meaning
  207. buffer swapping will occur immediately. On fast machines, many of those frames
  208. will never be seen, as the screen is still only updated typically 60-75 times
  209. per second, so this wastes a lot of CPU and GPU cycles.
  210. Also, because the buffers will be swapped in the middle the screen update,
  211. leading to [screen tearing](https://en.wikipedia.org/wiki/Screen_tearing).
  212. For these reasons, applications will typically want to set the swap interval to
  213. one. It can be set to higher values, but this is usually not recommended,
  214. because of the input latency it leads to.
  215. @code
  216. glfwSwapInterval(1);
  217. @endcode
  218. This function acts on the current context and will fail unless a context is
  219. current.
  220. @subsection quick_process_events Processing events
  221. GLFW needs to communicate regularly with the window system both in order to
  222. receive events and to show that the application hasn't locked up. Event
  223. processing must be done regularly while you have visible windows and is normally
  224. done each frame after buffer swapping.
  225. There are two methods for processing pending events; polling and waiting. This
  226. example will use event polling, which processes only those events that have
  227. already been received and then returns immediately.
  228. @code
  229. glfwPollEvents();
  230. @endcode
  231. This is the best choice when rendering continually, like most games do. If
  232. instead you only need to update your rendering once you have received new input,
  233. @ref glfwWaitEvents is a better choice. It waits until at least one event has
  234. been received, putting the thread to sleep in the meantime, and then processes
  235. all received events. This saves a great deal of CPU cycles and is useful for,
  236. for example, many kinds of editing tools.
  237. @section quick_example Putting it together
  238. Now that you know how to initialize GLFW, create a window and poll for
  239. keyboard input, it's possible to create a simple program.
  240. This program creates a 640 by 480 windowed mode window and starts a loop that
  241. clears the screen, renders a triangle and processes events until the user either
  242. presses _Escape_ or closes the window.
  243. @snippet simple.c code
  244. The program above can be found in the
  245. [source package](https://www.glfw.org/download.html) as `examples/simple.c`
  246. and is compiled along with all other examples when you build GLFW. If you
  247. built GLFW from the source package then you already have this as `simple.exe` on
  248. Windows, `simple` on Linux or `simple.app` on macOS.
  249. This tutorial used only a few of the many functions GLFW provides. There are
  250. guides for each of the areas covered by GLFW. Each guide will introduce all the
  251. functions for that category.
  252. - @ref intro_guide
  253. - @ref window_guide
  254. - @ref context_guide
  255. - @ref monitor_guide
  256. - @ref input_guide
  257. You can access reference documentation for any GLFW function by clicking it and
  258. the reference for each function links to related functions and guide sections.
  259. The tutorial ends here. Once you have written a program that uses GLFW, you
  260. will need to compile and link it. How to do that depends on the development
  261. environment you are using and is best explained by the documentation for that
  262. environment. To learn about the details that are specific to GLFW, see
  263. @ref build_guide.
  264. */