Subscribe to GameFromScratch on YouTube Support GameFromScratch on Patreon

13. February 2012

 

On the PlayN discussion forum PlayN 1.1 was just announced.  Complete release notes are available here.

 

 

Key new features include:

  • add HTML Canvas backend ( in addition to the WebGL and deprecated DOM HTML5 back ends ) as a fallback for browsers that don’t support WebGL *cough* Internet Explorer *cough*
  • iOS support… probably the biggie of this release
  • removed GAE dependencies ( this was a pain in the butt previously )
  • Android properly supporting mp3
  • various other bug fixes

 

As was mentioned in this earlier post iOS support isn’t complete, yet.

 

 

Good job PlayN team!

News, Programming

8. February 2012

 

The last week or so I’ve been looking at using Node for server side game programming and I have been quite impressed.  I have found a few things quite frustrating ( the documentation is extremely sparse and tooling support is… iffy ) but code wise everything has worked about exactly how I expect it.

 

That said, I ran into my first glaring bug and it is an annoying one.  In my current code, I want to write out some memory to file when my code exits, I imagine a fairly common event.  At first it seems like a remarkably simple thing to do.  Every node application has a process object, and process has an exit event.  Therefore handling on exit should be as simple as:

 

process.on('exit', function () { //handle your on exit code console.log("Exiting, have a nice day"); });

 

*Should* being the operative word.  Reality is, this doesn’t work.  The event simply does not fire on CTRL+C.   Hmmm.  So I decide to throw process.exit() at the end of my script and BAM, the on exit event is fired.  So it appears that contrary to the documentation, the CTRL+C event doesn’t raise an exit event, or so I thought.

 

Alright, no big deal, I’ll just trap the SIGINT event raised from CTRL+C and handle exit then.  Ohh… that didn’t work out well.

 



C:\Users\Mike\workspace\NodeDev\src>node server.js

node.js:218
        throw e; // process.nextTick error, or 'error' event on first tick
              ^
Error: No such module
    at EventEmitter.<anonymous> (node.js:403:27)
    at Object.<anonymous> (C:\Users\Mike\workspace\NodeDev\src\server.js:5:9)
    at Module._compile (module.js:434:26)
    at Object..js (module.js:452:10)
    at Module.load (module.js:353:32)
    at Function._load (module.js:310:12)
    at Array.0 (module.js:472:10)
    at EventEmitter._tickCallback (node.js:209:41)



Hmmm…

 

So off to Google I go!  After a few minutes I discover this tidbit.  Basically it’s a known issue, SIGINT isn’t raised on Windows and they claim it isn’t possible, which considering Python handles it just fine, I find a bit difficult to believe.  So, it is a currently unsolved issue, but hopefully it will be resolved in the future.

 

Considering that CTRL+C is the defacto way to shut down Node, I really hope this is resolved soon.  In the meanwhile, does anyone out there know an effective workaround?

 

 

Update

 

Stack Overflow came to my rescue on this issue.  User pimvdb posted this work around:

 

var tty = require("tty"); process.openStdin().on("keypress", function(chunk, key) { if(key && key.name === "c" && key.ctrl) { console.log("bye bye"); process.exit(); } }); tty.setRawMode(true);

 

It feels like a bit of a hack, but then, I suppose it is!  Hopefully this issue gets fixed in node eventually, but for now, here is an effective workaround.

Programming

31. January 2012

 

 

As I mentioned earlier, I have recently discovered Node.JS.  Quick version, Node is a server side Javascript engine, allowing you to easily create applications on the server.  Obviously you could use SFML on both the client and the server but many people find Javascript a much faster language to work with.  The libraries bundled with Node make this task extremely easy.

 

We are going to create a very simple “High score” system, allowing users to push their score to the server as well as get a list of the current high scores.

 

 

 

You may notice Node referred to as Node, Node.js and NodeJS, which is correct?  Beats the hell out of me!  I frankly just use them all, so if you notice me switching terms, remember they are all referring to the same thing.  I am assuming you are using Windows 7 and Visual Studio for this tutorial, but there are no reasons you can’t use different OSes or IDEs.

 

 

First off, lets get Node installed, which you can download here.  Run the installer and node will be installed to your machine.  Really that’s about it.  Optionally you can add the install directory to your PATH environment variable, in my case it was C:\Program Files(X86)\nodejs, this will save you a ton of key strokes later and for the following instructions I will assume you have performed this step.

 

 

Now that you have installed node, we can create our server; you are about to see how laughably easy this is using Node.  Create a file server.js in your editor of choice, in my case I am using JetBrain WebStorm ( on a 30 day trial ) and have been impressed so far, but you can use any text editor including Notepad.  In server.js add the following code:

 

var dgram = require('dgram'); var server = dgram.createSocket('udp4'); console.log("Socket created\n"); server.on("message",function (msg,rinfo) { console.log("Received message"+ msg.toString()); }); server.bind(9000);

 

 

And… that’s it.  A perfectly functioning UDP server, not that it does much yet.  To run the code, in a command prompt, change to the directory you saved server.js to and simply type “node server.js”.  To stop node from running, press CTRL + C twice.

 

dgram is a built in Node library for creating datagram sockets.  When we create the socket with tell it to use udp4, that is to say UDP connection for IP v 4.  Then we add an on “message” handler that for now simply echos whatever data it receives.

 

 

Now lets make a C++ client to talk to our server.  If you are unfamiliar with how to configure SFML, take a look at part 1 of my C++ with SFML tutorial.  In this case we aren’t going to need to link the graphics or audio libraries, which greatly cuts down the size of our project.  If you have trouble setting up a project or simply don’t want to, you can download a pre-configured project here.  Simply open the solution and run.

 

Once you have created and configured a new project, lets add our code.  In my case I created a file called Scoreboard.cpp and added the following code.

 

#include "SFML/Network.hpp" int main(int argc, char* argv[]) { if(argc <2) return-1; sf::IPAddress ip("127.0.0.1"); sf::SocketUDP socket; sf::Packet packet; packet.Append(argv[1],strlen(argv[1])); socket.Send(packet,ip,9000); return0; }

 

 

Extremely simple code again.  The app is simply going to send a string you specify on the command line over a UDP socket, so first we check to make sure you specified a string. Then you create the IP address that represents your server’s location.  In this case you are using 127.0.0.1 which is a special reserved IP address called a loopback, which basically means “this machine”.  In an actual production environment, you would use an external IP address or DNS entry like “gameserver.mydomain.com”.

 

Next we declare our Socket and  Packet.  Think of a Packet as the “stuff” we are sending across the wire.  Next we fill that picket with the string the user passed in on the command line.  The Socket represents the connection to the remote server.  When we say socket.Send() we are saying send packet, on ip IpAddress using port 9000.  Finally we simply exit.

 

You can now run this from the command line using ScoreBoard.exe “The text you want to send”  <—The quotes are critical, or it will end up looking like multiple parameters instead of a single string.  Additionally you could specify the string in your debug settings, then you can simply press F5 to run your code.  To set this value, right click your solution and select Properties, then on the left hand side choose “Command Arguments” and specify your string (quoted again ) there, like this:

 

image

 

 

Now your primitive client and server are up and running.  Start your server first.  Open a command prompt, CD to the directory you created server.js in and type “node server.js” ( no quotes ).   Note, if you didn’t set your PATH to include the path to nodejs, this wont work and instead you will have to fully resolve the path to node.  Now run your SFML application, either using F5 if you specified a Command Argument, or from the command line.

 

Now you should see:

 

image

 

 

Congratulations!  You just created a fully function client and server!  You may have noticed we actually received two socket connections there, an empty one, then our string.  What’s going on here?  Well, SFML sends Packets in two parts, the first contains only the size, the second contains the actually data.  Why?  Beats me.  Not really a big deal, as you will soon see.

 

 

Now, in the next part we will actually make them do something!

 

 

Click here to download the pre-configured Visual Studio project.

Click here to download Server.js.

 

 

Click for part 2

Programming ,

26. January 2012

 

 

You may remember me discussing PlayN in previous posts, it’s a Google run cross-platform, open source game library.  Previously the most missing feature was iOS support and frankly that feature is still missing, but there is light at the end of the tunnel!

 

 

PlayN developer Michael Bayne recently made a post announcing he had successfully got the Peas demo running on an iPhone4. IMG_0935Without a doubt this progress is a “very good thing”, as iOS support is easily the most important missing feature of PlayN today.

 

 

Michael’s progress:

 

I got side tracked by trying to get things working on a real device.
The simulator runs the Mono VM in JIT mode (though with various
restrictions in place to ensure that you don't do anything that's
incompatible with AOT compilation), but actually doing AOT compilation
enforces substantially more restrictions. I had to "refactor" IKVM to
contain no references whatsoever to System.Reflection.Emit even if
they were never called. I also bumped into a Mono compiler bug and
spent some time digging into the internals of IKVM and mcs (the Mono
compiler) so that I could come up with a work-around and file a
sensible bug report. It turned out to have already been fixed in trunk
(which made my investigations that much more perplexing), but since
MonoTouch is commercial software, I was necessarily working with the
latest beta release, not trunk; annoying!

Performance of the Pea Physics demo is not stellar on an iPhone 4
(it's quite reasonable, it's just not silky smooth 60 FPS with twenty
or thirty peas moving on screen). It's pretty comparable to what I've
see on actual Android devices. Depending on what Box2D's interfaces
are like, there's a possibility that it could be improved by writing
an interface-compatible implementation of Box2D directly in C#. C#
supports value types, and in a physics simulator, being able to store
your Vec2s and Matrix3s directly inline, rather than separately on the
heap, can substantially improve cache performance. That said, the
Box2D implementation, as is, is not very data-oriented. Rewriting it
to store all of the entity geometry in big flat arrays and to perform
its calculations by iterating over those arrays, rather than following
a zillion pointers through the heap, would probably help a lot on
every platform.

Now that I've got things running on an actual device, I'll go back to
finishing up the platform implementation. IOSCanvas is substantially
done. IOSStorage is done (built on Sqlite). IOSTouch (and IOSPointer)
are done. I need to implement the font and text rendering bits of
IOSGraphics/IOSCanvas. Then IOSSound, IOSNet and other niggling bits.
I'm not sure what I'll do about IOSKeyboard. I'd kind of like an
additional interface for requesting a string of text from the user,
which would allow the keyboard to pop up in "edit a line of text" mode
so that the user can use the standard copy/paste and click with
magnification text navigation. Having the app respond to key events
and attempt to implement text entry directly is a massively bad idea
(on any mobile device), IMO.

 

 

Excellent news and great work Michael! 

 

So for the people looking to see if PlayN works with iOS, the answer is no, but it’s damned close!

Programming

13. January 2012

 

 

 

Sadly SFML only included precompiled binaries for 1.6 and even in that case, they don’t support Visual Studio 2010.  If you need to build SFML 2.0 for use with Visual Studio 2010, use the following step.

 

 

Go to this page and download the SFML 2.0 snapshot.

 

 

It’s a tar.gz file, if using 7zip, it means you will have to extract it twice.  Extract it to a folder somewhere on your hard drive, the results should look like this:

 

image

 

 

 

Now you need CMake.  Download and install it from here.

 

 

One installed, the next step is slightly tricky.  CMake needs to be run with the environment variables for Visual Studio properly set so it can locate your compiler.  This means you need to launch if from the command prompt provided by Visual Studio.  If you are using Visual Studio Express, in the start menu launch the Visual Studio Command Prompt.

 

 

image

 

 

 

Now that you’ve launched the command prompt, change directory to your cmake executable folder.  In my case it was c:\Program Files (x86)\Cmake 2.8\bin.  CD to that directory then run cmake-gui.exe.

 

 

 

image

 

 

 

The CMake GUI will now pop up.  Click “Browse Source” and navigate to the folder you extracted SFML2 to.  Then click “Browse Build” and navigate to where you want it to create all the project files, like I have below.  Once done, click configure.

 

 

 

image

 

 

If the directory you selected  doesn’t exist, the following dialog will appear:

 

 

image

 

 

Simply click Yes.

 

 

Now you are going to be prompted for what kind of project to generate, assuming you are using Visual Studio 2010 Express, select Visual Studio 10 then Finish, as follows:

 

 

 

image

 

 

 

If everything went ok, it will look like this:

 

 

image

 

 

 

The default settings are most likely what you want, now click Generate.  If all things go correctly, your project files will have been generated in the folder you specified in “Where to build the binaries”.  Here is mine:

 

 

 

image

 

 

 

Now simply double-click SFML.sln.

 

 

Visual Studio will load, now all you need to do is select the menu item Build->Build Solution in Visual Studio.

 

 

Voila!  You just built SFML 2.0 using CMake and Visual Studio 2010.  Look for the binary files in the lib folder.  Keep in mind the include and extlib files are required too, and are in the directory you extracted to earlier.

 

 

If that didn’t work for you, you can simply download this version I've compiled.  Keep in mind, SFML 2.0 is under constant development, so this version was only current as of 1/13/2012.  This archive also included the Include folder as well as the extlib folder, which is all you should require to get started with SFML2.

Programming

Month List

Popular Comments