quick.dox 12 KB

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