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