aboutsummaryrefslogtreecommitdiff
path: root/contrib/glfw/glfw-3.3.5.bin.WIN64/docs/html/quick_guide.html
diff options
context:
space:
mode:
Diffstat (limited to 'contrib/glfw/glfw-3.3.5.bin.WIN64/docs/html/quick_guide.html')
-rw-r--r--contrib/glfw/glfw-3.3.5.bin.WIN64/docs/html/quick_guide.html391
1 files changed, 391 insertions, 0 deletions
diff --git a/contrib/glfw/glfw-3.3.5.bin.WIN64/docs/html/quick_guide.html b/contrib/glfw/glfw-3.3.5.bin.WIN64/docs/html/quick_guide.html
new file mode 100644
index 0000000..748a2ac
--- /dev/null
+++ b/contrib/glfw/glfw-3.3.5.bin.WIN64/docs/html/quick_guide.html
@@ -0,0 +1,391 @@
1<!DOCTYPE html>
2<html lang="en">
3<head>
4<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
5<meta name="viewport" content="width=device-width, initial-scale=1.0">
6<meta http-equiv="X-UA-Compatible" content="IE=9"/>
7<meta name="generator" content="Doxygen 1.9.2"/>
8<title>GLFW: Getting started</title>
9<link href="tabs.css" rel="stylesheet" type="text/css"/>
10<script type="text/javascript" src="jquery.js"></script>
11<script type="text/javascript" src="dynsections.js"></script>
12<link href="search/search.css" rel="stylesheet" type="text/css"/>
13<script type="text/javascript" src="search/searchdata.js"></script>
14<script type="text/javascript" src="search/search.js"></script>
15<link href="doxygen.css" rel="stylesheet" type="text/css" />
16<link href="extra.css" rel="stylesheet" type="text/css"/>
17</head>
18<body>
19<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
20<div id="titlearea">
21 <div class="glfwheader">
22 <a href="https://www.glfw.org/" id="glfwhome">GLFW</a>
23 <ul class="glfwnavbar">
24 <li><a href="https://www.glfw.org/documentation.html">Documentation</a></li>
25 <li><a href="https://www.glfw.org/download.html">Download</a></li>
26 <li><a href="https://www.glfw.org/community.html">Community</a></li>
27 </ul>
28 </div>
29</div>
30<!-- end header part -->
31<!-- Generated by Doxygen 1.9.2 -->
32<script type="text/javascript">
33/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
34var searchBox = new SearchBox("searchBox", "search",'Search','.html');
35/* @license-end */
36</script>
37<script type="text/javascript" src="menudata.js"></script>
38<script type="text/javascript" src="menu.js"></script>
39<script type="text/javascript">
40/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
41$(function() {
42 initMenu('',true,false,'search.php','Search');
43 $(document).ready(function() { init_search(); });
44});
45/* @license-end */
46</script>
47<div id="main-nav"></div>
48<!-- window showing the filter options -->
49<div id="MSearchSelectWindow"
50 onmouseover="return searchBox.OnSearchSelectShow()"
51 onmouseout="return searchBox.OnSearchSelectHide()"
52 onkeydown="return searchBox.OnSearchSelectKey(event)">
53</div>
54
55<!-- iframe showing the search results (closed by default) -->
56<div id="MSearchResultsWindow">
57<iframe src="javascript:void(0)" frameborder="0"
58 name="MSearchResults" id="MSearchResults">
59</iframe>
60</div>
61
62</div><!-- top -->
63<div><div class="header">
64 <div class="headertitle"><div class="title">Getting started </div></div>
65</div><!--header-->
66<div class="contents">
67<div class="toc"><h3>Table of Contents</h3>
68<ul><li class="level1"><a href="#quick_steps">Step by step</a><ul><li class="level2"><a href="#quick_include">Including the GLFW header</a></li>
69<li class="level2"><a href="#quick_init_term">Initializing and terminating GLFW</a></li>
70<li class="level2"><a href="#quick_capture_error">Setting an error callback</a></li>
71<li class="level2"><a href="#quick_create_window">Creating a window and context</a></li>
72<li class="level2"><a href="#quick_context_current">Making the OpenGL context current</a></li>
73<li class="level2"><a href="#quick_window_close">Checking the window close flag</a></li>
74<li class="level2"><a href="#quick_key_input">Receiving input events</a></li>
75<li class="level2"><a href="#quick_render">Rendering with OpenGL</a></li>
76<li class="level2"><a href="#quick_timer">Reading the timer</a></li>
77<li class="level2"><a href="#quick_swap_buffers">Swapping buffers</a></li>
78<li class="level2"><a href="#quick_process_events">Processing events</a></li>
79</ul>
80</li>
81<li class="level1"><a href="#quick_example">Putting it together</a></li>
82</ul>
83</div>
84<div class="textblock"><p >This guide takes you through writing a simple application using GLFW 3. The application will create a window and OpenGL context, render a rotating triangle and exit when the user closes the window or presses <em>Escape</em>. This guide will introduce a few of the most commonly used functions, but there are many more.</p>
85<p >This guide assumes no experience with earlier versions of GLFW. If you have used GLFW 2 in the past, read <a class="el" href="moving_guide.html">Moving from GLFW 2 to 3</a>, as some functions behave differently in GLFW 3.</p>
86<h1><a class="anchor" id="quick_steps"></a>
87Step by step</h1>
88<h2><a class="anchor" id="quick_include"></a>
89Including the GLFW header</h2>
90<p >In the source files of your application where you use GLFW, you need to include its header file.</p>
91<div class="fragment"><div class="line"><span class="preprocessor">#include &lt;<a class="code" href="glfw3_8h.html">GLFW/glfw3.h</a>&gt;</span></div>
92<div class="ttc" id="aglfw3_8h_html"><div class="ttname"><a href="glfw3_8h.html">glfw3.h</a></div><div class="ttdoc">The header of the GLFW 3 API.</div></div>
93</div><!-- fragment --><p >This header provides all the constants, types and function prototypes of the GLFW API.</p>
94<p >By default it also includes the OpenGL header from your development environment. On some platforms this header only supports older versions of OpenGL. The most extreme case is Windows, where it typically only supports OpenGL 1.2.</p>
95<p >Most programs will instead use an <a class="el" href="context_guide.html#context_glext_auto">extension loader library</a> and include its header. This example uses files generated by <a href="https://gen.glad.sh/">glad</a>. The GLFW header can detect most such headers if they are included first and will then not include the one from your development environment.</p>
96<div class="fragment"><div class="line"><span class="preprocessor">#include &lt;glad/gl.h&gt;</span></div>
97<div class="line"><span class="preprocessor">#include &lt;<a class="code" href="glfw3_8h.html">GLFW/glfw3.h</a>&gt;</span></div>
98</div><!-- fragment --><p >To make sure there will be no header conflicts, you can define <a class="el" href="build_guide.html#GLFW_INCLUDE_NONE">GLFW_INCLUDE_NONE</a> before the GLFW header to explicitly disable inclusion of the development environment header. This also allows the two headers to be included in any order.</p>
99<div class="fragment"><div class="line"><span class="preprocessor">#define GLFW_INCLUDE_NONE</span></div>
100<div class="line"><span class="preprocessor">#include &lt;<a class="code" href="glfw3_8h.html">GLFW/glfw3.h</a>&gt;</span></div>
101<div class="line"><span class="preprocessor">#include &lt;glad/gl.h&gt;</span></div>
102</div><!-- fragment --><h2><a class="anchor" id="quick_init_term"></a>
103Initializing and terminating GLFW</h2>
104<p >Before you can use most GLFW functions, the library must be initialized. On successful initialization, <code>GLFW_TRUE</code> is returned. If an error occurred, <code>GLFW_FALSE</code> is returned.</p>
105<div class="fragment"><div class="line"><span class="keywordflow">if</span> (!<a class="code hl_function" href="group__init.html#ga317aac130a235ab08c6db0834907d85e">glfwInit</a>())</div>
106<div class="line">{</div>
107<div class="line"> <span class="comment">// Initialization failed</span></div>
108<div class="line">}</div>
109<div class="ttc" id="agroup__init_html_ga317aac130a235ab08c6db0834907d85e"><div class="ttname"><a href="group__init.html#ga317aac130a235ab08c6db0834907d85e">glfwInit</a></div><div class="ttdeci">int glfwInit(void)</div><div class="ttdoc">Initializes the GLFW library.</div></div>
110</div><!-- fragment --><p >Note that <code>GLFW_TRUE</code> and <code>GLFW_FALSE</code> are and will always be one and zero.</p>
111<p >When you are done using GLFW, typically just before the application exits, you need to terminate GLFW.</p>
112<div class="fragment"><div class="line"><a class="code hl_function" href="group__init.html#gaaae48c0a18607ea4a4ba951d939f0901">glfwTerminate</a>();</div>
113<div class="ttc" id="agroup__init_html_gaaae48c0a18607ea4a4ba951d939f0901"><div class="ttname"><a href="group__init.html#gaaae48c0a18607ea4a4ba951d939f0901">glfwTerminate</a></div><div class="ttdeci">void glfwTerminate(void)</div><div class="ttdoc">Terminates the GLFW library.</div></div>
114</div><!-- fragment --><p >This destroys any remaining windows and releases any other resources allocated by GLFW. After this call, you must initialize GLFW again before using any GLFW functions that require it.</p>
115<h2><a class="anchor" id="quick_capture_error"></a>
116Setting an error callback</h2>
117<p >Most events are reported through callbacks, whether it's a key being pressed, a GLFW window being moved, or an error occurring. Callbacks are C functions (or C++ static methods) that are called by GLFW with arguments describing the event.</p>
118<p >In case a GLFW function fails, an error is reported to the GLFW error callback. You can receive these reports with an error callback. This function must have the signature below but may do anything permitted in other callbacks.</p>
119<div class="fragment"><div class="line"><span class="keywordtype">void</span> error_callback(<span class="keywordtype">int</span> error, <span class="keyword">const</span> <span class="keywordtype">char</span>* description)</div>
120<div class="line">{</div>
121<div class="line"> fprintf(stderr, <span class="stringliteral">&quot;Error: %s\n&quot;</span>, description);</div>
122<div class="line">}</div>
123</div><!-- fragment --><p >Callback functions must be set, so GLFW knows to call them. The function to set the error callback is one of the few GLFW functions that may be called before initialization, which lets you be notified of errors both during and after initialization.</p>
124<div class="fragment"><div class="line"><a class="code hl_function" href="group__init.html#gaff45816610d53f0b83656092a4034f40">glfwSetErrorCallback</a>(error_callback);</div>
125<div class="ttc" id="agroup__init_html_gaff45816610d53f0b83656092a4034f40"><div class="ttname"><a href="group__init.html#gaff45816610d53f0b83656092a4034f40">glfwSetErrorCallback</a></div><div class="ttdeci">GLFWerrorfun glfwSetErrorCallback(GLFWerrorfun callback)</div><div class="ttdoc">Sets the error callback.</div></div>
126</div><!-- fragment --><h2><a class="anchor" id="quick_create_window"></a>
127Creating a window and context</h2>
128<p >The window and its OpenGL context are created with a single call to <a class="el" href="group__window.html#ga3555a418df92ad53f917597fe2f64aeb">glfwCreateWindow</a>, which returns a handle to the created combined window and context object</p>
129<div class="fragment"><div class="line"><a class="code hl_typedef" href="group__window.html#ga3c96d80d363e67d13a41b5d1821f3242">GLFWwindow</a>* window = <a class="code hl_function" href="group__window.html#ga3555a418df92ad53f917597fe2f64aeb">glfwCreateWindow</a>(640, 480, <span class="stringliteral">&quot;My Title&quot;</span>, NULL, NULL);</div>
130<div class="line"><span class="keywordflow">if</span> (!window)</div>
131<div class="line">{</div>
132<div class="line"> <span class="comment">// Window or OpenGL context creation failed</span></div>
133<div class="line">}</div>
134<div class="ttc" id="agroup__window_html_ga3555a418df92ad53f917597fe2f64aeb"><div class="ttname"><a href="group__window.html#ga3555a418df92ad53f917597fe2f64aeb">glfwCreateWindow</a></div><div class="ttdeci">GLFWwindow * glfwCreateWindow(int width, int height, const char *title, GLFWmonitor *monitor, GLFWwindow *share)</div><div class="ttdoc">Creates a window and its associated context.</div></div>
135<div class="ttc" id="agroup__window_html_ga3c96d80d363e67d13a41b5d1821f3242"><div class="ttname"><a href="group__window.html#ga3c96d80d363e67d13a41b5d1821f3242">GLFWwindow</a></div><div class="ttdeci">struct GLFWwindow GLFWwindow</div><div class="ttdoc">Opaque window object.</div><div class="ttdef"><b>Definition:</b> glfw3.h:1186</div></div>
136</div><!-- fragment --><p >This creates a 640 by 480 windowed mode window with an OpenGL context. If window or OpenGL context creation fails, <code>NULL</code> will be returned. You should always check the return value. While window creation rarely fails, context creation depends on properly installed drivers and may fail even on machines with the necessary hardware.</p>
137<p >By default, the OpenGL context GLFW creates may have any version. You can require a minimum OpenGL version by setting the <code>GLFW_CONTEXT_VERSION_MAJOR</code> and <code>GLFW_CONTEXT_VERSION_MINOR</code> hints <em>before</em> creation. If the required minimum version is not supported on the machine, context (and window) creation fails.</p>
138<div class="fragment"><div class="line"><a class="code hl_function" href="group__window.html#ga7d9c8c62384b1e2821c4dc48952d2033">glfwWindowHint</a>(<a class="code hl_define" href="group__window.html#gafe5e4922de1f9932d7e9849bb053b0c0">GLFW_CONTEXT_VERSION_MAJOR</a>, 2);</div>
139<div class="line"><a class="code hl_function" href="group__window.html#ga7d9c8c62384b1e2821c4dc48952d2033">glfwWindowHint</a>(<a class="code hl_define" href="group__window.html#ga31aca791e4b538c4e4a771eb95cc2d07">GLFW_CONTEXT_VERSION_MINOR</a>, 0);</div>
140<div class="line"><a class="code hl_typedef" href="group__window.html#ga3c96d80d363e67d13a41b5d1821f3242">GLFWwindow</a>* window = <a class="code hl_function" href="group__window.html#ga3555a418df92ad53f917597fe2f64aeb">glfwCreateWindow</a>(640, 480, <span class="stringliteral">&quot;My Title&quot;</span>, NULL, NULL);</div>
141<div class="line"><span class="keywordflow">if</span> (!window)</div>
142<div class="line">{</div>
143<div class="line"> <span class="comment">// Window or context creation failed</span></div>
144<div class="line">}</div>
145<div class="ttc" id="agroup__window_html_ga31aca791e4b538c4e4a771eb95cc2d07"><div class="ttname"><a href="group__window.html#ga31aca791e4b538c4e4a771eb95cc2d07">GLFW_CONTEXT_VERSION_MINOR</a></div><div class="ttdeci">#define GLFW_CONTEXT_VERSION_MINOR</div><div class="ttdoc">Context client API minor version hint and attribute.</div><div class="ttdef"><b>Definition:</b> glfw3.h:962</div></div>
146<div class="ttc" id="agroup__window_html_ga7d9c8c62384b1e2821c4dc48952d2033"><div class="ttname"><a href="group__window.html#ga7d9c8c62384b1e2821c4dc48952d2033">glfwWindowHint</a></div><div class="ttdeci">void glfwWindowHint(int hint, int value)</div><div class="ttdoc">Sets the specified window hint to the desired value.</div></div>
147<div class="ttc" id="agroup__window_html_gafe5e4922de1f9932d7e9849bb053b0c0"><div class="ttname"><a href="group__window.html#gafe5e4922de1f9932d7e9849bb053b0c0">GLFW_CONTEXT_VERSION_MAJOR</a></div><div class="ttdeci">#define GLFW_CONTEXT_VERSION_MAJOR</div><div class="ttdoc">Context client API major version hint and attribute.</div><div class="ttdef"><b>Definition:</b> glfw3.h:956</div></div>
148</div><!-- fragment --><p >The window handle is passed to all window related functions and is provided to along to all window related callbacks, so they can tell which window received the event.</p>
149<p >When a window and context is no longer needed, destroy it.</p>
150<div class="fragment"><div class="line"><a class="code hl_function" href="group__window.html#gacdf43e51376051d2c091662e9fe3d7b2">glfwDestroyWindow</a>(window);</div>
151<div class="ttc" id="agroup__window_html_gacdf43e51376051d2c091662e9fe3d7b2"><div class="ttname"><a href="group__window.html#gacdf43e51376051d2c091662e9fe3d7b2">glfwDestroyWindow</a></div><div class="ttdeci">void glfwDestroyWindow(GLFWwindow *window)</div><div class="ttdoc">Destroys the specified window and its context.</div></div>
152</div><!-- fragment --><p >Once this function is called, no more events will be delivered for that window and its handle becomes invalid.</p>
153<h2><a class="anchor" id="quick_context_current"></a>
154Making the OpenGL context current</h2>
155<p >Before you can use the OpenGL API, you must have a current OpenGL context.</p>
156<div class="fragment"><div class="line"><a class="code hl_function" href="group__context.html#ga1c04dc242268f827290fe40aa1c91157">glfwMakeContextCurrent</a>(window);</div>
157<div class="ttc" id="agroup__context_html_ga1c04dc242268f827290fe40aa1c91157"><div class="ttname"><a href="group__context.html#ga1c04dc242268f827290fe40aa1c91157">glfwMakeContextCurrent</a></div><div class="ttdeci">void glfwMakeContextCurrent(GLFWwindow *window)</div><div class="ttdoc">Makes the context of the specified window current for the calling thread.</div></div>
158</div><!-- fragment --><p >The context will remain current until you make another context current or until the window owning the current context is destroyed.</p>
159<p >If you are using an <a class="el" href="context_guide.html#context_glext_auto">extension loader library</a> to access modern OpenGL then this is when to initialize it, as the loader needs a current context to load from. This example uses <a href="https://github.com/Dav1dde/glad">glad</a>, but the same rule applies to all such libraries.</p>
160<div class="fragment"><div class="line">gladLoadGL(<a class="code hl_function" href="group__context.html#ga35f1837e6f666781842483937612f163">glfwGetProcAddress</a>);</div>
161<div class="ttc" id="agroup__context_html_ga35f1837e6f666781842483937612f163"><div class="ttname"><a href="group__context.html#ga35f1837e6f666781842483937612f163">glfwGetProcAddress</a></div><div class="ttdeci">GLFWglproc glfwGetProcAddress(const char *procname)</div><div class="ttdoc">Returns the address of the specified function for the current context.</div></div>
162</div><!-- fragment --><h2><a class="anchor" id="quick_window_close"></a>
163Checking the window close flag</h2>
164<p >Each window has a flag indicating whether the window should be closed.</p>
165<p >When the user attempts to close the window, either by pressing the close widget in the title bar or using a key combination like Alt+F4, this flag is set to 1. Note that <b>the window isn't actually closed</b>, so you are expected to monitor this flag and either destroy the window or give some kind of feedback to the user.</p>
166<div class="fragment"><div class="line"><span class="keywordflow">while</span> (!<a class="code hl_function" href="group__window.html#ga24e02fbfefbb81fc45320989f8140ab5">glfwWindowShouldClose</a>(window))</div>
167<div class="line">{</div>
168<div class="line"> <span class="comment">// Keep running</span></div>
169<div class="line">}</div>
170<div class="ttc" id="agroup__window_html_ga24e02fbfefbb81fc45320989f8140ab5"><div class="ttname"><a href="group__window.html#ga24e02fbfefbb81fc45320989f8140ab5">glfwWindowShouldClose</a></div><div class="ttdeci">int glfwWindowShouldClose(GLFWwindow *window)</div><div class="ttdoc">Checks the close flag of the specified window.</div></div>
171</div><!-- fragment --><p >You can be notified when the user is attempting to close the window by setting a close callback with <a class="el" href="group__window.html#gada646d775a7776a95ac000cfc1885331">glfwSetWindowCloseCallback</a>. The callback will be called immediately after the close flag has been set.</p>
172<p >You can also set it yourself with <a class="el" href="group__window.html#ga49c449dde2a6f87d996f4daaa09d6708">glfwSetWindowShouldClose</a>. This can be useful if you want to interpret other kinds of input as closing the window, like for example pressing the <em>Escape</em> key.</p>
173<h2><a class="anchor" id="quick_key_input"></a>
174Receiving input events</h2>
175<p >Each window has a large number of callbacks that can be set to receive all the various kinds of events. To receive key press and release events, create a key callback function.</p>
176<div class="fragment"><div class="line"><span class="keyword">static</span> <span class="keywordtype">void</span> key_callback(<a class="code hl_typedef" href="group__window.html#ga3c96d80d363e67d13a41b5d1821f3242">GLFWwindow</a>* window, <span class="keywordtype">int</span> key, <span class="keywordtype">int</span> scancode, <span class="keywordtype">int</span> action, <span class="keywordtype">int</span> mods)</div>
177<div class="line">{</div>
178<div class="line"> <span class="keywordflow">if</span> (key == <a class="code hl_define" href="group__keys.html#gaac6596c350b635c245113b81c2123b93">GLFW_KEY_ESCAPE</a> &amp;&amp; action == <a class="code hl_define" href="group__input.html#ga2485743d0b59df3791c45951c4195265">GLFW_PRESS</a>)</div>
179<div class="line"> <a class="code hl_function" href="group__window.html#ga49c449dde2a6f87d996f4daaa09d6708">glfwSetWindowShouldClose</a>(window, <a class="code hl_define" href="group__init.html#ga2744fbb29b5631bb28802dbe0cf36eba">GLFW_TRUE</a>);</div>
180<div class="line">}</div>
181<div class="ttc" id="agroup__init_html_ga2744fbb29b5631bb28802dbe0cf36eba"><div class="ttname"><a href="group__init.html#ga2744fbb29b5631bb28802dbe0cf36eba">GLFW_TRUE</a></div><div class="ttdeci">#define GLFW_TRUE</div><div class="ttdoc">One.</div><div class="ttdef"><b>Definition:</b> glfw3.h:313</div></div>
182<div class="ttc" id="agroup__input_html_ga2485743d0b59df3791c45951c4195265"><div class="ttname"><a href="group__input.html#ga2485743d0b59df3791c45951c4195265">GLFW_PRESS</a></div><div class="ttdeci">#define GLFW_PRESS</div><div class="ttdoc">The key or mouse button was pressed.</div><div class="ttdef"><b>Definition:</b> glfw3.h:339</div></div>
183<div class="ttc" id="agroup__keys_html_gaac6596c350b635c245113b81c2123b93"><div class="ttname"><a href="group__keys.html#gaac6596c350b635c245113b81c2123b93">GLFW_KEY_ESCAPE</a></div><div class="ttdeci">#define GLFW_KEY_ESCAPE</div><div class="ttdef"><b>Definition:</b> glfw3.h:447</div></div>
184<div class="ttc" id="agroup__window_html_ga49c449dde2a6f87d996f4daaa09d6708"><div class="ttname"><a href="group__window.html#ga49c449dde2a6f87d996f4daaa09d6708">glfwSetWindowShouldClose</a></div><div class="ttdeci">void glfwSetWindowShouldClose(GLFWwindow *window, int value)</div><div class="ttdoc">Sets the close flag of the specified window.</div></div>
185</div><!-- fragment --><p >The key callback, like other window related callbacks, are set per-window.</p>
186<div class="fragment"><div class="line"><a class="code hl_function" href="group__input.html#ga1caf18159767e761185e49a3be019f8d">glfwSetKeyCallback</a>(window, key_callback);</div>
187<div class="ttc" id="agroup__input_html_ga1caf18159767e761185e49a3be019f8d"><div class="ttname"><a href="group__input.html#ga1caf18159767e761185e49a3be019f8d">glfwSetKeyCallback</a></div><div class="ttdeci">GLFWkeyfun glfwSetKeyCallback(GLFWwindow *window, GLFWkeyfun callback)</div><div class="ttdoc">Sets the key callback.</div></div>
188</div><!-- fragment --><p >In order for event callbacks to be called when events occur, you need to process events as described below.</p>
189<h2><a class="anchor" id="quick_render"></a>
190Rendering with OpenGL</h2>
191<p >Once you have a current OpenGL context, you can use OpenGL normally. In this tutorial, a multi-colored rotating triangle will be rendered. The framebuffer size needs to be retrieved for <code>glViewport</code>.</p>
192<div class="fragment"><div class="line"><span class="keywordtype">int</span> width, height;</div>
193<div class="line"><a class="code hl_function" href="group__window.html#ga0e2637a4161afb283f5300c7f94785c9">glfwGetFramebufferSize</a>(window, &amp;width, &amp;height);</div>
194<div class="line">glViewport(0, 0, width, height);</div>
195<div class="ttc" id="agroup__window_html_ga0e2637a4161afb283f5300c7f94785c9"><div class="ttname"><a href="group__window.html#ga0e2637a4161afb283f5300c7f94785c9">glfwGetFramebufferSize</a></div><div class="ttdeci">void glfwGetFramebufferSize(GLFWwindow *window, int *width, int *height)</div><div class="ttdoc">Retrieves the size of the framebuffer of the specified window.</div></div>
196</div><!-- fragment --><p >You can also set a framebuffer size callback using <a class="el" href="group__window.html#gab3fb7c3366577daef18c0023e2a8591f">glfwSetFramebufferSizeCallback</a> and be notified when the size changes.</p>
197<p >The details of how to render with OpenGL is outside the scope of this tutorial, but there are many excellent resources for learning modern OpenGL. Here are a few of them:</p>
198<ul>
199<li><a href="https://antongerdelan.net/opengl/">Anton's OpenGL 4 Tutorials</a></li>
200<li><a href="https://learnopengl.com/">Learn OpenGL</a></li>
201<li><a href="https://open.gl/">Open.GL</a></li>
202</ul>
203<p >These all happen to use GLFW, but OpenGL itself works the same whatever API you use to create the window and context.</p>
204<h2><a class="anchor" id="quick_timer"></a>
205Reading the timer</h2>
206<p >To create smooth animation, a time source is needed. GLFW provides a timer that returns the number of seconds since initialization. The time source used is the most accurate on each platform and generally has micro- or nanosecond resolution.</p>
207<div class="fragment"><div class="line"><span class="keywordtype">double</span> time = <a class="code hl_function" href="group__input.html#gaa6cf4e7a77158a3b8fd00328b1720a4a">glfwGetTime</a>();</div>
208<div class="ttc" id="agroup__input_html_gaa6cf4e7a77158a3b8fd00328b1720a4a"><div class="ttname"><a href="group__input.html#gaa6cf4e7a77158a3b8fd00328b1720a4a">glfwGetTime</a></div><div class="ttdeci">double glfwGetTime(void)</div><div class="ttdoc">Returns the GLFW time.</div></div>
209</div><!-- fragment --><h2><a class="anchor" id="quick_swap_buffers"></a>
210Swapping buffers</h2>
211<p >GLFW windows by default use double buffering. That means that each window has two rendering buffers; a front buffer and a back buffer. The front buffer is the one being displayed and the back buffer the one you render to.</p>
212<p >When the entire frame has been rendered, the buffers need to be swapped with one another, so the back buffer becomes the front buffer and vice versa.</p>
213<div class="fragment"><div class="line"><a class="code hl_function" href="group__window.html#ga15a5a1ee5b3c2ca6b15ca209a12efd14">glfwSwapBuffers</a>(window);</div>
214<div class="ttc" id="agroup__window_html_ga15a5a1ee5b3c2ca6b15ca209a12efd14"><div class="ttname"><a href="group__window.html#ga15a5a1ee5b3c2ca6b15ca209a12efd14">glfwSwapBuffers</a></div><div class="ttdeci">void glfwSwapBuffers(GLFWwindow *window)</div><div class="ttdoc">Swaps the front and back buffers of the specified window.</div></div>
215</div><!-- fragment --><p >The swap interval indicates how many frames to wait until swapping the buffers, commonly known as <em>vsync</em>. By default, the swap interval is zero, meaning buffer swapping will occur immediately. On fast machines, many of those frames will never be seen, as the screen is still only updated typically 60-75 times per second, so this wastes a lot of CPU and GPU cycles.</p>
216<p >Also, because the buffers will be swapped in the middle the screen update, leading to <a href="https://en.wikipedia.org/wiki/Screen_tearing">screen tearing</a>.</p>
217<p >For these reasons, applications will typically want to set the swap interval to one. It can be set to higher values, but this is usually not recommended, because of the input latency it leads to.</p>
218<div class="fragment"><div class="line"><a class="code hl_function" href="group__context.html#ga6d4e0cdf151b5e579bd67f13202994ed">glfwSwapInterval</a>(1);</div>
219<div class="ttc" id="agroup__context_html_ga6d4e0cdf151b5e579bd67f13202994ed"><div class="ttname"><a href="group__context.html#ga6d4e0cdf151b5e579bd67f13202994ed">glfwSwapInterval</a></div><div class="ttdeci">void glfwSwapInterval(int interval)</div><div class="ttdoc">Sets the swap interval for the current context.</div></div>
220</div><!-- fragment --><p >This function acts on the current context and will fail unless a context is current.</p>
221<h2><a class="anchor" id="quick_process_events"></a>
222Processing events</h2>
223<p >GLFW needs to communicate regularly with the window system both in order to receive events and to show that the application hasn't locked up. Event processing must be done regularly while you have visible windows and is normally done each frame after buffer swapping.</p>
224<p >There are two methods for processing pending events; polling and waiting. This example will use event polling, which processes only those events that have already been received and then returns immediately.</p>
225<div class="fragment"><div class="line"><a class="code hl_function" href="group__window.html#ga37bd57223967b4211d60ca1a0bf3c832">glfwPollEvents</a>();</div>
226<div class="ttc" id="agroup__window_html_ga37bd57223967b4211d60ca1a0bf3c832"><div class="ttname"><a href="group__window.html#ga37bd57223967b4211d60ca1a0bf3c832">glfwPollEvents</a></div><div class="ttdeci">void glfwPollEvents(void)</div><div class="ttdoc">Processes all pending events.</div></div>
227</div><!-- fragment --><p >This is the best choice when rendering continually, like most games do. If instead you only need to update your rendering once you have received new input, <a class="el" href="group__window.html#ga554e37d781f0a997656c26b2c56c835e">glfwWaitEvents</a> is a better choice. It waits until at least one event has been received, putting the thread to sleep in the meantime, and then processes all received events. This saves a great deal of CPU cycles and is useful for, for example, many kinds of editing tools.</p>
228<h1><a class="anchor" id="quick_example"></a>
229Putting it together</h1>
230<p >Now that you know how to initialize GLFW, create a window and poll for keyboard input, it's possible to create a simple program.</p>
231<p >This program creates a 640 by 480 windowed mode window and starts a loop that clears the screen, renders a triangle and processes events until the user either presses <em>Escape</em> or closes the window.</p>
232<div class="fragment"><div class="line"> </div>
233<div class="line"><span class="preprocessor">#include &lt;glad/gl.h&gt;</span></div>
234<div class="line"><span class="preprocessor">#define GLFW_INCLUDE_NONE</span></div>
235<div class="line"><span class="preprocessor">#include &lt;<a class="code" href="glfw3_8h.html">GLFW/glfw3.h</a>&gt;</span></div>
236<div class="line"> </div>
237<div class="line"><span class="preprocessor">#include &quot;linmath.h&quot;</span></div>
238<div class="line"> </div>
239<div class="line"><span class="preprocessor">#include &lt;stdlib.h&gt;</span></div>
240<div class="line"><span class="preprocessor">#include &lt;stdio.h&gt;</span></div>
241<div class="line"> </div>
242<div class="line"><span class="keyword">static</span> <span class="keyword">const</span> <span class="keyword">struct</span></div>
243<div class="line">{</div>
244<div class="line"> <span class="keywordtype">float</span> x, y;</div>
245<div class="line"> <span class="keywordtype">float</span> r, g, b;</div>
246<div class="line">} vertices[3] =</div>
247<div class="line">{</div>
248<div class="line"> { -0.6f, -0.4f, 1.f, 0.f, 0.f },</div>
249<div class="line"> { 0.6f, -0.4f, 0.f, 1.f, 0.f },</div>
250<div class="line"> { 0.f, 0.6f, 0.f, 0.f, 1.f }</div>
251<div class="line">};</div>
252<div class="line"> </div>
253<div class="line"><span class="keyword">static</span> <span class="keyword">const</span> <span class="keywordtype">char</span>* vertex_shader_text =</div>
254<div class="line"><span class="stringliteral">&quot;#version 110\n&quot;</span></div>
255<div class="line"><span class="stringliteral">&quot;uniform mat4 MVP;\n&quot;</span></div>
256<div class="line"><span class="stringliteral">&quot;attribute vec3 vCol;\n&quot;</span></div>
257<div class="line"><span class="stringliteral">&quot;attribute vec2 vPos;\n&quot;</span></div>
258<div class="line"><span class="stringliteral">&quot;varying vec3 color;\n&quot;</span></div>
259<div class="line"><span class="stringliteral">&quot;void main()\n&quot;</span></div>
260<div class="line"><span class="stringliteral">&quot;{\n&quot;</span></div>
261<div class="line"><span class="stringliteral">&quot; gl_Position = MVP * vec4(vPos, 0.0, 1.0);\n&quot;</span></div>
262<div class="line"><span class="stringliteral">&quot; color = vCol;\n&quot;</span></div>
263<div class="line"><span class="stringliteral">&quot;}\n&quot;</span>;</div>
264<div class="line"> </div>
265<div class="line"><span class="keyword">static</span> <span class="keyword">const</span> <span class="keywordtype">char</span>* fragment_shader_text =</div>
266<div class="line"><span class="stringliteral">&quot;#version 110\n&quot;</span></div>
267<div class="line"><span class="stringliteral">&quot;varying vec3 color;\n&quot;</span></div>
268<div class="line"><span class="stringliteral">&quot;void main()\n&quot;</span></div>
269<div class="line"><span class="stringliteral">&quot;{\n&quot;</span></div>
270<div class="line"><span class="stringliteral">&quot; gl_FragColor = vec4(color, 1.0);\n&quot;</span></div>
271<div class="line"><span class="stringliteral">&quot;}\n&quot;</span>;</div>
272<div class="line"> </div>
273<div class="line"><span class="keyword">static</span> <span class="keywordtype">void</span> error_callback(<span class="keywordtype">int</span> error, <span class="keyword">const</span> <span class="keywordtype">char</span>* description)</div>
274<div class="line">{</div>
275<div class="line"> fprintf(stderr, <span class="stringliteral">&quot;Error: %s\n&quot;</span>, description);</div>
276<div class="line">}</div>
277<div class="line"> </div>
278<div class="line"><span class="keyword">static</span> <span class="keywordtype">void</span> key_callback(<a class="code hl_typedef" href="group__window.html#ga3c96d80d363e67d13a41b5d1821f3242">GLFWwindow</a>* window, <span class="keywordtype">int</span> key, <span class="keywordtype">int</span> scancode, <span class="keywordtype">int</span> action, <span class="keywordtype">int</span> mods)</div>
279<div class="line">{</div>
280<div class="line"> <span class="keywordflow">if</span> (key == <a class="code hl_define" href="group__keys.html#gaac6596c350b635c245113b81c2123b93">GLFW_KEY_ESCAPE</a> &amp;&amp; action == <a class="code hl_define" href="group__input.html#ga2485743d0b59df3791c45951c4195265">GLFW_PRESS</a>)</div>
281<div class="line"> <a class="code hl_function" href="group__window.html#ga49c449dde2a6f87d996f4daaa09d6708">glfwSetWindowShouldClose</a>(window, <a class="code hl_define" href="group__init.html#ga2744fbb29b5631bb28802dbe0cf36eba">GLFW_TRUE</a>);</div>
282<div class="line">}</div>
283<div class="line"> </div>
284<div class="line"><span class="keywordtype">int</span> main(<span class="keywordtype">void</span>)</div>
285<div class="line">{</div>
286<div class="line"> <a class="code hl_typedef" href="group__window.html#ga3c96d80d363e67d13a41b5d1821f3242">GLFWwindow</a>* window;</div>
287<div class="line"> GLuint vertex_buffer, vertex_shader, fragment_shader, program;</div>
288<div class="line"> GLint mvp_location, vpos_location, vcol_location;</div>
289<div class="line"> </div>
290<div class="line"> <a class="code hl_function" href="group__init.html#gaff45816610d53f0b83656092a4034f40">glfwSetErrorCallback</a>(error_callback);</div>
291<div class="line"> </div>
292<div class="line"> <span class="keywordflow">if</span> (!<a class="code hl_function" href="group__init.html#ga317aac130a235ab08c6db0834907d85e">glfwInit</a>())</div>
293<div class="line"> exit(EXIT_FAILURE);</div>
294<div class="line"> </div>
295<div class="line"> <a class="code hl_function" href="group__window.html#ga7d9c8c62384b1e2821c4dc48952d2033">glfwWindowHint</a>(<a class="code hl_define" href="group__window.html#gafe5e4922de1f9932d7e9849bb053b0c0">GLFW_CONTEXT_VERSION_MAJOR</a>, 2);</div>
296<div class="line"> <a class="code hl_function" href="group__window.html#ga7d9c8c62384b1e2821c4dc48952d2033">glfwWindowHint</a>(<a class="code hl_define" href="group__window.html#ga31aca791e4b538c4e4a771eb95cc2d07">GLFW_CONTEXT_VERSION_MINOR</a>, 0);</div>
297<div class="line"> </div>
298<div class="line"> window = <a class="code hl_function" href="group__window.html#ga3555a418df92ad53f917597fe2f64aeb">glfwCreateWindow</a>(640, 480, <span class="stringliteral">&quot;Simple example&quot;</span>, NULL, NULL);</div>
299<div class="line"> <span class="keywordflow">if</span> (!window)</div>
300<div class="line"> {</div>
301<div class="line"> <a class="code hl_function" href="group__init.html#gaaae48c0a18607ea4a4ba951d939f0901">glfwTerminate</a>();</div>
302<div class="line"> exit(EXIT_FAILURE);</div>
303<div class="line"> }</div>
304<div class="line"> </div>
305<div class="line"> <a class="code hl_function" href="group__input.html#ga1caf18159767e761185e49a3be019f8d">glfwSetKeyCallback</a>(window, key_callback);</div>
306<div class="line"> </div>
307<div class="line"> <a class="code hl_function" href="group__context.html#ga1c04dc242268f827290fe40aa1c91157">glfwMakeContextCurrent</a>(window);</div>
308<div class="line"> gladLoadGL(<a class="code hl_function" href="group__context.html#ga35f1837e6f666781842483937612f163">glfwGetProcAddress</a>);</div>
309<div class="line"> <a class="code hl_function" href="group__context.html#ga6d4e0cdf151b5e579bd67f13202994ed">glfwSwapInterval</a>(1);</div>
310<div class="line"> </div>
311<div class="line"> <span class="comment">// NOTE: OpenGL error checks have been omitted for brevity</span></div>
312<div class="line"> </div>
313<div class="line"> glGenBuffers(1, &amp;vertex_buffer);</div>
314<div class="line"> glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer);</div>
315<div class="line"> glBufferData(GL_ARRAY_BUFFER, <span class="keyword">sizeof</span>(vertices), vertices, GL_STATIC_DRAW);</div>
316<div class="line"> </div>
317<div class="line"> vertex_shader = glCreateShader(GL_VERTEX_SHADER);</div>
318<div class="line"> glShaderSource(vertex_shader, 1, &amp;vertex_shader_text, NULL);</div>
319<div class="line"> glCompileShader(vertex_shader);</div>
320<div class="line"> </div>
321<div class="line"> fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);</div>
322<div class="line"> glShaderSource(fragment_shader, 1, &amp;fragment_shader_text, NULL);</div>
323<div class="line"> glCompileShader(fragment_shader);</div>
324<div class="line"> </div>
325<div class="line"> program = glCreateProgram();</div>
326<div class="line"> glAttachShader(program, vertex_shader);</div>
327<div class="line"> glAttachShader(program, fragment_shader);</div>
328<div class="line"> glLinkProgram(program);</div>
329<div class="line"> </div>
330<div class="line"> mvp_location = glGetUniformLocation(program, <span class="stringliteral">&quot;MVP&quot;</span>);</div>
331<div class="line"> vpos_location = glGetAttribLocation(program, <span class="stringliteral">&quot;vPos&quot;</span>);</div>
332<div class="line"> vcol_location = glGetAttribLocation(program, <span class="stringliteral">&quot;vCol&quot;</span>);</div>
333<div class="line"> </div>
334<div class="line"> glEnableVertexAttribArray(vpos_location);</div>
335<div class="line"> glVertexAttribPointer(vpos_location, 2, GL_FLOAT, GL_FALSE,</div>
336<div class="line"> <span class="keyword">sizeof</span>(vertices[0]), (<span class="keywordtype">void</span>*) 0);</div>
337<div class="line"> glEnableVertexAttribArray(vcol_location);</div>
338<div class="line"> glVertexAttribPointer(vcol_location, 3, GL_FLOAT, GL_FALSE,</div>
339<div class="line"> <span class="keyword">sizeof</span>(vertices[0]), (<span class="keywordtype">void</span>*) (<span class="keyword">sizeof</span>(<span class="keywordtype">float</span>) * 2));</div>
340<div class="line"> </div>
341<div class="line"> <span class="keywordflow">while</span> (!<a class="code hl_function" href="group__window.html#ga24e02fbfefbb81fc45320989f8140ab5">glfwWindowShouldClose</a>(window))</div>
342<div class="line"> {</div>
343<div class="line"> <span class="keywordtype">float</span> ratio;</div>
344<div class="line"> <span class="keywordtype">int</span> width, height;</div>
345<div class="line"> mat4x4 m, p, mvp;</div>
346<div class="line"> </div>
347<div class="line"> <a class="code hl_function" href="group__window.html#ga0e2637a4161afb283f5300c7f94785c9">glfwGetFramebufferSize</a>(window, &amp;width, &amp;height);</div>
348<div class="line"> ratio = width / (float) height;</div>
349<div class="line"> </div>
350<div class="line"> glViewport(0, 0, width, height);</div>
351<div class="line"> glClear(GL_COLOR_BUFFER_BIT);</div>
352<div class="line"> </div>
353<div class="line"> mat4x4_identity(m);</div>
354<div class="line"> mat4x4_rotate_Z(m, m, (<span class="keywordtype">float</span>) <a class="code hl_function" href="group__input.html#gaa6cf4e7a77158a3b8fd00328b1720a4a">glfwGetTime</a>());</div>
355<div class="line"> mat4x4_ortho(p, -ratio, ratio, -1.f, 1.f, 1.f, -1.f);</div>
356<div class="line"> mat4x4_mul(mvp, p, m);</div>
357<div class="line"> </div>
358<div class="line"> glUseProgram(program);</div>
359<div class="line"> glUniformMatrix4fv(mvp_location, 1, GL_FALSE, (<span class="keyword">const</span> GLfloat*) mvp);</div>
360<div class="line"> glDrawArrays(GL_TRIANGLES, 0, 3);</div>
361<div class="line"> </div>
362<div class="line"> <a class="code hl_function" href="group__window.html#ga15a5a1ee5b3c2ca6b15ca209a12efd14">glfwSwapBuffers</a>(window);</div>
363<div class="line"> <a class="code hl_function" href="group__window.html#ga37bd57223967b4211d60ca1a0bf3c832">glfwPollEvents</a>();</div>
364<div class="line"> }</div>
365<div class="line"> </div>
366<div class="line"> <a class="code hl_function" href="group__window.html#gacdf43e51376051d2c091662e9fe3d7b2">glfwDestroyWindow</a>(window);</div>
367<div class="line"> </div>
368<div class="line"> <a class="code hl_function" href="group__init.html#gaaae48c0a18607ea4a4ba951d939f0901">glfwTerminate</a>();</div>
369<div class="line"> exit(EXIT_SUCCESS);</div>
370<div class="line">}</div>
371<div class="line"> </div>
372</div><!-- fragment --><p >The program above can be found in the <a href="https://www.glfw.org/download.html">source package</a> as <code>examples/simple.c</code> and is compiled along with all other examples when you build GLFW. If you built GLFW from the source package then you already have this as <code>simple.exe</code> on Windows, <code>simple</code> on Linux or <code>simple.app</code> on macOS.</p>
373<p >This tutorial used only a few of the many functions GLFW provides. There are guides for each of the areas covered by GLFW. Each guide will introduce all the functions for that category.</p>
374<ul>
375<li><a class="el" href="intro_guide.html">Introduction to the API</a></li>
376<li><a class="el" href="window_guide.html">Window guide</a></li>
377<li><a class="el" href="context_guide.html">Context guide</a></li>
378<li><a class="el" href="monitor_guide.html">Monitor guide</a></li>
379<li><a class="el" href="input_guide.html">Input guide</a></li>
380</ul>
381<p >You can access reference documentation for any GLFW function by clicking it and the reference for each function links to related functions and guide sections.</p>
382<p >The tutorial ends here. Once you have written a program that uses GLFW, you will need to compile and link it. How to do that depends on the development environment you are using and is best explained by the documentation for that environment. To learn about the details that are specific to GLFW, see <a class="el" href="build_guide.html">Building applications</a>. </p>
383</div></div><!-- contents -->
384</div><!-- PageDoc -->
385<address class="footer">
386<p>
387Last update on Thu Oct 28 2021 for GLFW 3.3.5
388</p>
389</address>
390</body>
391</html>