Update V8 to version 4.7.53.
[chromium-blink-merge.git] / native_client_sdk / doc_generated / cds2014 / cpp.html
bloba676895a5695eaa8392c89ae0f3c43059e81cae7
1 {{+bindTo:partials.standard_nacl_article}}
3 <section id="a-saga-of-fire-and-water-codelab">
4 <span id="cds2014-cpp"></span><h1 id="a-saga-of-fire-and-water-codelab"><span id="cds2014-cpp"></span>A Saga of Fire and Water - Codelab</h1>
5 <h2 id="introduction">Introduction</h2>
6 <p>Learn the basics of using PPAPI to do 2D graphics from
7 a C++ program running in Native Client.
8 Modify our sample to turn fire into water.
9 Develop inside Google Chrome, using our NaCl Development Environment
10 Chrome App.
11 While this codelab currently targets conventional Native Client using our
12 GCC + GlibC based toolchain, the techniques involved are generally
13 applicable.</p>
14 <dl class="docutils">
15 <dt>Requirements:</dt>
16 <dd><ul class="first last small-gap">
17 <li>An x86 (sorry no arm) Desktop / Laptop
18 Windows, Mac, Linux, or ChromeOS browser
19 <br/><span id="cpp_compat" style="color: #cccc00"
20 >Checking browser compatibility...</span><br/>
21 <i id="cpp_compat2"></i>
22 <script>
23 var tag = document.getElementById('cpp_compat');
24 var tag2 = document.getElementById('cpp_compat2');
25 if (!('application/x-nacl' in navigator.mimeTypes)) {
26 tag.innerHTML =
27 'This codelab does not appear to be supported by your browser.';
28 tag.style.color = '#cc0000';
29 tag2.innerHTML =
30 'You do not appear to be running a browser such as ' +
31 'Google Chrome which supports Native Client ' +
32 'or you have disabled Native Client.';
33 } else if (navigator.userAgent.search(' arm') >= 0) {
34 tag.innerHTML =
35 'This codelab does not appear to be supported by your browser.';
36 tag.style.color = '#cc0000';
37 tag2.innerHTML =
38 'You appear to be running on an ARM based CPU. ' +
39 'While Native Client does support ARM, ' +
40 "our developer environment's GCC + GlibC based toolchain " +
41 'does not, at this time. Sorry.';
42 } else {
43 tag.innerHTML = 'This codelab appears to be supported by your browser.';
44 tag.style.color = '#00cc00';
46 </script></li>
47 <li>A fast broadband connection (500MB download)</li>
48 <li>Can read and write C++</li>
49 </ul>
50 </dd>
51 </dl>
52 <h2 id="setup">Setup</h2>
53 <p>For this codelab, you will need to install the our
54 Beta NaCl Dev Environment App in your web browser.</p>
55 <a href="https://chrome.google.com/webstore/detail/nacl-development-environm/aljpgkjeipgnmdpikaajmnepbcfkglfa"
56 target="_blank">Click here to install the environment.</a><p>You will need to click &#8220;Install&#8221; to add the NaCl Dev Environment
57 to your browser.</p>
58 <p>NOTE: This environment will leave a substantial (800MB) payload in your
59 browser. See the cleanup section below to learn how to recover this space.</p>
60 <p>Once the installation is complete, run the app.</p>
61 <p>On the first run, it will download several packages.
62 Once download is complete, you should see a bash prompt:</p>
63 <pre class="prettyprint">
64 bash.nmf-4.3$
65 </pre>
66 <h3 id="our-web-based-tools">Our Web-based Tools</h3>
67 <p>These development tools are a <a class="reference external" href="nacldev">work in progress</a>.
68 At this point, they are a learning tool and demonstration of NaCl&#8217;s
69 flexibility, but are not the recommended tools for a production application.
70 In the future, that may change, but for the moment,
71 to develop a substantial application for Native Client /
72 Portable Native Client,
73 we recommend you use the
74 <a class="reference external" href="/native-client/sdk/download">Native Client SDK</a>.</p>
75 <b><font color="#880000">
76 NOTE: The NaCl Development Environment is not yet stable.
77 Ideally, user data is preserved, but currently it can be lost during updates
78 or sporadically. We're working to resolve this.
79 </font></b><h3 id="navigating-in-the-dev-environment">Navigating in the Dev Environment</h3>
80 <p>The NaCl Dev Environment behaves like a mini-UNIX system.
81 You start-up in bash command prompt.
82 Standard UNIX commands like cd, ls, mkdir, rm, rmdir, etc.
83 can be used to navigate and modify the Dev Environment&#8217;s
84 virtual filesystem.</p>
85 <p>There are 3 locations mounted by the environment:</p>
86 <blockquote>
87 <div><ul class="small-gap">
88 <li>/tmp mounts the HTML5 Filesystem temporary storage area.</li>
89 <li>/mnt/html5 mounts the HTML5 Filesystem persistent storage area.</li>
90 <li>Your home directory ~/, located in /home/user,
91 mounts a portion of the HTML5 Filsystem persistent storage area
92 that corresponds to the /mnt/html5/home directory.</li>
93 </ul>
94 </div></blockquote>
95 <h3 id="clipboard">Clipboard</h3>
96 <p>Many of the steps in this tutorial will be easier to copy and paste
97 into the Dev Environment.
98 To copy and paste in the Dev Environment App,
99 you will need to use the keyboard.</p>
100 <p>On your platform use:</p>
101 <blockquote>
102 <div><ul class="small-gap">
103 <li><span id="copy_key">Loading...</span>
104 <script>
105 var tag = document.getElementById('copy_key');
106 if (navigator.appVersion.indexOf('Mac') >= 0) {
107 tag.innerHTML = '&#8984;-C';
108 } else {
109 tag.innerHTML = 'Ctrl-Shift-C';
111 </script> to Copy</li>
112 <li><span id="paste_key">Loading...</span>
113 <script>
114 var tag = document.getElementById('paste_key');
115 if (navigator.appVersion.indexOf('Mac') >= 0) {
116 tag.innerHTML = '&#8984;-V';
117 } else {
118 tag.innerHTML = 'Ctrl-Shift-V';
120 </script> to Paste</li>
121 </ul>
122 </div></blockquote>
123 <h4 id="editing">Editing</h4>
124 <p>To follow along in this codelab, you&#8217;ll need to use a text editor to modify
125 various files in our development environment.
126 There are currently two editor options, nano or vim.
127 We have an Emacs port, but it is not yet available in the Dev Environment
128 (coming soon).
129 If you&#8217;re unsure what to pick, nano is simpler to start with and has on-screen
130 help.</p>
131 <ul class="small-gap">
132 <li><p class="first">You can open <strong>nano</strong> like this:</p>
133 <pre class="prettyprint">
134 $ nano &lt;filename&gt;
135 </pre>
136 <p>Here&#8217;s an online <a class="reference external" href="http://mintaka.sdsu.edu/reu/nano.html">nano tutorial</a>.</p>
137 </li>
138 <li><p class="first">You can open <strong>vim</strong> like this:</p>
139 <pre class="prettyprint">
140 $ vim &lt;filename&gt;
141 </pre>
142 <p>Here&#8217;s an online <a class="reference external" href="http://www.openvim.com/tutorial.html">vim tutorial</a>.</p>
143 </li>
144 </ul>
145 <h3 id="git-setup">Git Setup</h3>
146 <p>This tutorial also uses a revision control program called
147 <a class="reference external" href="http://en.wikipedia.org/wiki/Git_(software)">git</a>.
148 In order to commit to a git repository,
149 you need to set up your identity.</p>
150 <p>Run these commands (with your info) to setup your <cite>~/.gitconfig</cite>
151 for use:</p>
152 <pre class="prettyprint">
153 git config --global user.name &quot;John Doe&quot;
154 git config --global user.email johndoe&#64;example.com
155 </pre>
156 <h2 id="get-the-code">Get the Code!</h2>
157 <p>Rather than start from nothing, for this codelab we&#8217;ve provided
158 you with a zip file containing a starting point.</p>
159 <p>Download the codelab:</p>
160 <pre class="prettyprint">
161 curl http://nacltools.storage.googleapis.com/cds2014/cds2014_cpp.zip -O
162 </pre>
163 <p>Unzip it:</p>
164 <pre class="prettyprint">
165 unzip cds2014_cpp.zip
166 </pre>
167 <p>Go into the codelab directory:</p>
168 <pre class="prettyprint">
169 cd cds2014_cpp
170 </pre>
171 <p>Create a new local git repo:</p>
172 <pre class="prettyprint">
173 git init
174 </pre>
175 <p>Add everything:</p>
176 <pre class="prettyprint">
177 git add .
178 </pre>
179 <p>Commit it:</p>
180 <pre class="prettyprint">
181 git commit -am &quot;initial&quot;
182 </pre>
183 <p>While working, you can see what you&#8217;ve changed by running:</p>
184 <pre class="prettyprint">
185 git diff
186 </pre>
187 <h2 id="fire-is-cool-let-s-burn-some-stuff">Fire is cool, let&#8217;s burn some stuff...</h2>
188 <p>Indulging your inner child, lets make some virtual fire!
189 Use the following shockingly intuitive incantation:</p>
190 <pre class="prettyprint">
191 make fire
192 </pre>
193 <p>You should now see a small popup window, smoldering away.
194 If you click, you can make more fire!
195 I think that&#8217;s pretty cool, but then I selected
196 the institution of higher learning I attended based
197 on the integral role fire played in its campus life.</p>
198 <h2 id="water">Water</h2>
199 <p>Remarkably, not everyone enjoys the primal illusion of fire.</p>
200 <p>Your task in this codelab is to transform the rising fire
201 effect you see before you into a beautiful, tranquil waterfall.
202 This will require digging into some C++ code.</p>
203 <p>Before you begin, you&#8217;ll want to copy our fire program to a new name,
204 since you might decide later that you like fire better, I know I do:</p>
205 <pre class="prettyprint">
206 cp fire.cc water.cc
207 git add water.cc
208 git commit -am &quot;adding water&quot;
209 </pre>
210 <p>For this codelab, you&#8217;ll only need to change <cite>water.cc</cite>.</p>
211 <p>The task of turning fire into water involves two key challenges:</p>
212 <blockquote>
213 <div><ul class="small-gap">
214 <li>Alter the red-yellow palette of fire into a blue-green one.</li>
215 <li>Reverse upward rising flame into downward falling water.</li>
216 <li>Seed the waterfall from the top instead of the bottom.</li>
217 </ul>
218 </div></blockquote>
219 <p>At this point you&#8217;ll want to open up <cite>water.cc</cite> in the editor you
220 picked earlier.</p>
221 <h3 id="i-see-a-red-door-and-i-want-it-painted-blue">I see a red door and I want it painted... blue</h3>
222 <p>While PPAPI&#8217;s 2D graphics API uses multi-component RGB pixels,
223 our flame effect is actually monochrome. A single intensity
224 value is used in the flame simulation. This is then converted
225 to color based on a multi-color gradient.
226 To alter the color-scheme, locate this palette, and exchange
227 the red component (first) with the blue one (third).</p>
228 <p>Hint: Focus your energies on the CreatePalette function.</p>
229 <p>You can test you changes at any time with:</p>
230 <pre class="prettyprint">
231 make water
232 </pre>
233 <h3 id="what-goes-up">What goes up...</h3>
234 <p>Now there&#8217;s the small matter of gravity.
235 While smoke, and well flame, rises, we want our water to go down.</p>
236 <p>The simulation of fire loops over each pixel,
237 bottom row to top row,
238 diffusing &#8220;fire stuff&#8221; behind the sweep.
239 You&#8217;ll want to reverse this.</p>
240 <p>Hint: You&#8217;ll need to change the y loop direction in the UpdateFlames function.</p>
241 <h3 id="up-high-down-low">Up high, down low</h3>
242 <p>While you can now use the mouse to inject a trickle of water.
243 The small line of blue at the bottom isn&#8217;t much of a waterfall.
244 Move it to the top to complete the effect.</p>
245 <p>Hint: You&#8217;ll want to change the area that the UpdateCoals function mutates.</p>
246 <h2 id="what-you-ve-learned">What you&#8217;ve learned</h2>
247 <p>In addition to learning a new appreciation for fire, you&#8217;ve also made water...
248 And while dusting off your C/C++ image manipulation skills,
249 you&#8217;ve discovered how easy it is to modify, build,
250 and run a NaCl application that uses PPAPI.</p>
251 <p>2D graphics is fun, but now you&#8217;re ready to check out the wealth of
252 other
253 <a class="reference external" href="https://src.chromium.org/viewvc/chrome/trunk/src/ppapi/cpp/">PPAPI interfaces available</a>.</p>
254 <p>While our in-browser environment is rapidly evolving
255 to become a complete development solution,
256 for the broadest range of development options, check out the
257 <a class="reference external" href="https://developer.chrome.com/native-client/cpp-api">NaCl SDK</a>.</p>
258 <p>Send us comments and feedback on the <a class="reference external" href="https://groups.google.com/forum/#!forum/native-client-discuss">native-client-discuss</a> mailing list,
259 or ask questions using Stack Overflow&#8217;s <a class="reference external" href="https://stackoverflow.com/questions/tagged/google-nativeclient">google-nativeclient</a> tag.</p>
260 <p>I hope this codelab has lit a fire in you to go out there,
261 and bring an awesome C/C++ application to NaCl or PNaCl today!</p>
262 <h2 id="cleanup">Cleanup</h2>
263 <p>The Chrome Dev Environment App installs &gt;800MB into
264 HTML5 Filesystem storage on your device.
265 To recover this storage, uninstall the app from the
266 <a href="https://chrome.google.com/webstore"
267 target="_blank">Chrome Web Store</a>.</p>
268 </section>
270 {{/partials.standard_nacl_article}}