quick.dox 13 KB

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