Sniff is a "Scratch-like" programming language that's designed to help Scratchers move gently from Scratch to more conventional languages. They can start writing programs, without having to learn a new language because Sniff is based on Scratch. They learn a little more about variables, compiling, syntax errors (!), and they can have fun controlling real hardware while they're doing it.

Tuesday, 7 July 2015

Sniff for the Web

Sniff is implemented by converting the Sniff code to C, which means that we can make it run on pretty much anything that has a C compiler. So we got pretty excited when we discovered Emscripten. Emscripten is a C compiler that outputs Javascript, which means you can write code in C and run it in a web page... which means you can now compile Sniff to run in a web page.

The first step is to install Emscripten, and update to the latest version of Sniff. With that done, you can write your Sniff program pretty much as normal, and compile it with the command "jsniff". This will produce a version of your code which you can then use online! That's basically all there is to it, though there are couple of minor things you need to watch for.

In addition to the actually js output file, we also generate an html file, so you can run your program by just opening the html file. However most modern web browsers won't let a web page start loading up file from your local hard disk. You need to place the files on a web server to access them (or use Firefox which still lets you do this!).

Similarly if you want to access any files - for example images to use in your game, you need to bundle them into the code. To make that easy, just add the names of any files or folders you want to include in the bundle after the .sniff filename on the command line. For example:

jsniff asteroids.sniff *.bmp
jsniff adventure.sniff objects rooms

Provided you bundle the files in at compile time, you can read files just as normal. You can also write files, but they get thrown away when your program finishes - thats just the web way!

We've mainly used this for games so far, and the window and sprite devices work really great online. Unofrtunatly Javascript can't use regular sockets, so minecraft, and leap motion don't work just yet.

The only other weirdness is that "ask" will pop up a modal dialog box, pausing your whole program. It works fine, but you can't run other scripts while the "ask" box is on screen.

Appart from that it just works. If you're running some kind of workshop you can write code to run on the local machine, and when you're done just put it on a website so everyone can see it!

No comments:

Post a Comment