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
11 While this codelab currently targets conventional Native Client using our
12 GCC + GlibC based toolchain, the techniques involved are generally
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>
23 var tag
= document
.getElementById('cpp_compat');
24 var tag2
= document
.getElementById('cpp_compat2');
25 if (!('application/x-nacl' in navigator
.mimeTypes
)) {
27 'This codelab does not appear to be supported by your browser.';
28 tag
.style
.color
= '#cc0000';
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) {
35 'This codelab does not appear to be supported by your browser.';
36 tag
.style
.color
= '#cc0000';
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.';
43 tag
.innerHTML
= 'This codelab appears to be supported by your browser.';
44 tag
.style
.color
= '#00cc00';
47 <li>A fast broadband connection (
500MB download)
</li>
48 <li>Can read and write C++
</li>
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
“Install
” to add the NaCl Dev Environment
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">
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
’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
’s
84 virtual filesystem.
</p>
85 <p>There are
3 locations mounted by the environment:
</p>
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>
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>
102 <div><ul class=
"small-gap">
103 <li><span id=
"copy_key">Loading...
</span>
105 var tag
= document
.getElementById('copy_key');
106 if (navigator
.appVersion
.indexOf('Mac') >= 0) {
107 tag
.innerHTML
= '⌘-C';
109 tag
.innerHTML
= 'Ctrl-Shift-C';
111 </script> to Copy
</li>
112 <li><span id=
"paste_key">Loading...
</span>
114 var tag
= document
.getElementById('paste_key');
115 if (navigator
.appVersion
.indexOf('Mac') >= 0) {
116 tag
.innerHTML
= '⌘-V';
118 tag
.innerHTML
= 'Ctrl-Shift-V';
120 </script> to Paste
</li>
123 <h4 id=
"editing">Editing
</h4>
124 <p>To follow along in this codelab, you
’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
129 If you
’re unsure what to pick, nano is simpler to start with and has on-screen
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
<filename
>
136 <p>Here
’s an online
<a class=
"reference external" href=
"http://mintaka.sdsu.edu/reu/nano.html">nano tutorial
</a>.
</p>
138 <li><p class=
"first">You can open
<strong>vim
</strong> like this:
</p>
139 <pre class=
"prettyprint">
140 $ vim
<filename
>
142 <p>Here
’s an online
<a class=
"reference external" href=
"http://www.openvim.com/tutorial.html">vim tutorial
</a>.
</p>
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>
152 <pre class=
"prettyprint">
153 git config --global user.name
"John Doe
"
154 git config --global user.email johndoe
@example.com
156 <h2 id=
"get-the-code">Get the Code!
</h2>
157 <p>Rather than start from nothing, for this codelab we
’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
164 <pre class=
"prettyprint">
165 unzip cds2014_cpp.zip
167 <p>Go into the codelab directory:
</p>
168 <pre class=
"prettyprint">
171 <p>Create a new local git repo:
</p>
172 <pre class=
"prettyprint">
175 <p>Add everything:
</p>
176 <pre class=
"prettyprint">
180 <pre class=
"prettyprint">
181 git commit -am
"initial
"
183 <p>While working, you can see what you
’ve changed by running:
</p>
184 <pre class=
"prettyprint">
187 <h2 id=
"fire-is-cool-let-s-burn-some-stuff">Fire is cool, let
’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">
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
’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
’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">
208 git commit -am
"adding water
"
210 <p>For this codelab, you
’ll only need to change
<cite>water.cc
</cite>.
</p>
211 <p>The task of turning fire into water involves two key challenges:
</p>
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>
219 <p>At this point you
’ll want to open up
<cite>water.cc
</cite> in the editor you
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
’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">
233 <h3 id=
"what-goes-up">What goes up...
</h3>
234 <p>Now there
’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
“fire stuff
” behind the sweep.
239 You
’ll want to reverse this.
</p>
240 <p>Hint: You
’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
’t much of a waterfall.
244 Move it to the top to complete the effect.
</p>
245 <p>Hint: You
’ll want to change the area that the UpdateCoals function mutates.
</p>
246 <h2 id=
"what-you-ve-learned">What you
’ve learned
</h2>
247 <p>In addition to learning a new appreciation for fire, you
’ve also made water...
248 And while dusting off your C/C++ image manipulation skills,
249 you
’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
’re ready to check out the wealth of
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
’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
>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>
270 {{/partials.standard_nacl_article}}