OLD | NEW |
(Empty) | |
| 1 {{+bindTo:partials.standard_nacl_article}} |
| 2 |
| 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'; |
| 45 } |
| 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/alj
pgkjeipgnmdpikaajmnepbcfkglfa" |
| 56 target="_blank">Click here to install the environment.</a><p>You will need to cl
ick “Install” 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">wo
rk 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 S
DK</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 Envi
ronment</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> |
| 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 = '⌘-C'; |
| 108 } else { |
| 109 tag.innerHTML = 'Ctrl-Shift-C'; |
| 110 } |
| 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 = '⌘-V'; |
| 117 } else { |
| 118 tag.innerHTML = 'Ctrl-Shift-V'; |
| 119 } |
| 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’ll need to use a text editor to mo
dify |
| 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’re unsure what to pick, nano is simpler to start with and has on-sc
reen |
| 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 <filename> |
| 135 </pre> |
| 136 <p>Here’s an online <a class="reference external" href="http://mintaka.sds
u.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 <filename> |
| 141 </pre> |
| 142 <p>Here’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 setup your environment to with 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 "John Doe" |
| 154 git config --global user.email johndoe@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’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 "initial" |
| 182 </pre> |
| 183 <p>While working, you can see what you’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’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’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"> |
| 206 cp fire.cc water.cc |
| 207 </pre> |
| 208 <p>For this codelab, you’ll only need to change <cite>water.cc</cite>.</p> |
| 209 <p>The task of turning fire into water involves two key challenges:</p> |
| 210 <blockquote> |
| 211 <div><ul class="small-gap"> |
| 212 <li>Alter the red-yellow palette of fire into a blue-green one.</li> |
| 213 <li>Reverse upward rising flame into downward falling water.</li> |
| 214 <li>Seed the waterfall from the top instead of the bottom.</li> |
| 215 </ul> |
| 216 </div></blockquote> |
| 217 <p>At this point you’ll want to open up <cite>water.cc</cite> in the edito
r you |
| 218 picked earlier.</p> |
| 219 <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> |
| 220 <p>While PPAPI’s 2D graphics API uses multi-component RGB pixels, |
| 221 our flame effect is actually monochrome. A single intensity |
| 222 value is used in the flame simulation. This is then converted |
| 223 to color based on a multi-color gradient. |
| 224 To alter the color-scheme, locate this palette, and exchange |
| 225 the red component for blue.</p> |
| 226 <p>Hint: Focus your energies on the CreatePalette function.</p> |
| 227 <p>You can test you changes at any time with:</p> |
| 228 <pre class="prettyprint"> |
| 229 make water |
| 230 </pre> |
| 231 <h3 id="what-goes-up">What goes up...</h3> |
| 232 <p>Now there’s the small matter of gravity. |
| 233 While smoke, and well flame, rises, we want our water to go down.</p> |
| 234 <p>The simulation of fire loops over each pixel, |
| 235 bottom row to top row, |
| 236 diffusing “fire stuff” behind the sweep. |
| 237 You’ll want to reverse this. |
| 238 Note the simulation buffer is stored in <a href="http://en.wikipedia.org/wiki/Ro
w-major_order" |
| 239 target="_blank">Row-major order</a> from bottom to top. |
| 240 Accesses of + width and - width are used to reach rows above and below |
| 241 the current line.</p> |
| 242 <p>Hint: You’ll need to change the y loop direction in the UpdateFlames fu
nction.</p> |
| 243 <h3 id="up-high-down-low">Up high, down low</h3> |
| 244 <p>While you can now use the mouse to inject a trickle of water. |
| 245 The small line of blue at the bottom isn’t much of a waterfall. |
| 246 Move it to the top to complete the effect.</p> |
| 247 <p>Hint: You’ll want to change the area that the UpdateCoals function muta
tes.</p> |
| 248 <h2 id="what-you-ve-learned">What you’ve learned</h2> |
| 249 <p>In addition to learning a new appreciation for fire, you’ve also made w
ater... |
| 250 And while dusting off your C/C++ image manipulation skills, |
| 251 you’ve discovered how easy it is to modify, build, |
| 252 and run a NaCl application that uses PPAPI.</p> |
| 253 <p>2D graphics is fun, but now you’re ready to check out the wealth of |
| 254 other |
| 255 <a class="reference external" href="http://src.chromium.org/viewvc/chrome/trunk/
src/ppapi/cpp/">PPAPI interfaces available</a>.</p> |
| 256 <p>While our in-browser environment is rapidly evolving |
| 257 to become a complete development solution, |
| 258 for the broadest range of development options, check out the |
| 259 <a class="reference external" href="https://developer.chrome.com/native-client/c
pp-api">NaCl SDK</a>.</p> |
| 260 <p>For questions, concerns, and help contact us at |
| 261 <a class="reference external" href="https://groups.google.com/forum/#!forum/nati
ve-client-discuss">native-client-discuss@googlegroups.com</a>.</p> |
| 262 <p>I hope this codelab has lit a fire in you to go out there, |
| 263 and bring an awesome C/C++ application to NaCl or PNaCl today!</p> |
| 264 <h2 id="cleanup">Cleanup</h2> |
| 265 <p>The Chrome Dev Environment App installs >800MB into |
| 266 HTML5 Filesystem storage on your device. |
| 267 To recover this storage, uninstall the app from the |
| 268 <a href="https://chrome.google.com/webstore" |
| 269 target="_blank">Chrome Web Store</a>.</p> |
| 270 </section> |
| 271 |
| 272 {{/partials.standard_nacl_article}} |
OLD | NEW |