Getting Native Client (NaCl) running with Pepper 18

After reading about Google’s Native Client (NaCl) for a while, I wanted to see how difficult it would be to share compiled code on the web, so I decided to dive into the docs and check it out.

I’ll start with the conclusion in case you aren’t really interested in the technical details (and since they are mostly just notes that will be outdated when NaCl updates anyway).

The problem I ran into is any code I would want to share is dependent on a set of libraries that I can only find in a .lib file (they are the libraries from CS106B), and  I can’t seem to find a way to successfully link this library and use the functions from it.

I have a few different options like trying to set up the g++ compiler with Visual Studio (this has improved since writing, see update below) or rewriting the parts of the program with a standard set of libraries that I have access to (this would take a lot more time).  Neither of those seem very appealing especially considering that I don’t have any major plans for using NaCl.

While I definitely find NaCl to be an interesting concept, I’ll be much more interested in playing around with it when it has an IDE (or plays nicer with existing ones) and up to date documentation.

Update

In the time since I drafted this post, two things have happened.  First, NaCl is playing nicer with Visual Studio.  Second, I’ve begun CS107.  My plan is to return to this after I have some more interesting C code from CS107 and try out the VS integration.

The following are my notes from this project.  Again, these will likely be outdated soon, but I ran into enough problems to share them so others might have an easier time.

Notes

Before I started developing, I had to enable NaCl in Chrome.  You can do this by going to chrome://flags/, clicking enable Native Client, and restarting the browser.

Next,  I went to the quick start guide which was straightforward: https://developers.google.com/native-client/quick-start

The main links are here to download and here to get started (though you may want to read on here before going through those).

After updating, Pepper 18 installed.  There are two problems with this.  First, the documentation is set by default to Pepper 16.  Second, even the Pepper 18 documentation is outdated (more on this later).

On the download page, the last step tells you to set an environment variable.  The instructions aren’t precise, but I’ve messed with environment variables before so this wasn’t too much trouble.  However, I’m fairly certain with Pepper 18 this isn’t necessary, but see the next paragraph for how to do this if you were a little confused.

In Windows 7:

  • type path into the start menu, go to edit system environment variables
  • click on the Environment Variables button
  • click ‘New’ under System variables
  • set the name to %NACL_SDK_ROOT% and set the path to C:/nacl_sdk  (or wherever you installed)

Why do I think this isn’t necessary?  As of Pepper 18, NaCl uses make files instead of SCons (though there are still references to SCons and not make files in the docs).  In the make file, they have a fail-safe if you don’t have the variable set.

I’m not sure if I missed a step somewhere or this is an artifact of the docs being outdated, but I was getting errors trying to run the examples on a local host in the tutorial, so I had to go to the console and execute ‘\pepper18\examples\make.bat’.  Now they seem to run fine.

Also a problem with the docs being outdated:  for Step 5 of the tutorial, Pepper 18 no longer includes init_project.py.  You should simply use the files from an example for your template.

These were the primary problems I ran into with the documentation.

I used the hello_world_interactive example as my template and started editing the ‘helper_functions.cc’ file.  To get things to show up, simply edit the code, save, run make.bat in the directory, then rehost (I was using the simple Python local host recommended in the docs).  Simple enough and I had a live NaCl app running.

 

CS106B and aiclass

For the latter part of 2011, I decided to focus more on writing code and learning than writing about code.  I did this by taking two courses online, Programming Abstractions (CS106B) and Intro to AI, both from Stanford, online and free.

I started CS106B because while I had some familiarity with the concepts in the course, I didn’t have much experience implementing them.  The course covers a range of topics including: recursion, complexity analysis, search/sort, use and implementation of many standard containers (vector, queue, stack, map, etc.), pointers, linked lists… and more!  While the previous course (CS106A) is a great introduction to coding and software engineering, this course digs much deeper into more complicated abstractions for data structures and algorithms.

Although most of my code at work hasn’t required much of what I gained from this class, more coding is good and coding out of my comfort zone (which these topics definitely were) is better.

More importantly, the final assignment of CS106B ended up teaching me a valuable lesson about planning.  The first part of the assignment was to implement Dijkstra’s algorithm (which finds the shortest path through a series of connected points).  While this wasn’t trivial, the planning wasn’t very difficult and the debugging was standard.

The second part of the assignment was to implement Kruskal’s algorithm (create a minimal spanning tree from a graph. That is, create the minimum number of connections to connect all points on a graph).  I ran into some serious trouble here.

Part of the problem may have been the artificial deadline I set for myself.  In any case, when it came time to put this together, I read the assignment several times and just wasn’t getting it.  Instead of taking a break and getting a solid plan in place, I decided I had some ideas about parts of the algorithm, and I could eventually hack those pieces together and figure out the rest.  I’ve made a huge mistake.

Without understanding how to attack the problem, I started using the same data structures I had used for Dijkstra’s algorithm with some awful workarounds. After too many hours debugging this hack, I finally had the insight that I needed to make a simple change to the data structure I was previously using.  If I would have decided to take some time to do this before diving into the code, I would have saved myself a lot of frustration.  

There is a significant difference between debugging and reimplementing on the fly. It is not possible to successfully debug an incomplete solution.

Following CS106B I began taking Intro to AI, which taught me that AI requires a great deal of probability theory. And that probability theory is hard.  To be clear, the calculations aren’t very difficult per se.  In fact, most of the implementation only required arithmetic (some of the derivations required calculus).  However, knowing how to implement them was quite difficult.  I have saved the programming assignments from the lecture version of the course and began work on the first one, but haven’t decided if I am going to dive into those. haven’t done them a few months after finishing the course.

In fact, I’m in the middle of deciding what to do next.  The trouble with the AI assignments is that they are going to be quite time consuming, and while I don’t necessarily have a problem with doing difficult coding work just for the experience (see above), I am debating if maybe I should spend time on something I’m a bit more likely to use in the near future, or is at least a bit more universal in its application.

I was considering taking Stanford’s Algorithms course, (set to begin in January but only started recently),  Building a Search Engine, or Programming Paradigms (CS107, a followup to CS106B). After some serious soul searching and few coin flips, I’ve decided I want to finish the Stanford Engineering Everywhere Computer Science offerings and take CS107. I plan to start early next month.

I’m also hoping to post my completed graph assignment described above using Google Chrome’s Native Client.

Google Analytics with WordPress

After several WordPress updates and some lost Analytics data, I have come to a conclusion: Get a plugin for Google Analytics to use with WordPress.   Before realizing that there was a simple plugin, I was placing the Analytics code on my site manually.  On at least two occasions, I upgraded WordPress and forgot to reinsert the code losing a week or so of Analytics data each time.  Now this isn’t vital for me, but the simplicity of the plugin is great.

I use Analytics with WordPress.  Some may prefer another option, but I haven’t had any trouble with this one through a few upgrades, so I feel comfortable recommending it.

The Difference between Sleep(), Wait(), and Pause()

I haven’t written in a while about the Stanford CS106B course I’ve been working through because I wanted to have some measurable progress behind me before discussing it again.  Recently, I completed the fourth asssignment, making a single player version of Boggle with a computer opponent.

Although the previous assignment also focused on recursion, this is the first assignment that requires creating recursive algorithms that piece together to form a useful program (read: not just data entry from the user to prove you have an algorithm).  After attacking the more difficult portion of the assignment (the recursive human turn), I decided to attack the easier task of highlighting and unhighlighting  the blocks of a word that is found.  I used the supplied HighlightCube function to, but I ran into trouble when I tried to remove the highlight.  The blocks would simply never highlight.  I tried using both the Windows Sleep() function and the following wait function from here:


void wait ( int seconds )
{
     clock_t endwait;
     endwait = clock () + seconds * CLOCKS_PER_SEC ;
     while (clock() < endwait) {}
}

When both of those produced the same problem, I began looking for a different solution and found someone who had already completed the project.  I tracked down the relevant code section and found the use of the function Pause().  This worked, but I wasn’t satisfied without knowing why it worked where the others didn’t.

The best place I could think of to ask was on Stack Overflow.  I got an answer, and even a bit of rep for asking.  It turns out the graphics buffer writing is a low priority message and the sleep/wait function pushes both the writing and clearing of the graphics to after the wait/sleep.  The difference between the sleep and the wait is that the sleep function halts a thread for a given time whereas the wait function checks the clock repeatedly until the time has elapsed (costing CPU cycles and power). So Stack Overflow taught me why the wait and sleep functions didn’t work, but I still wanted to know what the Pause function was doing to clear the highlights correctly (and so did a few of the commenters on SO).  After some searching through the CS106B supplied libraries, I found that the Pause function was simply a function to pause the graphics buffer (not the entire program).

In this instance, I found two functions that gave me obvious problems.  This made it easy for me to realize I needed another solution.  However, I found some more subtle bugs in my code that were a result of the same flaw I have in my coding style:  I tend to prefer to write code to perform a task without first considering what potential problems the function needs to avoid.

While debugging a separate issue, I noticed that the word SAGS was place vertically, in reverse, along the left hand side of the board, but my program would not recognize it as a valid word.  After tracing through my recursive algorithm (and realizing that it was correct), I found that the program was failing because it was only testing the first letter of the word the first time it found it.  Soon after solving this, I realized that my algorithm did not account for users entering words with a block used more than once.  Instantiating a second grid of bools to check if a letter was already used was easy enough, but seeing these three errors really drove home the need for a better approach to algorithm design.

Warning:  coding buzzwords

I’d like to think I’m using ‘best’ practices when it comes to coding, but I tend to err on the side of being a bit too Agile.  What I mean by this is that instead of writing specifications for a method/function/algorithm/etc., I tend to consider some aspects of it and get right into coding and compiling.  I love to see the result and have something to work with rather than planning for too long for the perfect function (that will likely need debugged anyway).  However, in cases such as those described above, not considering the possible weaknesses with an algorithm led me to overlook some fundamental problems that I may not have noticed if not for seeing the errors pop up by chance on the board.

This project has given me a practical example demonstrating why Test Driven Development is a widely used process.  It is much easier to ask hard questions of a function that doesn’t exist than one that is ‘working’.

How To: Get rid of ‘Application Data Space is Low’ on the Droid Incredible

A few weeks ago, I started getting a recurring notification on my Droid Incredible telling me that ‘Application Space is low’.  After removing some apps and moving others to the SD card, the notification continued popping up.  I checked the amount of space available for each storage device in the phone (memory, internal storage, and SD card), and all three had plenty of space left.

After some searching, I found that before a factory reset (something I did not want to do), some other users had found clearing application cache could solve the problem. You can do this by clicking the notification (or if it isn’t currently up, going to Setting > Applications), sort by size, then go through the largest applications (likely Mail, Facebook, Twitter, etc.), click the name, then click, ‘Clear Data’.  After doing this for several of the largest apps (and moving apps to SD earlier), the notification has not returned for a few weeks now.

I wanted to post this in hopes that anyone else with this problem can solve it without a factory reset. Good luck!

Moon Phases

Here is a sneak peak at half of the lesson we are designing to teach the phases of the moon.  I am currently extending the game to include rise/set/transit times.

Phases of the Moon

The game was designed with Unity3d.  At first, I had trouble with the webplayer again because I had forgotten about a previous problem I had with my host and unity files.  Luckily, I was able to read my old blog post to remedy it quickly.

Let me know if you have any thoughts on the game.

GDC 2011

I was lucky enough to be sent to GDC this year.  While the trip (travel and registration) is quite expensive, there is a reason this is the Game Developer’s Conference.  Just about everyone from the game industry is here: producers, designers, artists, programmers, media etc. from the biggest companies down to Indies whose names you may or may not have heard.  And there is something to be learned from just about everyone.

Having all of these developers together makes networking easier than at any other event.  This ca n lead you to find a new team member (temporary or permanent), discussions about how to solve problems in development, or even long term friendship.  I’m not lucky enough to be in a city with any significant game development meetups, so being able to meet so many great developers at GDC was fantastic.

I started out the week in the Level Design Tutorial.  While most of the talks didn’t apply directly to what I’m working on, I feel as though I got a good deal out of some of the lessons, specifically, how to think about level layout.  Despite focusing on shooters, many of the concepts apply to any game in which you need to concentrate your player’s focus, specifically if they are controlling an avatar in a 3D world.

On the second day of the conference, I attended the Unity track, which was like a mini Unite conference.  Despite being all about the engine I use, there were a lot of topics covered that weren’t important for my current project (mobile, art, etc.), but the presentations were mostly well done.  It would be nearly impossible for a set of talks this broad to be perfect for everyone.

Wednesday, the conference really began with the Keynote from Satoru Iwata and Reggie Fils-Aime.  After thinking that the conference was hectic through the first two days, I realized just how much crazier it gets for the full conference with sessions scheduled all day and enough on the expo floor to keep anyone busy for the remainder of the conference.  From the huge Unity booth, to playing unavailable handhelds like Xperia Play and the 3DS, to the new UDK reveal, the Expo floor was incredible.  I haven’t been to any of the other game industry shows, but I imagine the show floors are quite a bit more hectic. The GDC Expo floor was certainly busy, but save for a few big demos (3DS), everything was accessible even during the busier times of day.  Although I wouldn’t want to miss the sessions, there is easily enough to do on just the Expo floor to keep an attendee busy for all three days.

Before the conference, I spent probably too much time trying to determine which talks to go to at what times.  I realize now that isn’t too important considering the all access pass grants access to the GDC Vault where you can catch any talks that you miss.  That isn’t to say I didn’t enjoy a great number of the talks, but I am glad I spent a bit of extra time on the Expo floor and I can catch the talks now.  Some notes on my favorites:

The Post Mortems were great, though I didn’t learn a lot other than making games early on was a completely different process.  Back then after deciding the genre, constraints really defined your game.  Now, the bigger constraints are budget or deadline, not graphics or performance (this is becoming more and more true even on handhelds).  For Marble Madness, Mark Cerny spoke about how the arcade board he was given really determined how he created his sprites for the art.  For Maniac Mansion, Ron Gilbert spoke about how despite having too many characters and only one tester without any sort of test plan, the game has relatively few dead ends. For the Pac Man talk, Toru Iwatani spoke about how the simple, easy to grasp mechanics made for a great game and how he had to fight with his boss to get 4 different colored ghosts. I highly encourage anyone interested in game development to check them out at the GDC Vault.

The Indie failure talk was incredibly interesting.  5 drastically different lessons from how to grow an Indie studio to design mistakes by small teams applicable to nearly any project.  Most of the lessons came down to two important instructions:  figure out what is fun about the game you want to make, and iterate on that.

Two of the most entertaining talks I attended were Jeremy Bernstein’s Minimizing Exposition and Dustin Browder’s Starcraft 2 as an e-sport.  Jeremy is a freelance writer that explained how to cut, control, and hide exposition to make the game’s story more interesting to the player with examples from his experience on Dead Space 2.  Browder is a lead designer on Starcraft 2 who described the painful choices they made that affected nearly every facet of Starcraft 2 in order for it to be played as an e-sport.

The final day of the event includes talks about breaking into the industry to compliment the student passes which are only good for that day.  I attended the last two and most of the information was of the form: know what you want to do and do more of it so you have something to show when you apply for jobs.

With all of that activity during the day, it is important to note that some of the most important business at GDC gets done over lunch, dinner, and at the parties thrown by nearly every major company in attendance.  It was a struggle to get up for the keynote on Wednesday, but going to the parties at GDC is a must for networking.  It’s pretty incredible to see entire bars full of people with a passion for this industry and being able to talk to nearly anyone in the building about gaming and development.

After all of the activity at the conference, I was exhausted.  But I’m already looking forward to next year and trying to get even more out of the conference.

Backup/Restore and Why You Need an SSD

If you value your time and access your hard drive more than once per day (if you follow this blog, I can almost guarantee that you do), you should get a Solid State Drive (SSD).  For convincing arguments see here and here and here.  For more in depth analysis of SSDs see anandtach.com’s SSD Anthology and Relapse.  In my scientific tests (not scientific at all), I have found that everything from starting a game to starting Unity to starting Windows is about twice as fast with an SSD.  My laptop is also significantly quieter and uses less power thanks to the lack of moving parts in the SSD versus a traditional hard drive.

On top of the time and power saving, I’ve noticed  that Windows 7 has no problems hibernating and waking anymore.  Previously, hibernating could take up to a few minutes (which I don’t always have when leaving the office to catch the bus).  More frustrating are times when I thought the laptop had shut down or hibernated properly only to find out upon restarting/waking that it had crashed while doing so.  With the improved times for the SSD, I haven’t experienced these problems yet.

Now that you’ve bought the drive, you presumably want to put your current backup onto it right?  You do have a backup don’t you?  Even though I had a backup, I found that restoring that backup was a lot more work than I anticipated (in fact, I gave up and went with a new install).  After doing some reading on the problems I was having, it looks like the problem comes from the size of the old HDD being larger than the SDD (see here).  I can’t say for sure if the solutions offered there work as my HDD has an additional wrinkle:  It has 15GB of unallocated space at the front of the drive.  I tried every way I could think of to get the drive to fit onto my new SSD (120GB), but I couldn’t.

This got me thinking ‘What if I wasn’t moving to a SSD, but my HDD failed and I needed to get back up and running?’  I realized there are two problems that need to be addressed in that situation:

1.  Do I have  a back up that I can quickly and easily restore?

2.  If not, what about that data that I just can’t afford to lose?

For question 1, I am now working on getting all of the important programs that I need installed so that I can make a reasonably sized back up with the programs I absolutely need to get back up and running.  For question 2, my solution is Dropbox, which I’ve written about before.  I’m not saying that using Dropbox is necessary, but you should seriously consider what happens to that data in the worst circumstances.  If you are simply using an external hard drive, what happens if both get destroyed (fire, theft, etc.)?  There are plenty of other cloud based file systems to choose from, and I recommend using one of them for your most important files.

In my previous post, I recounted the frustrating experience of having to redo some of my own work due to a lack of foresight in my file management.  Luckily, in all but world ending scenarios, my data should be recoverable from at least one location so that even if my backup doesn’t restore and my hard drive fails, I will lose, at most, a few hours from reinstalling Windows and some programs.

My recommendations for the day:

1.  If you access your hard drive frequently (if you use a laptop, edit images, open files in Visual Studio or another IDE, etc.), you should get a SSD.  They have been going on sale recently and the new updates due out early this year should lower prices as they are based on a more efficient process.

2.  Make sure you have not only a backup, but one that can be easily restored.  Check your Hard Drive for any unallocated space on the drive (you can do this with Linux System Rescue).

3.  In case something does go wrong with your backup or restoration, ensure that your most important files are backed up some other way, preferably with an automated system like Dropbox.

Unity and Dropbox and popuplist and Dialogue, Oh my.

Over the past few weeks, when I’ve been able to work in Unity, I’ve found myself using (and modifying) some open source code.  I’d like to share what I’ve done here so others can use the (limited) functionality I’ve added.

Using a Dropdown in Unity

PopupList is a bit of code that easily allows a user to make a drop-down in a Unity GUI.  One thing I don’t like about this code is that it requires the user to click and hold down the mouse button, move to the menu item you want selected, then release.  I much prefer the user to be able to click (down, then up) see the choices, then click again.  I’ve added this functionality by putting in a simple counter that only works when the user clicks within the space of the drop-down menu.

You can use the link above to see the source code.  I used Popup.cs, not the updated version.

[...] indicates verbose sections I removed that I did not edit.

using UnityEngine;

public class Popup
{
    static int popupListHash = "PopupList".GetHashCode();
    static int count = 0;

[...]

        switch (Event.current.GetTypeForControl(controlID))
        {
            case EventType.mouseDown:
                if (position.Contains(Event.current.mousePosition))
                {
                    showList = true;
                }
                break;
            case EventType.mouseUp:
                if (count == 1)
                {
                    if (showList)
                    {
                        if (position.Contains(Event.current.mousePosition))
                        {
                            GUIUtility.hotControl = controlID;
                        }
                        done = true;
                    }
                    count = 0;
                    break;
                }
                if (position.Contains(Event.current.mousePosition))
                {
                    count = 1;
                }
                break;
        }

        [...]

    }
}

Sidebar: I should mention that I am documenting this because I recently had to redo this work when I broke my project. I made the mistake of having the project in my auto-updating Dropbox with no recent backups and it updated before I could revert back within Unity . The irony is that this happened because I really wanted to test something from home, and I opened the project in an old version of Unity.

Free Dialogue!

A fellow blogger and Unity user over at kimmonsdesign has created an open source dialogue editor for use in the Unity Inspector/Editor.  The implementation is great, but I wanted a way to have the dialogue open and close when I click on specific characters in my game.  If you want to see the discussion we had about how to implement it, you can do so in the comments of the previous link.  I also wanted to post the wrapper code I’ve created here.  There may be a better way to do it, and I’ll be sure to update later if I make changes, but here it is:


var clickTarget: String;

function Start () {
var d : DialogueInstance = gameObject.GetComponent(DialogueInstance);
if (d) d.enabled = false;
}

function OnMouseOver () {
    if (Input.GetButtonDown ("Fire1")) {
		var npc = GameObject.Find(clickTarget);
		var dialogueScript = npc.GetComponent(DialogueInstance);
//		var dialogueScript = GameObject.Find("Sphere").GetComponent("DialogueInstance");
		dialogueScript.enabled = !dialogueScript.enabled;
    }
}

Yep, that code was back when I was getting used to Unity and still using Javascript.

First time posting code!

How to post code in WordPress:  I searched and found this link suggesting different ways to place code in WordPress.  I chose SyntaxHighlighter Plus which you can see in this post. Caveat: make sure to be in HTML mode not visual or it will destroy formatting by getting rid of tabs.

http://kimmonsdesign.com/node/21has

Evernote 2.0

In my previous post on Evernote, I discussed it’s value for simple note taking.

Since that post, Evernote has released a comprehensive update of their mobile application which is significantly improved.  The new interface changes it from a simple note taking app to a mobile version of the full app.  Admittedly, I am not a power user of this software, but I haven’t found anything that I can’t do on the mobile version that I do on the desktop. For taking, searching, and viewing your notes, the mobile app is now just as good for me as the full version.  In fact, for note taking at the Unite conference , I took notes on my phone for almost every session simply so I didn’t have to fire up my laptop each time.

One of the best functions of the mobile app is the ability to snap pictures with a smart phone and keeping it as a note (whole or part).  I like to write notes when I’m listening to a talk, but some slides just have too much code that I’d rather just see as a whole later.  Evernote’s mobile version has a button explicitly for this purpose.  I can take the picture, place it right in line with  the notes, and get right back to taking notes.

In addition to note taking, I have begun using Evernote for lists as well.  The two that I reference the most often are a list of games I want to play (AKA my backlog), as well as a list of links I want to read on various subjects.  The latter is incredibly helpful to me.  Without it, I tend to end up with a ridiculous number of tabs open and end up not reading some of the more in depth articles.  Now, I have a go to list of articles to read if I’m waiting around somewhere or if I actually make it through the open tabs on my computer at the time.

Additionally, I’ve been using Evernote to keep my notes on CS106B (update coming soon) all in one place.  I like this far more than a physical notebook as I just need to click through the notes, and I am able to add more notes (for example, the code on the website for that lecture) and reorganize after I’ve written them.

Finally, I plan to start using it to track my major electronics purchases so I don’t need to worry about where i placed receipts. I can simply take a picture of the receipt and search for it later.

If you haven’t started using it yet, you can find the desktop client here or use the QR code for the mobile app:

Any ways you’re using Evernote that I’m not?